xt-mouse-tests.el 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ;;; xt-mouse-tests.el --- Test suite for xt-mouse. -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2016-2017 Free Software Foundation, Inc.
  3. ;; Author: Philipp Stephani <phst@google.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;; Code:
  17. (require 'xt-mouse)
  18. (defmacro with-xterm-mouse-mode (&rest body)
  19. "Run BODY with `xterm-mouse-mode' temporarily enabled."
  20. (declare (indent 0))
  21. ;; Make the frame huge so that the test input events below don't hit
  22. ;; the menu bar.
  23. `(cl-letf (((frame-width nil) 2000)
  24. ((frame-height nil) 2000)
  25. ;; Reset XTerm parameters so that the tests don't get
  26. ;; confused.
  27. ((terminal-parameter nil 'xterm-mouse-x) nil)
  28. ((terminal-parameter nil 'xterm-mouse-y) nil)
  29. ((terminal-parameter nil 'xterm-mouse-last-down) nil)
  30. ((terminal-parameter nil 'xterm-mouse-last-click) nil))
  31. (if xterm-mouse-mode
  32. (progn ,@body)
  33. (unwind-protect
  34. (progn
  35. ;; `xterm-mouse-mode' doesn't work in the initial
  36. ;; terminal. Since we can't create a second terminal in
  37. ;; batch mode, fake it temporarily.
  38. (cl-letf (((symbol-function 'terminal-name)
  39. (lambda (&optional _terminal) "fake-terminal")))
  40. (xterm-mouse-mode))
  41. ,@body)
  42. (xterm-mouse-mode 0)))))
  43. (ert-deftest xt-mouse-tracking-basic ()
  44. (should (equal (xterm-mouse-tracking-enable-sequence)
  45. "\e[?1000h\e[?1002h\e[?1006h"))
  46. (should (equal (xterm-mouse-tracking-disable-sequence)
  47. "\e[?1006l\e[?1002l\e[?1000l"))
  48. (with-xterm-mouse-mode
  49. (should xterm-mouse-mode)
  50. (should (terminal-parameter nil 'xterm-mouse-mode))
  51. (should-not (terminal-parameter nil 'xterm-mouse-utf-8))
  52. (let* ((unread-command-events (append "\e[M%\xD9\x81"
  53. "\e[M'\xD9\x81" nil))
  54. (key (read-key)))
  55. (should (consp key))
  56. (cl-destructuring-bind (event-type position . rest) key
  57. (should (equal event-type 'S-mouse-2))
  58. (should (consp position))
  59. (cl-destructuring-bind (_ _ xy . rest) position
  60. (should (equal xy '(184 . 95))))))))
  61. (ert-deftest xt-mouse-tracking-utf-8 ()
  62. (let ((xterm-mouse-utf-8 t))
  63. (should (equal (xterm-mouse-tracking-enable-sequence)
  64. "\e[?1000h\e[?1002h\e[?1005h\e[?1006h"))
  65. (should (equal (xterm-mouse-tracking-disable-sequence)
  66. "\e[?1006l\e[?1005l\e[?1002l\e[?1000l"))
  67. (with-xterm-mouse-mode
  68. (should xterm-mouse-mode)
  69. (should (terminal-parameter nil 'xterm-mouse-mode))
  70. (should (terminal-parameter nil 'xterm-mouse-utf-8))
  71. ;; The keyboard driver doesn't decode bytes in
  72. ;; `unread-command-events'.
  73. (let* ((unread-command-events (append "\e[M%\u0640\u0131"
  74. "\e[M'\u0640\u0131" nil))
  75. (key (read-key)))
  76. (should (consp key))
  77. (cl-destructuring-bind (event-type position . rest) key
  78. (should (equal event-type 'S-mouse-2))
  79. (should (consp position))
  80. (cl-destructuring-bind (_ _ xy . rest) position
  81. (should (equal xy '(1567 . 271)))))))))
  82. (ert-deftest xt-mouse-tracking-sgr ()
  83. (with-xterm-mouse-mode
  84. (should xterm-mouse-mode)
  85. (should (terminal-parameter nil 'xterm-mouse-mode))
  86. (should-not (terminal-parameter nil 'xterm-mouse-utf-8))
  87. (let* ((unread-command-events (append "\e[<5;1569;273;M"
  88. "\e[<5;1569;273;m" nil))
  89. (key (read-key)))
  90. (should (consp key))
  91. (cl-destructuring-bind (event-type position . rest) key
  92. (should (equal event-type 'S-mouse-2))
  93. (should (consp position))
  94. (cl-destructuring-bind (_ _ xy . rest) position
  95. (should (equal xy '(1568 . 271))))))))
  96. ;;; xt-mouse-tests.el ends here