blob: 58ced56ce3115d88d5b4f041537191b84faba0cf [file] [log] [blame]
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01001" 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
Bram Moolenaara2845b82017-03-25 15:20:06 +010014 if has('unix')
15 try
16 call remote_send('xxx', '')
17 catch
18 if v:exception =~ 'E240:'
19 " No connection to the X server, give up.
20 return
21 endif
22 " ignore other errors
23 endtry
24 endif
Bram Moolenaar42205552017-03-18 19:42:22 +010025
Bram Moolenaar42205552017-03-18 19:42:22 +010026 let name = 'XVIMTEST'
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010027 let cmd .= ' --servername ' . name
28 let g:job = job_start(cmd, {'stoponexit': 'kill', 'out_io': 'null'})
29 call WaitFor('job_status(g:job) == "run"')
30 if job_status(g:job) != 'run'
Bram Moolenaar42205552017-03-18 19:42:22 +010031 call assert_report('Cannot run the Vim server')
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010032 return
33 endif
34
35 " Takes a short while for the server to be active.
36 call WaitFor('serverlist() =~ "' . name . '"')
37 call assert_match(name, serverlist())
38
39 call remote_foreground(name)
40
41 call remote_send(name, ":let testvar = 'yes'\<CR>")
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010042 call WaitFor('remote_expr("' . name . '", "testvar", "", 1) == "yes"')
43 call assert_equal('yes', remote_expr(name, "testvar", "", 2))
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010044
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010045 if has('unix') && has('gui') && !has('gui_running')
46 " Running in a terminal and the GUI is avaiable: Tell the server to open
47 " the GUI and check that the remote command still works.
48 " Need to wait for the GUI to start up, otherwise the send hangs in trying
49 " to send to the terminal window.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010050 if has('gui_athena') || has('gui_motif')
51 " For those GUIs, ignore the 'failed to create input context' error.
52 call remote_send(name, ":call test_ignore_error('E285') | gui -f\<CR>")
53 else
54 call remote_send(name, ":gui -f\<CR>")
55 endif
56 " Wait for the server to be up and answering requests.
57 call WaitFor('remote_expr("' . name . '", "v:version", "", 1) != ""')
58
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010059 call remote_send(name, ":let testvar = 'maybe'\<CR>")
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010060 call WaitFor('remote_expr("' . name . '", "testvar", "", 1) == "maybe"')
61 call assert_equal('maybe', remote_expr(name, "testvar", "", 2))
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010062 endif
63
64 call assert_fails('call remote_send("XXX", ":let testvar = ''yes''\<CR>")', 'E241')
65
66 " Expression evaluated locally.
67 if v:servername == ''
68 call remote_startserver('MYSELF')
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010069 " May get MYSELF1 when running the test again.
70 call assert_match('MYSELF', v:servername)
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010071 endif
72 let g:testvar = 'myself'
73 call assert_equal('myself', remote_expr(v:servername, 'testvar'))
74
75 call remote_send(name, ":call server2client(expand('<client>'), 'got it')\<CR>", 'g:myserverid')
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010076 call assert_equal('got it', remote_read(g:myserverid, 2))
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010077
Bram Moolenaar6caf6062017-03-18 20:45:05 +010078 call remote_send(name, ":call server2client(expand('<client>'), 'another')\<CR>", 'g:myserverid')
Bram Moolenaar6caf6062017-03-18 20:45:05 +010079 let peek_result = 'nothing'
80 let r = remote_peek(g:myserverid, 'peek_result')
Bram Moolenaar6caf6062017-03-18 20:45:05 +010081 " unpredictable whether the result is already avaialble.
82 if r > 0
83 call assert_equal('another', peek_result)
84 elseif r == 0
85 call assert_equal('nothing', peek_result)
86 else
87 call assert_report('remote_peek() failed')
88 endif
89 let g:peek_result = 'empty'
90 call WaitFor('remote_peek(g:myserverid, "g:peek_result") > 0')
Bram Moolenaar6caf6062017-03-18 20:45:05 +010091 call assert_equal('another', g:peek_result)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010092 call assert_equal('another', remote_read(g:myserverid, 2))
Bram Moolenaar6caf6062017-03-18 20:45:05 +010093
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010094 call remote_send(name, ":qa!\<CR>")
95 call WaitFor('job_status(g:job) == "dead"')
96 if job_status(g:job) != 'dead'
Bram Moolenaar42205552017-03-18 19:42:22 +010097 call assert_report('Server did not exit')
Bram Moolenaar15bf76d2017-03-18 16:18:37 +010098 call job_stop(g:job, 'kill')
99 endif
100endfunc
101
102" Uncomment this line to get a debugging log
103" call ch_logfile('channellog', 'w')