blob: 6abd3ad2667c54472ff45f3af678aa6bed407279 (
plain)
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
|
;;; test-integration-mousetrap-mode-profiles.el --- Integration tests -*- lexical-binding: t; -*-
;;; Commentary:
;; Integration tests for mousetrap-mode profile system.
;; Tests complete workflows including profile lookup, keymap building,
;; mode activation, inheritance, and dynamic reconfiguration.
;;
;; Components integrated:
;; - mouse-trap--get-profile-for-mode (profile lookup)
;; - mouse-trap--build-keymap (keymap generation)
;; - mouse-trap-mode (minor mode activation)
;; - derived-mode-p (Emacs mode inheritance)
;; - mouse-trap-maybe-enable (auto-activation logic)
;;; Code:
(require 'ert)
(require 'mousetrap-mode)
;;; Integration Tests - Normal Workflows
(ert-deftest test-integration-mousetrap-mode-profiles-org-mode-inherits-text-mode-disabled ()
"Test org-mode inherits disabled profile from text-mode.
Components integrated:
- mouse-trap--get-profile-for-mode (lookup with inheritance)
- derived-mode-p (mode hierarchy checking)
- org-mode (real major mode)
Validates:
- Mode inheritance chain works correctly
- org-mode → text-mode → disabled profile"
(with-temp-buffer
(org-mode)
(let ((profile (mouse-trap--get-profile-for-mode)))
(should (eq 'disabled profile)))))
(ert-deftest test-integration-mousetrap-mode-profiles-pdf-view-full-allows-all-events ()
"Test pdf-view-mode gets full profile with all events allowed.
Components integrated:
- mouse-trap--get-profile-for-mode (exact match lookup)
- mouse-trap--build-keymap (full profile keymap)
Validates:
- Full profile configuration
- All event categories allowed (empty/minimal keymap)"
(let ((major-mode 'pdf-view-mode))
(let ((profile (mouse-trap--get-profile-for-mode))
(map (mouse-trap--build-keymap)))
(should (eq 'full profile))
(should (keymapp map))
;; All events should be allowed (not bound)
(should (eq (lookup-key map (kbd "<mouse-1>")) nil))
(should (eq (lookup-key map (kbd "<wheel-up>")) nil))
(should (eq (lookup-key map (kbd "<drag-mouse-1>")) nil)))))
(ert-deftest test-integration-mousetrap-mode-profiles-dashboard-primary-click-only ()
"Test dashboard-mode gets primary-click profile.
Components integrated:
- mouse-trap--get-profile-for-mode (lookup)
- mouse-trap--build-keymap (selective event binding)
Validates:
- Primary-click profile allows mouse-1
- Blocks mouse-2/3 and scroll events"
(let ((major-mode 'dashboard-mode))
(let ((profile (mouse-trap--get-profile-for-mode))
(map (mouse-trap--build-keymap)))
(should (eq 'primary-click profile))
;; mouse-1 allowed
(should (eq (lookup-key map (kbd "<mouse-1>")) nil))
;; mouse-2/3 blocked
(should (eq (lookup-key map (kbd "<mouse-2>")) 'ignore))
;; scroll blocked
(should (eq (lookup-key map (kbd "<wheel-up>")) 'ignore)))))
(ert-deftest test-integration-mousetrap-mode-profiles-emacs-lisp-uses-default-disabled ()
"Test unmapped mode uses default disabled profile.
Components integrated:
- mouse-trap--get-profile-for-mode (fallback to default)
- mouse-trap--build-keymap (disabled keymap)
Validates:
- Default profile fallback works
- All events blocked by default"
(with-temp-buffer
(emacs-lisp-mode)
(let ((profile (mouse-trap--get-profile-for-mode))
(map (mouse-trap--build-keymap)))
(should (eq 'disabled profile))
;; All events blocked
(should (eq (lookup-key map (kbd "<wheel-up>")) 'ignore))
(should (eq (lookup-key map (kbd "<mouse-1>")) 'ignore)))))
(ert-deftest test-integration-mousetrap-mode-profiles-change-profile-no-reload ()
"Test changing profiles and re-enabling mode without Emacs reload.
Components integrated:
- mouse-trap--get-profile-for-mode (re-reads configuration)
- mouse-trap--build-keymap (rebuilds dynamically)
- mouse-trap-mode (mode toggle)
Validates:
- KEY FEATURE: Dynamic reconfiguration
- Profile changes take effect without reload"
(let ((original-profiles mouse-trap-mode-profiles))
(unwind-protect
(with-temp-buffer
(emacs-lisp-mode)
;; Start with unmapped mode (gets default scroll-only)
(setq mouse-trap-mode-profiles nil)
(mouse-trap-mode 1)
(let ((map mouse-trap-mode-map))
(should (eq (lookup-key map (kbd "<mouse-1>")) 'ignore)))
(mouse-trap-mode -1)
;; Change configuration
(setq mouse-trap-mode-profiles '((emacs-lisp-mode . full)))
;; Re-enable and verify new profile
(mouse-trap-mode 1)
(let ((map mouse-trap-mode-map))
;; Full profile - all events allowed
(should (eq (lookup-key map (kbd "<mouse-1>")) nil))
(should (eq (lookup-key map (kbd "<wheel-up>")) nil))))
;; Restore original configuration
(setq mouse-trap-mode-profiles original-profiles))))
(ert-deftest test-integration-mousetrap-mode-profiles-switch-major-mode-updates-profile ()
"Test switching major-mode and re-enabling updates profile.
Components integrated:
- mouse-trap--get-profile-for-mode (mode-sensitive lookup)
- Major mode switching
- Mode re-activation
Validates:
- Profile changes with major-mode
- Mode-sensitive behavior"
(with-temp-buffer
(text-mode)
(mouse-trap-mode 1)
(let ((map1 mouse-trap-mode-map))
;; text-mode = disabled (inherits from default), all blocked
(should (eq (lookup-key map1 (kbd "<wheel-up>")) 'ignore))
(should (eq (lookup-key map1 (kbd "<mouse-1>")) 'ignore))
(mouse-trap-mode -1))
;; Switch to pdf-view-mode which has full profile
(setq major-mode 'pdf-view-mode)
(mouse-trap-mode 1)
(let ((map2 mouse-trap-mode-map))
;; pdf-view-mode = full, all events allowed
(should (eq (lookup-key map2 (kbd "<wheel-up>")) nil))
(should (eq (lookup-key map2 (kbd "<mouse-1>")) nil)))))
(ert-deftest test-integration-mousetrap-mode-profiles-auto-enable-respects-exclusions ()
"Test auto-enable respects exclusion list.
Components integrated:
- mouse-trap-maybe-enable (auto-activation logic)
- mouse-trap-excluded-modes (exclusion list)
- derived-mode-p (mode checking)
Validates:
- Exclusion list prevents auto-activation
- dired-mode is excluded"
(with-temp-buffer
(dired-mode default-directory)
;; Manually call maybe-enable
(mouse-trap-maybe-enable)
;; Should NOT enable
(should-not mouse-trap-mode)))
(ert-deftest test-integration-mousetrap-mode-profiles-manual-enable-in-excluded-mode ()
"Test manual activation works in excluded modes.
Components integrated:
- mouse-trap-mode (manual activation)
- Exclusion list (should not affect manual activation)
Validates:
- Manual activation bypasses auto-enable exclusions
- Exclusions only affect hooks, not manual toggling"
(with-temp-buffer
(dired-mode default-directory)
;; Manually enable
(mouse-trap-mode 1)
;; Should be enabled despite being in exclusion list
(should mouse-trap-mode)))
;;; Integration Tests - Boundary Cases
(ert-deftest test-integration-mousetrap-mode-profiles-markdown-inherits-text-disabled ()
"Test markdown-mode inherits disabled profile from text-mode.
Components integrated:
- Mode inheritance (markdown-mode → text-mode)
- Profile lookup with inheritance
Validates:
- Multi-level inheritance works
- Markdown gets disabled profile"
(with-temp-buffer
(when (fboundp 'markdown-mode)
(markdown-mode)
(let ((profile (mouse-trap--get-profile-for-mode)))
(should (eq 'disabled profile))))))
(ert-deftest test-integration-mousetrap-mode-profiles-help-mode-inherits-special-disabled ()
"Test help-mode inherits disabled from special-mode.
Components integrated:
- Mode inheritance (help-mode → special-mode)
- Profile lookup
Validates:
- special-mode inheritance works
- Help buffers get disabled profile"
(with-temp-buffer
(help-mode)
(let ((profile (mouse-trap--get-profile-for-mode)))
(should (eq 'disabled profile)))))
(ert-deftest test-integration-mousetrap-mode-profiles-toggle-multiple-times ()
"Test toggling mode multiple times is stable.
Components integrated:
- mouse-trap-mode (activation/deactivation)
- Keymap building (multiple times)
Validates:
- Mode toggle robustness
- No errors on rapid toggling"
(with-temp-buffer
(emacs-lisp-mode)
;; Toggle multiple times
(dotimes (_ 5)
(mouse-trap-mode 1)
(should mouse-trap-mode)
(mouse-trap-mode -1)
(should-not mouse-trap-mode))))
(ert-deftest test-integration-mousetrap-mode-profiles-multiple-buffers-independent ()
"Test multiple buffers have independent profiles.
Components integrated:
- Buffer-local mode behavior
- Profile lookup per buffer
- Multiple mode activation
Validates:
- Buffer-local mode isolation
- Each buffer gets correct profile"
(let ((buf1 (generate-new-buffer "test1"))
(buf2 (generate-new-buffer "test2")))
(unwind-protect
(progn
;; Buffer 1: text-mode (disabled = default)
(with-current-buffer buf1
(text-mode)
(mouse-trap-mode 1)
(should mouse-trap-mode)
(let ((map1 mouse-trap-mode-map))
(should (eq (lookup-key map1 (kbd "<wheel-up>")) 'ignore))
(should (eq (lookup-key map1 (kbd "<mouse-1>")) 'ignore))))
;; Buffer 2: pdf-view-mode (full profile)
(with-current-buffer buf2
(setq major-mode 'pdf-view-mode)
(mouse-trap-mode 1)
(should mouse-trap-mode)
(let ((map2 mouse-trap-mode-map))
;; All events allowed
(should (eq (lookup-key map2 (kbd "<wheel-up>")) nil))
(should (eq (lookup-key map2 (kbd "<mouse-1>")) nil)))))
;; Cleanup
(kill-buffer buf1)
(kill-buffer buf2))))
;;; Integration Tests - Edge Cases
(ert-deftest test-integration-mousetrap-mode-profiles-change-default-profile ()
"Test changing default profile takes effect.
Components integrated:
- mouse-trap-default-profile (configuration)
- Profile fallback logic
- Dynamic reconfiguration
Validates:
- Default profile configuration works
- Changes take effect on re-enable"
(let ((original-default mouse-trap-default-profile)
(original-profiles mouse-trap-mode-profiles))
(unwind-protect
(with-temp-buffer
;; Unmapped mode uses default
(setq mouse-trap-mode-profiles nil)
(setq mouse-trap-default-profile 'disabled)
(emacs-lisp-mode)
(mouse-trap-mode 1)
(let ((map1 mouse-trap-mode-map))
;; Default = disabled, all blocked
(should (eq (lookup-key map1 (kbd "<wheel-up>")) 'ignore))
(should (eq (lookup-key map1 (kbd "<mouse-1>")) 'ignore))
(mouse-trap-mode -1))
;; Change default
(setq mouse-trap-default-profile 'full)
(mouse-trap-mode 1)
(let ((map2 mouse-trap-mode-map))
;; Default = full, all allowed
(should (eq (lookup-key map2 (kbd "<wheel-up>")) nil))
(should (eq (lookup-key map2 (kbd "<mouse-1>")) nil))))
;; Restore original configuration
(setq mouse-trap-default-profile original-default)
(setq mouse-trap-mode-profiles original-profiles))))
(ert-deftest test-integration-mousetrap-mode-profiles-add-new-profile-runtime ()
"Test adding new profile at runtime.
Components integrated:
- mouse-trap-profiles (extensibility)
- Profile lookup
- Runtime configuration
Validates:
- Runtime extensibility
- New profiles work immediately"
(let ((original-profiles mouse-trap-profiles)
(original-mode-profiles mouse-trap-mode-profiles))
(unwind-protect
(with-temp-buffer
(setq mouse-trap-profiles
(append mouse-trap-profiles
'((custom-profile . (primary-click scroll)))))
(setq mouse-trap-mode-profiles '((emacs-lisp-mode . custom-profile)))
(emacs-lisp-mode)
(mouse-trap-mode 1)
(let ((map mouse-trap-mode-map))
;; Custom profile: primary-click and scroll allowed
(should (eq (lookup-key map (kbd "<mouse-1>")) nil))
(should (eq (lookup-key map (kbd "<wheel-up>")) nil))
;; Secondary click blocked
(should (eq (lookup-key map (kbd "<mouse-2>")) 'ignore))))
;; Restore original configuration
(setq mouse-trap-profiles original-profiles)
(setq mouse-trap-mode-profiles original-mode-profiles))))
(ert-deftest test-integration-mousetrap-mode-profiles-remove-mode-mapping-uses-default ()
"Test removing mode mapping falls back to default.
Components integrated:
- Profile lookup fallback
- Dynamic configuration
Validates:
- Graceful handling of removed mappings
- Fallback to default profile"
(with-temp-buffer
(let ((mouse-trap-mode-profiles nil) ; Dashboard not mapped
(mouse-trap-default-profile 'scroll-only)
(major-mode 'dashboard-mode))
(let ((profile (mouse-trap--get-profile-for-mode)))
;; Should fall back to default
(should (eq 'scroll-only profile))))))
(provide 'test-integration-mousetrap-mode-profiles)
;;; test-integration-mousetrap-mode-profiles.el ends here
|