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
| | ;;; erc-desktop-notifications-tests.el --- Notifications tests -*- lexical-binding:t -*-
;; Copyright (C) 2024 Free Software Foundation, Inc.
;; This file is part of GNU Emacs.
;;
;; GNU Emacs 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.
;;
;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(require 'erc-desktop-notifications)
(require 'ert-x)
(eval-and-compile
(let ((load-path (cons (ert-resource-directory) load-path)))
(require 'erc-tests-common)))
(defun erc-desktop-notifications-tests--perform (test)
(erc-tests-common-make-server-buf)
(erc-notifications-mode +1)
(setq erc-server-current-nick "tester")
(cl-letf* ((calls nil)
((frame-parameter nil 'last-focus-update)
t)
((symbol-function 'erc-notifications-notify)
(lambda (&rest r) (push r calls))))
(with-current-buffer (erc--open-target "#chan")
(funcall test (lambda () (prog1 calls (setq calls nil))))))
(when noninteractive
(erc-notifications-mode -1)
(erc-tests-common-kill-buffers)))
(defun erc-desktop-notifications-tests--populate-chan (test)
(erc-desktop-notifications-tests--perform
(lambda (check)
(erc-tests-common-add-cmem "bob")
(erc-tests-common-add-cmem "alice")
(erc-tests-common-simulate-line
":irc.foonet.org 353 tester = #chan :alice bob tester")
(erc-tests-common-simulate-line
":irc.foonet.org 366 tester #chan :End of NAMES list")
(erc-tests-common-simulate-privmsg "bob" "hi tester")
(should (equal (current-buffer) (get-buffer "#chan")))
(should (not (eq (current-buffer) (window-buffer)))) ; *ert* or *scratch*
(funcall test check))))
(ert-deftest erc-notifications-focused-contexts/default ()
(should (equal erc-notifications-focused-contexts '(query mention)))
(erc-desktop-notifications-tests--populate-chan
(lambda (check)
;; A private query triggers a notification.
(erc-tests-common-simulate-line ":bob!~bob@fsf.org PRIVMSG tester yo")
(should (eq (current-buffer) (get-buffer "bob")))
;; A NOTICE command doesn't trigger a notification because it's
;; absent from `erc-desktop-notifications-match-query-commands'.
(erc-tests-common-simulate-line ":irc.foonet.org NOTICE tester nope")
(should (equal (funcall check)
'(("bob" "yo")
("bob" "hi tester\n"))))
;; Setting the window to the buffer where insertions are happening
;; makes no difference: notifications are still sent.
(erc-tests-common-simulate-line ":bob!~bob@fsf.org PRIVMSG tester ho")
(set-window-buffer nil (set-buffer "#chan"))
(erc-tests-common-simulate-privmsg "alice" "hi tester")
(should (equal (funcall check)
'(("alice" "hi tester\n")
("bob" "ho")))))))
(ert-deftest erc-notifications-focused-contexts/unselected ()
(should (equal erc-notifications-focused-contexts '(query mention)))
(let ((erc-notifications-focused-contexts))
(erc-desktop-notifications-tests--populate-chan
(lambda (check)
(should (equal (funcall check) '(("bob" "hi tester\n"))))
;; Buffer #chan is current and displayed in the selected window,
;; so no notification is sent.
(set-window-buffer nil "#chan") ; #chan
(erc-tests-common-simulate-privmsg "alice" "hi tester")
;; A new query comes in for a buffer that doesn't exist. The
;; option `erc-receive-query-display' tells ERC to switch to that
;; buffer and show it before insertion. Therefore, no
;; notification is sent.
(let ((erc-receive-query-display 'buffer))
(erc-tests-common-simulate-line
":bob!~bob@fsf.org PRIVMSG tester yo"))
(should-not (funcall check))))))
;;; erc-desktop-notifications-tests.el ends here
|