xwidget-plus-follow-link-test.el 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. ;;; xwidget-plus-follow-link-test.el -- xwwp follow link test suite -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2020 Damien Merenne <dam@cosinux.org>
  3. ;; This program is free software: you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation, either version 3 of the License, or
  6. ;; (at your option) any later version.
  7. ;; This program is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ;; GNU General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public License
  12. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ;;; Commentary:
  14. ;; Test suite for xwidget-webkit-plus-follow-link feature
  15. ;;; Code:
  16. (require 'test-helper)
  17. (require 'with-simulated-input)
  18. (require 'ivy)
  19. (require 'helm)
  20. (require 'xwidget-plus-follow-link)
  21. ;; Disable helm and ivy, otherwise they hijack completing-read and break the
  22. ;; default completion backend test.
  23. (setq completing-read-function #'completing-read-default)
  24. ;; A mocked backend class that doesn't interactively read anything.
  25. (defclass xwidget-plus-completion-backend-test (xwidget-plus-completion-backend)
  26. ((candidates-mock :initarg :candidates-mock)
  27. (selected-mock :initarg :selected-mock)
  28. (action-fn)
  29. (classes)))
  30. (defun xwidget-plus-update-fn-callback (result)
  31. "Called after updating candidates with the css classes in RESULT."
  32. (let ((backend xwidget-plus-completion-backend-instance))
  33. ;; Store the results.
  34. (oset backend classes result)
  35. ;; Trigger the action function with the mocked selected link
  36. (funcall (oref backend action-fn) (oref backend selected-mock))
  37. (xwidget-plus-event-dispatch)))
  38. (cl-defmethod xwidget-plus-follow-link-candidates ((backend xwidget-plus-completion-backend-test))
  39. "Return the list of BACKEND mocked candidates."
  40. (oref backend candidates-mock))
  41. (cl-defmethod xwidget-plus-follow-link-read ((backend xwidget-plus-completion-backend-test) _ _ action-fn update-fn)
  42. "Store ACTION-FN in BACKEND, call the UPDATE-FN, and fetch the link element classes."
  43. ;; Stoire action so that we can call it after having fetch the css classes.
  44. (oset backend action-fn action-fn)
  45. ;; Trigger the javascript update.
  46. (funcall update-fn)
  47. ;; Fetch css classes.
  48. (xwidget-webkit-execute-script (xwidget-webkit-current-session) (--js "
  49. map = Array.prototype.map;
  50. r = {};
  51. document.querySelectorAll('a').forEach(l => {
  52. r[l.id] = map.call(l.classList, t => {
  53. return t.toString();
  54. });
  55. });
  56. r
  57. " js--) #'xwidget-plus-update-fn-callback)
  58. (xwidget-plus-event-dispatch))
  59. (cl-defmethod backend-test-link-classes ((backend xwidget-plus-completion-backend-test) link-id)
  60. "Return test BACKEND css class names for LINK-ID."
  61. (cdr (assoc link-id (oref backend classes))))
  62. (defmacro with-backend (backend &rest body)
  63. "Run BODY with the specified BACKEND."
  64. (declare (indent 1))
  65. `(let* ((backend (,(intern (concat "xwidget-plus-completion-backend-" (symbol-name backend)))))
  66. (xwidget-plus-completion-backend-instance backend))
  67. ,@body))
  68. (defmacro with-test-backend-browse (candidates selected url &rest body)
  69. "Run BODY with the specified BACKEND mocking CANDIDATES and SELECTED while browsing URL."
  70. (declare (indent 3))
  71. `(let* ((backend (xwidget-plus-completion-backend-test :candidates-mock ,candidates
  72. :selected-mock ,selected))
  73. (xwidget-plus-completion-backend-instance backend))
  74. (with-browse ,url
  75. ,@body)))
  76. (ert-deftest test-xwidget-plus-follow-link-prepare-links ()
  77. (let ((links '(("3" . "Functions")
  78. ("1" . "Function Cells")
  79. ("12" . "Structures")
  80. ("2" . "Anonymous Functions")
  81. ("9" . "Declare Form"))))
  82. (should (equal '(("Function Cells" . 1)
  83. ("Anonymous Functions" . 2)
  84. ("Functions" . 3)
  85. ("Declare Form" . 9)
  86. ("Structures" . 12))
  87. (xwidget-plus-follow-link-prepare-links links)))))
  88. (ert-deftest test-xwidget-plus-follow-link-highlight ()
  89. (with-test-backend-browse '(0 0 1) 0 "links.html"
  90. (xwidget-plus-follow-link)
  91. (xwidget-plus-event-dispatch)
  92. (should (string= "test-1.html" (file-name-nondirectory (xwidget-webkit-current-url))))
  93. (should (equal (backend-test-link-classes backend "test-1") '["xwidget-plus-follow-link-selected"]))
  94. (should (equal (backend-test-link-classes backend "test-2") '["xwidget-plus-follow-link-candidate"])))
  95. (with-test-backend-browse '(1 0 1) 1 "links.html"
  96. (split-window-vertically)
  97. (save-excursion (switch-to-buffer-other-window "*Messages*"))
  98. (xwidget-plus-follow-link)
  99. (xwidget-plus-event-dispatch)
  100. (should (string= "test-2.html" (file-name-nondirectory (xwidget-webkit-current-url))))
  101. (should (equal (backend-test-link-classes backend "test-1") '["xwidget-plus-follow-link-candidate"]))
  102. (should (equal (backend-test-link-classes backend "test-2") '["xwidget-plus-follow-link-selected"]))))
  103. (defmacro with-read-fixtures (backend &rest body)
  104. (declare (indent 1))
  105. `(let* ((links '(("test 1" . 0) ("test 2" . 1)))
  106. link
  107. (action (lambda (l) (setq link l)))
  108. (update (lambda ())))
  109. (with-backend ,backend
  110. (with-browse "links.html"
  111. ,@body))))
  112. (ert-deftest test-xwidget-plus-follow-link-read-default ()
  113. (with-read-fixtures default
  114. (with-simulated-input "test SPC 2 RET"
  115. (xwidget-plus-follow-link-read backend "Test: " links action update)
  116. (should (= 1 link)))))
  117. (ert-deftest test-xwidget-plus-follow-link-read-ido ()
  118. (with-read-fixtures ido
  119. (with-simulated-input "2 RET"
  120. (xwidget-plus-follow-link-read backend "Test: " links action update)
  121. (should (= 1 link)))))
  122. (ert-deftest test-xwidget-plus-follow-link-read-ivy ()
  123. (with-read-fixtures ivy
  124. (with-simulated-input "2 RET"
  125. (xwidget-plus-follow-link-read backend "Test: " links action update)
  126. (should (= 1 link)))))
  127. (ert-deftest test-xwidget-plus-follow-link-read-helm ()
  128. (with-read-fixtures helm
  129. (with-simulated-input '("2" (wsi-simulate-idle-time 0.1) "RET")
  130. (xwidget-plus-follow-link-read backend "Test: " links action update)
  131. (should (= 1 link)))))
  132. ;; Local Variables:
  133. ;; eval: (mmm-mode)
  134. ;; eval: (mmm-add-classes '((elisp-js :submode js-mode :face mmm-code-submode-face :delimiter-mode nil :front "--js \"" :back "\" js--")))
  135. ;; mmm-classes: elisp-js
  136. ;; End:
  137. (provide 'xwidget-plus-follow-link-test)
  138. ;;; xwidget-plus-follow-link-test.el ends here