xwwp-follow-link-test.el 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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" "http://www.this.is.a.test.de/functions"])
  91. ("1" . ["Function Cells" "http://www.this.is.a.test.de/function-cells"])
  92. ("12" . ["Structures" "http://www.this.is.a.test.de/structures"])
  93. ("2" . ["Anonymous Functions" "http://www.this.is.a.test.de/anon"])
  94. ("9" . ["Declare Form" "http://www.this.is.a.test.de/declare-form"]))))
  95. (should (equal '(("Function Cells" . (1 "http://www.this.is.a.test.de/function-cells"))
  96. ("Anonymous Functions" . (2 "http://www.this.is.a.test.de/anon"))
  97. ("Functions" . (3 "http://www.this.is.a.test.de/functions"))
  98. ("Declare Form" . (9 "http://www.this.is.a.test.de/declare-form"))
  99. ("Structures" . (12 "http://www.this.is.a.test.de/structures")))
  100. (xwwp-follow-link-prepare-links links)))))
  101. (ert-deftest test-xwwp-follow-link-highlight ()
  102. (with-test-backend-browse '((0 "http://") (0 "http://") (1 "http://")) '(0 "http://") "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 "http://") (0 "http://") (1 "http://")) '(1 "http://") "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. (with-test-backend-browse '((1 "http://")) '(1 "http://") "links.html"
  123. (xwwp-follow-link)
  124. (xwwp-event-loop)
  125. (let ((backend xwwp-follow-link-completion-backend-instance))
  126. (xwwp-js-inject xwidget 'test)
  127. (xwwp-test-current-location xwidget #'xwwp-location-callback)
  128. (xwwp-event-dispatch)
  129. (should (string= "test-2.html" (file-name-nondirectory (oref backend location))))
  130. (should (equal (backend-test-link-classes backend "test-1") '[]))
  131. (should (equal (backend-test-link-classes backend "test-2") '["xwwp-follow-link-selected"])))))
  132. (defmacro with-read-fixtures (backend &rest body)
  133. (declare (indent 1))
  134. `(let* ((links '(("test 1" . 0) ("test 2" . 1)))
  135. link
  136. (action (lambda (l) (setq link l)))
  137. (update (lambda ())))
  138. (with-backend ,backend
  139. (with-browse "links.html"
  140. ,@body))))
  141. (ert-deftest test-xwwp-follow-link-read-default ()
  142. (with-read-fixtures default
  143. (with-simulated-input "test SPC 2 RET"
  144. (xwwp-follow-link-read backend "Test: " links action update)
  145. (should (= 1 link)))))
  146. (ert-deftest test-xwwp-follow-link-read-ido ()
  147. (require 'ido)
  148. (with-read-fixtures ido
  149. (with-simulated-input "2 RET"
  150. (xwwp-follow-link-read backend "Test: " links action update)
  151. (should (= 1 link)))))
  152. (ert-deftest test-xwwp-follow-link-read-ivy ()
  153. (require 'ivy)
  154. (with-read-fixtures ivy
  155. (with-simulated-input "2 RET"
  156. (xwwp-follow-link-read backend "Test: " links action update)
  157. (should (= 1 link)))))
  158. (ert-deftest test-xwwp-follow-link-read-helm ()
  159. (require 'helm)
  160. (with-read-fixtures helm
  161. (with-simulated-input '("2" (wsi-simulate-idle-time 0.1) "RET")
  162. (xwwp-follow-link-read backend "Test: " links action update)
  163. (should (= 1 link)))))
  164. (defmacro with-feature (feature &rest body)
  165. (declare (indent 1))
  166. (let ((fsym (intern (concat "xwwp-follow-link-" (symbol-name feature)))))
  167. `(cl-letf (((symbol-function 'require) (lambda (f &optional filename no-errors) (eq (quote ,fsym) f))))
  168. ,@body)))
  169. (ert-deftest test-xwwp-follow-link-make-backend-use-custom ()
  170. (let ((xwwp-follow-link-completion-system 'default))
  171. (with-feature nil
  172. (should (eq #'xwwp-follow-link-completion-backend-default (xwwp-follow-link-make-backend)))))
  173. (let ((xwwp-follow-link-completion-system 'ido))
  174. (with-feature ido
  175. (should (eq #'xwwp-follow-link-completion-backend-ido (xwwp-follow-link-make-backend)))))
  176. (let ((xwwp-follow-link-completion-system 'ivy))
  177. (with-feature ivy
  178. (should (eq #'xwwp-follow-link-completion-backend-ivy (xwwp-follow-link-make-backend)))))
  179. (let ((xwwp-follow-link-completion-system 'helm))
  180. (with-feature helm
  181. (should (eq #'xwwp-follow-link-completion-backend-helm (xwwp-follow-link-make-backend)))))
  182. (let ((xwwp-follow-link-completion-system #'identity))
  183. (should (eq #'identity (xwwp-follow-link-make-backend)))))
  184. ;; Local Variables:
  185. ;; eval: (mmm-mode)
  186. ;; eval: (mmm-add-group 'elisp-js '((elisp-rawjs :submode js-mode
  187. ;; :face mmm-code-submode-face
  188. ;; :delimiter-mode nil
  189. ;; :front "xwwp--js \"" :back "\" js--")
  190. ;; (elisp-defjs :submode js-mode
  191. ;; :face mmm-code-submode-face
  192. ;; :delimiter-mode nil
  193. ;; :front "xwwp-js-def .*\n.*\"\"\n" :back "\")\n")))
  194. ;; mmm-classes: elisp-js
  195. ;; End:
  196. (provide 'xwwp-follow-link-test)
  197. ;;; xwwp-follow-link-test.el ends here