aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorPo Lu2022-07-01 11:31:25 +0800
committerPo Lu2022-07-01 11:31:25 +0800
commit23df6df775c7cb88534ea310287ff9b057cc98f9 (patch)
treeb4098542edf3815c9ef859d036456d4b72834300 /test
parent2289fafeaf4dc21c0c9751a3a195d855bf5e91f8 (diff)
downloademacs-23df6df775c7cb88534ea310287ff9b057cc98f9.tar.gz
emacs-23df6df775c7cb88534ea310287ff9b057cc98f9.zip
Add tests for XDS protocol support
* test/lisp/x-dnd-tests.el (x-dnd-tests-xds-property-value): New variable. (x-window-property): Handle new kind of window property. (x-dnd-tests-xds-target-dir, x-dnd-tests-xds-name) (x-dnd-tests-xds-include-hostname): New variables. (x-dnd-tests-call-xds-converter): New function. (x-begin-drag, x-change-window-property): (x-delete-window-property): New replacement functions. (x-dnd-tests-do-direct-save-internal): New function. (x-dnd-tests-do-direct-save): New test.
Diffstat (limited to 'test')
-rw-r--r--test/lisp/x-dnd-tests.el125
1 files changed, 123 insertions, 2 deletions
diff --git a/test/lisp/x-dnd-tests.el b/test/lisp/x-dnd-tests.el
index 35cda3b10a0..8856be79ebc 100644
--- a/test/lisp/x-dnd-tests.el
+++ b/test/lisp/x-dnd-tests.el
@@ -24,6 +24,7 @@
24;;; Code: 24;;; Code:
25 25
26(require 'x-dnd) 26(require 'x-dnd)
27(require 'cl-lib)
27 28
28(when (display-graphic-p) 29(when (display-graphic-p)
29 (error "This test cannot be run under X")) 30 (error "This test cannot be run under X"))
@@ -33,6 +34,9 @@
33(defconst x-dnd-tests-drag-window-xid 3948573 34(defconst x-dnd-tests-drag-window-xid 3948573
34 "XID of the drag window returned during the test.") 35 "XID of the drag window returned during the test.")
35 36
37(defvar x-dnd-tests-xds-property-value nil
38 "The value of the `XdndDirectSave0' window property.")
39
36(defconst x-dnd-tests-targets-table 40(defconst x-dnd-tests-targets-table
37 (base64-decode-string 41 (base64-decode-string
38 "bAArAKIBAAAGAB8AAABqAQAANgIAAJMCAAAFAwAABgMAAAEAkMJbAAEAINNbAAUAHwAAAGoBAAA2 42 "bAArAKIBAAAGAB8AAABqAQAANgIAAJMCAAAFAwAABgMAAAEAkMJbAAEAINNbAAUAHwAAAGoBAAA2
@@ -62,7 +66,7 @@ AgAABQMAAAYDAAATGwAAGhsAAA==")
62 "The expected result of parsing that targets table.") 66 "The expected result of parsing that targets table.")
63 67
64(defalias 'x-window-property 68(defalias 'x-window-property
65 (lambda (prop &optional _frame type window-id _delete-p _vector-ret-p) 69 (lambda (prop &optional _frame type window-id delete-p _vector-ret-p)
66 (cond 70 (cond
67 ((and (equal prop "_MOTIF_DRAG_WINDOW") 71 ((and (equal prop "_MOTIF_DRAG_WINDOW")
68 (zerop window-id) (equal type "WINDOW")) 72 (zerop window-id) (equal type "WINDOW"))
@@ -70,7 +74,13 @@ AgAABQMAAAYDAAATGwAAGhsAAA==")
70 ((and (equal prop "_MOTIF_DRAG_TARGETS") 74 ((and (equal prop "_MOTIF_DRAG_TARGETS")
71 (equal type "_MOTIF_DRAG_TARGETS") 75 (equal type "_MOTIF_DRAG_TARGETS")
72 (equal window-id x-dnd-tests-drag-window-xid)) 76 (equal window-id x-dnd-tests-drag-window-xid))
73 x-dnd-tests-targets-table)))) 77 x-dnd-tests-targets-table)
78 ((and (equal prop "XdndDirectSave0")
79 (or (equal type "text/plain")
80 (equal type "AnyPropertyType")))
81 (prog1 x-dnd-tests-xds-property-value
82 (when delete-p
83 (setq x-dnd-tests-xds-property-value nil)))))))
74 84
75;; This test also serves to exercise most of the Motif value 85;; This test also serves to exercise most of the Motif value
76;; extraction code. 86;; extraction code.
@@ -78,5 +88,116 @@ AgAABQMAAAYDAAATGwAAGhsAAA==")
78 (should (equal (x-dnd-xm-read-targets-table nil) 88 (should (equal (x-dnd-xm-read-targets-table nil)
79 x-dnd-tests-lispy-targets-table))) 89 x-dnd-tests-lispy-targets-table)))
80 90
91;;; XDS tests.
92
93(defvar x-dnd-tests-xds-target-dir nil
94 "The name of the target directory where the file will be saved.")
95
96(defvar x-dnd-tests-xds-name nil
97 "The name that the dragged file should be saved under.")
98
99(defvar x-dnd-tests-xds-include-hostname nil
100 "Whether or not to include the hostname inside the XDS URI.")
101
102(defun x-dnd-tests-call-xds-converter ()
103 "Look up the XDS selection converter and call it.
104Return the result of the selection."
105 (let ((conv (cdr (assq 'XdndDirectSave0
106 selection-converter-alist))))
107 (should (functionp conv))
108 (funcall conv 'XdndDirectSave0 'XdndDirectSave0 nil)))
109
110(defalias 'x-begin-drag
111 (lambda (_targets &optional action frame &rest _)
112 ;; Verify that frame is either nil or a valid frame.
113 (when (and frame (not (frame-live-p frame)))
114 (signal 'wrong-type-argument frame))
115 (prog1 'XdndActionDirectSave
116 ;; Verify that the action is `XdndActionDirectSave'.
117 (should (eq action 'XdndActionDirectSave))
118 ;; Set the property value to the URI of the new file.
119 (should (and (stringp x-dnd-tests-xds-property-value)
120 (not (multibyte-string-p x-dnd-tests-xds-property-value))))
121 (let ((uri (if x-dnd-tests-xds-include-hostname
122 (format "file://%s%s" (system-name)
123 (expand-file-name x-dnd-tests-xds-property-value
124 x-dnd-tests-xds-target-dir))
125 (concat "file:///" (expand-file-name x-dnd-tests-xds-property-value
126 x-dnd-tests-xds-target-dir)))))
127 (setq x-dnd-tests-xds-property-value
128 (encode-coding-string (url-encode-url uri)
129 'raw-text)))
130 ;; Convert the selection and verify its success.
131 (should (equal (x-dnd-tests-call-xds-converter)
132 '(STRING . "S"))))))
133
134(defalias 'x-change-window-property
135 (lambda (prop value &optional _frame type format outer-p _window-id)
136 ;; Check that the properties are the right type.
137 (should (equal prop "XdndDirectSave0"))
138 (should (equal value (encode-coding-string
139 x-dnd-tests-xds-name
140 (or file-name-coding-system
141 default-file-name-coding-system))))
142 (should (equal type "text/plain"))
143 (should (equal format 8))
144 (should (not outer-p))
145 (setq x-dnd-tests-xds-property-value value)))
146
147(defalias 'x-delete-window-property
148 (lambda (&rest _args)
149 ;; This function shouldn't ever be reached during XDS.
150 (setq x-dnd-tests-xds-property-value nil)))
151
152(defun x-dnd-tests-do-direct-save-internal (include-hostname)
153 "Test the behavior of `x-dnd-do-direct-save'.
154Make it perform a direct save to a randomly generated directory,
155and check that the file exists. If INCLUDE-HOSTNAME, include the
156hostname in the target URI."
157 (let ((x-dnd-tests-xds-include-hostname include-hostname)
158 (x-dnd-tests-xds-target-dir
159 (file-name-as-directory (expand-file-name
160 (make-temp-name "x-dnd-test")
161 temporary-file-directory)))
162 (original-file (expand-file-name
163 (make-temp-name "x-dnd-test")
164 temporary-file-directory))
165 (x-dnd-tests-xds-name (make-temp-name "x-dnd-test-target")))
166 ;; The call to `gui-set-selection' is only used for providing the
167 ;; conventional `text/uri-list' target and can be ignored.
168 (cl-flet ((gui-set-selection #'ignore))
169 (unwind-protect
170 (progn
171 ;; Touch `original-file' if it doesn't exist.
172 (unless (file-exists-p original-file)
173 (write-region "" 0 original-file))
174 ;; Create `x-dnd-tests-xds-target-dir'.
175 (make-directory x-dnd-tests-xds-target-dir)
176 ;; Start the direct save and verify it returns the correct action.
177 (should (eq (x-dnd-do-direct-save original-file
178 x-dnd-tests-xds-name
179 nil nil)
180 'XdndActionDirectSave))
181 ;; Now verify that the new file exists.
182 (should (file-exists-p
183 (expand-file-name x-dnd-tests-xds-name
184 x-dnd-tests-xds-target-dir)))
185 ;; The XDS protocol makes very clear that the window
186 ;; property must be deleted after the drag-and-drop
187 ;; operation completes.
188 (should (not x-dnd-tests-xds-property-value)))
189 ;; Clean up after ourselves.
190 (ignore-errors
191 (delete-file original-file))
192 (ignore-errors
193 (delete-directory x-dnd-tests-xds-target-dir t))))))
194
195(ert-deftest x-dnd-tests-do-direct-save ()
196 ;; TODO: add tests for application/octet-stream transfer.
197 (x-dnd-tests-do-direct-save-internal nil)
198 ;; Test with both kinds of file: URIs, since different programs
199 ;; generate different kinds.
200 (x-dnd-tests-do-direct-save-internal t))
201
81(provide 'x-dnd-tests) 202(provide 'x-dnd-tests)
82;;; x-dnd-tests.el ends here 203;;; x-dnd-tests.el ends here