test-helper.el 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. (unless frames (setq frames (backtrace-get-frames 'backtrace-to-string)))
  20. (let ((backtrace-fontify nil))
  21. (with-temp-buffer
  22. (backtrace-mode)
  23. (setq backtrace-view '(:show-flags t)
  24. backtrace-frames frames
  25. backtrace-print-function #'cl-prin1)
  26. (backtrace-print)
  27. (ert-runner-message "%s" (substring-no-properties (filter-buffer-substring (point-min)
  28. (point-max))))))))
  29. (defconst xwwp-test-path (file-name-as-directory
  30. (file-name-directory (or load-file-name buffer-file-name)))
  31. "The test directory.")
  32. (defconst xwwp-test-data-path (file-name-as-directory
  33. (concat xwwp-test-path "data"))
  34. "The test data directory.")
  35. (defconst xwwp-root-path (file-name-as-directory
  36. (file-name-directory
  37. (directory-file-name xwwp-test-path)))
  38. "The package root path.")
  39. (add-to-list 'load-path xwwp-root-path)
  40. (defun xwwp-event-dispatch (&optional seconds)
  41. (save-excursion
  42. (with-current-buffer (xwidget-buffer (xwidget-webkit-last-session))
  43. (let ((event (read-event nil nil seconds)))
  44. (when event
  45. (message "event:%s " event)
  46. (xwidget-event-handler))
  47. event))))
  48. (defun xwwp-event-loop ()
  49. (save-excursion
  50. (with-current-buffer (xwidget-buffer (xwidget-webkit-last-session))
  51. (while (xwwp-event-dispatch 0.3)))))
  52. (defmacro with-browse (file &rest body)
  53. (declare (indent 1))
  54. (let ((url (format "file://%s%s" (expand-file-name xwwp-test-data-path) file)))
  55. `(progn
  56. (xwidget-webkit-browse-url ,url)
  57. ;; this will trigger a loading event
  58. (xwwp-event-dispatch)
  59. (let ((xwidget (xwidget-webkit-last-session)))
  60. (xwwp-js-inject xwidget 'test)
  61. (xwwp-event-loop)
  62. (with-current-buffer (xwidget-buffer xwidget)
  63. ,@body)))))
  64. (defun xwwp-wait-for (script)
  65. "Wait until scripts evaluate to true")
  66. ;;; test-helper.el ends here