xwwp-follow-link-test.el 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. ;;; xwwp-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 'cl-lib)
  17. (require 'with-simulated-input)
  18. (require 'xwwp-follow-link)
  19. (require 'xwwp-follow-link-ido)
  20. (require 'xwwp-follow-link-ivy)
  21. (require 'xwwp-follow-link-helm)
  22. (setq completing-read-function #'completing-read-default)
  23. ;; Some usefull javascript
  24. (xwwp-js-def test element-classes (selector)
  25. "Fetch the list of css class for element matching SELECTOR.""
  26. map = Array.prototype.map;
  27. r = {};
  28. document.querySelectorAll(selector).forEach(l => {
  29. r[l.id] = map.call(l.classList, t => {
  30. return t.toString();
  31. });
  32. });
  33. return r;
  34. ")
  35. (xwwp-js-def test current-location ()
  36. "Fetch the current url.""
  37. return '' + window.location;
  38. ")
  39. ;; A mocked backend class that doesn't interactively read anything.
  40. (defclass xwwp-follow-link-completion-backend-test (xwwp-follow-link-completion-backend)
  41. ((candidates-mock :initarg :candidates-mock)
  42. (selected-mock :initarg :selected-mock)
  43. (action-fn)
  44. (classes)
  45. (location)))
  46. (defun xwwp-update-fn-callback (result)
  47. "Called after updating candidates with the css classes in RESULT."
  48. (let ((backend xwwp-follow-link-completion-backend-instance))
  49. ;; Store the results.
  50. (oset backend classes (seq-map #'identity result))
  51. ;; Trigger the action function with the mocked selected link
  52. (funcall (oref backend action-fn) (oref backend selected-mock))
  53. (xwwp-event-dispatch)))
  54. (defun xwwp-location-callback (result)
  55. "Called after updating candidates with the css classes in RESULT."
  56. (let ((backend xwwp-follow-link-completion-backend-instance))
  57. ;; Store the results.
  58. (oset backend location result)))
  59. (cl-defmethod xwwp-follow-link-candidates ((backend xwwp-follow-link-completion-backend-test))
  60. "Return the list of BACKEND mocked candidates."
  61. (oref backend candidates-mock))
  62. (cl-defmethod xwwp-follow-link-read ((backend xwwp-follow-link-completion-backend-test) _ _ action-fn update-fn)
  63. "Store ACTION-FN in BACKEND, call the UPDATE-FN, and fetch the link element classes."
  64. ;; Stoire action so that we can call it after having fetch the css classes.
  65. (oset backend action-fn action-fn)
  66. ;; Trigger the javascript update.
  67. (funcall update-fn)
  68. ;; Fetch css classes.
  69. (xwwp-js-inject (xwidget-webkit-current-session) 'test)
  70. (xwwp-test-element-classes (xwidget-webkit-current-session) "a" #'xwwp-update-fn-callback)
  71. (xwwp-event-dispatch))
  72. (cl-defmethod backend-test-link-classes ((backend xwwp-follow-link-completion-backend-test) link-id)
  73. "Return test BACKEND css class names for LINK-ID."
  74. (cdr (assoc link-id (oref backend classes))))
  75. (defmacro with-backend (backend &rest body)
  76. "Run BODY with the specified BACKEND."
  77. (declare (indent 1))
  78. `(let* ((backend (,(intern (concat "xwwp-follow-link-completion-backend-" (symbol-name backend)))))
  79. (xwwp-follow-link-completion-backend-instance backend))
  80. ,@body))
  81. (defmacro with-test-backend-browse (candidates selected url &rest body)
  82. "Run BODY with the specified BACKEND mocking CANDIDATES and SELECTED while browsing URL."
  83. (declare (indent 3))
  84. `(let* ((xwwp-follow-link-completion-system
  85. (lambda () (xwwp-follow-link-completion-backend-test :candidates-mock ,candidates
  86. :selected-mock ,selected))))
  87. (with-browse ,url
  88. ,@body)))
  89. (ert-deftest test-xwwp-follow-link-prepare-links ()
  90. (let ((links '(("3" . "Functions")
  91. ("1" . "Function Cells")
  92. ("12" . "Structures")
  93. ("2" . "Anonymous Functions")
  94. ("9" . "Declare Form"))))
  95. (should (equal '(("Function Cells" . 1)
  96. ("Anonymous Functions" . 2)
  97. ("Functions" . 3)
  98. ("Declare Form" . 9)
  99. ("Structures" . 12))
  100. (xwwp-follow-link-prepare-links links)))))
  101. (ert-deftest test-xwwp-follow-link-highlight ()
  102. (with-test-backend-browse '(0 0 1) 0 "links.html"
  103. (xwwp-follow-link)
  104. (xwwp-event-loop)
  105. (let ((backend xwwp-follow-link-completion-backend-instance))
  106. (xwwp-js-inject xwidget 'test)
  107. (xwwp-test-current-location xwidget #'xwwp-location-callback)
  108. (xwwp-event-dispatch)
  109. (should (string= "test-1.html" (file-name-nondirectory (oref backend location))))
  110. (should (equal (backend-test-link-classes backend "test-1") '["xwwp-follow-link-selected"]))
  111. (should (equal (backend-test-link-classes backend "test-2") '["xwwp-follow-link-candidate"]))))
  112. (with-test-backend-browse '(1 0 1) 1 "links.html"
  113. (xwwp-follow-link)
  114. (xwwp-event-loop)
  115. (let ((backend xwwp-follow-link-completion-backend-instance))
  116. (xwwp-js-inject xwidget 'test)
  117. (xwwp-test-current-location xwidget #'xwwp-location-callback)
  118. (xwwp-event-dispatch)
  119. (should (string= "test-2.html" (file-name-nondirectory (oref backend location))))
  120. (should (equal (backend-test-link-classes backend "test-1") '["xwwp-follow-link-candidate"]))
  121. (should (equal (backend-test-link-classes backend "test-2") '["xwwp-follow-link-selected"])))))
  122. (ert-deftest test-xwwp-follow-link-highlight-no-candidates ()
  123. (with-test-backend-browse '(1) 1 "links.html"
  124. (xwwp-follow-link)
  125. (xwwp-event-loop)
  126. (let ((backend xwwp-follow-link-completion-backend-instance))
  127. (xwwp-js-inject xwidget 'test)
  128. (xwwp-test-current-location xwidget #'xwwp-location-callback)
  129. (xwwp-event-dispatch)
  130. (should (string= "test-2.html" (file-name-nondirectory (oref backend location))))
  131. (should (equal (backend-test-link-classes backend "test-1") '[]))
  132. (should (equal (backend-test-link-classes backend "test-2") '["xwwp-follow-link-selected"])))))
  133. (defmacro with-read-fixtures (backend &rest body)
  134. (declare (indent 1))
  135. `(let* ((links '(("test 1" . 0) ("test 2" . 1)))
  136. link
  137. (action (lambda (l) (setq link l)))
  138. (update (lambda ())))
  139. (with-backend ,backend
  140. (with-browse "links.html"
  141. ,@body))))
  142. (ert-deftest test-xwwp-follow-link-read-default ()
  143. (with-read-fixtures default
  144. (with-simulated-input "test SPC 2 RET"
  145. (xwwp-follow-link-read backend "Test: " links action update)
  146. (should (= 1 link)))))
  147. (ert-deftest test-xwwp-follow-link-read-ido ()
  148. (require 'ido)
  149. (with-read-fixtures ido
  150. (with-simulated-input "2 RET"
  151. (xwwp-follow-link-read backend "Test: " links action update)
  152. (should (= 1 link)))))
  153. (ert-deftest test-xwwp-follow-link-read-ivy ()
  154. (require 'ivy)
  155. (with-read-fixtures ivy
  156. (with-simulated-input "2 RET"
  157. (xwwp-follow-link-read backend "Test: " links action update)
  158. (should (= 1 link)))))
  159. (ert-deftest test-xwwp-follow-link-read-helm ()
  160. (require 'helm)
  161. (with-read-fixtures helm
  162. (with-simulated-input '("2" (wsi-simulate-idle-time 0.1) "RET")
  163. (xwwp-follow-link-read backend "Test: " links action update)
  164. (should (= 1 link)))))
  165. (defmacro with-feature (feature &rest body)
  166. (declare (indent 1))
  167. (let ((fsym (intern (concat "xwwp-follow-link-" (symbol-name feature)))))
  168. `(cl-letf (((symbol-function 'require) (lambda (f &optional filename no-errors) (eq (quote ,fsym) f))))
  169. ,@body)))
  170. (ert-deftest test-xwwp-follow-link-make-backend-use-custom ()
  171. (let ((xwwp-follow-link-completion-system 'default))
  172. (with-feature nil
  173. (should (eq #'xwwp-follow-link-completion-backend-default (xwwp-follow-link-make-backend)))))
  174. (let ((xwwp-follow-link-completion-system 'ido))
  175. (with-feature ido
  176. (should (eq #'xwwp-follow-link-completion-backend-ido (xwwp-follow-link-make-backend)))))
  177. (let ((xwwp-follow-link-completion-system 'ivy))
  178. (with-feature ivy
  179. (should (eq #'xwwp-follow-link-completion-backend-ivy (xwwp-follow-link-make-backend)))))
  180. (let ((xwwp-follow-link-completion-system 'helm))
  181. (with-feature helm
  182. (should (eq #'xwwp-follow-link-completion-backend-helm (xwwp-follow-link-make-backend)))))
  183. (let ((xwwp-follow-link-completion-system #'identity))
  184. (should (eq #'identity (xwwp-follow-link-make-backend)))))
  185. ;; Local Variables:
  186. ;; eval: (mmm-mode)
  187. ;; eval: (mmm-add-group 'elisp-js '((elisp-rawjs :submode js-mode
  188. ;; :face mmm-code-submode-face
  189. ;; :delimiter-mode nil
  190. ;; :front "xwwp--js \"" :back "\" js--")
  191. ;; (elisp-defjs :submode js-mode
  192. ;; :face mmm-code-submode-face
  193. ;; :delimiter-mode nil
  194. ;; :front "xwwp-js-def .*\n.*\"\"\n" :back "\")\n")))
  195. ;; mmm-classes: elisp-js
  196. ;; End:
  197. (provide 'xwwp-follow-link-test)
  198. ;;; xwwp-follow-link-test.el ends here