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