blob: 954cf2a0660c2f4005782a7b39ef5aaa6a349795 [file] [log] [blame]
Bram Moolenaar877e9572016-08-04 20:05:50 +02001" Tests specifically for the GUI
2
Bram Moolenaar9f0139a2017-08-13 20:26:20 +02003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
5CheckCanRunGui
Bram Moolenaar877e9572016-08-04 20:05:50 +02006
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +01007source setup_gui.vim
Bram Moolenaar13c724f2017-02-05 20:54:26 +01008
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +01009func Setup()
10 call GUISetUpCommon()
Bram Moolenaar50fa8dd2016-08-09 22:58:21 +020011endfunc
12
13func TearDown()
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +010014 call GUITearDownCommon()
Bram Moolenaar877e9572016-08-04 20:05:50 +020015endfunc
16
17" Test for resetting "secure" flag after GUI has started.
Bram Moolenaar5d7ead32018-02-27 17:17:42 +010018" Must be run first, since it starts the GUI on Unix.
Bram Moolenaar877e9572016-08-04 20:05:50 +020019func Test_1_set_secure()
20 set exrc secure
21 gui -f
22 call assert_equal(1, has('gui_running'))
23endfunc
24
Bram Moolenaard5841f22017-03-05 13:27:25 +010025" As for non-GUI, a balloon_show() test was already added with patch 8.0.0401
26func Test_balloon_show()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020027 CheckFeature balloon_eval
28 " This won't do anything but must not crash either.
29 call balloon_show('hi!')
Bram Moolenaard5841f22017-03-05 13:27:25 +010030endfunc
31
Bram Moolenaar87748452017-03-12 17:10:33 +010032func Test_colorscheme()
Bram Moolenaaracc770a2020-04-12 15:11:06 +020033 call assert_equal('16777216', &t_Co)
34
Bram Moolenaar87748452017-03-12 17:10:33 +010035 let colorscheme_saved = exists('g:colors_name') ? g:colors_name : 'default'
Bram Moolenaar60a68362018-04-30 15:40:48 +020036 let g:color_count = 0
37 augroup TestColors
38 au!
39 au ColorScheme * let g:color_count += 1| let g:after_colors = g:color_count
40 au ColorSchemePre * let g:color_count += 1 |let g:before_colors = g:color_count
41 augroup END
Bram Moolenaar87748452017-03-12 17:10:33 +010042
43 colorscheme torte
44 redraw!
Bram Moolenaar87748452017-03-12 17:10:33 +010045 call assert_equal('dark', &background)
Bram Moolenaar60a68362018-04-30 15:40:48 +020046 call assert_equal(1, g:before_colors)
47 call assert_equal(2, g:after_colors)
Bram Moolenaar6d4470b2019-01-08 21:05:51 +010048 call assert_equal("\ntorte", execute('colorscheme'))
49
50 let a = substitute(execute('hi Search'), "\n\\s\\+", ' ', 'g')
Bram Moolenaar98feace2022-05-14 12:34:43 +010051 " FIXME: temporarily check less while the colorscheme changes
52 " call assert_match("\nSearch xxx term=reverse cterm=reverse ctermfg=196 ctermbg=16 gui=reverse guifg=#ff0000 guibg=#000000", a)
53 call assert_match("\nSearch xxx term=reverse ", a)
Bram Moolenaar6d4470b2019-01-08 21:05:51 +010054
55 call assert_fails('colorscheme does_not_exist', 'E185:')
Bram Moolenaar87748452017-03-12 17:10:33 +010056
57 exec 'colorscheme' colorscheme_saved
Bram Moolenaar60a68362018-04-30 15:40:48 +020058 augroup TestColors
59 au!
60 augroup END
61 unlet g:color_count g:after_colors g:before_colors
Bram Moolenaar87748452017-03-12 17:10:33 +010062 redraw!
63endfunc
64
Bram Moolenaar6f785742017-02-06 22:11:55 +010065func Test_getfontname_with_arg()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +010066 CheckX11BasedGui
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010067
Bram Moolenaar0b962e52022-04-03 18:02:37 +010068 if has('gui_motif')
Bram Moolenaar6f785742017-02-06 22:11:55 +010069 " Invalid font name. The result should be an empty string.
70 call assert_equal('', getfontname('notexist'))
71
72 " Valid font name. This is usually the real name of 7x13 by default.
Bram Moolenaar87748452017-03-12 17:10:33 +010073 let fname = '-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1'
74 call assert_match(fname, getfontname(fname))
Bram Moolenaar6f785742017-02-06 22:11:55 +010075
76 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
77 " Invalid font name. The result should be the name plus the default size.
78 call assert_equal('notexist 10', getfontname('notexist'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010079 call assert_equal('', getfontname('*'))
Bram Moolenaar6f785742017-02-06 22:11:55 +010080
81 " Valid font name. This is usually the real name of Monospace by default.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010082 let fname = 'Bitstream Vera Sans Mono 12'
83 call assert_equal(fname, getfontname(fname))
84 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +010085endfunc
86
87func Test_getfontname_without_arg()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +010088 CheckX11BasedGui
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010089
90 let fname = getfontname()
91
Bram Moolenaar40bd5a12021-10-16 21:58:27 +010092 if has('gui_kde')
Bram Moolenaar6f785742017-02-06 22:11:55 +010093 " 'expected' is the value specified by SetUp() above.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010094 call assert_equal('Courier 10 Pitch/8/-1/5/50/0/0/0/0/0', fname)
Bram Moolenaar0b962e52022-04-03 18:02:37 +010095 elseif has('gui_motif')
Bram Moolenaar87748452017-03-12 17:10:33 +010096 " 'expected' is DFLT_FONT of gui_x11.c or its real name.
97 let pat = '\(7x13\)\|\(\c-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1\)'
98 call assert_match(pat, fname)
Bram Moolenaar6f785742017-02-06 22:11:55 +010099 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
100 " 'expected' is DEFAULT_FONT of gui_gtk_x11.c.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100101 call assert_equal('Monospace 10', fname)
102 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +0100103endfunc
104
Bram Moolenaar87748452017-03-12 17:10:33 +0100105func Test_getwinpos()
106 call assert_match('Window position: X \d\+, Y \d\+', execute('winpos'))
107 call assert_true(getwinposx() >= 0)
108 call assert_true(getwinposy() >= 0)
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200109 call assert_equal([getwinposx(), getwinposy()], getwinpos())
Bram Moolenaar87748452017-03-12 17:10:33 +0100110endfunc
111
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100112func Test_quoteplus()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100113 CheckX11BasedGui
114
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100115 let g:test_is_flaky = 1
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100116
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100117 let quoteplus_saved = @+
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100118
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100119 let test_call = 'Can you hear me?'
120 let test_response = 'Yes, I can.'
Bram Moolenaarde8be2b2022-06-16 14:45:41 +0100121 let testee = 'VIMRUNTIME=' .. $VIMRUNTIME .. '; export VIMRUNTIME;'
122 \ .. GetVimCommand() .. ' --noplugin --not-a-term -c ''%s'''
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100123 " Ignore the "failed to create input context" error.
124 let cmd = 'call test_ignore_error("E285") | '
125 \ . 'gui -f | '
126 \ . 'call feedkeys("'
127 \ . '\"+p'
128 \ . ':s/' . test_call . '/' . test_response . '/\<CR>'
129 \ . '\"+yis'
130 \ . ':q!\<CR>", "tx")'
131 let run_vimtest = printf(testee, cmd)
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100132
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100133 " Set the quoteplus register to test_call, and another gvim will launched.
134 " Then, it first tries to paste the content of its own quotedplus register
135 " onto it. Second, it tries to substitute test_response for the pasted
136 " sentence. If the sentence is identical to test_call, the substitution
137 " should succeed. Third, it tries to yank the result of the substitution
138 " to its own quoteplus register, and last it quits. When system()
139 " returns, the content of the quoteplus register should be identical to
140 " test_response if those quoteplus registers are synchronized properly
141 " with/through the X11 clipboard.
142 let @+ = test_call
143 call system(run_vimtest)
144 call assert_equal(test_response, @+)
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100145
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100146 let @+ = quoteplus_saved
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100147endfunc
148
Bram Moolenaard68a0042021-10-20 23:08:11 +0100149func Test_gui_read_stdin()
150 CheckUnix
151
152 call writefile(['some', 'lines'], 'Xstdin')
153 let script =<< trim END
154 call writefile(getline(1, '$'), 'XstdinOK')
155 qa!
156 END
157 call writefile(script, 'Xscript')
158
159 " Cannot use --not-a-term here, the "reading from stdin" message would not be
160 " displayed.
Bram Moolenaarb5912e02022-05-31 17:03:14 +0100161 " However, when using XIM we might get E285, do use it then.
162 if has('xim')
163 let vimcmd = GetVimCommand()
164 else
165 let vimcmd = substitute(GetVimCommand(), '--not-a-term', '', '')
166 endif
Bram Moolenaard68a0042021-10-20 23:08:11 +0100167
168 call system('cat Xstdin | ' .. vimcmd .. ' -f -g -S Xscript -')
169 call assert_equal(['some', 'lines'], readfile('XstdinOK'))
170
171 call delete('Xstdin')
172 call delete('XstdinOK')
173 call delete('Xscript')
174endfunc
175
Bram Moolenaar87748452017-03-12 17:10:33 +0100176func Test_set_background()
177 let background_saved = &background
178
179 set background&
180 call assert_equal('light', &background)
181
182 set background=dark
183 call assert_equal('dark', &background)
184
185 let &background = background_saved
186endfunc
187
Bram Moolenaard5841f22017-03-05 13:27:25 +0100188func Test_set_balloondelay()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200189 CheckOption balloondelay
Bram Moolenaard5841f22017-03-05 13:27:25 +0100190
191 let balloondelay_saved = &balloondelay
192
193 " Check if the default value is identical to that described in the manual.
194 set balloondelay&
195 call assert_equal(600, &balloondelay)
196
197 " Edge cases
198
199 " XXX This fact should be hidden so that people won't be tempted to write
200 " plugin/TimeMachine.vim. TODO Add reasonable range checks to the source
201 " code.
202 set balloondelay=-1
203 call assert_equal(-1, &balloondelay)
204
205 " Though it's possible to interpret the zero delay to be 'as soon as
206 " possible' or even 'indefinite', its actual meaning depends on the GUI
207 " toolkit in use after all.
208 set balloondelay=0
209 call assert_equal(0, &balloondelay)
210
211 set balloondelay=1
212 call assert_equal(1, &balloondelay)
213
214 " Since p_bdelay is of type long currently, the upper bound can be
215 " impractically huge and machine-dependent. Practically, it's sufficient
Bram Moolenaar0f9ea222017-03-05 13:48:13 +0100216 " to check if balloondelay works with 0x7fffffff (32 bits) for now.
217 set balloondelay=2147483647
218 call assert_equal(2147483647, &balloondelay)
Bram Moolenaard5841f22017-03-05 13:27:25 +0100219
220 let &balloondelay = balloondelay_saved
221endfunc
222
223func Test_set_ballooneval()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200224 CheckOption ballooneval
Bram Moolenaard5841f22017-03-05 13:27:25 +0100225
226 let ballooneval_saved = &ballooneval
227
228 set ballooneval&
229 call assert_equal(0, &ballooneval)
230
231 set ballooneval
232 call assert_notequal(0, &ballooneval)
233
234 set noballooneval
235 call assert_equal(0, &ballooneval)
236
237 let &ballooneval = ballooneval_saved
238endfunc
239
240func Test_set_balloonexpr()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200241 CheckOption balloonexpr
Bram Moolenaard5841f22017-03-05 13:27:25 +0100242
243 let balloonexpr_saved = &balloonexpr
244
245 " Default value
246 set balloonexpr&
247 call assert_equal('', &balloonexpr)
248
249 " User-defined function
250 new
251 func MyBalloonExpr()
252 return 'Cursor is at line ' . v:beval_lnum .
253 \', column ' . v:beval_col .
254 \ ' of file ' . bufname(v:beval_bufnr) .
255 \ ' on word "' . v:beval_text . '"' .
256 \ ' in window ' . v:beval_winid . ' (#' . v:beval_winnr . ')'
257 endfunc
258 setl balloonexpr=MyBalloonExpr()
259 setl ballooneval
260 call assert_equal('MyBalloonExpr()', &balloonexpr)
261 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200262 " and check if the content of the balloon is the same as what is expected.
Bram Moolenaard5841f22017-03-05 13:27:25 +0100263 " Also, check if textlock works as expected.
264 setl balloonexpr&
265 call assert_equal('', &balloonexpr)
266 delfunc MyBalloonExpr
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +0000267
268 " Using a script-local function
269 func s:NewBalloonExpr()
270 endfunc
271 set balloonexpr=s:NewBalloonExpr()
272 call assert_equal(expand('<SID>') .. 'NewBalloonExpr()', &balloonexpr)
273 set balloonexpr=<SID>NewBalloonExpr()
274 call assert_equal(expand('<SID>') .. 'NewBalloonExpr()', &balloonexpr)
275 delfunc s:NewBalloonExpr
Bram Moolenaard5841f22017-03-05 13:27:25 +0100276 bwipe!
277
278 " Multiline support
279 if has('balloon_multiline')
280 " Multiline balloon using NL
281 new
282 func MyBalloonFuncForMultilineUsingNL()
Dominique Pelle81b573d2022-03-22 21:14:55 +0000283 return "Multiline\nSupported\nBalloon\nusing NL"
Bram Moolenaard5841f22017-03-05 13:27:25 +0100284 endfunc
285 setl balloonexpr=MyBalloonFuncForMultilineUsingNL()
286 setl ballooneval
287 call assert_equal('MyBalloonFuncForMultilineUsingNL()', &balloonexpr)
288 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200289 " and check if the content of the balloon is the same as what is
Bram Moolenaard5841f22017-03-05 13:27:25 +0100290 " expected. Also, check if textlock works as expected.
291 setl balloonexpr&
292 delfunc MyBalloonFuncForMultilineUsingNL
293 bwipe!
294
295 " Multiline balloon using List
296 new
297 func MyBalloonFuncForMultilineUsingList()
Dominique Pelle81b573d2022-03-22 21:14:55 +0000298 return [ 'Multiline', 'Supported', 'Balloon', 'using List' ]
Bram Moolenaard5841f22017-03-05 13:27:25 +0100299 endfunc
300 setl balloonexpr=MyBalloonFuncForMultilineUsingList()
301 setl ballooneval
302 call assert_equal('MyBalloonFuncForMultilineUsingList()', &balloonexpr)
303 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200304 " and check if the content of the balloon is the same as what is
Bram Moolenaard5841f22017-03-05 13:27:25 +0100305 " expected. Also, check if textlock works as expected.
306 setl balloonexpr&
307 delfunc MyBalloonFuncForMultilineUsingList
308 bwipe!
309 endif
310
311 let &balloonexpr = balloonexpr_saved
312endfunc
313
Bram Moolenaar87748452017-03-12 17:10:33 +0100314" Invalid arguments are tested with test_options in conjunction with segfaults
315" caused by them (Patch 8.0.0357, 24922ec233).
316func Test_set_guicursor()
317 let guicursor_saved = &guicursor
318
319 let default = [
320 \ "n-v-c:block-Cursor/lCursor",
321 \ "ve:ver35-Cursor",
322 \ "o:hor50-Cursor",
323 \ "i-ci:ver25-Cursor/lCursor",
324 \ "r-cr:hor20-Cursor/lCursor",
325 \ "sm:block-Cursor-blinkwait175-blinkoff150-blinkon175"
326 \ ]
327
328 " Default Value
329 set guicursor&
330 call assert_equal(join(default, ','), &guicursor)
331
332 " Argument List Example 1
333 let opt_list = copy(default)
334 let opt_list[0] = "n-c-v:block-nCursor"
335 exec "set guicursor=" . join(opt_list, ',')
336 call assert_equal(join(opt_list, ','), &guicursor)
337 unlet opt_list
338
339 " Argument List Example 2
340 let opt_list = copy(default)
341 let opt_list[3] = "i-ci:ver30-iCursor-blinkwait300-blinkon200-blinkoff150"
342 exec "set guicursor=" . join(opt_list, ',')
343 call assert_equal(join(opt_list, ','), &guicursor)
344 unlet opt_list
345
346 " 'a' Mode
347 set guicursor&
348 let &guicursor .= ',a:blinkon0'
349 call assert_equal(join(default, ',') . ",a:blinkon0", &guicursor)
350
351 let &guicursor = guicursor_saved
352endfunc
353
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100354func Test_set_guifont_errors()
355 if has('win32')
356 " Invalid font names are accepted in GTK GUI
357 call assert_fails('set guifont=xa1bc23d7f', 'E596:')
358 endif
359
360 " This only works if 'renderoptions' exists and does not work for Windows XP
361 " and older.
362 if exists('+renderoptions') && windowsversion() !~ '^[345]\.'
363 " doing this four times used to cause a crash
364 set renderoptions=type:directx
365 for i in range(5)
366 set guifont=
367 endfor
368 set renderoptions=
369 for i in range(5)
370 set guifont=
371 endfor
372 endif
373endfunc
374
Bram Moolenaar43dded82017-02-09 16:06:17 +0100375func Test_set_guifont()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100376 CheckX11BasedGui
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100377
378 let guifont_saved = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100379 if has('xfontset')
380 " Prevent 'guifontset' from canceling 'guifont'.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100381 let guifontset_saved = &guifontset
Bram Moolenaar43dded82017-02-09 16:06:17 +0100382 set guifontset=
383 endif
384
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100385 if has('gui_motif')
Bram Moolenaar43dded82017-02-09 16:06:17 +0100386 " Non-empty font list with invalid font names.
387 "
388 " This test is twofold: (1) It checks if the command fails as expected
389 " when there are no loadable fonts found in the list. (2) It checks if
390 " 'guifont' remains the same after the command loads none of the fonts
391 " listed.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100392 let flist = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100393 call assert_fails('set guifont=-notexist1-*,-notexist2-*')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100394 call assert_equal(flist, &guifont)
Bram Moolenaar43dded82017-02-09 16:06:17 +0100395
396 " Non-empty font list with a valid font name. Should pick up the first
397 " valid font.
398 set guifont=-notexist1-*,fixed,-notexist2-*
Bram Moolenaar87748452017-03-12 17:10:33 +0100399 let pat = '\(fixed\)\|\(\c-Misc-Fixed-Medium-R-SemiCondensed--13-120-75-75-C-60-ISO8859-1\)'
400 call assert_match(pat, getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100401
402 " Empty list. Should fallback to the built-in default.
403 set guifont=
Bram Moolenaar87748452017-03-12 17:10:33 +0100404 let pat = '\(7x13\)\|\(\c-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1\)'
405 call assert_match(pat, getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100406
407 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
408 " For GTK, what we refer to as 'font names' in our manual are actually
409 " 'initial font patterns'. A valid font which matches the 'canonical font
410 " pattern' constructed from a given 'initial pattern' is to be looked up
411 " and loaded. That explains why the GTK GUIs appear to accept 'invalid
412 " font names'.
413 "
414 " Non-empty list. Should always pick up the first element, no matter how
415 " strange it is, as explained above.
416 set guifont=(´・ω・`)\ 12,Courier\ 12
417 call assert_equal('(´・ω・`) 12', getfontname())
418
419 " Empty list. Should fallback to the built-in default.
420 set guifont=
421 call assert_equal('Monospace 10', getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100422 endif
423
424 if has('xfontset')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100425 let &guifontset = guifontset_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100426 endif
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100427 let &guifont = guifont_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100428endfunc
429
Bram Moolenaar10434672017-02-12 19:59:08 +0100430func Test_set_guifontset()
Bram Moolenaarce90e362019-09-08 18:58:44 +0200431 CheckFeature xfontset
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100432 let skipped = ''
Bram Moolenaar10434672017-02-12 19:59:08 +0100433
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200434 call assert_fails('set guifontset=*', 'E597:')
435
Bram Moolenaarce90e362019-09-08 18:58:44 +0200436 let ctype_saved = v:ctype
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100437
Bram Moolenaarce90e362019-09-08 18:58:44 +0200438 " First, since XCreateFontSet(3) is very sensitive to locale, fonts must
439 " be chosen meticulously.
440 let font_head = '-misc-fixed-medium-r-normal--14'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100441
Bram Moolenaarce90e362019-09-08 18:58:44 +0200442 let font_aw70 = font_head . '-130-75-75-c-70'
443 let font_aw140 = font_head . '-130-75-75-c-140'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100444
Bram Moolenaarce90e362019-09-08 18:58:44 +0200445 let font_jisx0201 = font_aw70 . '-jisx0201.1976-0'
446 let font_jisx0208 = font_aw140 . '-jisx0208.1983-0'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100447
Bram Moolenaarce90e362019-09-08 18:58:44 +0200448 let full_XLFDs = join([ font_jisx0208, font_jisx0201 ], ',')
449 let short_XLFDs = join([ font_aw140, font_aw70 ], ',')
450 let singleton = font_head . '-*'
451 let aliases = 'k14,r14'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100452
Bram Moolenaarce90e362019-09-08 18:58:44 +0200453 " Second, among 'locales', look up such a locale that gets 'set
454 " guifontset=' to work successfully with every fontset specified with
455 " 'fontsets'.
456 let locales = [ 'ja_JP.UTF-8', 'ja_JP.eucJP', 'ja_JP.SJIS' ]
457 let fontsets = [ full_XLFDs, short_XLFDs, singleton, aliases ]
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100458
Bram Moolenaarce90e362019-09-08 18:58:44 +0200459 let feasible = 0
460 for locale in locales
461 try
462 exec 'language ctype' locale
463 catch /^Vim\%((\a\+)\)\=:E197/
464 continue
465 endtry
466 let done = 0
467 for fontset in fontsets
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100468 try
Bram Moolenaarce90e362019-09-08 18:58:44 +0200469 exec 'set guifontset=' . fontset
470 catch /^Vim\%((\a\+)\)\=:E\%(250\|252\|234\|597\|598\)/
471 break
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100472 endtry
Bram Moolenaarce90e362019-09-08 18:58:44 +0200473 let done += 1
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100474 endfor
Bram Moolenaarce90e362019-09-08 18:58:44 +0200475 if done == len(fontsets)
476 let feasible = 1
477 break
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100478 endif
Bram Moolenaarce90e362019-09-08 18:58:44 +0200479 endfor
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100480
Bram Moolenaarce90e362019-09-08 18:58:44 +0200481 " Third, give a set of tests if it is found feasible.
482 if !feasible
483 let skipped = g:not_hosted
484 else
485 " N.B. 'v:ctype' has already been set to an appropriate value in the
486 " previous loop.
487 for fontset in fontsets
488 exec 'set guifontset=' . fontset
489 call assert_equal(fontset, &guifontset)
490 endfor
Bram Moolenaar10434672017-02-12 19:59:08 +0100491 endif
492
Bram Moolenaarce90e362019-09-08 18:58:44 +0200493 " Finally, restore ctype.
494 exec 'language ctype' ctype_saved
495
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100496 if !empty(skipped)
497 throw skipped
Bram Moolenaar10434672017-02-12 19:59:08 +0100498 endif
499endfunc
500
501func Test_set_guifontwide()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100502 CheckX11BasedGui
Bram Moolenaar10434672017-02-12 19:59:08 +0100503
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100504 call assert_fails('set guifontwide=*', 'E533:')
505
506 if has('gui_gtk')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100507 let guifont_saved = &guifont
508 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100509
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100510 let fc_match = exepath('fc-match')
511 if empty(fc_match)
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100512 let skipped = g:not_hosted
Bram Moolenaar10434672017-02-12 19:59:08 +0100513 else
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100514 let &guifont = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=en')
515 let wide = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=ja')
516 exec 'set guifontwide=' . fnameescape(wide)
517 call assert_equal(wide, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100518 endif
519
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100520 let &guifontwide = guifontwide_saved
521 let &guifont = guifont_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100522
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100523 elseif has('gui_motif')
Bram Moolenaar10434672017-02-12 19:59:08 +0100524 " guifontwide is premised upon the xfontset feature.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100525 if !has('xfontset')
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100526 let skipped = g:not_supported . 'xfontset'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100527 else
528 let encoding_saved = &encoding
529 let guifont_saved = &guifont
530 let guifontset_saved = &guifontset
531 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100532
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100533 let nfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-90-iso10646-1'
534 let wfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-180-iso10646-1'
Bram Moolenaar10434672017-02-12 19:59:08 +0100535
536 set encoding=utf-8
537
538 " Case 1: guifontset is empty
539 set guifontset=
540
541 " Case 1-1: Automatic selection
542 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100543 exec 'set guifont=' . nfont
544 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100545
546 " Case 1-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100547 exec 'set guifontwide=' . wfont
548 exec 'set guifont=' . nfont
549 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100550
551 " Case 2: guifontset is invalid
552 try
553 set guifontset=-*-notexist-*
Bram Moolenaar37175402017-03-18 20:18:45 +0100554 call assert_report("'set guifontset=-*-notexist-*' should have failed")
Bram Moolenaar10434672017-02-12 19:59:08 +0100555 catch
Bram Moolenaare2e40752020-09-04 21:18:46 +0200556 call assert_exception('E598:')
Bram Moolenaar10434672017-02-12 19:59:08 +0100557 endtry
Bram Moolenaar10434672017-02-12 19:59:08 +0100558
559 " Case 2-1: Automatic selection
560 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100561 exec 'set guifont=' . nfont
562 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100563
564 " Case 2-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100565 exec 'set guifontwide=' . wfont
566 exec 'set guifont=' . nfont
567 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100568
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100569 let &guifontwide = guifontwide_saved
570 let &guifontset = guifontset_saved
571 let &guifont = guifont_saved
572 let &encoding = encoding_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100573 endif
Bram Moolenaar10434672017-02-12 19:59:08 +0100574 endif
Bram Moolenaar10434672017-02-12 19:59:08 +0100575endfunc
576
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100577func Test_set_guiligatures()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100578 CheckX11BasedGui
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100579
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100580 if has('gui_gtk') || has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
581 " Try correct value
582 set guiligatures=<>=ab
583 call assert_equal("<>=ab", &guiligatures)
584 " Try to throw error
585 try
586 set guiligatures=<>=šab
587 call assert_report("'set guiligatures=<>=šab should have failed")
588 catch
589 call assert_exception('E1243:')
590 endtry
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100591 endif
592endfunc
593
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100594func Test_set_guiheadroom()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100595 CheckX11BasedGui
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100596
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100597 " Since this script is to be read together with '-U NONE', the default
598 " value must be preserved.
599 call assert_equal(50, &guiheadroom)
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100600endfunc
601
Bram Moolenaar87748452017-03-12 17:10:33 +0100602func Test_set_guioptions()
603 let guioptions_saved = &guioptions
604 let duration = '200m'
605
606 if has('win32')
607 " Default Value
608 set guioptions&
609 call assert_equal('egmrLtT', &guioptions)
610
611 else
612 " Default Value
613 set guioptions&
614 call assert_equal('aegimrLtT', &guioptions)
615
616 " To activate scrollbars of type 'L' or 'R'.
617 wincmd v
618 redraw!
619
620 " Remove all default GUI ornaments
621 set guioptions-=T
622 exec 'sleep' . duration
623 call assert_equal('aegimrLt', &guioptions)
624 set guioptions-=t
625 exec 'sleep' . duration
626 call assert_equal('aegimrL', &guioptions)
627 set guioptions-=L
628 exec 'sleep' . duration
629 call assert_equal('aegimr', &guioptions)
630 set guioptions-=r
631 exec 'sleep' . duration
632 call assert_equal('aegim', &guioptions)
633 set guioptions-=m
634 exec 'sleep' . duration
635 call assert_equal('aegi', &guioptions)
636
637 " Try non-default GUI ornaments
638 set guioptions+=l
639 exec 'sleep' . duration
640 call assert_equal('aegil', &guioptions)
641 set guioptions-=l
642 exec 'sleep' . duration
643 call assert_equal('aegi', &guioptions)
644
645 set guioptions+=R
646 exec 'sleep' . duration
647 call assert_equal('aegiR', &guioptions)
648 set guioptions-=R
649 exec 'sleep' . duration
650 call assert_equal('aegi', &guioptions)
651
652 set guioptions+=b
653 exec 'sleep' . duration
654 call assert_equal('aegib', &guioptions)
655 set guioptions+=h
656 exec 'sleep' . duration
657 call assert_equal('aegibh', &guioptions)
658 set guioptions-=h
659 exec 'sleep' . duration
660 call assert_equal('aegib', &guioptions)
661 set guioptions-=b
662 exec 'sleep' . duration
663 call assert_equal('aegi', &guioptions)
664
665 set guioptions+=v
666 exec 'sleep' . duration
667 call assert_equal('aegiv', &guioptions)
668 set guioptions-=v
669 exec 'sleep' . duration
670 call assert_equal('aegi', &guioptions)
671
672 if has('gui_motif')
673 set guioptions+=F
674 exec 'sleep' . duration
675 call assert_equal('aegiF', &guioptions)
676 set guioptions-=F
677 exec 'sleep' . duration
678 call assert_equal('aegi', &guioptions)
679 endif
680
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +0200681 if has('gui_gtk3')
682 set guioptions+=d
683 exec 'sleep' . duration
684 call assert_equal('aegid', &guioptions)
685 set guioptions-=d
686 exec 'sleep' . duration
687 call assert_equal('aegi', &guioptions)
688 endif
689
Bram Moolenaar87748452017-03-12 17:10:33 +0100690 " Restore GUI ornaments to the default state.
691 set guioptions+=m
692 exec 'sleep' . duration
693 call assert_equal('aegim', &guioptions)
694 set guioptions+=r
695 exec 'sleep' . duration
696 call assert_equal('aegimr', &guioptions)
697 set guioptions+=L
698 exec 'sleep' . duration
699 call assert_equal('aegimrL', &guioptions)
700 set guioptions+=t
701 exec 'sleep' . duration
702 call assert_equal('aegimrLt', &guioptions)
703 set guioptions+=T
704 exec 'sleep' . duration
705 call assert_equal("aegimrLtT", &guioptions)
706
707 wincmd o
708 redraw!
709 endif
710
711 let &guioptions = guioptions_saved
712endfunc
713
Bram Moolenaarab186732018-09-14 21:27:06 +0200714func Test_scrollbars()
715 new
716 " buffer with 200 lines
717 call setline(1, repeat(['one', 'two'], 100))
718 set guioptions+=rlb
719
720 " scroll to move line 11 at top, moves the cursor there
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +0000721 let args = #{which: 'left', value: 10, dragging: 0}
722 call test_gui_event('scrollbar', args)
Bram Moolenaarab186732018-09-14 21:27:06 +0200723 redraw
724 call assert_equal(1, winline())
725 call assert_equal(11, line('.'))
726
727 " scroll to move line 1 at top, cursor stays in line 11
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +0000728 let args = #{which: 'right', value: 0, dragging: 0}
729 call test_gui_event('scrollbar', args)
Bram Moolenaarab186732018-09-14 21:27:06 +0200730 redraw
731 call assert_equal(11, winline())
732 call assert_equal(11, line('.'))
733
734 set nowrap
735 call setline(11, repeat('x', 150))
736 redraw
737 call assert_equal(1, wincol())
Bram Moolenaarf6d50f12019-06-06 15:40:08 +0200738 set number
739 redraw
740 call assert_equal(5, wincol())
741 set nonumber
742 redraw
Bram Moolenaarab186732018-09-14 21:27:06 +0200743 call assert_equal(1, col('.'))
744
745 " scroll to character 11, cursor is moved
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +0000746 let args = #{which: 'hor', value: 10, dragging: 0}
747 call test_gui_event('scrollbar', args)
Bram Moolenaarab186732018-09-14 21:27:06 +0200748 redraw
749 call assert_equal(1, wincol())
Bram Moolenaarf6d50f12019-06-06 15:40:08 +0200750 set number
751 redraw
752 call assert_equal(5, wincol())
753 set nonumber
754 redraw
Bram Moolenaarab186732018-09-14 21:27:06 +0200755 call assert_equal(11, col('.'))
756
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +0000757 " Invalid arguments
758 call assert_false(test_gui_event('scrollbar', {}))
759 call assert_false(test_gui_event('scrollbar', #{value: 10, dragging: 0}))
760 call assert_false(test_gui_event('scrollbar', #{which: 'hor', dragging: 0}))
761 call assert_false(test_gui_event('scrollbar', #{which: 'hor', value: 1}))
762 call assert_fails("call test_gui_event('scrollbar', #{which: 'a', value: 1, dragging: 0})", 'E475:')
763
Bram Moolenaarab186732018-09-14 21:27:06 +0200764 set guioptions&
765 set wrap&
766 bwipe!
767endfunc
768
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100769func Test_menu()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100770 CheckFeature quickfix
771
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100772 " Check Help menu exists
773 let help_menu = execute('menu Help')
774 call assert_match('Overview', help_menu)
775
776 " Check Help menu works
777 emenu Help.Overview
778 call assert_equal('help', &buftype)
779 close
780
781 " Check deleting menu doesn't cause trouble.
782 aunmenu Help
Bram Moolenaarb45cd362020-09-28 21:41:49 +0200783 if exists(':tlmenu')
784 tlunmenu Help
785 endif
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100786 call assert_fails('menu Help', 'E329:')
787endfunc
788
Bram Moolenaar87748452017-03-12 17:10:33 +0100789func Test_set_guipty()
790 let guipty_saved = &guipty
791
792 " Default Value
793 set guipty&
794 call assert_equal(1, &guipty)
795
796 set noguipty
797 call assert_equal(0, &guipty)
798
799 let &guipty = guipty_saved
Bram Moolenaar6f785742017-02-06 22:11:55 +0100800endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100801
Bram Moolenaarc701f322019-03-28 21:49:21 +0100802func Test_encoding_conversion()
803 " GTK supports conversion between 'encoding' and "utf-8"
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200804 CheckFeature gui_gtk
805 let encoding_saved = &encoding
806 set encoding=latin1
Bram Moolenaarc701f322019-03-28 21:49:21 +0100807
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200808 " would be nice if we could take a screenshot
809 intro
810 " sets the window title
811 edit SomeFile
Bram Moolenaarc701f322019-03-28 21:49:21 +0100812
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200813 let &encoding = encoding_saved
Bram Moolenaarc701f322019-03-28 21:49:21 +0100814endfunc
815
Bram Moolenaar877e9572016-08-04 20:05:50 +0200816func Test_shell_command()
817 new
Bram Moolenaar9d5b8762016-08-04 21:21:13 +0200818 r !echo hello
819 call assert_equal('hello', substitute(getline(2), '\W', '', 'g'))
Bram Moolenaar877e9572016-08-04 20:05:50 +0200820 bwipe!
Bram Moolenaar877e9572016-08-04 20:05:50 +0200821endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100822
Bram Moolenaar87748452017-03-12 17:10:33 +0100823func Test_syntax_colortest()
824 runtime syntax/colortest.vim
825 redraw!
826 sleep 200m
827 bwipe!
828endfunc
829
830func Test_set_term()
831 " It's enough to check the current value since setting 'term' to anything
832 " other than builtin_gui makes no sense at all.
833 call assert_equal('builtin_gui', &term)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200834 call assert_fails('set term=xterm', 'E530:')
Bram Moolenaar87748452017-03-12 17:10:33 +0100835endfunc
836
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100837func Test_windowid_variable()
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100838 if g:x11_based_gui || has('win32')
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100839 call assert_true(v:windowid > 0)
840 else
841 call assert_equal(0, v:windowid)
842 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +0100843endfunc
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200844
845" Test "vim -g" and also the GUIEnter autocommand.
846func Test_gui_dash_g()
847 let cmd = GetVimCommand('Xscriptgui')
848 call writefile([""], "Xtestgui")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200849 let lines =<< trim END
850 au GUIEnter * call writefile(["insertmode: " . &insertmode], "Xtestgui")
851 au GUIEnter * qall
852 END
853 call writefile(lines, 'Xscriptgui')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200854 call system(cmd . ' -g')
855 call WaitForAssert({-> assert_equal(['insertmode: 0'], readfile('Xtestgui'))})
856
857 call delete('Xscriptgui')
858 call delete('Xtestgui')
859endfunc
860
861" Test "vim -7" and also the GUIEnter autocommand.
862func Test_gui_dash_y()
863 let cmd = GetVimCommand('Xscriptgui')
864 call writefile([""], "Xtestgui")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200865 let lines =<< trim END
866 au GUIEnter * call writefile(["insertmode: " . &insertmode], "Xtestgui")
867 au GUIEnter * qall
868 END
869 call writefile(lines, 'Xscriptgui')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200870 call system(cmd . ' -y')
871 call WaitForAssert({-> assert_equal(['insertmode: 1'], readfile('Xtestgui'))})
872
873 call delete('Xscriptgui')
874 call delete('Xtestgui')
875endfunc
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100876
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200877" Test for "!" option in 'guioptions'. Use a terminal for running external
878" commands
879func Test_gui_run_cmd_in_terminal()
Bram Moolenaar4457e1d2020-05-15 22:46:44 +0200880 CheckFeature terminal
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200881 let save_guioptions = &guioptions
882 set guioptions+=!
883 if has('win32')
884 let cmd = 'type'
885 else
886 " assume all the other systems have a cat command
887 let cmd = 'cat'
888 endif
Bram Moolenaar98f16712020-05-22 13:34:01 +0200889 exe "silent !" . cmd . " test_gui.vim"
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200890 " TODO: how to check that the command ran in a separate terminal?
891 " Maybe check for $TERM (dumb vs xterm) in the spawned shell?
892 let &guioptions = save_guioptions
893endfunc
894
Bram Moolenaar46cd43b2020-06-04 22:22:11 +0200895func Test_gui_recursive_mapping()
896 nmap ' <C-W>
897 nmap <C-W>a :let didit = 1<CR>
898 call feedkeys("'a", 'xt')
899 call assert_equal(1, didit)
900
901 nunmap '
902 nunmap <C-W>a
903endfunc
904
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200905" Test GUI mouse events
906func Test_gui_mouse_event()
907 set mousemodel=extend
908 call test_override('no_query_mouse', 1)
909 new
910 call setline(1, ['one two three', 'four five six'])
911
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200912 " place the cursor using left click in normal mode
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200913 call cursor(1, 1)
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000914 let args = #{button: 0, row: 2, col: 4, multiclick: 0, modifiers: 0}
915 call test_gui_event('mouse', args)
916 let args.button = 3
917 eval 'mouse'->test_gui_event(args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200918 call feedkeys("\<Esc>", 'Lx!')
919 call assert_equal([0, 2, 4, 0], getpos('.'))
920
921 " select and yank a word
922 let @" = ''
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000923 let args = #{button: 0, row: 1, col: 9, multiclick: 0, modifiers: 0}
924 call test_gui_event('mouse', args)
925 let args.multiclick = 1
926 call test_gui_event('mouse', args)
927 let args.button = 3
928 let args.multiclick = 0
929 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200930 call feedkeys("y", 'Lx!')
931 call assert_equal('three', @")
932
933 " create visual selection using right click
934 let @" = ''
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000935 let args = #{button: 0, row: 2, col: 6, multiclick: 0, modifiers: 0}
936 call test_gui_event('mouse', args)
937 let args.button = 3
938 call test_gui_event('mouse', args)
939 let args = #{button: 2, row: 2, col: 13, multiclick: 0, modifiers: 0}
940 call test_gui_event('mouse', args)
941 let args.button = 3
942 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200943 call feedkeys("y", 'Lx!')
944 call assert_equal('five six', @")
945
946 " paste using middle mouse button
947 let @* = 'abc '
948 call feedkeys('""', 'Lx!')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000949 let args = #{button: 1, row: 1, col: 9, multiclick: 0, modifiers: 0}
950 call test_gui_event('mouse', args)
951 let args.button = 3
952 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200953 call feedkeys("\<Esc>", 'Lx!')
954 call assert_equal(['one two abc three', 'four five six'], getline(1, '$'))
955
956 " extend visual selection using right click in visual mode
957 let @" = ''
958 call cursor(1, 1)
959 call feedkeys('v', 'Lx!')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000960 let args = #{button: 2, row: 1, col: 17, multiclick: 0, modifiers: 0}
961 call test_gui_event('mouse', args)
962 let args.button = 3
963 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200964 call feedkeys("y", 'Lx!')
965 call assert_equal('one two abc three', @")
966
967 " extend visual selection using mouse drag
968 let @" = ''
969 call cursor(1, 1)
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000970 let args = #{button: 0, row: 2, col: 1, multiclick: 0, modifiers: 0}
971 call test_gui_event('mouse', args)
972 let args = #{button: 0x43, row: 2, col: 9, multiclick: 0, modifiers: 0}
973 call test_gui_event('mouse', args)
974 let args.button = 0x3
975 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200976 call feedkeys("y", 'Lx!')
977 call assert_equal('four five', @")
978
979 " select text by moving the mouse
980 let @" = ''
981 call cursor(1, 1)
982 redraw!
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000983 let args = #{button: 0, row: 1, col: 4, multiclick: 0, modifiers: 0}
984 call test_gui_event('mouse', args)
985 let args.button = 0x700
986 let args.col = 9
987 call test_gui_event('mouse', args)
988 let args.col = 13
989 call test_gui_event('mouse', args)
990 let args.button = 3
991 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200992 call feedkeys("y", 'Lx!')
993 call assert_equal(' two abc t', @")
994
995 " Using mouse in insert mode
996 call cursor(1, 1)
997 call feedkeys('i', 't')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000998 let args = #{button: 0, row: 2, col: 11, multiclick: 0, modifiers: 0}
999 call test_gui_event('mouse', args)
1000 let args.button = 3
1001 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001002 call feedkeys("po\<Esc>", 'Lx!')
1003 call assert_equal(['one two abc three', 'four five posix'], getline(1, '$'))
1004
1005 %d _
LemonBoyc27747e2022-05-07 12:25:40 +01001006 set scrolloff=0
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001007 call setline(1, range(1, 100))
1008 " scroll up
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001009 let args = #{button: 0x200, row: 2, col: 1, multiclick: 0, modifiers: 0}
1010 call test_gui_event('mouse', args)
1011 call test_gui_event('mouse', args)
1012 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001013 call feedkeys("H", 'Lx!')
1014 call assert_equal(10, line('.'))
1015
1016 " scroll down
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001017 let args = #{button: 0x100, row: 2, col: 1, multiclick: 0, modifiers: 0}
1018 call test_gui_event('mouse', args)
1019 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001020 call feedkeys("H", 'Lx!')
1021 call assert_equal(4, line('.'))
LemonBoyc27747e2022-05-07 12:25:40 +01001022 set scrolloff&
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001023
1024 %d _
1025 set nowrap
1026 call setline(1, range(10)->join('')->repeat(10))
1027 " scroll left
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001028 let args = #{button: 0x500, row: 1, col: 5, multiclick: 0, modifiers: 0}
1029 call test_gui_event('mouse', args)
1030 let args.col = 10
1031 call test_gui_event('mouse', args)
1032 let args.col = 15
1033 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001034 call feedkeys('g0', 'Lx!')
1035 call assert_equal(19, col('.'))
1036
1037 " scroll right
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001038 let args = #{button: 0x600, row: 1, col: 15, multiclick: 0, modifiers: 0}
1039 call test_gui_event('mouse', args)
1040 let args.col = 10
1041 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001042 call feedkeys('g0', 'Lx!')
1043 call assert_equal(7, col('.'))
1044 set wrap&
1045
1046 %d _
1047 call setline(1, repeat([repeat('a', 60)], 10))
1048
1049 " record various mouse events
1050 let mouseEventNames = [
1051 \ 'LeftMouse', 'LeftRelease', '2-LeftMouse', '3-LeftMouse',
1052 \ 'S-LeftMouse', 'A-LeftMouse', 'C-LeftMouse', 'MiddleMouse',
1053 \ 'MiddleRelease', '2-MiddleMouse', '3-MiddleMouse',
1054 \ 'S-MiddleMouse', 'A-MiddleMouse', 'C-MiddleMouse',
1055 \ 'RightMouse', 'RightRelease', '2-RightMouse',
1056 \ '3-RightMouse', 'S-RightMouse', 'A-RightMouse', 'C-RightMouse',
1057 \ 'X1Mouse', 'S-X1Mouse', 'A-X1Mouse', 'C-X1Mouse', 'X2Mouse',
1058 \ 'S-X2Mouse', 'A-X2Mouse', 'C-X2Mouse'
1059 \ ]
1060 let mouseEventCodes = map(copy(mouseEventNames), "'<' .. v:val .. '>'")
1061 let g:events = []
1062 for e in mouseEventCodes
1063 exe 'nnoremap ' .. e .. ' <Cmd>call add(g:events, "' ..
1064 \ substitute(e, '[<>]', '', 'g') .. '")<CR>'
1065 endfor
1066
1067 " Test various mouse buttons (0 - Left, 1 - Middle, 2 - Right, 0x300 - X1,
1068 " 0x300- X2)
1069 for button in [0, 1, 2, 0x300, 0x400]
1070 " Single click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001071 let args = #{button: button, row: 2, col: 5, multiclick: 0, modifiers: 0}
1072 call test_gui_event('mouse', args)
1073 let args.button = 3
1074 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001075
1076 " Double/Triple click is supported by only the Left/Middle/Right mouse
1077 " buttons
1078 if button <= 2
1079 " Double Click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001080 let args.button = button
1081 call test_gui_event('mouse', args)
1082 let args.multiclick = 1
1083 call test_gui_event('mouse', args)
1084 let args.button = 3
1085 let args.multiclick = 0
1086 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001087
1088 " Triple Click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001089 let args.button = button
1090 call test_gui_event('mouse', args)
1091 let args.multiclick = 1
1092 call test_gui_event('mouse', args)
1093 call test_gui_event('mouse', args)
1094 let args.button = 3
1095 let args.multiclick = 0
1096 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001097 endif
1098
1099 " Shift click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001100 let args = #{button: button, row: 3, col: 7, multiclick: 0, modifiers: 4}
1101 call test_gui_event('mouse', args)
1102 let args.button = 3
1103 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001104
1105 " Alt click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001106 let args.button = button
1107 let args.modifiers = 8
1108 call test_gui_event('mouse', args)
1109 let args.button = 3
1110 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001111
1112 " Ctrl click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001113 let args.button = button
1114 let args.modifiers = 16
1115 call test_gui_event('mouse', args)
1116 let args.button = 3
1117 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001118
1119 call feedkeys("\<Esc>", 'Lx!')
1120 endfor
1121
1122 call assert_equal(['LeftMouse', 'LeftRelease', 'LeftMouse', '2-LeftMouse',
1123 \ 'LeftMouse', '2-LeftMouse', '3-LeftMouse', 'S-LeftMouse',
1124 \ 'A-LeftMouse', 'C-LeftMouse', 'MiddleMouse', 'MiddleRelease',
1125 \ 'MiddleMouse', '2-MiddleMouse', 'MiddleMouse', '2-MiddleMouse',
1126 \ '3-MiddleMouse', 'S-MiddleMouse', 'A-MiddleMouse', 'C-MiddleMouse',
1127 \ 'RightMouse', 'RightRelease', 'RightMouse', '2-RightMouse',
1128 \ 'RightMouse', '2-RightMouse', '3-RightMouse', 'S-RightMouse',
1129 \ 'A-RightMouse', 'C-RightMouse', 'X1Mouse', 'S-X1Mouse', 'A-X1Mouse',
1130 \ 'C-X1Mouse', 'X2Mouse', 'S-X2Mouse', 'A-X2Mouse', 'C-X2Mouse'],
1131 \ g:events)
1132
1133 for e in mouseEventCodes
1134 exe 'nunmap ' .. e
1135 endfor
1136
1137 " modeless selection
1138 set mouse=
1139 let save_guioptions = &guioptions
1140 set guioptions+=A
1141 %d _
1142 call setline(1, ['one two three', 'four five sixteen'])
1143 call cursor(1, 1)
1144 redraw!
1145 " Double click should select the word and copy it to clipboard
1146 let @* = ''
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001147 let args = #{button: 0, row: 2, col: 11, multiclick: 0, modifiers: 0}
1148 call test_gui_event('mouse', args)
1149 let args.multiclick = 1
1150 call test_gui_event('mouse', args)
1151 let args.button = 3
1152 let args.multiclick = 0
1153 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001154 call feedkeys("\<Esc>", 'Lx!')
1155 call assert_equal([0, 1, 1, 0], getpos('.'))
1156 call assert_equal('sixteen', @*)
1157 " Right click should extend the selection from cursor
1158 call cursor(1, 6)
1159 redraw!
1160 let @* = ''
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001161 let args = #{button: 2, row: 1, col: 11, multiclick: 0, modifiers: 0}
1162 call test_gui_event('mouse', args)
1163 let args.button = 3
1164 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001165 call feedkeys("\<Esc>", 'Lx!')
1166 call assert_equal([0, 1, 6, 0], getpos('.'))
1167 call assert_equal('wo thr', @*)
1168 " Middle click should paste the clipboard contents
1169 call cursor(2, 1)
1170 redraw!
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001171 let args = #{button: 1, row: 1, col: 11, multiclick: 0, modifiers: 0}
1172 call test_gui_event('mouse', args)
1173 let args.button = 3
1174 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001175 call feedkeys("\<Esc>", 'Lx!')
1176 call assert_equal([0, 2, 7, 0], getpos('.'))
1177 call assert_equal('wo thrfour five sixteen', getline(2))
1178 set mouse&
1179 let &guioptions = save_guioptions
1180
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001181 " Test invalid parameters for test_gui_event()
1182 let args = #{row: 2, col: 4, multiclick: 0, modifiers: 0}
1183 call assert_false(test_gui_event('mouse', args))
1184 let args = #{button: 0, col: 4, multiclick: 0, modifiers: 0}
1185 call assert_false(test_gui_event('mouse', args))
1186 let args = #{button: 0, row: 2, multiclick: 0, modifiers: 0}
1187 call assert_false(test_gui_event('mouse', args))
1188 let args = #{button: 0, row: 2, col: 4, modifiers: 0}
1189 call assert_false(test_gui_event('mouse', args))
1190 let args = #{button: 0, row: 2, col: 4, multiclick: 0}
1191 call assert_false(test_gui_event('mouse', args))
1192
1193 " Error cases for test_gui_event()
1194 call assert_fails("call test_gui_event('a1b2c3', args)", 'E475:')
1195 call assert_fails("call test_gui_event([], args)", 'E1174:')
1196 call assert_fails("call test_gui_event('abc', [])", 'E1206:')
1197 call assert_fails("call test_gui_event(test_null_string(), {})", 'E475:')
1198 call assert_false(test_gui_event('mouse', test_null_dict()))
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001199
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001200 bw!
1201 call test_override('no_query_mouse', 0)
1202 set mousemodel&
1203endfunc
1204
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001205" Move the mouse to the top-left in preparation for mouse events
1206func PrepareForMouseEvent(args)
1207 call extend(a:args, #{row: 1, col:1})
1208 call test_gui_event('mouse', a:args)
1209 call feedkeys('', 'Lx!')
1210 " on MS-Windows the event may have a slight delay
1211 if has('win32')
1212 sleep 20m
1213 endif
1214endfunc
1215
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001216func MouseWasMoved()
1217 let pos = getmousepos()
1218 call add(g:eventlist, #{row: pos.screenrow, col: pos.screencol})
1219endfunc
1220
Ernie Raelc4cb5442022-04-03 15:47:28 +01001221func Test_gui_mouse_move_event()
1222 let args = #{move: 1, button: 0, multiclick: 0, modifiers: 0}
1223
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001224 " by default, does not generate mouse move events
Ernie Raelc4cb5442022-04-03 15:47:28 +01001225 set mousemev&
1226 call assert_false(&mousemev)
1227
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001228 let g:eventlist = []
1229 nnoremap <special> <silent> <MouseMove> :call MouseWasMoved()<CR>
Ernie Raelc4cb5442022-04-03 15:47:28 +01001230
1231 " start at mouse pos (1,1), clear counter
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001232 call PrepareForMouseEvent(args)
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001233 let g:eventlist = []
Ernie Raelc4cb5442022-04-03 15:47:28 +01001234
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001235 call extend(args, #{row: 3, col: 30, cell: v:true})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001236 call test_gui_event('mouse', args)
1237 call feedkeys('', 'Lx!')
1238
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001239 call extend(args, #{row: 10, col: 30, cell: v:true})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001240 call test_gui_event('mouse', args)
1241 call feedkeys('', 'Lx!')
1242
1243 " no events since mousemev off
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001244 call assert_equal([], g:eventlist)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001245
1246 " turn on mouse events and try the same thing
1247 set mousemev
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001248 call PrepareForMouseEvent(args)
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001249 let g:eventlist = []
Ernie Raelc4cb5442022-04-03 15:47:28 +01001250
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001251 call extend(args, #{row: 3, col: 30, cell: v:true})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001252 call test_gui_event('mouse', args)
1253 call feedkeys('', 'Lx!')
1254
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001255 call extend(args, #{row: 10, col: 30, cell: v:true})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001256 call test_gui_event('mouse', args)
1257 call feedkeys('', 'Lx!')
1258
Bram Moolenaar3c25a862022-05-16 17:07:41 +01001259 " FIXME: on MS-Windows we get a stray event first
1260 if has('win32') && len(g:eventlist) == 3
Bram Moolenaare5162e72022-05-16 16:41:35 +01001261 let g:eventlist = g:eventlist[1 : ]
1262 endif
1263
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001264 call assert_equal([#{row: 4, col: 31}, #{row: 11, col: 31}], g:eventlist)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001265
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001266 " wiggle the mouse around within a screen cell, shouldn't trigger events
1267 call extend(args, #{cell: v:false})
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001268 call PrepareForMouseEvent(args)
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001269 let g:eventlist = []
Ernie Raelc4cb5442022-04-03 15:47:28 +01001270
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001271 call extend(args, #{row: 1, col: 2, cell: v:false})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001272 call test_gui_event('mouse', args)
1273 call feedkeys('', 'Lx!')
1274
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001275 call extend(args, #{row: 2, col: 2, cell: v:false})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001276 call test_gui_event('mouse', args)
1277 call feedkeys('', 'Lx!')
1278
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001279 call extend(args, #{row: 2, col: 1, cell: v:false})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001280 call test_gui_event('mouse', args)
1281 call feedkeys('', 'Lx!')
1282
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001283 call assert_equal([], g:eventlist)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001284
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001285 unlet g:eventlist
Ernie Raelc4cb5442022-04-03 15:47:28 +01001286 unmap <MouseMove>
1287 set mousemev&
1288endfunc
1289
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001290" Test for 'guitablabel' and 'guitabtooltip' options
1291func TestGuiTabLabel()
1292 call add(g:TabLabels, v:lnum + 100)
1293 let bufnrlist = tabpagebuflist(v:lnum)
1294 return bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
1295endfunc
1296
1297func TestGuiTabToolTip()
1298 call add(g:TabToolTips, v:lnum + 200)
1299 let bufnrlist = tabpagebuflist(v:lnum)
1300 return bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
1301endfunc
1302
1303func Test_gui_tablabel_tooltip()
1304 %bw!
1305 " Removing the tabline at the end of this test, reduces the window height by
1306 " one. Save and restore it after the test.
1307 let save_lines = &lines
1308 edit one
1309 set modified
1310 tabnew two
1311 set modified
1312 tabnew three
1313 set modified
1314 let g:TabLabels = []
1315 set guitablabel=%{TestGuiTabLabel()}
1316 call test_override('starting', 1)
1317 redrawtabline
1318 call test_override('starting', 0)
1319 call assert_true(index(g:TabLabels, 101) != -1)
1320 call assert_true(index(g:TabLabels, 102) != -1)
1321 call assert_true(index(g:TabLabels, 103) != -1)
1322 set guitablabel&
1323 unlet g:TabLabels
1324
1325 if has('gui_gtk')
1326 " Only on GTK+, the tooltip function is called even if the mouse is not
1327 " on the tabline. on Win32 and Motif, the tooltip function is called only
1328 " when the mouse pointer is over the tabline.
1329 let g:TabToolTips = []
1330 set guitabtooltip=%{TestGuiTabToolTip()}
1331 call test_override('starting', 1)
1332 redrawtabline
1333 call test_override('starting', 0)
1334 call assert_true(index(g:TabToolTips, 201) != -1)
1335 call assert_true(index(g:TabToolTips, 202) != -1)
1336 call assert_true(index(g:TabToolTips, 203) != -1)
1337 set guitabtooltip&
1338 unlet g:TabToolTips
1339 endif
1340 %bw!
1341 let &lines = save_lines
1342endfunc
1343
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001344" Test for dropping files into a window in GUI
1345func DropFilesInCmdLine()
Bram Moolenaar1d1ce612021-06-27 19:02:52 +02001346 CheckFeature drop_file
1347
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001348 call feedkeys(":\"", 'L')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001349 let d = #{files: ['a.c', 'b.c'], row: &lines, col: 1, modifiers: 0}
1350 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001351 call feedkeys("\<CR>", 'L')
1352endfunc
1353
1354func Test_gui_drop_files()
Bram Moolenaar1d1ce612021-06-27 19:02:52 +02001355 CheckFeature drop_file
1356
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001357 %bw!
1358 %argdelete
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001359 let d = #{files: [], row: 1, col: 1, modifiers: 0}
1360 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001361 call assert_equal([], argv())
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001362 let d = #{files: [1, 2], row: 1, col: 1, modifiers: 0}
1363 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001364 call assert_equal([], argv())
1365
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001366 let d = #{files: ['a.c', 'b.c'], row: 1, col: 1, modifiers: 0}
1367 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001368 call assert_equal(['a.c', 'b.c'], argv())
1369 %bw!
1370 %argdelete
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001371 let d = #{files: [], row: 1, col: 1, modifiers: 0}
1372 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001373 call assert_equal([], argv())
1374 %bw!
1375 " if the buffer in the window is modified, then the file should be opened in
1376 " a new window
1377 set modified
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001378 let d = #{files: ['x.c', 'y.c'], row: 1, col: 1, modifiers: 0}
1379 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001380 call assert_equal(['x.c', 'y.c'], argv())
1381 call assert_equal(2, winnr('$'))
1382 call assert_equal('x.c', bufname(winbufnr(1)))
1383 %bw!
1384 %argdelete
1385 " if Ctrl is pressed, then the file should be opened in a new window
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001386 let d = #{files: ['s.py', 't.py'], row: 1, col: 1, modifiers: 0x10}
1387 eval 'dropfiles'->test_gui_event(d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001388 call assert_equal(['s.py', 't.py'], argv())
1389 call assert_equal(2, winnr('$'))
1390 call assert_equal('s.py', bufname(winbufnr(1)))
1391 %bw!
1392 %argdelete
1393 " drop the files in a non-current window
1394 belowright new
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001395 let d = #{files: ['a.py', 'b.py'], row: 1, col: 1, modifiers: 0}
1396 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001397 call assert_equal(['a.py', 'b.py'], argv())
1398 call assert_equal(2, winnr('$'))
1399 call assert_equal(1, winnr())
1400 call assert_equal('a.py', bufname(winbufnr(1)))
1401 %bw!
1402 %argdelete
1403 " pressing shift when dropping files should change directory
1404 let save_cwd = getcwd()
1405 call mkdir('Xdir1')
1406 call writefile([], 'Xdir1/Xfile1')
1407 call writefile([], 'Xdir1/Xfile2')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001408 let d = #{files: ['Xdir1/Xfile1', 'Xdir1/Xfile2'], row: 1, col: 1,
1409 \ modifiers: 0x4}
1410 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001411 call assert_equal('Xdir1', fnamemodify(getcwd(), ':t'))
1412 call assert_equal('Xfile1', @%)
1413 call chdir(save_cwd)
1414 " pressing shift when dropping directory and files should change directory
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001415 let d = #{files: ['Xdir1', 'Xdir1/Xfile2'], row: 1, col: 1, modifiers: 0x4}
1416 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001417 call assert_equal('Xdir1', fnamemodify(getcwd(), ':t'))
1418 call assert_equal('Xdir1', fnamemodify(@%, ':t'))
1419 call chdir(save_cwd)
1420 %bw!
1421 %argdelete
1422 " dropping a directory should edit it
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001423 let d = #{files: ['Xdir1'], row: 1, col: 1, modifiers: 0}
1424 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001425 call assert_equal('Xdir1', @%)
1426 %bw!
1427 %argdelete
1428 " dropping only a directory name with Shift should ignore it
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001429 let d = #{files: ['Xdir1'], row: 1, col: 1, modifiers: 0x4}
1430 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001431 call assert_equal('', @%)
1432 %bw!
1433 %argdelete
1434 call delete('Xdir1', 'rf')
1435 " drop files in the command line. The GUI drop files adds the file names to
1436 " the low level input buffer. So need to use a cmdline map and feedkeys()
1437 " with 'Lx!' to process it in this function itself.
1438 cnoremap <expr> <buffer> <F4> DropFilesInCmdLine()
1439 call feedkeys(":\"\<F4>\<CR>", 'xt')
1440 call feedkeys('k', 'Lx!')
1441 call assert_equal('"a.c b.c', @:)
1442 cunmap <buffer> <F4>
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001443
1444 " Invalid arguments
1445 call assert_false(test_gui_event("dropfiles", {}))
1446 let d = #{row: 1, col: 1, modifiers: 0}
1447 call assert_false(test_gui_event("dropfiles", d))
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +00001448 let d = #{files: 1, row: 1, col: 1, modifiers: 0}
1449 call assert_false(test_gui_event("dropfiles", d))
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001450 let d = #{files: test_null_list(), row: 1, col: 1, modifiers: 0}
1451 call assert_false(test_gui_event("dropfiles", d))
1452 let d = #{files: [test_null_string()], row: 1, col: 1, modifiers: 0}
1453 call assert_true(test_gui_event("dropfiles", d))
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001454endfunc
1455
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001456" Test for generating a GUI tabline event to select a tab page
1457func Test_gui_tabline_event()
1458 %bw!
1459 edit Xfile1
1460 tabedit Xfile2
1461 tabedit Xfile3
1462
1463 tabfirst
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001464 call assert_equal(v:true, test_gui_event('tabline', #{tabnr: 2}))
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001465 call feedkeys("y", "Lx!")
1466 call assert_equal(2, tabpagenr())
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001467 call assert_equal(v:true, test_gui_event('tabline', #{tabnr: 3}))
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001468 call feedkeys("y", "Lx!")
1469 call assert_equal(3, tabpagenr())
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001470 call assert_equal(v:false, 'tabline'->test_gui_event(#{tabnr: 3}))
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001471
1472 " From the cmdline window, tabline event should not be handled
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001473 call feedkeys("q::let t = test_gui_event('tabline', #{tabnr: 2})\<CR>:q\<CR>", 'x!')
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001474 call assert_equal(v:false, t)
1475
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001476 " Invalid arguments
1477 call assert_false(test_gui_event('tabline', {}))
1478 call assert_false(test_gui_event('tabline', #{abc: 1}))
1479
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001480 %bw!
1481endfunc
1482
1483" Test for generating a GUI tabline menu event to execute an action
1484func Test_gui_tabmenu_event()
1485 %bw!
1486
1487 " Try to close the last tab page
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001488 call test_gui_event('tabmenu', #{tabnr: 1, item: 1})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001489 call feedkeys("y", "Lx!")
1490
1491 edit Xfile1
1492 tabedit Xfile2
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001493 call test_gui_event('tabmenu', #{tabnr: 1, item: 1})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001494 call feedkeys("y", "Lx!")
1495 call assert_equal(1, tabpagenr('$'))
1496 call assert_equal('Xfile2', bufname())
1497
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001498 eval 'tabmenu'->test_gui_event(#{tabnr: 1, item: 2})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001499 call feedkeys("y", "Lx!")
1500 call assert_equal(2, tabpagenr('$'))
1501
1502 " If tabnr is 0, then the current tabpage should be used.
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001503 call test_gui_event('tabmenu', #{tabnr: 0, item: 2})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001504 call feedkeys("y", "Lx!")
1505 call assert_equal(3, tabpagenr('$'))
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001506 call test_gui_event('tabmenu', #{tabnr: 0, item: 1})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001507 call feedkeys("y", "Lx!")
1508 call assert_equal(2, tabpagenr('$'))
1509
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001510 " Invalid arguments
1511 call assert_false(test_gui_event('tabmenu', {}))
1512 call assert_false(test_gui_event('tabmenu', #{tabnr: 1}))
1513 call assert_false(test_gui_event('tabmenu', #{item: 1}))
1514 call assert_false(test_gui_event('tabmenu', #{abc: 1}))
1515
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001516 %bw!
1517endfunc
1518
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001519" Test for find/replace text dialog event
1520func Test_gui_findrepl()
Yegappan Lakshmanan08238042022-03-07 16:57:22 +00001521 " Find/Replace dialog is supported only on GTK, Motif and MS-Windows.
1522 if !has('gui_gtk') && !has('gui_motif') && !has('gui_win32')
1523 return
1524 endif
1525
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001526 new
1527 call setline(1, ['one two one', 'Twoo One two oneo'])
1528
1529 " Replace all instances of a string with another
1530 let args = #{find_text: 'one', repl_text: 'ONE', flags: 0x4, forward: 1}
1531 call test_gui_event('findrepl', args)
1532 call assert_equal(['ONE two ONE', 'Twoo ONE two ONEo'], getline(1, '$'))
1533
1534 " Replace all instances of a whole string with another
1535 call cursor(1, 1)
1536 let args = #{find_text: 'two', repl_text: 'TWO', flags: 0xC, forward: 1}
1537 call test_gui_event('findrepl', args)
1538 call assert_equal(['ONE TWO ONE', 'Twoo ONE TWO ONEo'], getline(1, '$'))
1539
Dominique Pelle81b573d2022-03-22 21:14:55 +00001540 " Find next occurrence of a string (in a find dialog)
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001541 call cursor(1, 11)
1542 let args = #{find_text: 'TWO', repl_text: '', flags: 0x11, forward: 1}
1543 call test_gui_event('findrepl', args)
1544 call assert_equal([2, 10], [line('.'), col('.')])
1545
Dominique Pelle81b573d2022-03-22 21:14:55 +00001546 " Find previous occurrences of a string (in a find dialog)
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001547 call cursor(1, 11)
1548 let args = #{find_text: 'TWO', repl_text: '', flags: 0x11, forward: 0}
1549 call test_gui_event('findrepl', args)
1550 call assert_equal([1, 5], [line('.'), col('.')])
1551
Dominique Pelle81b573d2022-03-22 21:14:55 +00001552 " Find next occurrence of a string (in a replace dialog)
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001553 call cursor(1, 1)
1554 let args = #{find_text: 'Twoo', repl_text: '', flags: 0x2, forward: 1}
1555 call test_gui_event('findrepl', args)
1556 call assert_equal([2, 1], [line('.'), col('.')])
1557
Dominique Pelle81b573d2022-03-22 21:14:55 +00001558 " Replace only the next occurrence of a string (once)
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001559 call cursor(1, 5)
1560 let args = #{find_text: 'TWO', repl_text: 'two', flags: 0x3, forward: 1}
1561 call test_gui_event('findrepl', args)
1562 call assert_equal(['ONE two ONE', 'Twoo ONE TWO ONEo'], getline(1, '$'))
1563
1564 " Replace all instances of a whole string with another matching case
1565 call cursor(1, 1)
1566 let args = #{find_text: 'TWO', repl_text: 'two', flags: 0x1C, forward: 1}
1567 call test_gui_event('findrepl', args)
1568 call assert_equal(['ONE two ONE', 'Twoo ONE two ONEo'], getline(1, '$'))
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +00001569
1570 " Invalid arguments
1571 call assert_false(test_gui_event('findrepl', {}))
1572 let args = #{repl_text: 'a', flags: 1, forward: 1}
1573 call assert_false(test_gui_event('findrepl', args))
1574 let args = #{find_text: 'a', flags: 1, forward: 1}
1575 call assert_false(test_gui_event('findrepl', args))
1576 let args = #{find_text: 'a', repl_text: 'b', forward: 1}
1577 call assert_false(test_gui_event('findrepl', args))
1578 let args = #{find_text: 'a', repl_text: 'b', flags: 1}
1579 call assert_false(test_gui_event('findrepl', args))
1580
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001581 bw!
1582endfunc
1583
zeertzjqbad8a012022-04-29 16:44:00 +01001584func Test_gui_CTRL_SHIFT_V()
1585 call feedkeys(":let g:str = '\<*C-S-V>\<*C-S-I>\<*C-S-V>\<*C-S-@>'\<CR>", 'tx')
zeertzjq758a8d12022-04-29 11:06:34 +01001586 call assert_equal('<C-S-I><C-S-@>', g:str)
1587 unlet g:str
1588endfunc
1589
Bram Moolenaarde8be2b2022-06-16 14:45:41 +01001590func Test_gui_dialog_file()
1591 let lines =<< trim END
1592 file Xfile
1593 normal axxx
1594 confirm qa
1595 END
1596 call writefile(lines, 'Xlines')
1597 execute '!' .. GetVimCommand() .. ' -g -f --clean --gui-dialog-file Xdialog -S Xlines'
1598
1599 call WaitForAssert({-> assert_true(filereadable('Xdialog'))})
1600 call assert_match('Question: Save changes to "Xfile"?', readfile('Xdialog')->join('<NL>'))
1601
1602 call delete('Xdialog')
1603 call delete('Xfile')
1604 call delete('Xlines')
1605endfunc
1606
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001607" vim: shiftwidth=2 sts=2 expandtab