blob: 9b34658426b42c2faf3344489b4dfb7a7cbe447e [file] [log] [blame]
Foxe Chenb90c2392025-06-27 21:10:35 +02001" Tests for clipmethod
2
Christian Brabandteb380b92025-07-07 20:53:55 +02003source util/window_manager.vim
Foxe Chenb90c2392025-06-27 21:10:35 +02004
5CheckFeature clipboard_working
6CheckFeature xterm_clipboard
7CheckFeature wayland_clipboard
8CheckUnix
9
10" Test if no available clipmethod sets v:clipmethod to none and deinits clipboard
11func Test_no_clipmethod_sets_v_clipmethod_none()
12 CheckNotGui
13
14 set clipmethod=
15 call assert_equal("none", v:clipmethod)
16 call assert_equal(0, has('clipboard_working'))
17endfunc
18
19" Test if method chosen is in line with clipmethod order
20func Test_clipmethod_order()
21 CheckNotGui
22
23 set cpm=wayland,x11
24
25 let l:wayland_display = StartWaylandCompositor()
26
27 let $WAYLAND_DISPLAY = l:wayland_display
28 exe 'wlrestore ' .. l:wayland_display
29
30 call assert_equal("wayland", v:clipmethod)
31
32 :wlrestore 1239
33 clipreset
34
Christian Brabandta4874d42025-07-07 20:07:06 +020035 if exists("$DISPLAY")
36 call assert_equal("x11", v:clipmethod)
37 endif
Foxe Chenb90c2392025-06-27 21:10:35 +020038
39 :xrestore 1239
40 clipreset
41
42 call assert_equal("none", v:clipmethod)
43 call assert_equal(0, has('clipboard_working'))
44
45 exe ":wlrestore " . $WAYLAND_DISPLAY
46 exe ":xrestore " . $DISPLAY
47 clipreset
48
49 call assert_equal("wayland", v:clipmethod)
50 call assert_equal(1, has('clipboard_working'))
51
Christian Brabandta4874d42025-07-07 20:07:06 +020052 if exists("$DISPLAY")
53 set cpm=x11
Foxe Chenb90c2392025-06-27 21:10:35 +020054
Christian Brabandta4874d42025-07-07 20:07:06 +020055 call assert_equal("x11", v:clipmethod)
56 endif
Foxe Chenb90c2392025-06-27 21:10:35 +020057
58 set cpm=wayland
59
60 call assert_equal("wayland", v:clipmethod)
61
62 call EndWaylandCompositor(l:wayland_display)
63endfunc
64
65" Test if clipmethod is set to 'none' when gui is started
66func Test_clipmethod_is_none_when_gui()
67 CheckCanRunGui
68
69 let lines =<< trim END
70 set cpm=wayland,x11
71 call writefile([v:clipmethod != ""], 'Cbdscript')
72 gui -f
73 call writefile([v:clipmethod], 'Cbdscript', 'a')
74 clipreset
75 call writefile([v:clipmethod], 'Cbdscript', 'a')
76 quit
77 END
78
79 call writefile(lines, 'Cbdscript', 'D')
80 call system($'{GetVimCommand()} -S Cbdscript')
81 call assert_equal(['1', 'none', 'none'], readfile('Cbdscript'))
82endfunc
83
84" Test if :clipreset switches methods when current one doesn't work
85func Test_clipreset_switches()
86 CheckNotGui
87 CheckFeature clientserver
88 CheckXServer
89 CheckWaylandCompositor
90
91 let l:wayland_display = StartWaylandCompositor()
92
93 set cpm=wayland,x11
94
95 exe 'wlrestore ' .. l:wayland_display
96
97 call assert_equal(l:wayland_display, v:wayland_display)
98 call assert_equal("wayland", v:clipmethod)
99
100 call EndWaylandCompositor(l:wayland_display)
101
102 " wlrestore updates clipmethod as well
103 wlrestore!
104
105 call assert_equal("", v:wayland_display)
Christian Brabandta4874d42025-07-07 20:07:06 +0200106 if exists("$DISPLAY")
107 call assert_equal("x11", v:clipmethod)
108 endif
Foxe Chenb90c2392025-06-27 21:10:35 +0200109
110 " Do the same but kill a X11 server
111
112 " X11 error handling relies on longjmp magic, but essentially if the X server
113 " is killed then it will simply abandon the current commands, making the test
114 " hang.
115
116 " This will only happen for commands given from the command line, which
117 " is why we cannot just directly call Vim or use the actual Vim instance thats
118 " doing all the testing, since main_loop() is never executed.
119
120 " Therefore we should start a separate Vim instance and communicate with it
121 " remotely, so we can execute the actual testing stuff with main_loop()
122 " running.
123
124 let l:lines =<< trim END
125 set cpm=x11
Christian Brabandteb380b92025-07-07 20:53:55 +0200126 source util/shared.vim
Foxe Chenb90c2392025-06-27 21:10:35 +0200127
128 func Test()
129 clipreset
130
131 if v:clipmethod ==# 'none'
132 return 1
133 endif
134 return 0
135 endfunc
136
137 func DoIt()
138 call WaitFor(function('Test'))
139
140 if v:clipmethod == 'none'
141 call writefile(['SUCCESS'], 'Xtest')
142 else
143 call writefile(['FAIL'], 'Xtest')
144 endif
145 quitall
146 endfunc
147 END
148 call writefile(l:lines, 'Xtester', 'D')
149
150 let l:xdisplay = StartXServer()
151
152 let l:name = 'XVIMTEST'
153 let l:cmd = GetVimCommand() .. ' -S Xtester --servername ' .. l:name
154 let l:job = job_start(l:cmd, { 'stoponexit': 'kill', 'out_io': 'null'})
155
156 call WaitForAssert({-> assert_equal("run", job_status(l:job))})
Christian Brabandta4874d42025-07-07 20:07:06 +0200157 if exists("$DISPLAY")
158 call WaitForAssert({-> assert_match(l:name, serverlist())})
159 endif
Foxe Chenb90c2392025-06-27 21:10:35 +0200160
161 " Change x server to the one that will be killed, then block until
162 " v:clipmethod is none.
Christian Brabandta4874d42025-07-07 20:07:06 +0200163 if exists("$DISPLAY")
164 call remote_send(l:name, ":xrestore " .. l:xdisplay ..
Foxe Chenb90c2392025-06-27 21:10:35 +0200165 \ ' | call DoIt()' .. "\<CR>")
166
Christian Brabandta4874d42025-07-07 20:07:06 +0200167 call EndXServer(l:xdisplay)
168 call WaitFor({-> filereadable('Xtest')})
Foxe Chenb90c2392025-06-27 21:10:35 +0200169
Christian Brabandta4874d42025-07-07 20:07:06 +0200170 " For some reason readfile sometimes returns an empty list despite the file
171 " existing, this why WaitForAssert() is used.
172 call WaitForAssert({-> assert_equal(['SUCCESS'], readfile('Xtest'))}, 1000)
173 endif
Foxe Chenb90c2392025-06-27 21:10:35 +0200174endfunc
175
176" vim: shiftwidth=2 sts=2 expandtab