summaryrefslogtreecommitdiff
path: root/mnv/src/testdir/test_clipmethod.mnv
blob: 6fdab83a0ade5801ca8291e85cb51955403dd2d4 (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
" Tests for clipmethod

source util/window_manager.mnv

CheckFeature clipboard_working

" Test if no available clipmethod sets v:clipmethod to none and deinits clipboard
func Test_no_clipmethod_sets_v_clipmethod_none()
  CheckFeature xterm_clipboard
  CheckFeature wayland_clipboard
  CheckUnix
  CheckNotGui

  set clipmethod=
  call assert_equal("none", v:clipmethod)
  call assert_equal(0, has('clipboard_working'))

  set clipmethod&
endfunc

" Test if method chosen is in line with clipmethod order
func Test_clipmethod_order()
  CheckFeature xterm_clipboard
  CheckFeature wayland_clipboard
  CheckUnix
  CheckNotGui

  set cpm=wayland,x11

  let l:wayland_display = StartWaylandCompositor()

  let $WAYLAND_DISPLAY = l:wayland_display
  exe 'wlrestore ' .. l:wayland_display

  call assert_equal("wayland", v:clipmethod)

  :wlrestore 1239
  clipreset

  if exists("$DISPLAY")
    call assert_equal("x11", v:clipmethod)
  endif

  :xrestore 1239
  clipreset

  call assert_equal("none", v:clipmethod)
  call assert_equal(0, has('clipboard_working'))

  exe ":wlrestore " . $WAYLAND_DISPLAY
  exe ":xrestore " . $DISPLAY
  clipreset

  call assert_equal("wayland", v:clipmethod)
  call assert_equal(1, has('clipboard_working'))

  if exists("$DISPLAY")
    set cpm=x11

    call assert_equal("x11", v:clipmethod)
  endif

  set cpm=wayland

  call assert_equal("wayland", v:clipmethod)

  call EndWaylandCompositor(l:wayland_display)

  set clipmethod&
endfunc

" Test if clipmethod is set to 'none' when gui is started
func Test_clipmethod_is_none_when_gui()
  CheckCanRunGui

  let lines =<< trim END
    set cpm=wayland,x11
    call writefile([v:clipmethod != ""], 'Cbdscript')
    gui -f
    call writefile([v:clipmethod], 'Cbdscript', 'a')
    clipreset
    call writefile([v:clipmethod], 'Cbdscript', 'a')
    quit
  END

  call writefile(lines, 'Cbdscript', 'D')
  call system($'{GetMNVCommand()} -S Cbdscript')
  call assert_equal(['1', 'none', 'none'], readfile('Cbdscript'))
endfunc

" Test if :clipreset switches methods when current one doesn't work
func Test_clipreset_switches()
  CheckFeature xterm_clipboard
  CheckFeature wayland_clipboard
  CheckUnix
  CheckNotGui
  CheckFeature clientserver
  CheckXServer
  CheckWaylandCompositor

  let l:wayland_display = StartWaylandCompositor()

  set cpm=wayland,x11

  exe 'wlrestore ' .. l:wayland_display

  call assert_equal(l:wayland_display, v:wayland_display)
  call assert_equal("wayland", v:clipmethod)

  call EndWaylandCompositor(l:wayland_display)

  " wlrestore updates clipmethod as well
  wlrestore!

  call assert_equal("", v:wayland_display)
  if exists("$DISPLAY")
    call assert_equal("x11", v:clipmethod)
  endif

  " Do the same but kill a X11 server

  " X11 error handling relies on longjmp magic, but essentially if the X server
  " is killed then it will simply abandon the current commands, making the test
  " hang.

  " This will only happen for commands given from the command line, which
  " is why we cannot just directly call MNV or use the actual MNV instance
  " that's doing all the testing, since main_loop() is never executed.

  " Therefore we should start a separate MNV instance and communicate with it
  " remotely, so we can execute the actual testing stuff with main_loop()
  " running.

  let l:lines =<< trim END
    set cpm=x11
    source util/shared.mnv

    func Test()
      clipreset

      if v:clipmethod ==# 'none'
        return 1
      endif
      return 0
    endfunc

    func DoIt()
      call WaitFor(function('Test'))

      if v:clipmethod == 'none'
        call writefile(['SUCCESS'], 'Xtest')
      else
        call writefile(['FAIL'], 'Xtest')
      endif
      quitall
    endfunc
  END
  call writefile(l:lines, 'Xtester', 'D')

  let l:xdisplay = StartXServer()

  let l:name = 'XMNVTEST'
  let l:cmd = GetMNVCommand() .. ' -S Xtester --servername ' .. l:name
  let l:job = job_start(l:cmd, { 'stoponexit': 'kill', 'out_io': 'null'})

  call WaitForAssert({-> assert_equal("run", job_status(l:job))})
  if exists("$DISPLAY")
    call WaitForAssert({-> assert_match(l:name, serverlist())})
  endif

  " Change x server to the one that will be killed, then block until
  " v:clipmethod is none.
  if exists("$DISPLAY")
    call remote_send(l:name, ":xrestore " .. l:xdisplay ..
        \ ' | call DoIt()' .. "\<CR>")

    call EndXServer(l:xdisplay)
    call WaitFor({-> filereadable('Xtest')})

    " For some reason readfile sometimes returns an empty list despite the file
    " existing, this why WaitForAssert() is used.
    call WaitForAssert({-> assert_equal(['SUCCESS'], readfile('Xtest'))}, 1000)
  endif

  set clipmethod&
endfunc

func s:AAvailable()
  return g:a_available
endfunc

func s:BAvailable()
  return g:b_available
endfunc

" Test clipmethod when using provider
func Test_clipmethod_provider()
  CheckFeature clipboard_provider

  let v:clipproviders["a"] = {
        \ "available": function("s:AAvailable"),
        \ }
  let v:clipproviders["b"] = {
        \ "available": function("s:BAvailable"),
        \ }
  let g:a_available = 1
  let g:b_available = 1

  set clipmethod=a,b
  call assert_equal("a", v:clipmethod)

  let g:a_available = 0
  clipreset
  call assert_equal("b", v:clipmethod)

  let g:b_available = 0
  clipreset
  call assert_equal("none", v:clipmethod)

  let g:a_available = 1
  let g:b_available = 1
  clipreset
  call assert_equal("a", v:clipmethod)

  set clipmethod&
endfunc

" mnv: shiftwidth=2 sts=2 expandtab