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