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