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

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