1" Tests for the +clientserver feature.
2
3source check.vim
4CheckFeature job
5
6if !has('clientserver')
7  call assert_fails('call remote_startserver("local")', 'E942:')
8endif
9
10CheckFeature clientserver
11
12source shared.vim
13
14func Check_X11_Connection()
15  if has('x11')
16    CheckEnv DISPLAY
17    try
18      call remote_send('xxx', '')
19    catch
20      if v:exception =~ 'E240:'
21        throw 'Skipped: no connection to the X server'
22      endif
23      " ignore other errors
24    endtry
25  endif
26endfunc
27
28func Test_client_server()
29  let g:test_is_flaky = 1
30  let cmd = GetVimCommand()
31  if cmd == ''
32    throw 'GetVimCommand() failed'
33  endif
34  call Check_X11_Connection()
35
36  let name = 'XVIMTEST'
37  let cmd .= ' --servername ' . name
38  let job = job_start(cmd, {'stoponexit': 'kill', 'out_io': 'null'})
39  call WaitForAssert({-> assert_equal("run", job_status(job))})
40
41  " Takes a short while for the server to be active.
42  " When using valgrind it takes much longer.
43  call WaitForAssert({-> assert_match(name, serverlist())})
44
45  if !has('win32')
46    if RunVim([], [], '--serverlist >Xtest_serverlist')
47      let lines = readfile('Xtest_serverlist')
48      call assert_true(index(lines, 'XVIMTEST') >= 0)
49    endif
50    call delete('Xtest_serverlist')
51  endif
52
53  eval name->remote_foreground()
54
55  call remote_send(name, ":let testvar = 'yes'\<CR>")
56  call WaitFor('remote_expr("' . name . '", "exists(\"testvar\") ? testvar : \"\"", "", 1) == "yes"')
57  call assert_equal('yes', remote_expr(name, "testvar", "", 2))
58  call assert_fails("let x=remote_expr(name, '2+x')", 'E449:')
59  call assert_fails("let x=remote_expr('[], '2+2')", 'E116:')
60
61  if has('unix') && has('gui') && !has('gui_running')
62    " Running in a terminal and the GUI is available: Tell the server to open
63    " the GUI and check that the remote command still works.
64    " Need to wait for the GUI to start up, otherwise the send hangs in trying
65    " to send to the terminal window.
66    if has('gui_athena') || has('gui_motif')
67      " For those GUIs, ignore the 'failed to create input context' error.
68      call remote_send(name, ":call test_ignore_error('E285') | gui -f\<CR>")
69    else
70      call remote_send(name, ":gui -f\<CR>")
71    endif
72    " Wait for the server to be up and answering requests.
73    " When using valgrind this can be very, very slow.
74    sleep 1
75    call WaitForAssert({-> assert_match('\d', name->remote_expr("v:version", "", 1))}, 10000)
76
77    call remote_send(name, ":let testvar = 'maybe'\<CR>")
78    call WaitForAssert({-> assert_equal('maybe', remote_expr(name, "testvar", "", 2))})
79  endif
80
81  call assert_fails('call remote_send("XXX", ":let testvar = ''yes''\<CR>")', 'E241:')
82
83  call writefile(['one'], 'Xclientfile')
84  let cmd = GetVimProg() .. ' --servername ' .. name .. ' --remote Xclientfile'
85  call system(cmd)
86  call WaitForAssert({-> assert_equal('Xclientfile', remote_expr(name, "bufname()", "", 2))})
87  call WaitForAssert({-> assert_equal('one', remote_expr(name, "getline(1)", "", 2))})
88  call writefile(['one', 'two'], 'Xclientfile')
89  call system(cmd)
90  call WaitForAssert({-> assert_equal('two', remote_expr(name, "getline(2)", "", 2))})
91  call delete('Xclientfile')
92
93  " Expression evaluated locally.
94  if v:servername == ''
95    eval 'MYSELF'->remote_startserver()
96    " May get MYSELF1 when running the test again.
97    call assert_match('MYSELF', v:servername)
98    call assert_fails("call remote_startserver('MYSELF')", 'E941:')
99  endif
100  let g:testvar = 'myself'
101  call assert_equal('myself', remote_expr(v:servername, 'testvar'))
102  call remote_send(v:servername, ":let g:testvar2 = 75\<CR>")
103  call feedkeys('', 'x')
104  call assert_equal(75, g:testvar2)
105  call assert_fails('let v = remote_expr(v:servername, "/2")', ['E15:.*/2'])
106
107  call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid')
108  call assert_equal('got it', g:myserverid->remote_read(2))
109
110  call remote_send(name, ":eval expand('<client>')->server2client('another')\<CR>", 'g:myserverid')
111  let peek_result = 'nothing'
112  let r = g:myserverid->remote_peek('peek_result')
113  " unpredictable whether the result is already available.
114  if r > 0
115    call assert_equal('another', peek_result)
116  elseif r == 0
117    call assert_equal('nothing', peek_result)
118  else
119    call assert_report('remote_peek() failed')
120  endif
121  let g:peek_result = 'empty'
122  call WaitFor('remote_peek(g:myserverid, "g:peek_result") > 0')
123  call assert_equal('another', g:peek_result)
124  call assert_equal('another', remote_read(g:myserverid, 2))
125
126  if !has('gui_running')
127    " In GUI vim, the following tests display a dialog box
128
129    let cmd = GetVimProg() .. ' --servername ' .. name
130
131    " Run a separate instance to send a command to the server
132    call remote_expr(name, 'execute("only")')
133    call system(cmd .. ' --remote-send ":new Xfile<CR>"')
134    call assert_equal('2', remote_expr(name, 'winnr("$")'))
135    call assert_equal('Xfile', remote_expr(name, 'winbufnr(1)->bufname()'))
136    call remote_expr(name, 'execute("only")')
137
138    " Invoke a remote-expr. On MS-Windows, the returned value has a carriage
139    " return.
140    let l = system(cmd .. ' --remote-expr "2 + 2"')
141    call assert_equal(['4'], split(l, "\n"))
142
143    " Edit multiple files using --remote
144    call system(cmd .. ' --remote Xfile1 Xfile2 Xfile3')
145    call assert_match(".*Xfile1\n.*Xfile2\n.*Xfile3\n", remote_expr(name, 'argv()'))
146    eval name->remote_send(":%bw!\<CR>")
147
148    " Edit files in separate tab pages
149    call system(cmd .. ' --remote-tab Xfile1 Xfile2 Xfile3')
150    call WaitForAssert({-> assert_equal('3', remote_expr(name, 'tabpagenr("$")'))})
151    call assert_match('.*\<Xfile2', remote_expr(name, 'bufname(tabpagebuflist(2)[0])'))
152    eval name->remote_send(":%bw!\<CR>")
153
154    " Edit a file using --remote-wait
155    eval name->remote_send(":source $VIMRUNTIME/plugin/rrhelper.vim\<CR>")
156    call system(cmd .. ' --remote-wait +enew Xfile1')
157    call assert_match('.*\<Xfile1', remote_expr(name, 'bufname("#")'))
158    eval name->remote_send(":%bw!\<CR>")
159
160    " Edit files using --remote-tab-wait
161    call system(cmd .. ' --remote-tabwait +tabonly\|enew Xfile1 Xfile2')
162    call assert_equal('1', remote_expr(name, 'tabpagenr("$")'))
163    eval name->remote_send(":%bw!\<CR>")
164
165    " Error cases
166    if v:lang == "C" || v:lang =~ '^[Ee]n'
167      let l = split(system(cmd .. ' --remote +pwd'), "\n")
168      call assert_equal("Argument missing after: \"+pwd\"", l[1])
169    endif
170    let l = system(cmd .. ' --remote-expr "abcd"')
171    call assert_match('^E449: ', l)
172  endif
173
174  eval name->remote_send(":%bw!\<CR>")
175  eval name->remote_send(":qa!\<CR>")
176  try
177    call WaitForAssert({-> assert_equal("dead", job_status(job))})
178  finally
179    if job_status(job) != 'dead'
180      call assert_report('Server did not exit')
181      call job_stop(job, 'kill')
182    endif
183  endtry
184
185  call assert_fails('call remote_startserver([])', 'E730:')
186  call assert_fails("let x = remote_peek([])", 'E730:')
187  call assert_fails("let x = remote_read('vim10')",
188        \ has('unix') ? ['E573:.*vim10'] : 'E277:')
189  call assert_fails("call server2client('abc', 'xyz')",
190        \ has('unix') ? ['E573:.*abc'] : 'E258:')
191endfunc
192
193" Uncomment this line to get a debugging log
194" call ch_logfile('channellog', 'w')
195
196" vim: shiftwidth=2 sts=2 expandtab
197