1" Tests for the +clientserver feature.
2
3if !has('job') || !has('clientserver')
4  finish
5endif
6
7source shared.vim
8
9func Test_client_server()
10  let cmd = GetVimCommand()
11  if cmd == ''
12    return
13  endif
14  if has('x11')
15    if empty($DISPLAY)
16      throw 'Skipped: $DISPLAY is not set'
17    endif
18    try
19      call remote_send('xxx', '')
20    catch
21      if v:exception =~ 'E240:'
22	throw 'Skipped: no connection to the X server'
23      endif
24      " ignore other errors
25    endtry
26  endif
27
28  let name = 'XVIMTEST'
29  let cmd .= ' --servername ' . name
30  let job = job_start(cmd, {'stoponexit': 'kill', 'out_io': 'null'})
31  call WaitFor({-> job_status(job) == "run"})
32
33  " Takes a short while for the server to be active.
34  " When using valgrind it takes much longer.
35  call WaitFor('serverlist() =~ "' . name . '"', 5000)
36  call assert_match(name, serverlist())
37
38  call remote_foreground(name)
39
40  call remote_send(name, ":let testvar = 'yes'\<CR>")
41  call WaitFor('remote_expr("' . name . '", "exists(\"testvar\") ? testvar : \"\"", "", 1) == "yes"')
42  call assert_equal('yes', remote_expr(name, "testvar", "", 2))
43
44  if has('unix') && has('gui') && !has('gui_running')
45    " Running in a terminal and the GUI is available: Tell the server to open
46    " the GUI and check that the remote command still works.
47    " Need to wait for the GUI to start up, otherwise the send hangs in trying
48    " to send to the terminal window.
49    if has('gui_athena') || has('gui_motif')
50      " For those GUIs, ignore the 'failed to create input context' error.
51      call remote_send(name, ":call test_ignore_error('E285') | gui -f\<CR>")
52    else
53      call remote_send(name, ":gui -f\<CR>")
54    endif
55    " Wait for the server to be up and answering requests.
56    sleep 100m
57    call WaitFor('remote_expr("' . name . '", "v:version", "", 1) != ""')
58    call assert_true(remote_expr(name, "v:version", "", 1) != "")
59
60    call remote_send(name, ":let testvar = 'maybe'\<CR>")
61    call WaitFor('remote_expr("' . name . '", "testvar", "", 1) == "maybe"')
62    call assert_equal('maybe', remote_expr(name, "testvar", "", 2))
63  endif
64
65  call assert_fails('call remote_send("XXX", ":let testvar = ''yes''\<CR>")', 'E241')
66
67  " Expression evaluated locally.
68  if v:servername == ''
69    call remote_startserver('MYSELF')
70    " May get MYSELF1 when running the test again.
71    call assert_match('MYSELF', v:servername)
72  endif
73  let g:testvar = 'myself'
74  call assert_equal('myself', remote_expr(v:servername, 'testvar'))
75
76  call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid')
77  call assert_equal('got it', remote_read(g:myserverid, 2))
78
79  call remote_send(name, ":call server2client(expand('<client>'), 'another')\<CR>", 'g:myserverid')
80  let peek_result = 'nothing'
81  let r = remote_peek(g:myserverid, 'peek_result')
82  " unpredictable whether the result is already available.
83  if r > 0
84    call assert_equal('another', peek_result)
85  elseif r == 0
86    call assert_equal('nothing', peek_result)
87  else
88    call assert_report('remote_peek() failed')
89  endif
90  let g:peek_result = 'empty'
91  call WaitFor('remote_peek(g:myserverid, "g:peek_result") > 0')
92  call assert_equal('another', g:peek_result)
93  call assert_equal('another', remote_read(g:myserverid, 2))
94
95  call remote_send(name, ":qa!\<CR>")
96  try
97    call WaitFor({-> job_status(job) == "dead"})
98  finally
99    if job_status(job) != 'dead'
100      call assert_report('Server did not exit')
101      call job_stop(job, 'kill')
102    endif
103  endtry
104endfunc
105
106" Uncomment this line to get a debugging log
107" call ch_logfile('channellog', 'w')
108