blob: 756ad68c4dc19f375238648ae307cc362fc04438 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
3if !has('textprop')
4 finish
5endif
6
7source screendump.vim
8
9func Test_simple_popup()
10 if !CanRunVimInTerminal()
11 return
12 endif
13 call writefile([
14 \ "call setline(1, range(1, 100))",
Bram Moolenaar20c023a2019-05-26 21:03:24 +020015 \ "hi PopupColor1 ctermbg=lightblue",
16 \ "hi PopupColor2 ctermbg=lightcyan",
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020017 \ "hi Comment ctermfg=red",
18 \ "call prop_type_add('comment', {'highlight': 'Comment'})",
Bram Moolenaar60cdb302019-05-27 21:54:10 +020019 \ "let winid = popup_create('hello there', {'line': 3, 'col': 11, 'minwidth': 20, 'highlight': 'PopupColor1'})",
20 \ "let winid2 = popup_create(['another one', 'another two', 'another three'], {'line': 3, 'col': 25, 'minwidth': 20})",
Bram Moolenaar20c023a2019-05-26 21:03:24 +020021 \ "call setwinvar(winid2, '&wincolor', 'PopupColor2')",
Bram Moolenaar4d784b22019-05-25 19:51:39 +020022 \], 'XtestPopup')
23 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
24 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
25
Bram Moolenaarec583842019-05-26 14:11:23 +020026 " Add a tabpage
27 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020028 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020029 \ .. "{'text': 'other tab'},"
30 \ .. "{'text': 'a comment line', 'props': [{"
Bram Moolenaar60cdb302019-05-27 21:54:10 +020031 \ .. "'col': 3, 'length': 7, 'minwidth': 20, 'type': 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020032 \ .. "}]},"
Bram Moolenaar60cdb302019-05-27 21:54:10 +020033 \ .. "], {'line': 4, 'col': 9, 'minwidth': 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020034 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
35
36 " switch back to first tabpage
37 call term_sendkeys(buf, "gt")
38 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
39
40 " close that tabpage
41 call term_sendkeys(buf, ":quit!\<CR>")
42 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
43
Bram Moolenaar60cdb302019-05-27 21:54:10 +020044 " resize popup
45 call term_sendkeys(buf, ":call popup_move(popupwin, {'minwidth': 15, 'maxwidth': 25, 'minheight': 3, 'maxheight': 5})\<CR>")
46 call term_sendkeys(buf, ":redraw\<CR>")
47 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
48
Bram Moolenaar4d784b22019-05-25 19:51:39 +020049 " clean up
50 call StopVimInTerminal(buf)
51 call delete('XtestPopup')
52endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020053
54func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +020055 if !has('timers')
56 return
57 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020058 topleft vnew
59 call setline(1, 'hello')
60
61 call popup_create('world', {
62 \ 'line': 1,
63 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +020064 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020065 \ 'time': 500,
66 \})
67 redraw
68 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
69 call assert_equal('world', line)
70
71 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +020072 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020073 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
74 call assert_equal('hello', line)
75
76 call popup_create('on the command line', {
77 \ 'line': &lines,
78 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +020079 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020080 \ 'time': 500,
81 \})
82 redraw
83 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
84 call assert_match('.*on the command line.*', line)
85
86 sleep 700m
87 redraw
88 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
89 call assert_notmatch('.*on the command line.*', line)
90
91 bwipe!
92endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +020093
94func Test_popup_hide()
95 topleft vnew
96 call setline(1, 'hello')
97
98 let winid = popup_create('world', {
99 \ 'line': 1,
100 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200101 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200102 \})
103 redraw
104 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
105 call assert_equal('world', line)
106
107 call popup_hide(winid)
108 redraw
109 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
110 call assert_equal('hello', line)
111
112 call popup_show(winid)
113 redraw
114 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
115 call assert_equal('world', line)
116
117
118 call popup_close(winid)
119 redraw
120 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
121 call assert_equal('hello', line)
122
123 " error is given for existing non-popup window
124 call assert_fails('call popup_hide(win_getid())', 'E993:')
125
126 " no error non-existing window
127 call popup_hide(1234234)
128 call popup_show(41234234)
129
130 bwipe!
131endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200132
133func Test_popup_move()
134 topleft vnew
135 call setline(1, 'hello')
136
137 let winid = popup_create('world', {
138 \ 'line': 1,
139 \ 'col': 1,
140 \ 'minwidth': 20,
141 \})
142 redraw
143 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
144 call assert_equal('world ', line)
145
146 call popup_move(winid, {'line': 2, 'col': 2})
147 redraw
148 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
149 call assert_equal('hello ', line)
150 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
151 call assert_equal('~world', line)
152
153 call popup_move(winid, {'line': 1})
154 redraw
155 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
156 call assert_equal('hworld', line)
157
158 call popup_close(winid)
159
160 bwipe!
161endfunc