aboutsummaryrefslogtreecommitdiff
path: root/tests/test-host-environment--display-predicates.el
blob: 15dff2ef8ef0a208d34cce5990d1ab1df95dde97 (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
;;; test-host-environment--display-predicates.el --- Tests for env-x/x11/wayland/terminal/gui-p -*- lexical-binding: t; -*-

;;; Commentary:
;; Tests for the display-environment predicates in host-environment.el.
;; Mocks `window-system', `getenv', and `display-graphic-p' at the
;; framework boundary so each predicate can be exercised under every
;; relevant combination of (window-system, WAYLAND_DISPLAY, graphic-p).

;;; Code:

(require 'ert)
(require 'cl-lib)

(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
(require 'host-environment)

(defmacro test-host-env--with-display (window-system-value
                                       wayland-display
                                       graphic-p
                                       &rest body)
  "Run BODY with display state stubbed.
WINDOW-SYSTEM-VALUE becomes the return of `(window-system)'.
WAYLAND-DISPLAY becomes the value of $WAYLAND_DISPLAY (nil for unset).
GRAPHIC-P becomes the return of `(display-graphic-p)'."
  (declare (indent 3) (debug t))
  `(cl-letf (((symbol-function 'window-system)
              (lambda (&optional _) ,window-system-value))
             ((symbol-function 'getenv)
              (lambda (name)
                (when (string= name "WAYLAND_DISPLAY") ,wayland-display)))
             ((symbol-function 'display-graphic-p)
              (lambda (&optional _) ,graphic-p)))
     ,@body))

;;; env-x-p

(ert-deftest test-host-environment-x-p-true-when-window-system-is-x ()
  "Normal: env-x-p returns t when window-system is `x'."
  (test-host-env--with-display 'x nil t
    (should (env-x-p))))

(ert-deftest test-host-environment-x-p-false-when-window-system-is-pgtk ()
  "Boundary: env-x-p returns nil when window-system is `pgtk'."
  (test-host-env--with-display 'pgtk nil t
    (should-not (env-x-p))))

(ert-deftest test-host-environment-x-p-false-when-no-window-system ()
  "Boundary: env-x-p returns nil under no window system (terminal)."
  (test-host-env--with-display nil nil nil
    (should-not (env-x-p))))

;;; env-x11-p

(ert-deftest test-host-environment-x11-p-true-on-x-without-wayland-display ()
  "Normal: env-x11-p returns t under X with no WAYLAND_DISPLAY."
  (test-host-env--with-display 'x nil t
    (should (env-x11-p))))

(ert-deftest test-host-environment-x11-p-false-under-xwayland ()
  "Boundary: env-x11-p returns nil when running through XWayland.
Detected via WAYLAND_DISPLAY being set even though window-system is `x'."
  (test-host-env--with-display 'x "wayland-0" t
    (should-not (env-x11-p))))

(ert-deftest test-host-environment-x11-p-false-on-pgtk ()
  "Boundary: env-x11-p returns nil under pure-GTK Wayland."
  (test-host-env--with-display 'pgtk "wayland-0" t
    (should-not (env-x11-p))))

;;; env-wayland-p

(ert-deftest test-host-environment-wayland-p-true-when-wayland-display-set ()
  "Normal: env-wayland-p returns t when WAYLAND_DISPLAY is set."
  (test-host-env--with-display 'pgtk "wayland-0" t
    (should (env-wayland-p))))

(ert-deftest test-host-environment-wayland-p-false-when-wayland-display-unset ()
  "Boundary: env-wayland-p returns nil when WAYLAND_DISPLAY is unset."
  (test-host-env--with-display 'x nil t
    (should-not (env-wayland-p))))

(ert-deftest test-host-environment-wayland-p-true-under-xwayland ()
  "Boundary: env-wayland-p returns t even under XWayland.
WAYLAND_DISPLAY is set by the compositor regardless of which X surface
Emacs uses, so XWayland counts as Wayland by this predicate."
  (test-host-env--with-display 'x "wayland-0" t
    (should (env-wayland-p))))

;;; env-terminal-p / env-gui-p

(ert-deftest test-host-environment-terminal-p-true-when-not-graphical ()
  "Normal: env-terminal-p returns t when display-graphic-p is nil."
  (test-host-env--with-display nil nil nil
    (should (env-terminal-p))))

(ert-deftest test-host-environment-terminal-p-false-when-graphical ()
  "Boundary: env-terminal-p returns nil under a graphical frame."
  (test-host-env--with-display 'x nil t
    (should-not (env-terminal-p))))

(ert-deftest test-host-environment-gui-p-true-when-graphical ()
  "Normal: env-gui-p returns t under a graphical frame."
  (test-host-env--with-display 'x nil t
    (should (env-gui-p))))

(ert-deftest test-host-environment-gui-p-false-in-terminal ()
  "Boundary: env-gui-p returns nil under a terminal frame."
  (test-host-env--with-display nil nil nil
    (should-not (env-gui-p))))

(provide 'test-host-environment--display-predicates)
;;; test-host-environment--display-predicates.el ends here