test-helper.el 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ;;; test-helper.el -- Test helpers for xwidget-webkit-plus
  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. ;;
  15. ;;; Code:
  16. (when (> emacs-major-version 26)
  17. (defun ert--print-backtrace (frames)
  18. (insert (backtrace-to-string frames))))
  19. (defconst xwidget-plus-test-path (file-name-as-directory
  20. (file-name-directory (or load-file-name buffer-file-name)))
  21. "The test directory.")
  22. (defconst xwidget-plus-test-data-path (file-name-as-directory
  23. (concat xwidget-plus-test-path "data"))
  24. "The test data directory.")
  25. (defconst xwidget-plus-root-path (file-name-as-directory
  26. (file-name-directory
  27. (directory-file-name xwidget-plus-test-path)))
  28. "The package root path.")
  29. (add-to-list 'load-path xwidget-plus-root-path)
  30. (defun xwidget-plus-event-dispatch (&optional seconds)
  31. (save-excursion
  32. (with-current-buffer (xwidget-buffer (xwidget-webkit-last-session))
  33. (let ((event (read-event nil nil seconds)))
  34. (when event
  35. (message "event:%s " event)
  36. (xwidget-event-handler))
  37. event))))
  38. (defun xwidget-plus-event-loop ()
  39. (save-excursion
  40. (with-current-buffer (xwidget-buffer (xwidget-webkit-last-session))
  41. (while (xwidget-plus-event-dispatch 0.3)))))
  42. (defmacro with-browse (file &rest body)
  43. (declare (indent 1))
  44. (let ((url (format "file://%s%s" (expand-file-name xwidget-plus-test-data-path) file)))
  45. `(progn
  46. (xwidget-webkit-browse-url ,url)
  47. ;; this will trigger a loading event
  48. (xwidget-plus-event-dispatch)
  49. (let ((xwidget (xwidget-webkit-last-session)))
  50. (xwidget-plus-js-inject xwidget 'test)
  51. (xwidget-plus-event-loop)
  52. (with-current-buffer (xwidget-buffer xwidget)
  53. ,@body)))))
  54. (defun xwidget-plus-wait-for (script)
  55. "Wait until scripts evaluate to true")
  56. (provide 'test-helper)
  57. ;;; test-helper.el ends here