-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.el
471 lines (406 loc) · 20.5 KB
/
test.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
;;; test.el --- EDNC tests -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Simon Nicolussi
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(require 'ednc)
(require 'notifications)
(require 'subr-x)
;; Load the use cases documented in README.org:
(require 'ob-tangle)
(load-file (car (org-babel-tangle-file "README.org")))
;; Helpers for testing:
(defmacro ednc--with-temp-server (&rest body)
(declare (indent defun))
`(let ((ednc-log-name "*ednc-test-log*")
(ednc-notification-amendment-functions)
(ednc-notification-presentation-functions)
(ednc--state (list 0)))
(skip-unless (not ednc-mode)) ; conflicting servers?
(ednc--start-server)
,@body
(ednc--test-state-consistency)
(ednc--stop-server)
(when (get-buffer ednc-log-name)
(kill-buffer ednc-log-name))))
(defconst ednc--default-test-args
'(:title "foo" :body "bar baz" :app-name "test" :replaces-id 0 :app-icon nil
:actions ("default" "qux" "other" "quux") :timeout 0))
(defun ednc--test-arg-matches (notification slot plist prop)
(should (equal (cl-struct-slot-value 'ednc-notification slot notification)
(plist-get plist prop))))
(defun ednc--test-args-match (id args)
(let ((found (cl-find id (cdr ednc--state) :key #'ednc-notification-id)))
(ednc--test-arg-matches found 'summary args :title)
(when (plist-get args :body)
(ednc--test-arg-matches found 'body args :body))
(ednc--test-arg-matches found 'app-name args :app-name)
(if (zerop (plist-get args :replaces-id))
(should-not (zerop (ednc-notification-id found)))
(ednc--test-arg-matches found 'id args :replaces-id))
;(ednc--test-arg-matches found 'app-icon args :app-icon)
(ednc--test-arg-matches found 'actions args :actions)
(if (zerop (plist-get args :timeout))
(should-not (ednc-notification-timer found))
(should (timerp (ednc-notification-timer found))))
(should (ednc-notification-client found))
(should (ednc-notification-parent found))))
(defun ednc--test-state-consistency ()
(let ((distinct (car ednc--state))
(ids (mapcar #'ednc-notification-id (cdr ednc--state))))
(should (and (integerp distinct) (>= distinct 0))) ; non-negative integer,
(should (<= (if ids (apply #'max ids) 0) distinct)) ; bounded from above,
(should (= (length ids) (length (delete-dups ids))))) ; without duplicates,
(dolist (notification (cdr ednc--state)) ; and with consistent elements?
(ednc--test-notification-consistency notification)))
(defun ednc--test-notification-consistency (notification)
(let ((id (ednc-notification-id notification)))
(should (and (integerp id) (> id 0))) ; with positive integer and with
(should (stringp (ednc-notification-summary notification))) ; required
(should (stringp (ednc-notification-app-name notification))))) ; slots?
;; Test helpers for testing:
(ert-deftest ednc--with-temporary-server-test ()
(ednc--with-temp-server)) ; no real test yet
(ert-deftest ednc--test-match-of-matching-arguments-test ()
(ednc--with-temp-server
(let* ((args (nconc (list :body "baz bar") ednc--default-test-args))
(id (apply #'notifications-notify args))
(test (make-ert-test
:body (lambda () (ednc--test-args-match id args)))))
(should (ert-test-passed-p (ert-run-test test))))))
(ert-deftest ednc--test-match-of-mismatching-arguments-test ()
(ednc--with-temp-server
(let* ((args (nconc (list :body "baz bar") ednc--default-test-args))
(id (apply #'notifications-notify ednc--default-test-args))
(test (make-ert-test
:body (lambda () (ednc--test-args-match id args)))))
(should-error (ert-test-passed-p (ert-run-test test))))))
(ert-deftest ednc--test-consistency-of-consistent-state-test ()
(dolist (ednc--state `((0) (42) (23 ,(ednc--notification-create
:id 5 :app-name "foo" :summary "bar"))))
(let ((test (make-ert-test :body #'ednc--test-state-consistency)))
(should (ert-test-passed-p (ert-run-test test))))))
(ert-deftest ednc--test-consistency-of-inconsistent-state-test ()
(dolist (ednc--state `(() (5 (23 ,(ednc--notification-create
:app-name "foo" :summary "bar")))
(,(ednc--notification-create
:id 5 :app-name "foo" :summary "bar"))
(23 ,(ednc--notification-create
:id 5 :app-name "foo"))))
(let ((test (make-ert-test :body #'ednc--test-state-consistency)))
(should (ert-test-failed-p (ert-run-test test))))))
;; Test introspection:
(ert-deftest ednc--handle-introspection-test ()
(ednc--with-temp-server
(should (stringp (dbus-introspect :session ednc--service ednc--path)))))
;; Test use case list-notifcations:
(ert-deftest ednc--list-no-notifications-test ()
(ednc--with-temp-server
(should (string-empty-p (list-notifications)))))
(ert-deftest ednc--list-single-notification-test ()
(ednc--with-temp-server
(apply #'notifications-notify ednc--default-test-args)
(should (string-equal (list-notifications) " [test: foo]\nbar baz\n"))))
(ert-deftest ednc--list-multiple-notifications-test ()
(ednc--with-temp-server
(apply #'notifications-notify ednc--default-test-args)
(apply #'notifications-notify :app-name "tes1" ednc--default-test-args)
(should (string-equal (list-notifications)
" [tes1: foo]\nbar baz\n [test: foo]\nbar baz\n"))))
;; Test use case stack-notifications:
(ert-deftest ednc--stack-no-notifications-test ()
(ednc--with-temp-server
(should (string-empty-p (stack-notifications)))))
(ert-deftest ednc--stack-hidden-notification-test ()
(ednc--with-temp-server
(apply #'notifications-notify ednc--default-test-args)
(should (string-empty-p (stack-notifications '("test"))))))
(ert-deftest ednc--stack-for-single-notification-test ()
(ednc--with-temp-server
(apply #'notifications-notify ednc--default-test-args)
(should (string-equal (stack-notifications) " [test: foo]\nbar baz\n"))))
(ert-deftest ednc--stack-non-stacking-notifications-test ()
(ednc--with-temp-server
(apply #'notifications-notify ednc--default-test-args)
(apply #'notifications-notify :app-name "tes1" ednc--default-test-args)
(should (string-equal (stack-notifications)
" [tes1: foo]\nbar baz\n [test: foo]\nbar baz\n"))))
(ert-deftest ednc--stack-stacking-notifications-test ()
(ednc--with-temp-server
(apply #'notifications-notify ednc--default-test-args)
(apply #'notifications-notify :title "bar" ednc--default-test-args)
(should (string-equal (stack-notifications)
" [test: bar]\nbar baz\n"))))
;; Test use case show-notification-in-buffer:
(ert-deftest ednc--show-notification-in-buffer-test ()
(ednc--with-temp-server
(let ((id (apply #'notifications-notify ednc--default-test-args)))
(with-temp-buffer
(rename-buffer (format "Notification %d" id))
(show-notification-in-buffer nil (cadr ednc--state))
(should (string-equal (buffer-string) " [test: foo]\nbar baz\n"))))))
(ert-deftest ednc--show-replaced-notification-in-buffer-test ()
(ednc--with-temp-server
(let* ((id (apply #'notifications-notify ednc--default-test-args))
(old (cadr ednc--state)))
(with-temp-buffer
(rename-buffer (format "Notification %d" id))
(show-notification-in-buffer nil old)
(apply #'notifications-notify :body "corge" :replaces-id id
ednc--default-test-args)
(show-notification-in-buffer old (cadr ednc--state))
(should (string-equal (buffer-string) " [test: foo]\ncorge\n"))))))
(ert-deftest ednc--show-closed-notification-in-buffer-test ()
(ednc--with-temp-server
(let* ((id (apply #'notifications-notify ednc--default-test-args))
(old (cadr ednc--state))
(buffer))
(with-temp-buffer
(setq buffer (current-buffer))
(rename-buffer (format "Notification %d" id))
(show-notification-in-buffer nil old)
(notifications-close-notification id)
(show-notification-in-buffer old nil))
(should-not (buffer-live-p buffer)))))
;; Test logging:
(ert-deftest ednc--log-single-notification-test ()
(ednc--with-temp-server
(apply #'notifications-notify ednc--default-test-args)
(ednc--update-log-buffer nil (cadr ednc--state))
(with-current-buffer ednc-log-name
(should (string-equal (buffer-string) " [test: foo]\nbar baz\n\n")))))
(ert-deftest ednc--log-multiple-notifications-test ()
(ednc--with-temp-server
(apply #'notifications-notify ednc--default-test-args)
(ednc--update-log-buffer nil (cadr ednc--state))
(apply #'notifications-notify :body "corge" ednc--default-test-args)
(ednc--update-log-buffer nil (cadr ednc--state))
(with-current-buffer ednc-log-name
(should (string-equal " [test: foo]\nbar baz\n\n [test: foo]\ncorge\n\n"
(buffer-string))))))
(ert-deftest ednc--log-closed-notifications-test ()
(ednc--with-temp-server
(let* ((id (apply #'notifications-notify ednc--default-test-args))
(notification (cl-find id (cdr ednc--state)
:key #'ednc-notification-id)))
(ednc--update-log-buffer nil notification)
(apply #'notifications-notify :body "corge" ednc--default-test-args)
(ednc--update-log-buffer nil (cadr ednc--state))
(notifications-close-notification (ednc-notification-id
(cadr ednc--state)))
(ednc--update-log-buffer notification nil)
(with-current-buffer ednc-log-name
(should (string-equal " [test: foo]\nbar baz\n\n [test: foo]\ncorge\n\n"
(buffer-string)))
(should (equal (get-text-property (point-min) 'face)
ednc-dismissed-face))))))
(ert-deftest ednc--log-replaced-notifications-test ()
(ednc--with-temp-server
(let* ((id (apply #'notifications-notify ednc--default-test-args))
(notification (cl-find id (cdr ednc--state)
:key #'ednc-notification-id)))
(ednc--update-log-buffer nil notification)
(apply #'notifications-notify :body "corge" :replaces-id id
ednc--default-test-args)
(ednc--update-log-buffer notification (cadr ednc--state))
(with-current-buffer ednc-log-name
(should (string-equal " [test: foo]\nbar baz\n\n [test: foo]\ncorge\n\n"
(buffer-string)))
(should (equal (get-text-property (point-min) 'face)
ednc-dismissed-face))))))
;; Test ednc-invoke-action:
(ert-deftest ednc--invoke-default-action-test ()
(ednc--with-temp-server
(apply #'notifications-notify
:on-action (lambda (_id key) (should (string-equal key "default")))
ednc--default-test-args)
(ednc-invoke-action (cadr ednc--state))))
(ert-deftest ednc--invoke-alternative-action-test ()
(ednc--with-temp-server
(apply #'notifications-notify
:on-action (lambda (_id key) (should (string-equal key "other")))
ednc--default-test-args)
(ednc-invoke-action (cadr ednc--state) "other")))
;; Test ednc--close-notification:
(ert-deftest ednc--close-notification-test ()
(ednc--with-temp-server
(dolist (pair notifications-closed-reason)
(let ((id (apply #'notifications-notify
:on-close (lambda (_id reason)
(should (eq reason (cadr pair))))
ednc--default-test-args)))
(ednc--close-notification (cadr ednc--state) (car pair))
(should-not (cl-find id (cdr ednc--state)
:key #'ednc-notification-id))))))
;; Test ednc--format-notification:
(ert-deftest ednc--format-notification-test ()
(ednc--with-temp-server
(apply #'notifications-notify ednc--default-test-args)
(should (string-equal (ednc-format-notification (cadr ednc--state))
" [test: foo]\nbar baz\n"))))
(ert-deftest ednc--format-notification-without-body-test ()
(ednc--with-temp-server
(apply #'notifications-notify :body nil ednc--default-test-args)
(should (string-equal (ednc-format-notification (cadr ednc--state))
" [test: foo]\n\n"))))
;; Test ednc--format-summary:
(ert-deftest ednc--format-summary-test ()
(ednc--with-temp-server
(apply #'notifications-notify ednc--default-test-args)
(let* ((new (cadr ednc--state))
(result (ednc--format-summary new)))
(should (string-equal result (ednc-notification-summary new))))))
;; Test ednc--get-actions-keymap:
(ert-deftest ednc--get-empty-actions-keymap-test ()
(ednc--with-temp-server
(apply #'notifications-notify :actions nil ednc--default-test-args)
(let* ((new (cadr ednc--state))
(result (ednc--get-actions-keymap new)))
(should (eq (car result) 'keymap))
(should (string-equal (cadr result) "Actions")))))
(ert-deftest ednc--get-actions-keymap-test ()
(ednc--with-temp-server
(apply #'notifications-notify ednc--default-test-args)
(let* ((new (cadr ednc--state))
(result (ednc--get-actions-keymap new)))
(should (eq (car result) 'keymap))
(dotimes (i 2)
(let ((entry (cadr result)))
(should (and (= (car entry) i) (eq (cadr entry) 'menu-item)))
(should (string-equal (caddr entry)
(nth (1+ (* i 2)) ; odd elements
(ednc-notification-actions new))))
(should (functionp (cadddr entry))))
(setq result (cdr result)))
(should (string-equal (cadr result) "Actions")))))
;; Test handler of Notify:
(ert-deftest ednc--handle-notify-test ()
(ednc--with-temp-server
(let ((id (apply #'notifications-notify ednc--default-test-args)))
(ednc--test-args-match id ednc--default-test-args))))
(ert-deftest ednc--handle-notify-with-expiration-time-test ()
(ednc--with-temp-server
(let* ((timeout 5)
(args (nconc (list :timeout timeout) ednc--default-test-args))
(id (apply #'notifications-notify args)))
(ednc--test-args-match id args)
(sleep-for (/ (* 2 timeout) 1000.0))
(should-not (cl-find id (cdr ednc--state) :key #'ednc-notification-id)))))
(ert-deftest ednc--handle-notify-replace-test ()
(ednc--with-temp-server
(let* ((id (apply #'notifications-notify ednc--default-test-args))
(args (nconc (list :replaces-id id) ednc--default-test-args)))
(apply #'notifications-notify args)
(ednc--test-args-match id args))))
(ert-deftest ednc--handle-notify-replace-nonexistent-test ()
(ednc--with-temp-server
(let* ((id (apply #'notifications-notify ednc--default-test-args))
(args (nconc (list :replaces-id (+ 5 id)) ednc--default-test-args)))
(ednc--test-args-match id ednc--default-test-args)
(setq id (apply #'notifications-notify args))
(ednc--test-args-match id ednc--default-test-args))))
;; Test handler of CloseNotification:
(ert-deftest ednc--handle-close-notification-test ()
(ednc--with-temp-server
(let ((id (apply #'notifications-notify ednc--default-test-args)))
(notifications-close-notification id)
(should-not (cl-find id (cdr ednc--state) :key #'ednc-notification-id)))))
(ert-deftest ednc--handle-close-previously-closed-notification-test ()
(ednc--with-temp-server
(let ((id (apply #'notifications-notify ednc--default-test-args)))
(notifications-close-notification id)
(should-error (notifications-close-notification id)))))
(ert-deftest ednc--handle-close-nonexistent-notification-test ()
(ednc--with-temp-server
(let ((unused (1+ (apply #'notifications-notify ednc--default-test-args))))
(should-error (notifications-close-notification unused)))))
;; Test informational handlers:
(ert-deftest ednc--handle-get-server-information-test ()
(ednc--with-temp-server
(let ((info (notifications-get-server-information)))
(should (and (listp info) (= (length info) 4))) ; correct aggregate type
(dolist (field info)
(should (stringp field)))))) ; and correct types in aggregate?
(ert-deftest ednc--handle-get-capabilities-test ()
(ednc--with-temp-server
(let ((capabilities (notifications-get-capabilities)))
(dolist (required '(:actions :body)) ; minimal feature set?
(should (member required capabilities))))))
;; Test ednc--get-hint:
(ert-deftest ednc--get-hint-test ()
(let ((hints '(("foo" (("bar" "baz"))) ("qux" ("quux")))))
(should (equal '("bar" "baz") (ednc--get-hint hints "foo")))
(should (string-equal "quux" (ednc--get-hint hints "qux")))))
(ert-deftest ednc--get-and-remove-hint-test ()
(let ((hints (list (list "foo" '(("bar" "baz"))) (list "qux" '("quux")))))
(should (equal '("bar" "baz") (ednc--get-hint hints "foo" t)))
(should-not (ednc--get-hint hints "foo")) ; older gone, and
(should (string-equal "quux" (ednc--get-hint hints "qux" t)))
(should-not (ednc--get-hint hints "qux")))) ; newest gone?
(ert-deftest ednc--get-or-remove-nonexistent-hint-test ()
(let ((hints nil))
(should-not (ednc--get-hint hints "foo" t)) ; neither remove,
(should-not (ednc--get-hint hints "foo")))) ; nor get only?
;; Test ednc--path-to-image and ednc--data-to-image:
(ert-deftest ednc--nil-to-image-test ()
(should-not (ednc--path-to-image nil))
(should-not (ednc--data-to-image nil)))
(ert-deftest ednc--unsupported-paths-to-image-test ()
(should-error (ednc--path-to-image ; with unsupported schema
"https://www.gnu.org/software/emacs/images/emacs.png")))
(ert-deftest ednc--path-to-image-test ()
(let ((path (expand-file-name "screenshot.png")))
(should (ednc--path-to-image path))
(should (ednc--path-to-image (concat "file://" path)))))
(ert-deftest ednc--nonexistent-path-to-image-test ()
(should-not (ednc--path-to-image ""))
(should-not (ednc--path-to-image "/nonexistent"))
(should-not (ednc--path-to-image "file:///nonexistent")))
(ert-deftest ednc--unsupported-data-to-image-test ()
(when (image-type-available-p 'pbm)
(let ((raw (append "abcABCxyzXYZ" nil)))
(should-error (ednc--data-to-image (list 2 2 6 t 7 3 raw))) ; bit-depth
(should-error (ednc--data-to-image (list 2 2 6 t 8 2 raw))) ; non-RGB(A)
(should-error (ednc--data-to-image (list 2 2 6 t 8 5 raw))))))
(ert-deftest ednc--data-to-image-test ()
(let* ((expect (when (image-type-available-p 'pbm)
"P6\n2 2\n255\nabcABCxyzXYZ"))
(data (append "abcABCxyzXYZ" nil))
(image (ednc--data-to-image (list 2 2 6 nil 8 3 data))))
(should (equal (image-property image :data) expect)) ; RGB
(setq data (append "abc!ABC?xyz?XYZ!" nil)
image (ednc--data-to-image (list 2 2 8 t 8 4 data)))
(should (equal (image-property image :data) expect)))) ; RGBA
;; Test ednc--delete-padding:
(ert-deftest ednc--delete-padding-from-empty-list-test ()
(let ((list (list)))
(ednc--delete-padding list 3 5)
(should (null list))))
(ert-deftest ednc--delete-zero-length-padding-from-list-test ()
(let ((list (list 'foo 'bar 'baz 'qux 'quux 'corge)))
(ednc--delete-padding list 3 3)
(should (equal list '(foo bar baz qux quux corge)))))
(ert-deftest ednc--delete-padding-from-short-list-test ()
(let ((list (list 'foo 'bar 'baz 'qux 'quux)))
(ednc--delete-padding list 3 5)
(should (equal list '(foo bar baz)))))
(ert-deftest ednc--delete-alpha-channel-from-short-list-test ()
(let ((list (list 'foo 'bar 'baz 'qux)))
(ednc--delete-padding list 3 4)
(should (equal list '(foo bar baz)))))
(ert-deftest ednc--delete-padding-from-longer-list-test ()
(let ((list (list 'foo 'bar 'baz 'qux 'quux 'corge)))
(ednc--delete-padding list 1 3)
(should (equal list '(foo qux)))))
(ert-deftest ednc--delete-alpha-channel-from-longer-list-test ()
(let ((list (list 'foo 'bar 'baz 'qux 'quux 'corge 'grault 'garply)))
(ednc--delete-padding list 3 4)
(should (equal list '(foo bar baz quux corge grault)))))
;;; test.el ends here