test-helper.el 2.5 KB

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