Foxe Chen | b90c239 | 2025-06-27 21:10:35 +0200 | [diff] [blame] | 1 | " Tests for clipmethod |
| 2 | |
Christian Brabandt | eb380b9 | 2025-07-07 20:53:55 +0200 | [diff] [blame] | 3 | source util/window_manager.vim |
Foxe Chen | b90c239 | 2025-06-27 21:10:35 +0200 | [diff] [blame] | 4 | |
| 5 | CheckFeature clipboard_working |
| 6 | CheckFeature xterm_clipboard |
| 7 | CheckFeature wayland_clipboard |
| 8 | CheckUnix |
| 9 | |
| 10 | " Test if no available clipmethod sets v:clipmethod to none and deinits clipboard |
| 11 | func 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')) |
| 17 | endfunc |
| 18 | |
| 19 | " Test if method chosen is in line with clipmethod order |
| 20 | func 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 Brabandt | a4874d4 | 2025-07-07 20:07:06 +0200 | [diff] [blame] | 35 | if exists("$DISPLAY") |
| 36 | call assert_equal("x11", v:clipmethod) |
| 37 | endif |
Foxe Chen | b90c239 | 2025-06-27 21:10:35 +0200 | [diff] [blame] | 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 | |
Christian Brabandt | a4874d4 | 2025-07-07 20:07:06 +0200 | [diff] [blame] | 52 | if exists("$DISPLAY") |
| 53 | set cpm=x11 |
Foxe Chen | b90c239 | 2025-06-27 21:10:35 +0200 | [diff] [blame] | 54 | |
Christian Brabandt | a4874d4 | 2025-07-07 20:07:06 +0200 | [diff] [blame] | 55 | call assert_equal("x11", v:clipmethod) |
| 56 | endif |
Foxe Chen | b90c239 | 2025-06-27 21:10:35 +0200 | [diff] [blame] | 57 | |
| 58 | set cpm=wayland |
| 59 | |
| 60 | call assert_equal("wayland", v:clipmethod) |
| 61 | |
| 62 | call EndWaylandCompositor(l:wayland_display) |
| 63 | endfunc |
| 64 | |
| 65 | " Test if clipmethod is set to 'none' when gui is started |
| 66 | func 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')) |
| 82 | endfunc |
| 83 | |
| 84 | " Test if :clipreset switches methods when current one doesn't work |
| 85 | func 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 Brabandt | a4874d4 | 2025-07-07 20:07:06 +0200 | [diff] [blame] | 106 | if exists("$DISPLAY") |
| 107 | call assert_equal("x11", v:clipmethod) |
| 108 | endif |
Foxe Chen | b90c239 | 2025-06-27 21:10:35 +0200 | [diff] [blame] | 109 | |
| 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 Brabandt | eb380b9 | 2025-07-07 20:53:55 +0200 | [diff] [blame] | 126 | source util/shared.vim |
Foxe Chen | b90c239 | 2025-06-27 21:10:35 +0200 | [diff] [blame] | 127 | |
| 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 Brabandt | a4874d4 | 2025-07-07 20:07:06 +0200 | [diff] [blame] | 157 | if exists("$DISPLAY") |
| 158 | call WaitForAssert({-> assert_match(l:name, serverlist())}) |
| 159 | endif |
Foxe Chen | b90c239 | 2025-06-27 21:10:35 +0200 | [diff] [blame] | 160 | |
| 161 | " Change x server to the one that will be killed, then block until |
| 162 | " v:clipmethod is none. |
Christian Brabandt | a4874d4 | 2025-07-07 20:07:06 +0200 | [diff] [blame] | 163 | if exists("$DISPLAY") |
| 164 | call remote_send(l:name, ":xrestore " .. l:xdisplay .. |
Foxe Chen | b90c239 | 2025-06-27 21:10:35 +0200 | [diff] [blame] | 165 | \ ' | call DoIt()' .. "\<CR>") |
| 166 | |
Christian Brabandt | a4874d4 | 2025-07-07 20:07:06 +0200 | [diff] [blame] | 167 | call EndXServer(l:xdisplay) |
| 168 | call WaitFor({-> filereadable('Xtest')}) |
Foxe Chen | b90c239 | 2025-06-27 21:10:35 +0200 | [diff] [blame] | 169 | |
Christian Brabandt | a4874d4 | 2025-07-07 20:07:06 +0200 | [diff] [blame] | 170 | " 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 Chen | b90c239 | 2025-06-27 21:10:35 +0200 | [diff] [blame] | 174 | endfunc |
| 175 | |
| 176 | " vim: shiftwidth=2 sts=2 expandtab |