blob: ae65310627014155e771ea1a3446abbaf5a129a1 [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!
Bram Moolenaar5d09a402022-08-31 21:17:10 +010039 au ColorScheme * let g:color_count += 1
40 \ | let g:after_colors = g:color_count
41 \ | let g:color_after = expand('<amatch>')
42 au ColorSchemePre * let g:color_count += 1
43 \ | let g:before_colors = g:color_count
44 \ | let g:color_pre = expand('<amatch>')
Bram Moolenaar60a68362018-04-30 15:40:48 +020045 augroup END
Bram Moolenaar87748452017-03-12 17:10:33 +010046
47 colorscheme torte
48 redraw!
Bram Moolenaar87748452017-03-12 17:10:33 +010049 call assert_equal('dark', &background)
Bram Moolenaar60a68362018-04-30 15:40:48 +020050 call assert_equal(1, g:before_colors)
51 call assert_equal(2, g:after_colors)
Bram Moolenaar5d09a402022-08-31 21:17:10 +010052 call assert_equal('torte', g:color_pre)
53 call assert_equal('torte', g:color_after)
Bram Moolenaar6d4470b2019-01-08 21:05:51 +010054 call assert_equal("\ntorte", execute('colorscheme'))
55
56 let a = substitute(execute('hi Search'), "\n\\s\\+", ' ', 'g')
Bram Moolenaar98feace2022-05-14 12:34:43 +010057 " FIXME: temporarily check less while the colorscheme changes
58 " call assert_match("\nSearch xxx term=reverse cterm=reverse ctermfg=196 ctermbg=16 gui=reverse guifg=#ff0000 guibg=#000000", a)
59 call assert_match("\nSearch xxx term=reverse ", a)
Bram Moolenaar6d4470b2019-01-08 21:05:51 +010060
61 call assert_fails('colorscheme does_not_exist', 'E185:')
Bram Moolenaar5d09a402022-08-31 21:17:10 +010062 call assert_equal('does_not_exist', g:color_pre)
63 call assert_equal('torte', g:color_after)
Bram Moolenaar87748452017-03-12 17:10:33 +010064
65 exec 'colorscheme' colorscheme_saved
Bram Moolenaar60a68362018-04-30 15:40:48 +020066 augroup TestColors
67 au!
68 augroup END
69 unlet g:color_count g:after_colors g:before_colors
Bram Moolenaar87748452017-03-12 17:10:33 +010070 redraw!
71endfunc
72
Bram Moolenaar6f785742017-02-06 22:11:55 +010073func Test_getfontname_with_arg()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +010074 CheckX11BasedGui
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010075
Bram Moolenaar0b962e52022-04-03 18:02:37 +010076 if has('gui_motif')
Bram Moolenaar6f785742017-02-06 22:11:55 +010077 " Invalid font name. The result should be an empty string.
78 call assert_equal('', getfontname('notexist'))
79
80 " Valid font name. This is usually the real name of 7x13 by default.
Bram Moolenaar87748452017-03-12 17:10:33 +010081 let fname = '-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1'
82 call assert_match(fname, getfontname(fname))
Bram Moolenaar6f785742017-02-06 22:11:55 +010083
84 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
85 " Invalid font name. The result should be the name plus the default size.
86 call assert_equal('notexist 10', getfontname('notexist'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010087 call assert_equal('', getfontname('*'))
Bram Moolenaar6f785742017-02-06 22:11:55 +010088
89 " Valid font name. This is usually the real name of Monospace by default.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010090 let fname = 'Bitstream Vera Sans Mono 12'
91 call assert_equal(fname, getfontname(fname))
92 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +010093endfunc
94
95func Test_getfontname_without_arg()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +010096 CheckX11BasedGui
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010097
98 let fname = getfontname()
99
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100100 if has('gui_kde')
Bram Moolenaar6f785742017-02-06 22:11:55 +0100101 " 'expected' is the value specified by SetUp() above.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100102 call assert_equal('Courier 10 Pitch/8/-1/5/50/0/0/0/0/0', fname)
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100103 elseif has('gui_motif')
Bram Moolenaar87748452017-03-12 17:10:33 +0100104 " 'expected' is DFLT_FONT of gui_x11.c or its real name.
105 let pat = '\(7x13\)\|\(\c-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1\)'
106 call assert_match(pat, fname)
Bram Moolenaar6f785742017-02-06 22:11:55 +0100107 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
108 " 'expected' is DEFAULT_FONT of gui_gtk_x11.c.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100109 call assert_equal('Monospace 10', fname)
110 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +0100111endfunc
112
Bram Moolenaar87748452017-03-12 17:10:33 +0100113func Test_getwinpos()
lilydjwg6e0a18f2024-01-29 20:54:28 +0100114 CheckX11
115
Bram Moolenaar87748452017-03-12 17:10:33 +0100116 call assert_match('Window position: X \d\+, Y \d\+', execute('winpos'))
117 call assert_true(getwinposx() >= 0)
118 call assert_true(getwinposy() >= 0)
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200119 call assert_equal([getwinposx(), getwinposy()], getwinpos())
Bram Moolenaar87748452017-03-12 17:10:33 +0100120endfunc
121
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100122func Test_quoteplus()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100123 CheckX11BasedGui
124
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100125 let g:test_is_flaky = 1
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100126
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100127 let quoteplus_saved = @+
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100128
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100129 let test_call = 'Can you hear me?'
130 let test_response = 'Yes, I can.'
Bram Moolenaarde8be2b2022-06-16 14:45:41 +0100131 let testee = 'VIMRUNTIME=' .. $VIMRUNTIME .. '; export VIMRUNTIME;'
132 \ .. GetVimCommand() .. ' --noplugin --not-a-term -c ''%s'''
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100133 " Ignore the "failed to create input context" error.
134 let cmd = 'call test_ignore_error("E285") | '
135 \ . 'gui -f | '
136 \ . 'call feedkeys("'
137 \ . '\"+p'
138 \ . ':s/' . test_call . '/' . test_response . '/\<CR>'
139 \ . '\"+yis'
140 \ . ':q!\<CR>", "tx")'
141 let run_vimtest = printf(testee, cmd)
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100142
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100143 " Set the quoteplus register to test_call, and another gvim will launched.
144 " Then, it first tries to paste the content of its own quotedplus register
145 " onto it. Second, it tries to substitute test_response for the pasted
146 " sentence. If the sentence is identical to test_call, the substitution
147 " should succeed. Third, it tries to yank the result of the substitution
148 " to its own quoteplus register, and last it quits. When system()
149 " returns, the content of the quoteplus register should be identical to
150 " test_response if those quoteplus registers are synchronized properly
151 " with/through the X11 clipboard.
152 let @+ = test_call
153 call system(run_vimtest)
154 call assert_equal(test_response, @+)
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100155
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100156 let @+ = quoteplus_saved
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100157endfunc
158
Bram Moolenaard68a0042021-10-20 23:08:11 +0100159func Test_gui_read_stdin()
160 CheckUnix
161
Bram Moolenaar572a4432022-09-28 21:07:03 +0100162 call writefile(['some', 'lines'], 'Xstdin', 'D')
Bram Moolenaard68a0042021-10-20 23:08:11 +0100163 let script =<< trim END
164 call writefile(getline(1, '$'), 'XstdinOK')
165 qa!
166 END
Bram Moolenaar572a4432022-09-28 21:07:03 +0100167 call writefile(script, 'Xscript', 'D')
Bram Moolenaard68a0042021-10-20 23:08:11 +0100168
169 " Cannot use --not-a-term here, the "reading from stdin" message would not be
170 " displayed.
Bram Moolenaarb5912e02022-05-31 17:03:14 +0100171 " However, when using XIM we might get E285, do use it then.
172 if has('xim')
173 let vimcmd = GetVimCommand()
174 else
175 let vimcmd = substitute(GetVimCommand(), '--not-a-term', '', '')
176 endif
Bram Moolenaard68a0042021-10-20 23:08:11 +0100177
178 call system('cat Xstdin | ' .. vimcmd .. ' -f -g -S Xscript -')
179 call assert_equal(['some', 'lines'], readfile('XstdinOK'))
180
Bram Moolenaard68a0042021-10-20 23:08:11 +0100181 call delete('XstdinOK')
Bram Moolenaard68a0042021-10-20 23:08:11 +0100182endfunc
183
Bram Moolenaar87748452017-03-12 17:10:33 +0100184func Test_set_background()
185 let background_saved = &background
186
187 set background&
188 call assert_equal('light', &background)
189
190 set background=dark
191 call assert_equal('dark', &background)
192
193 let &background = background_saved
194endfunc
195
Bram Moolenaard5841f22017-03-05 13:27:25 +0100196func Test_set_balloondelay()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200197 CheckOption balloondelay
Bram Moolenaard5841f22017-03-05 13:27:25 +0100198
199 let balloondelay_saved = &balloondelay
200
201 " Check if the default value is identical to that described in the manual.
202 set balloondelay&
203 call assert_equal(600, &balloondelay)
204
205 " Edge cases
206
207 " XXX This fact should be hidden so that people won't be tempted to write
208 " plugin/TimeMachine.vim. TODO Add reasonable range checks to the source
209 " code.
210 set balloondelay=-1
211 call assert_equal(-1, &balloondelay)
212
213 " Though it's possible to interpret the zero delay to be 'as soon as
214 " possible' or even 'indefinite', its actual meaning depends on the GUI
215 " toolkit in use after all.
216 set balloondelay=0
217 call assert_equal(0, &balloondelay)
218
219 set balloondelay=1
220 call assert_equal(1, &balloondelay)
221
222 " Since p_bdelay is of type long currently, the upper bound can be
223 " impractically huge and machine-dependent. Practically, it's sufficient
Bram Moolenaar0f9ea222017-03-05 13:48:13 +0100224 " to check if balloondelay works with 0x7fffffff (32 bits) for now.
225 set balloondelay=2147483647
226 call assert_equal(2147483647, &balloondelay)
Bram Moolenaard5841f22017-03-05 13:27:25 +0100227
228 let &balloondelay = balloondelay_saved
229endfunc
230
231func Test_set_ballooneval()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200232 CheckOption ballooneval
Bram Moolenaard5841f22017-03-05 13:27:25 +0100233
234 let ballooneval_saved = &ballooneval
235
236 set ballooneval&
237 call assert_equal(0, &ballooneval)
238
239 set ballooneval
240 call assert_notequal(0, &ballooneval)
241
242 set noballooneval
243 call assert_equal(0, &ballooneval)
244
245 let &ballooneval = ballooneval_saved
246endfunc
247
248func Test_set_balloonexpr()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200249 CheckOption balloonexpr
Bram Moolenaard5841f22017-03-05 13:27:25 +0100250
251 let balloonexpr_saved = &balloonexpr
252
253 " Default value
254 set balloonexpr&
255 call assert_equal('', &balloonexpr)
256
257 " User-defined function
258 new
259 func MyBalloonExpr()
260 return 'Cursor is at line ' . v:beval_lnum .
261 \', column ' . v:beval_col .
262 \ ' of file ' . bufname(v:beval_bufnr) .
263 \ ' on word "' . v:beval_text . '"' .
264 \ ' in window ' . v:beval_winid . ' (#' . v:beval_winnr . ')'
265 endfunc
266 setl balloonexpr=MyBalloonExpr()
267 setl ballooneval
268 call assert_equal('MyBalloonExpr()', &balloonexpr)
269 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200270 " and check if the content of the balloon is the same as what is expected.
Bram Moolenaard5841f22017-03-05 13:27:25 +0100271 " Also, check if textlock works as expected.
272 setl balloonexpr&
273 call assert_equal('', &balloonexpr)
274 delfunc MyBalloonExpr
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +0000275
276 " Using a script-local function
277 func s:NewBalloonExpr()
278 endfunc
279 set balloonexpr=s:NewBalloonExpr()
280 call assert_equal(expand('<SID>') .. 'NewBalloonExpr()', &balloonexpr)
281 set balloonexpr=<SID>NewBalloonExpr()
282 call assert_equal(expand('<SID>') .. 'NewBalloonExpr()', &balloonexpr)
283 delfunc s:NewBalloonExpr
Bram Moolenaard5841f22017-03-05 13:27:25 +0100284 bwipe!
285
286 " Multiline support
287 if has('balloon_multiline')
288 " Multiline balloon using NL
289 new
290 func MyBalloonFuncForMultilineUsingNL()
Dominique Pelle81b573d2022-03-22 21:14:55 +0000291 return "Multiline\nSupported\nBalloon\nusing NL"
Bram Moolenaard5841f22017-03-05 13:27:25 +0100292 endfunc
293 setl balloonexpr=MyBalloonFuncForMultilineUsingNL()
294 setl ballooneval
295 call assert_equal('MyBalloonFuncForMultilineUsingNL()', &balloonexpr)
296 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200297 " and check if the content of the balloon is the same as what is
Bram Moolenaard5841f22017-03-05 13:27:25 +0100298 " expected. Also, check if textlock works as expected.
299 setl balloonexpr&
300 delfunc MyBalloonFuncForMultilineUsingNL
301 bwipe!
302
303 " Multiline balloon using List
304 new
305 func MyBalloonFuncForMultilineUsingList()
Dominique Pelle81b573d2022-03-22 21:14:55 +0000306 return [ 'Multiline', 'Supported', 'Balloon', 'using List' ]
Bram Moolenaard5841f22017-03-05 13:27:25 +0100307 endfunc
308 setl balloonexpr=MyBalloonFuncForMultilineUsingList()
309 setl ballooneval
310 call assert_equal('MyBalloonFuncForMultilineUsingList()', &balloonexpr)
311 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200312 " and check if the content of the balloon is the same as what is
Bram Moolenaard5841f22017-03-05 13:27:25 +0100313 " expected. Also, check if textlock works as expected.
314 setl balloonexpr&
315 delfunc MyBalloonFuncForMultilineUsingList
316 bwipe!
317 endif
318
319 let &balloonexpr = balloonexpr_saved
320endfunc
321
Bram Moolenaar87748452017-03-12 17:10:33 +0100322" Invalid arguments are tested with test_options in conjunction with segfaults
323" caused by them (Patch 8.0.0357, 24922ec233).
324func Test_set_guicursor()
325 let guicursor_saved = &guicursor
326
327 let default = [
328 \ "n-v-c:block-Cursor/lCursor",
329 \ "ve:ver35-Cursor",
330 \ "o:hor50-Cursor",
331 \ "i-ci:ver25-Cursor/lCursor",
332 \ "r-cr:hor20-Cursor/lCursor",
333 \ "sm:block-Cursor-blinkwait175-blinkoff150-blinkon175"
334 \ ]
335
336 " Default Value
337 set guicursor&
338 call assert_equal(join(default, ','), &guicursor)
339
340 " Argument List Example 1
341 let opt_list = copy(default)
342 let opt_list[0] = "n-c-v:block-nCursor"
343 exec "set guicursor=" . join(opt_list, ',')
344 call assert_equal(join(opt_list, ','), &guicursor)
345 unlet opt_list
346
347 " Argument List Example 2
348 let opt_list = copy(default)
349 let opt_list[3] = "i-ci:ver30-iCursor-blinkwait300-blinkon200-blinkoff150"
350 exec "set guicursor=" . join(opt_list, ',')
351 call assert_equal(join(opt_list, ','), &guicursor)
352 unlet opt_list
353
354 " 'a' Mode
355 set guicursor&
356 let &guicursor .= ',a:blinkon0'
357 call assert_equal(join(default, ',') . ",a:blinkon0", &guicursor)
358
359 let &guicursor = guicursor_saved
360endfunc
361
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100362func Test_set_guifont_errors()
363 if has('win32')
364 " Invalid font names are accepted in GTK GUI
365 call assert_fails('set guifont=xa1bc23d7f', 'E596:')
366 endif
367
368 " This only works if 'renderoptions' exists and does not work for Windows XP
369 " and older.
370 if exists('+renderoptions') && windowsversion() !~ '^[345]\.'
371 " doing this four times used to cause a crash
372 set renderoptions=type:directx
373 for i in range(5)
374 set guifont=
375 endfor
376 set renderoptions=
377 for i in range(5)
378 set guifont=
379 endfor
380 endif
381endfunc
382
Bram Moolenaar43dded82017-02-09 16:06:17 +0100383func Test_set_guifont()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100384 CheckX11BasedGui
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100385
386 let guifont_saved = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100387 if has('xfontset')
388 " Prevent 'guifontset' from canceling 'guifont'.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100389 let guifontset_saved = &guifontset
Bram Moolenaar43dded82017-02-09 16:06:17 +0100390 set guifontset=
391 endif
392
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100393 if has('gui_motif')
Bram Moolenaar43dded82017-02-09 16:06:17 +0100394 " Non-empty font list with invalid font names.
395 "
396 " This test is twofold: (1) It checks if the command fails as expected
397 " when there are no loadable fonts found in the list. (2) It checks if
398 " 'guifont' remains the same after the command loads none of the fonts
399 " listed.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100400 let flist = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100401 call assert_fails('set guifont=-notexist1-*,-notexist2-*')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100402 call assert_equal(flist, &guifont)
Bram Moolenaar43dded82017-02-09 16:06:17 +0100403
404 " Non-empty font list with a valid font name. Should pick up the first
405 " valid font.
406 set guifont=-notexist1-*,fixed,-notexist2-*
Bram Moolenaar87748452017-03-12 17:10:33 +0100407 let pat = '\(fixed\)\|\(\c-Misc-Fixed-Medium-R-SemiCondensed--13-120-75-75-C-60-ISO8859-1\)'
408 call assert_match(pat, getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100409
410 " Empty list. Should fallback to the built-in default.
411 set guifont=
Bram Moolenaar87748452017-03-12 17:10:33 +0100412 let pat = '\(7x13\)\|\(\c-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1\)'
413 call assert_match(pat, getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100414
415 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
416 " For GTK, what we refer to as 'font names' in our manual are actually
417 " 'initial font patterns'. A valid font which matches the 'canonical font
418 " pattern' constructed from a given 'initial pattern' is to be looked up
419 " and loaded. That explains why the GTK GUIs appear to accept 'invalid
420 " font names'.
421 "
422 " Non-empty list. Should always pick up the first element, no matter how
423 " strange it is, as explained above.
424 set guifont=(´・ω・`)\ 12,Courier\ 12
425 call assert_equal('(´・ω・`) 12', getfontname())
426
427 " Empty list. Should fallback to the built-in default.
428 set guifont=
429 call assert_equal('Monospace 10', getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100430 endif
431
432 if has('xfontset')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100433 let &guifontset = guifontset_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100434 endif
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100435 let &guifont = guifont_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100436endfunc
437
Bram Moolenaar10434672017-02-12 19:59:08 +0100438func Test_set_guifontset()
Bram Moolenaarce90e362019-09-08 18:58:44 +0200439 CheckFeature xfontset
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100440 let skipped = ''
Bram Moolenaar10434672017-02-12 19:59:08 +0100441
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200442 call assert_fails('set guifontset=*', 'E597:')
443
Bram Moolenaarce90e362019-09-08 18:58:44 +0200444 let ctype_saved = v:ctype
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100445
Bram Moolenaarce90e362019-09-08 18:58:44 +0200446 " First, since XCreateFontSet(3) is very sensitive to locale, fonts must
447 " be chosen meticulously.
448 let font_head = '-misc-fixed-medium-r-normal--14'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100449
Bram Moolenaarce90e362019-09-08 18:58:44 +0200450 let font_aw70 = font_head . '-130-75-75-c-70'
451 let font_aw140 = font_head . '-130-75-75-c-140'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100452
Bram Moolenaarce90e362019-09-08 18:58:44 +0200453 let font_jisx0201 = font_aw70 . '-jisx0201.1976-0'
454 let font_jisx0208 = font_aw140 . '-jisx0208.1983-0'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100455
Bram Moolenaarce90e362019-09-08 18:58:44 +0200456 let full_XLFDs = join([ font_jisx0208, font_jisx0201 ], ',')
457 let short_XLFDs = join([ font_aw140, font_aw70 ], ',')
458 let singleton = font_head . '-*'
459 let aliases = 'k14,r14'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100460
Bram Moolenaarce90e362019-09-08 18:58:44 +0200461 " Second, among 'locales', look up such a locale that gets 'set
462 " guifontset=' to work successfully with every fontset specified with
463 " 'fontsets'.
464 let locales = [ 'ja_JP.UTF-8', 'ja_JP.eucJP', 'ja_JP.SJIS' ]
465 let fontsets = [ full_XLFDs, short_XLFDs, singleton, aliases ]
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100466
Bram Moolenaarce90e362019-09-08 18:58:44 +0200467 let feasible = 0
468 for locale in locales
469 try
470 exec 'language ctype' locale
471 catch /^Vim\%((\a\+)\)\=:E197/
472 continue
473 endtry
474 let done = 0
475 for fontset in fontsets
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100476 try
Bram Moolenaarce90e362019-09-08 18:58:44 +0200477 exec 'set guifontset=' . fontset
478 catch /^Vim\%((\a\+)\)\=:E\%(250\|252\|234\|597\|598\)/
479 break
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100480 endtry
Bram Moolenaarce90e362019-09-08 18:58:44 +0200481 let done += 1
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100482 endfor
Bram Moolenaarce90e362019-09-08 18:58:44 +0200483 if done == len(fontsets)
484 let feasible = 1
485 break
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100486 endif
Bram Moolenaarce90e362019-09-08 18:58:44 +0200487 endfor
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100488
Bram Moolenaarce90e362019-09-08 18:58:44 +0200489 " Third, give a set of tests if it is found feasible.
490 if !feasible
491 let skipped = g:not_hosted
492 else
493 " N.B. 'v:ctype' has already been set to an appropriate value in the
494 " previous loop.
495 for fontset in fontsets
496 exec 'set guifontset=' . fontset
497 call assert_equal(fontset, &guifontset)
498 endfor
Bram Moolenaar10434672017-02-12 19:59:08 +0100499 endif
500
Bram Moolenaarce90e362019-09-08 18:58:44 +0200501 " Finally, restore ctype.
502 exec 'language ctype' ctype_saved
503
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100504 if !empty(skipped)
505 throw skipped
Bram Moolenaar10434672017-02-12 19:59:08 +0100506 endif
507endfunc
508
509func Test_set_guifontwide()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100510 CheckX11BasedGui
Bram Moolenaar10434672017-02-12 19:59:08 +0100511
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100512 call assert_fails('set guifontwide=*', 'E533:')
513
514 if has('gui_gtk')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100515 let guifont_saved = &guifont
516 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100517
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100518 let fc_match = exepath('fc-match')
519 if empty(fc_match)
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100520 let skipped = g:not_hosted
Bram Moolenaar10434672017-02-12 19:59:08 +0100521 else
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100522 let &guifont = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=en')
523 let wide = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=ja')
524 exec 'set guifontwide=' . fnameescape(wide)
525 call assert_equal(wide, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100526 endif
527
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100528 let &guifontwide = guifontwide_saved
529 let &guifont = guifont_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100530
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100531 elseif has('gui_motif')
Bram Moolenaar10434672017-02-12 19:59:08 +0100532 " guifontwide is premised upon the xfontset feature.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100533 if !has('xfontset')
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100534 let skipped = g:not_supported . 'xfontset'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100535 else
536 let encoding_saved = &encoding
537 let guifont_saved = &guifont
538 let guifontset_saved = &guifontset
539 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100540
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100541 let nfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-90-iso10646-1'
542 let wfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-180-iso10646-1'
Bram Moolenaar10434672017-02-12 19:59:08 +0100543
544 set encoding=utf-8
545
546 " Case 1: guifontset is empty
547 set guifontset=
548
549 " Case 1-1: Automatic selection
550 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100551 exec 'set guifont=' . nfont
552 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100553
554 " Case 1-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100555 exec 'set guifontwide=' . wfont
556 exec 'set guifont=' . nfont
557 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100558
559 " Case 2: guifontset is invalid
560 try
561 set guifontset=-*-notexist-*
Bram Moolenaar37175402017-03-18 20:18:45 +0100562 call assert_report("'set guifontset=-*-notexist-*' should have failed")
Bram Moolenaar10434672017-02-12 19:59:08 +0100563 catch
Bram Moolenaare2e40752020-09-04 21:18:46 +0200564 call assert_exception('E598:')
Bram Moolenaar10434672017-02-12 19:59:08 +0100565 endtry
Bram Moolenaar10434672017-02-12 19:59:08 +0100566
567 " Case 2-1: Automatic selection
568 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100569 exec 'set guifont=' . nfont
570 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100571
572 " Case 2-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100573 exec 'set guifontwide=' . wfont
574 exec 'set guifont=' . nfont
575 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100576
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100577 let &guifontwide = guifontwide_saved
578 let &guifontset = guifontset_saved
579 let &guifont = guifont_saved
580 let &encoding = encoding_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100581 endif
Bram Moolenaar10434672017-02-12 19:59:08 +0100582 endif
Bram Moolenaar10434672017-02-12 19:59:08 +0100583endfunc
584
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200585func Test_expand_guifont()
586 if has('gui_win32')
587 let guifont_saved = &guifont
588 let guifontwide_saved = &guifontwide
589
590 " Test recalling existing option, and suggesting current font size
591 set guifont=Courier\ New:h11:cANSI
592 call assert_equal('Courier\ New:h11:cANSI', getcompletion('set guifont=', 'cmdline')[0])
593 call assert_equal('h11', getcompletion('set guifont=Lucida\ Console:', 'cmdline')[0])
594
595 " Test auto-completion working for font names
596 call assert_equal(['Courier\ New'], getcompletion('set guifont=Couri*ew$', 'cmdline'))
597 call assert_equal(['Courier\ New'], getcompletion('set guifontwide=Couri*ew$', 'cmdline'))
598
599 " Make sure non-monospace fonts are filtered out
600 call assert_equal([], getcompletion('set guifont=Arial', 'cmdline'))
601 call assert_equal([], getcompletion('set guifontwide=Arial', 'cmdline'))
602
603 " Test auto-completion working for font options
604 call assert_notequal(-1, index(getcompletion('set guifont=Courier\ New:', 'cmdline'), 'b'))
605 call assert_equal(['cDEFAULT'], getcompletion('set guifont=Courier\ New:cD*T', 'cmdline'))
606 call assert_equal(['qCLEARTYPE'], getcompletion('set guifont=Courier\ New:qC*TYPE', 'cmdline'))
607
608 let &guifontwide = guifontwide_saved
609 let &guifont = guifont_saved
610 elseif has('gui_gtk')
611 let guifont_saved = &guifont
612 let guifontwide_saved = &guifontwide
613
614 " Test recalling default and existing option
615 set guifont=
616 call assert_equal('Monospace\ 10', getcompletion('set guifont=', 'cmdline')[0])
617 set guifont=Monospace\ 9
618 call assert_equal('Monospace\ 9', getcompletion('set guifont=', 'cmdline')[0])
619
620 " Test auto-completion working for font names
621 call assert_equal(['Monospace'], getcompletion('set guifont=Mono*pace$', 'cmdline'))
622 call assert_equal(['Monospace'], getcompletion('set guifontwide=Mono*pace$', 'cmdline'))
623
624 " Make sure non-monospace fonts are filtered out only in 'guifont'
625 call assert_equal([], getcompletion('set guifont=Sans$', 'cmdline'))
626 call assert_equal(['Sans'], getcompletion('set guifontwide=Sans$', 'cmdline'))
627
628 let &guifontwide = guifontwide_saved
629 let &guifont = guifont_saved
630 else
631 call assert_equal([], getcompletion('set guifont=', 'cmdline'))
632 endif
633endfunc
634
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100635func Test_set_guiligatures()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100636 CheckX11BasedGui
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100637
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +0100638 if has('gui_gtk') || has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3') || has('win32')
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100639 " Try correct value
640 set guiligatures=<>=ab
641 call assert_equal("<>=ab", &guiligatures)
642 " Try to throw error
643 try
644 set guiligatures=<>=šab
645 call assert_report("'set guiligatures=<>=šab should have failed")
646 catch
647 call assert_exception('E1243:')
648 endtry
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100649 endif
650endfunc
651
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100652func Test_set_guiheadroom()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100653 CheckX11BasedGui
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100654
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100655 " Since this script is to be read together with '-U NONE', the default
656 " value must be preserved.
657 call assert_equal(50, &guiheadroom)
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100658endfunc
659
Bram Moolenaar87748452017-03-12 17:10:33 +0100660func Test_set_guioptions()
661 let guioptions_saved = &guioptions
662 let duration = '200m'
663
664 if has('win32')
665 " Default Value
666 set guioptions&
667 call assert_equal('egmrLtT', &guioptions)
668
669 else
670 " Default Value
671 set guioptions&
672 call assert_equal('aegimrLtT', &guioptions)
673
674 " To activate scrollbars of type 'L' or 'R'.
675 wincmd v
676 redraw!
677
678 " Remove all default GUI ornaments
679 set guioptions-=T
680 exec 'sleep' . duration
681 call assert_equal('aegimrLt', &guioptions)
682 set guioptions-=t
683 exec 'sleep' . duration
684 call assert_equal('aegimrL', &guioptions)
685 set guioptions-=L
686 exec 'sleep' . duration
687 call assert_equal('aegimr', &guioptions)
688 set guioptions-=r
689 exec 'sleep' . duration
690 call assert_equal('aegim', &guioptions)
691 set guioptions-=m
692 exec 'sleep' . duration
693 call assert_equal('aegi', &guioptions)
694
695 " Try non-default GUI ornaments
696 set guioptions+=l
697 exec 'sleep' . duration
698 call assert_equal('aegil', &guioptions)
699 set guioptions-=l
700 exec 'sleep' . duration
701 call assert_equal('aegi', &guioptions)
702
703 set guioptions+=R
704 exec 'sleep' . duration
705 call assert_equal('aegiR', &guioptions)
706 set guioptions-=R
707 exec 'sleep' . duration
708 call assert_equal('aegi', &guioptions)
709
710 set guioptions+=b
711 exec 'sleep' . duration
712 call assert_equal('aegib', &guioptions)
713 set guioptions+=h
714 exec 'sleep' . duration
715 call assert_equal('aegibh', &guioptions)
716 set guioptions-=h
717 exec 'sleep' . duration
718 call assert_equal('aegib', &guioptions)
719 set guioptions-=b
720 exec 'sleep' . duration
721 call assert_equal('aegi', &guioptions)
722
723 set guioptions+=v
724 exec 'sleep' . duration
725 call assert_equal('aegiv', &guioptions)
726 set guioptions-=v
727 exec 'sleep' . duration
728 call assert_equal('aegi', &guioptions)
729
730 if has('gui_motif')
731 set guioptions+=F
732 exec 'sleep' . duration
733 call assert_equal('aegiF', &guioptions)
734 set guioptions-=F
735 exec 'sleep' . duration
736 call assert_equal('aegi', &guioptions)
737 endif
738
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +0200739 if has('gui_gtk3')
740 set guioptions+=d
741 exec 'sleep' . duration
742 call assert_equal('aegid', &guioptions)
743 set guioptions-=d
744 exec 'sleep' . duration
745 call assert_equal('aegi', &guioptions)
746 endif
747
Bram Moolenaar87748452017-03-12 17:10:33 +0100748 " Restore GUI ornaments to the default state.
749 set guioptions+=m
750 exec 'sleep' . duration
751 call assert_equal('aegim', &guioptions)
752 set guioptions+=r
753 exec 'sleep' . duration
754 call assert_equal('aegimr', &guioptions)
755 set guioptions+=L
756 exec 'sleep' . duration
757 call assert_equal('aegimrL', &guioptions)
758 set guioptions+=t
759 exec 'sleep' . duration
760 call assert_equal('aegimrLt', &guioptions)
761 set guioptions+=T
762 exec 'sleep' . duration
763 call assert_equal("aegimrLtT", &guioptions)
764
765 wincmd o
766 redraw!
767 endif
768
769 let &guioptions = guioptions_saved
770endfunc
771
Bram Moolenaarab186732018-09-14 21:27:06 +0200772func Test_scrollbars()
Christian Brabandtcf3c1ca2023-03-06 15:29:29 +0000773 " this test sometimes fails on CI
774 let g:test_is_flaky = 1
775
Bram Moolenaarab186732018-09-14 21:27:06 +0200776 " buffer with 200 lines
Christian Brabandtcf3c1ca2023-03-06 15:29:29 +0000777 new
Bram Moolenaarab186732018-09-14 21:27:06 +0200778 call setline(1, repeat(['one', 'two'], 100))
779 set guioptions+=rlb
780
781 " scroll to move line 11 at top, moves the cursor there
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +0000782 let args = #{which: 'left', value: 10, dragging: 0}
783 call test_gui_event('scrollbar', args)
Bram Moolenaarab186732018-09-14 21:27:06 +0200784 redraw
785 call assert_equal(1, winline())
786 call assert_equal(11, line('.'))
787
Bram Moolenaar74ccbb12023-05-18 20:07:12 +0100788 " FIXME: This test should also pass with Motif and 24 lines
789 if &lines > 24 || !has('gui_motif')
790 " scroll to move line 1 at top, cursor stays in line 11
791 let args = #{which: 'right', value: 0, dragging: 0}
792 call test_gui_event('scrollbar', args)
793 redraw
794 call assert_equal(11, winline())
795 call assert_equal(11, line('.'))
796 endif
Bram Moolenaarab186732018-09-14 21:27:06 +0200797
798 set nowrap
799 call setline(11, repeat('x', 150))
800 redraw
801 call assert_equal(1, wincol())
Bram Moolenaarf6d50f12019-06-06 15:40:08 +0200802 set number
803 redraw
804 call assert_equal(5, wincol())
805 set nonumber
806 redraw
Bram Moolenaarab186732018-09-14 21:27:06 +0200807 call assert_equal(1, col('.'))
808
809 " scroll to character 11, cursor is moved
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +0000810 let args = #{which: 'hor', value: 10, dragging: 0}
811 call test_gui_event('scrollbar', args)
Bram Moolenaarab186732018-09-14 21:27:06 +0200812 redraw
813 call assert_equal(1, wincol())
Bram Moolenaarf6d50f12019-06-06 15:40:08 +0200814 set number
815 redraw
816 call assert_equal(5, wincol())
817 set nonumber
818 redraw
Bram Moolenaarab186732018-09-14 21:27:06 +0200819 call assert_equal(11, col('.'))
820
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +0000821 " Invalid arguments
822 call assert_false(test_gui_event('scrollbar', {}))
823 call assert_false(test_gui_event('scrollbar', #{value: 10, dragging: 0}))
824 call assert_false(test_gui_event('scrollbar', #{which: 'hor', dragging: 0}))
825 call assert_false(test_gui_event('scrollbar', #{which: 'hor', value: 1}))
826 call assert_fails("call test_gui_event('scrollbar', #{which: 'a', value: 1, dragging: 0})", 'E475:')
827
Bram Moolenaarab186732018-09-14 21:27:06 +0200828 set guioptions&
829 set wrap&
830 bwipe!
831endfunc
832
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100833func Test_menu()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100834 CheckFeature quickfix
835
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100836 " Check Help menu exists
837 let help_menu = execute('menu Help')
838 call assert_match('Overview', help_menu)
839
840 " Check Help menu works
841 emenu Help.Overview
842 call assert_equal('help', &buftype)
843 close
844
845 " Check deleting menu doesn't cause trouble.
846 aunmenu Help
Bram Moolenaarb45cd362020-09-28 21:41:49 +0200847 if exists(':tlmenu')
848 tlunmenu Help
849 endif
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100850 call assert_fails('menu Help', 'E329:')
851endfunc
852
Bram Moolenaar87748452017-03-12 17:10:33 +0100853func Test_set_guipty()
854 let guipty_saved = &guipty
855
856 " Default Value
857 set guipty&
858 call assert_equal(1, &guipty)
859
860 set noguipty
861 call assert_equal(0, &guipty)
862
863 let &guipty = guipty_saved
Bram Moolenaar6f785742017-02-06 22:11:55 +0100864endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100865
Bram Moolenaarc701f322019-03-28 21:49:21 +0100866func Test_encoding_conversion()
867 " GTK supports conversion between 'encoding' and "utf-8"
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200868 CheckFeature gui_gtk
869 let encoding_saved = &encoding
870 set encoding=latin1
Bram Moolenaarc701f322019-03-28 21:49:21 +0100871
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200872 " would be nice if we could take a screenshot
873 intro
874 " sets the window title
875 edit SomeFile
Bram Moolenaarc701f322019-03-28 21:49:21 +0100876
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200877 let &encoding = encoding_saved
Bram Moolenaarc701f322019-03-28 21:49:21 +0100878endfunc
879
Bram Moolenaar877e9572016-08-04 20:05:50 +0200880func Test_shell_command()
881 new
Bram Moolenaar9d5b8762016-08-04 21:21:13 +0200882 r !echo hello
883 call assert_equal('hello', substitute(getline(2), '\W', '', 'g'))
Bram Moolenaar877e9572016-08-04 20:05:50 +0200884 bwipe!
Bram Moolenaar877e9572016-08-04 20:05:50 +0200885endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100886
Bram Moolenaar87748452017-03-12 17:10:33 +0100887func Test_syntax_colortest()
888 runtime syntax/colortest.vim
889 redraw!
890 sleep 200m
891 bwipe!
892endfunc
893
894func Test_set_term()
895 " It's enough to check the current value since setting 'term' to anything
896 " other than builtin_gui makes no sense at all.
897 call assert_equal('builtin_gui', &term)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200898 call assert_fails('set term=xterm', 'E530:')
Bram Moolenaar87748452017-03-12 17:10:33 +0100899endfunc
900
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100901func Test_windowid_variable()
Christian Brabandte5bc2e42024-06-01 20:55:09 +0200902 if g:x11_based_gui || has('win32')
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100903 call assert_true(v:windowid > 0)
904 else
905 call assert_equal(0, v:windowid)
906 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +0100907endfunc
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200908
909" Test "vim -g" and also the GUIEnter autocommand.
910func Test_gui_dash_g()
911 let cmd = GetVimCommand('Xscriptgui')
Bram Moolenaar572a4432022-09-28 21:07:03 +0100912 call writefile([""], "Xtestgui", 'D')
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200913 let lines =<< trim END
914 au GUIEnter * call writefile(["insertmode: " . &insertmode], "Xtestgui")
915 au GUIEnter * qall
916 END
Bram Moolenaar572a4432022-09-28 21:07:03 +0100917 call writefile(lines, 'Xscriptgui', 'D')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200918 call system(cmd . ' -g')
919 call WaitForAssert({-> assert_equal(['insertmode: 0'], readfile('Xtestgui'))})
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200920endfunc
921
922" Test "vim -7" and also the GUIEnter autocommand.
923func Test_gui_dash_y()
924 let cmd = GetVimCommand('Xscriptgui')
Bram Moolenaar572a4432022-09-28 21:07:03 +0100925 call writefile([""], "Xtestgui", 'D')
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200926 let lines =<< trim END
927 au GUIEnter * call writefile(["insertmode: " . &insertmode], "Xtestgui")
928 au GUIEnter * qall
929 END
Bram Moolenaar572a4432022-09-28 21:07:03 +0100930 call writefile(lines, 'Xscriptgui', 'D')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200931 call system(cmd . ' -y')
932 call WaitForAssert({-> assert_equal(['insertmode: 1'], readfile('Xtestgui'))})
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200933endfunc
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100934
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200935" Test for "!" option in 'guioptions'. Use a terminal for running external
936" commands
937func Test_gui_run_cmd_in_terminal()
Bram Moolenaar4457e1d2020-05-15 22:46:44 +0200938 CheckFeature terminal
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200939 let save_guioptions = &guioptions
940 set guioptions+=!
941 if has('win32')
942 let cmd = 'type'
943 else
944 " assume all the other systems have a cat command
945 let cmd = 'cat'
946 endif
Bram Moolenaar98f16712020-05-22 13:34:01 +0200947 exe "silent !" . cmd . " test_gui.vim"
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200948 " TODO: how to check that the command ran in a separate terminal?
949 " Maybe check for $TERM (dumb vs xterm) in the spawned shell?
950 let &guioptions = save_guioptions
951endfunc
952
Bram Moolenaar46cd43b2020-06-04 22:22:11 +0200953func Test_gui_recursive_mapping()
954 nmap ' <C-W>
955 nmap <C-W>a :let didit = 1<CR>
956 call feedkeys("'a", 'xt')
957 call assert_equal(1, didit)
958
959 nunmap '
960 nunmap <C-W>a
961endfunc
962
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200963" Test GUI mouse events
964func Test_gui_mouse_event()
Bram Moolenaarc13e9982022-11-28 21:20:48 +0000965 " Low level input isn't 100% reliable
966 let g:test_is_flaky = 1
967
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200968 set mousemodel=extend
969 call test_override('no_query_mouse', 1)
970 new
971 call setline(1, ['one two three', 'four five six'])
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200972 call cursor(1, 1)
Bram Moolenaarc13e9982022-11-28 21:20:48 +0000973 redraw!
974
975 " place the cursor using left click and release in normal mode
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000976 let args = #{button: 0, row: 2, col: 4, multiclick: 0, modifiers: 0}
977 call test_gui_event('mouse', args)
978 let args.button = 3
979 eval 'mouse'->test_gui_event(args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200980 call feedkeys("\<Esc>", 'Lx!')
981 call assert_equal([0, 2, 4, 0], getpos('.'))
982
983 " select and yank a word
984 let @" = ''
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000985 let args = #{button: 0, row: 1, col: 9, multiclick: 0, modifiers: 0}
986 call test_gui_event('mouse', args)
987 let args.multiclick = 1
988 call test_gui_event('mouse', args)
989 let args.button = 3
990 let args.multiclick = 0
991 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200992 call feedkeys("y", 'Lx!')
993 call assert_equal('three', @")
994
995 " create visual selection using right click
996 let @" = ''
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000997 let args = #{button: 0, row: 2, col: 6, multiclick: 0, modifiers: 0}
998 call test_gui_event('mouse', args)
999 let args.button = 3
1000 call test_gui_event('mouse', args)
1001 let args = #{button: 2, row: 2, col: 13, multiclick: 0, modifiers: 0}
1002 call test_gui_event('mouse', args)
1003 let args.button = 3
1004 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001005 call feedkeys("y", 'Lx!')
1006 call assert_equal('five six', @")
1007
1008 " paste using middle mouse button
1009 let @* = 'abc '
1010 call feedkeys('""', 'Lx!')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001011 let args = #{button: 1, row: 1, col: 9, multiclick: 0, modifiers: 0}
1012 call test_gui_event('mouse', args)
1013 let args.button = 3
1014 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001015 call feedkeys("\<Esc>", 'Lx!')
1016 call assert_equal(['one two abc three', 'four five six'], getline(1, '$'))
1017
1018 " extend visual selection using right click in visual mode
1019 let @" = ''
1020 call cursor(1, 1)
1021 call feedkeys('v', 'Lx!')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001022 let args = #{button: 2, row: 1, col: 17, multiclick: 0, modifiers: 0}
1023 call test_gui_event('mouse', args)
1024 let args.button = 3
1025 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001026 call feedkeys("y", 'Lx!')
1027 call assert_equal('one two abc three', @")
1028
1029 " extend visual selection using mouse drag
1030 let @" = ''
1031 call cursor(1, 1)
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001032 let args = #{button: 0, row: 2, col: 1, multiclick: 0, modifiers: 0}
1033 call test_gui_event('mouse', args)
1034 let args = #{button: 0x43, row: 2, col: 9, multiclick: 0, modifiers: 0}
1035 call test_gui_event('mouse', args)
1036 let args.button = 0x3
1037 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001038 call feedkeys("y", 'Lx!')
1039 call assert_equal('four five', @")
1040
1041 " select text by moving the mouse
1042 let @" = ''
1043 call cursor(1, 1)
1044 redraw!
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001045 let args = #{button: 0, row: 1, col: 4, multiclick: 0, modifiers: 0}
1046 call test_gui_event('mouse', args)
1047 let args.button = 0x700
1048 let args.col = 9
1049 call test_gui_event('mouse', args)
1050 let args.col = 13
1051 call test_gui_event('mouse', args)
1052 let args.button = 3
1053 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001054 call feedkeys("y", 'Lx!')
1055 call assert_equal(' two abc t', @")
1056
1057 " Using mouse in insert mode
1058 call cursor(1, 1)
1059 call feedkeys('i', 't')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001060 let args = #{button: 0, row: 2, col: 11, multiclick: 0, modifiers: 0}
1061 call test_gui_event('mouse', args)
1062 let args.button = 3
1063 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001064 call feedkeys("po\<Esc>", 'Lx!')
1065 call assert_equal(['one two abc three', 'four five posix'], getline(1, '$'))
1066
1067 %d _
LemonBoyc27747e2022-05-07 12:25:40 +01001068 set scrolloff=0
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001069 call setline(1, range(1, 100))
1070 " scroll up
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001071 let args = #{button: 0x200, row: 2, col: 1, multiclick: 0, modifiers: 0}
1072 call test_gui_event('mouse', args)
1073 call test_gui_event('mouse', args)
1074 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001075 call feedkeys("H", 'Lx!')
1076 call assert_equal(10, line('.'))
1077
1078 " scroll down
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001079 let args = #{button: 0x100, row: 2, col: 1, multiclick: 0, modifiers: 0}
1080 call test_gui_event('mouse', args)
1081 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001082 call feedkeys("H", 'Lx!')
1083 call assert_equal(4, line('.'))
LemonBoyc27747e2022-05-07 12:25:40 +01001084 set scrolloff&
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001085
1086 %d _
1087 set nowrap
1088 call setline(1, range(10)->join('')->repeat(10))
1089 " scroll left
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001090 let args = #{button: 0x500, row: 1, col: 5, multiclick: 0, modifiers: 0}
1091 call test_gui_event('mouse', args)
1092 let args.col = 10
1093 call test_gui_event('mouse', args)
1094 let args.col = 15
1095 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001096 call feedkeys('g0', 'Lx!')
1097 call assert_equal(19, col('.'))
1098
1099 " scroll right
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001100 let args = #{button: 0x600, row: 1, col: 15, multiclick: 0, modifiers: 0}
1101 call test_gui_event('mouse', args)
1102 let args.col = 10
1103 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001104 call feedkeys('g0', 'Lx!')
1105 call assert_equal(7, col('.'))
1106 set wrap&
1107
1108 %d _
1109 call setline(1, repeat([repeat('a', 60)], 10))
1110
1111 " record various mouse events
1112 let mouseEventNames = [
1113 \ 'LeftMouse', 'LeftRelease', '2-LeftMouse', '3-LeftMouse',
1114 \ 'S-LeftMouse', 'A-LeftMouse', 'C-LeftMouse', 'MiddleMouse',
1115 \ 'MiddleRelease', '2-MiddleMouse', '3-MiddleMouse',
1116 \ 'S-MiddleMouse', 'A-MiddleMouse', 'C-MiddleMouse',
1117 \ 'RightMouse', 'RightRelease', '2-RightMouse',
1118 \ '3-RightMouse', 'S-RightMouse', 'A-RightMouse', 'C-RightMouse',
1119 \ 'X1Mouse', 'S-X1Mouse', 'A-X1Mouse', 'C-X1Mouse', 'X2Mouse',
1120 \ 'S-X2Mouse', 'A-X2Mouse', 'C-X2Mouse'
1121 \ ]
1122 let mouseEventCodes = map(copy(mouseEventNames), "'<' .. v:val .. '>'")
1123 let g:events = []
1124 for e in mouseEventCodes
1125 exe 'nnoremap ' .. e .. ' <Cmd>call add(g:events, "' ..
1126 \ substitute(e, '[<>]', '', 'g') .. '")<CR>'
1127 endfor
1128
1129 " Test various mouse buttons (0 - Left, 1 - Middle, 2 - Right, 0x300 - X1,
1130 " 0x300- X2)
1131 for button in [0, 1, 2, 0x300, 0x400]
1132 " Single click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001133 let args = #{button: button, row: 2, col: 5, multiclick: 0, modifiers: 0}
1134 call test_gui_event('mouse', args)
1135 let args.button = 3
1136 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001137
1138 " Double/Triple click is supported by only the Left/Middle/Right mouse
1139 " buttons
1140 if button <= 2
1141 " Double Click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001142 let args.button = button
1143 call test_gui_event('mouse', args)
1144 let args.multiclick = 1
1145 call test_gui_event('mouse', args)
1146 let args.button = 3
1147 let args.multiclick = 0
1148 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001149
1150 " Triple Click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001151 let args.button = button
1152 call test_gui_event('mouse', args)
1153 let args.multiclick = 1
1154 call test_gui_event('mouse', args)
1155 call test_gui_event('mouse', args)
1156 let args.button = 3
1157 let args.multiclick = 0
1158 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001159 endif
1160
1161 " Shift click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001162 let args = #{button: button, row: 3, col: 7, multiclick: 0, modifiers: 4}
1163 call test_gui_event('mouse', args)
1164 let args.button = 3
1165 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001166
1167 " Alt click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001168 let args.button = button
1169 let args.modifiers = 8
1170 call test_gui_event('mouse', args)
1171 let args.button = 3
1172 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001173
1174 " Ctrl click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001175 let args.button = button
1176 let args.modifiers = 16
1177 call test_gui_event('mouse', args)
1178 let args.button = 3
1179 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001180
1181 call feedkeys("\<Esc>", 'Lx!')
1182 endfor
1183
1184 call assert_equal(['LeftMouse', 'LeftRelease', 'LeftMouse', '2-LeftMouse',
1185 \ 'LeftMouse', '2-LeftMouse', '3-LeftMouse', 'S-LeftMouse',
1186 \ 'A-LeftMouse', 'C-LeftMouse', 'MiddleMouse', 'MiddleRelease',
1187 \ 'MiddleMouse', '2-MiddleMouse', 'MiddleMouse', '2-MiddleMouse',
1188 \ '3-MiddleMouse', 'S-MiddleMouse', 'A-MiddleMouse', 'C-MiddleMouse',
1189 \ 'RightMouse', 'RightRelease', 'RightMouse', '2-RightMouse',
1190 \ 'RightMouse', '2-RightMouse', '3-RightMouse', 'S-RightMouse',
1191 \ 'A-RightMouse', 'C-RightMouse', 'X1Mouse', 'S-X1Mouse', 'A-X1Mouse',
1192 \ 'C-X1Mouse', 'X2Mouse', 'S-X2Mouse', 'A-X2Mouse', 'C-X2Mouse'],
1193 \ g:events)
1194
1195 for e in mouseEventCodes
1196 exe 'nunmap ' .. e
1197 endfor
1198
1199 " modeless selection
1200 set mouse=
1201 let save_guioptions = &guioptions
1202 set guioptions+=A
1203 %d _
1204 call setline(1, ['one two three', 'four five sixteen'])
1205 call cursor(1, 1)
1206 redraw!
1207 " Double click should select the word and copy it to clipboard
1208 let @* = ''
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001209 let args = #{button: 0, row: 2, col: 11, multiclick: 0, modifiers: 0}
1210 call test_gui_event('mouse', args)
1211 let args.multiclick = 1
1212 call test_gui_event('mouse', args)
1213 let args.button = 3
1214 let args.multiclick = 0
1215 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001216 call feedkeys("\<Esc>", 'Lx!')
1217 call assert_equal([0, 1, 1, 0], getpos('.'))
1218 call assert_equal('sixteen', @*)
1219 " Right click should extend the selection from cursor
1220 call cursor(1, 6)
1221 redraw!
1222 let @* = ''
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001223 let args = #{button: 2, row: 1, col: 11, multiclick: 0, modifiers: 0}
1224 call test_gui_event('mouse', args)
1225 let args.button = 3
1226 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001227 call feedkeys("\<Esc>", 'Lx!')
1228 call assert_equal([0, 1, 6, 0], getpos('.'))
1229 call assert_equal('wo thr', @*)
1230 " Middle click should paste the clipboard contents
1231 call cursor(2, 1)
1232 redraw!
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001233 let args = #{button: 1, row: 1, col: 11, multiclick: 0, modifiers: 0}
1234 call test_gui_event('mouse', args)
1235 let args.button = 3
1236 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001237 call feedkeys("\<Esc>", 'Lx!')
1238 call assert_equal([0, 2, 7, 0], getpos('.'))
1239 call assert_equal('wo thrfour five sixteen', getline(2))
Bram Moolenaarc13e9982022-11-28 21:20:48 +00001240
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001241 set mouse&
1242 let &guioptions = save_guioptions
Bram Moolenaarc13e9982022-11-28 21:20:48 +00001243 bw!
1244 call test_override('no_query_mouse', 0)
1245 set mousemodel&
1246endfunc
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001247
Bram Moolenaarc13e9982022-11-28 21:20:48 +00001248" Test invalid parameters for test_gui_event()
1249func Test_gui_event_mouse_fails()
1250 call test_override('no_query_mouse', 1)
1251 new
1252 call setline(1, ['one two three', 'four five six'])
1253 set mousemodel=extend
1254
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001255 let args = #{row: 2, col: 4, multiclick: 0, modifiers: 0}
1256 call assert_false(test_gui_event('mouse', args))
1257 let args = #{button: 0, col: 4, multiclick: 0, modifiers: 0}
1258 call assert_false(test_gui_event('mouse', args))
1259 let args = #{button: 0, row: 2, multiclick: 0, modifiers: 0}
1260 call assert_false(test_gui_event('mouse', args))
1261 let args = #{button: 0, row: 2, col: 4, modifiers: 0}
1262 call assert_false(test_gui_event('mouse', args))
1263 let args = #{button: 0, row: 2, col: 4, multiclick: 0}
1264 call assert_false(test_gui_event('mouse', args))
1265
1266 " Error cases for test_gui_event()
1267 call assert_fails("call test_gui_event('a1b2c3', args)", 'E475:')
1268 call assert_fails("call test_gui_event([], args)", 'E1174:')
1269 call assert_fails("call test_gui_event('abc', [])", 'E1206:')
1270 call assert_fails("call test_gui_event(test_null_string(), {})", 'E475:')
1271 call assert_false(test_gui_event('mouse', test_null_dict()))
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001272
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001273 bw!
1274 call test_override('no_query_mouse', 0)
1275 set mousemodel&
1276endfunc
1277
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001278" Move the mouse to the top-left in preparation for mouse events
1279func PrepareForMouseEvent(args)
Bram Moolenaar36eb14f2022-09-01 14:38:01 +01001280 call extend(a:args, #{row: 1, col: 1})
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001281 call test_gui_event('mouse', a:args)
Bram Moolenaar36eb14f2022-09-01 14:38:01 +01001282 let g:eventlist = []
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001283 call feedkeys('', 'Lx!')
Bram Moolenaar36eb14f2022-09-01 14:38:01 +01001284
1285 " Wait a bit for the event. I may not come if the mouse didn't move, wait up
1286 " to 100 msec.
1287 for n in range(10)
1288 if len(g:eventlist) > 0
1289 break
1290 endif
1291 sleep 10m
1292 endfor
1293 let g:eventlist = []
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001294endfunc
1295
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001296func MouseWasMoved()
1297 let pos = getmousepos()
1298 call add(g:eventlist, #{row: pos.screenrow, col: pos.screencol})
1299endfunc
1300
Ernie Raelc4cb5442022-04-03 15:47:28 +01001301func Test_gui_mouse_move_event()
1302 let args = #{move: 1, button: 0, multiclick: 0, modifiers: 0}
1303
Bram Moolenaar36eb14f2022-09-01 14:38:01 +01001304 " by default, no mouse move events are generated
Ernie Raelc4cb5442022-04-03 15:47:28 +01001305 set mousemev&
1306 call assert_false(&mousemev)
1307
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001308 let g:eventlist = []
1309 nnoremap <special> <silent> <MouseMove> :call MouseWasMoved()<CR>
Ernie Raelc4cb5442022-04-03 15:47:28 +01001310
1311 " start at mouse pos (1,1), clear counter
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001312 call PrepareForMouseEvent(args)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001313
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001314 call extend(args, #{row: 3, col: 30, cell: v:true})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001315 call test_gui_event('mouse', args)
1316 call feedkeys('', 'Lx!')
1317
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001318 call extend(args, #{row: 10, col: 30, cell: v:true})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001319 call test_gui_event('mouse', args)
1320 call feedkeys('', 'Lx!')
1321
Bram Moolenaar36eb14f2022-09-01 14:38:01 +01001322 " no events since 'mousemev' is off
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001323 call assert_equal([], g:eventlist)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001324
1325 " turn on mouse events and try the same thing
1326 set mousemev
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001327 call PrepareForMouseEvent(args)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001328
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001329 call extend(args, #{row: 3, col: 30, cell: v:true})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001330 call test_gui_event('mouse', args)
1331 call feedkeys('', 'Lx!')
1332
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001333 call extend(args, #{row: 10, col: 30, cell: v:true})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001334 call test_gui_event('mouse', args)
1335 call feedkeys('', 'Lx!')
1336
Bram Moolenaar3c25a862022-05-16 17:07:41 +01001337 " FIXME: on MS-Windows we get a stray event first
1338 if has('win32') && len(g:eventlist) == 3
Bram Moolenaare5162e72022-05-16 16:41:35 +01001339 let g:eventlist = g:eventlist[1 : ]
1340 endif
1341
Christopher Plewright20b795e2022-12-20 20:01:58 +00001342 call assert_equal([#{row: 3, col: 30}, #{row: 10, col: 30}], g:eventlist)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001343
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001344 " wiggle the mouse around within a screen cell, shouldn't trigger events
1345 call extend(args, #{cell: v:false})
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001346 call PrepareForMouseEvent(args)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001347
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001348 call extend(args, #{row: 1, col: 2, cell: v:false})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001349 call test_gui_event('mouse', args)
1350 call feedkeys('', 'Lx!')
1351
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001352 call extend(args, #{row: 2, col: 2, cell: v:false})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001353 call test_gui_event('mouse', args)
1354 call feedkeys('', 'Lx!')
1355
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001356 call extend(args, #{row: 2, col: 1, cell: v:false})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001357 call test_gui_event('mouse', args)
1358 call feedkeys('', 'Lx!')
1359
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001360 call assert_equal([], g:eventlist)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001361
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001362 unlet g:eventlist
Ernie Raelc4cb5442022-04-03 15:47:28 +01001363 unmap <MouseMove>
1364 set mousemev&
1365endfunc
1366
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001367" Test for 'guitablabel' and 'guitabtooltip' options
1368func TestGuiTabLabel()
1369 call add(g:TabLabels, v:lnum + 100)
1370 let bufnrlist = tabpagebuflist(v:lnum)
1371 return bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
1372endfunc
1373
1374func TestGuiTabToolTip()
1375 call add(g:TabToolTips, v:lnum + 200)
1376 let bufnrlist = tabpagebuflist(v:lnum)
1377 return bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
1378endfunc
1379
1380func Test_gui_tablabel_tooltip()
1381 %bw!
1382 " Removing the tabline at the end of this test, reduces the window height by
1383 " one. Save and restore it after the test.
1384 let save_lines = &lines
1385 edit one
1386 set modified
1387 tabnew two
1388 set modified
1389 tabnew three
1390 set modified
1391 let g:TabLabels = []
1392 set guitablabel=%{TestGuiTabLabel()}
1393 call test_override('starting', 1)
1394 redrawtabline
1395 call test_override('starting', 0)
1396 call assert_true(index(g:TabLabels, 101) != -1)
1397 call assert_true(index(g:TabLabels, 102) != -1)
1398 call assert_true(index(g:TabLabels, 103) != -1)
1399 set guitablabel&
1400 unlet g:TabLabels
1401
1402 if has('gui_gtk')
1403 " Only on GTK+, the tooltip function is called even if the mouse is not
1404 " on the tabline. on Win32 and Motif, the tooltip function is called only
1405 " when the mouse pointer is over the tabline.
1406 let g:TabToolTips = []
1407 set guitabtooltip=%{TestGuiTabToolTip()}
1408 call test_override('starting', 1)
1409 redrawtabline
1410 call test_override('starting', 0)
1411 call assert_true(index(g:TabToolTips, 201) != -1)
1412 call assert_true(index(g:TabToolTips, 202) != -1)
1413 call assert_true(index(g:TabToolTips, 203) != -1)
1414 set guitabtooltip&
1415 unlet g:TabToolTips
1416 endif
1417 %bw!
1418 let &lines = save_lines
1419endfunc
1420
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001421" Test for dropping files into a window in GUI
1422func DropFilesInCmdLine()
1423 call feedkeys(":\"", 'L')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001424 let d = #{files: ['a.c', 'b.c'], row: &lines, col: 1, modifiers: 0}
1425 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001426 call feedkeys("\<CR>", 'L')
1427endfunc
1428
1429func Test_gui_drop_files()
Bram Moolenaar1d1ce612021-06-27 19:02:52 +02001430 CheckFeature drop_file
1431
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001432 %bw!
1433 %argdelete
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001434 let d = #{files: [], row: 1, col: 1, modifiers: 0}
1435 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001436 call assert_equal([], argv())
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001437 let d = #{files: [1, 2], row: 1, col: 1, modifiers: 0}
1438 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001439 call assert_equal([], argv())
1440
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001441 let d = #{files: ['a.c', 'b.c'], row: 1, col: 1, modifiers: 0}
1442 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001443 call assert_equal(['a.c', 'b.c'], argv())
1444 %bw!
1445 %argdelete
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001446 let d = #{files: [], row: 1, col: 1, modifiers: 0}
1447 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001448 call assert_equal([], argv())
1449 %bw!
1450 " if the buffer in the window is modified, then the file should be opened in
1451 " a new window
1452 set modified
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001453 let d = #{files: ['x.c', 'y.c'], row: 1, col: 1, modifiers: 0}
1454 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001455 call assert_equal(['x.c', 'y.c'], argv())
1456 call assert_equal(2, winnr('$'))
1457 call assert_equal('x.c', bufname(winbufnr(1)))
1458 %bw!
1459 %argdelete
1460 " if Ctrl is pressed, then the file should be opened in a new window
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001461 let d = #{files: ['s.py', 't.py'], row: 1, col: 1, modifiers: 0x10}
1462 eval 'dropfiles'->test_gui_event(d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001463 call assert_equal(['s.py', 't.py'], argv())
1464 call assert_equal(2, winnr('$'))
1465 call assert_equal('s.py', bufname(winbufnr(1)))
1466 %bw!
1467 %argdelete
1468 " drop the files in a non-current window
1469 belowright new
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001470 let d = #{files: ['a.py', 'b.py'], row: 1, col: 1, modifiers: 0}
1471 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001472 call assert_equal(['a.py', 'b.py'], argv())
1473 call assert_equal(2, winnr('$'))
1474 call assert_equal(1, winnr())
1475 call assert_equal('a.py', bufname(winbufnr(1)))
1476 %bw!
1477 %argdelete
1478 " pressing shift when dropping files should change directory
1479 let save_cwd = getcwd()
Bram Moolenaar572a4432022-09-28 21:07:03 +01001480 call mkdir('Xdropdir1', 'R')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001481 call writefile([], 'Xdropdir1/Xfile1')
1482 call writefile([], 'Xdropdir1/Xfile2')
1483 let d = #{files: ['Xdropdir1/Xfile1', 'Xdropdir1/Xfile2'], row: 1, col: 1,
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001484 \ modifiers: 0x4}
1485 call test_gui_event('dropfiles', d)
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001486 call assert_equal('Xdropdir1', fnamemodify(getcwd(), ':t'))
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001487 call assert_equal('Xfile1', @%)
1488 call chdir(save_cwd)
1489 " pressing shift when dropping directory and files should change directory
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001490 let d = #{files: ['Xdropdir1', 'Xdropdir1/Xfile2'], row: 1, col: 1, modifiers: 0x4}
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001491 call test_gui_event('dropfiles', d)
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001492 call assert_equal('Xdropdir1', fnamemodify(getcwd(), ':t'))
1493 call assert_equal('Xdropdir1', fnamemodify(@%, ':t'))
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001494 call chdir(save_cwd)
1495 %bw!
1496 %argdelete
1497 " dropping a directory should edit it
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001498 let d = #{files: ['Xdropdir1'], row: 1, col: 1, modifiers: 0}
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001499 call test_gui_event('dropfiles', d)
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001500 call assert_equal('Xdropdir1', @%)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001501 %bw!
1502 %argdelete
1503 " dropping only a directory name with Shift should ignore it
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001504 let d = #{files: ['Xdropdir1'], row: 1, col: 1, modifiers: 0x4}
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001505 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001506 call assert_equal('', @%)
1507 %bw!
1508 %argdelete
Bram Moolenaar00d4cee2022-08-28 13:38:43 +01001509
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001510 " drop files in the command line. The GUI drop files adds the file names to
1511 " the low level input buffer. So need to use a cmdline map and feedkeys()
1512 " with 'Lx!' to process it in this function itself.
Bram Moolenaar00d4cee2022-08-28 13:38:43 +01001513 " This sometimes fails, e.g. when using valgrind.
1514 let g:test_is_flaky = 1
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001515 cnoremap <expr> <buffer> <F4> DropFilesInCmdLine()
1516 call feedkeys(":\"\<F4>\<CR>", 'xt')
1517 call feedkeys('k', 'Lx!')
1518 call assert_equal('"a.c b.c', @:)
1519 cunmap <buffer> <F4>
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001520
1521 " Invalid arguments
1522 call assert_false(test_gui_event("dropfiles", {}))
1523 let d = #{row: 1, col: 1, modifiers: 0}
1524 call assert_false(test_gui_event("dropfiles", d))
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +00001525 let d = #{files: 1, row: 1, col: 1, modifiers: 0}
1526 call assert_false(test_gui_event("dropfiles", d))
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001527 let d = #{files: test_null_list(), row: 1, col: 1, modifiers: 0}
1528 call assert_false(test_gui_event("dropfiles", d))
1529 let d = #{files: [test_null_string()], row: 1, col: 1, modifiers: 0}
1530 call assert_true(test_gui_event("dropfiles", d))
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001531endfunc
1532
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001533" Test for generating a GUI tabline event to select a tab page
1534func Test_gui_tabline_event()
1535 %bw!
1536 edit Xfile1
1537 tabedit Xfile2
1538 tabedit Xfile3
1539
1540 tabfirst
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001541 call assert_equal(v:true, test_gui_event('tabline', #{tabnr: 2}))
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001542 call feedkeys("y", "Lx!")
1543 call assert_equal(2, tabpagenr())
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001544 call assert_equal(v:true, test_gui_event('tabline', #{tabnr: 3}))
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001545 call feedkeys("y", "Lx!")
1546 call assert_equal(3, tabpagenr())
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001547 call assert_equal(v:false, 'tabline'->test_gui_event(#{tabnr: 3}))
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001548
1549 " From the cmdline window, tabline event should not be handled
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001550 call feedkeys("q::let t = test_gui_event('tabline', #{tabnr: 2})\<CR>:q\<CR>", 'x!')
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001551 call assert_equal(v:false, t)
1552
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001553 " Invalid arguments
1554 call assert_false(test_gui_event('tabline', {}))
1555 call assert_false(test_gui_event('tabline', #{abc: 1}))
1556
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001557 %bw!
1558endfunc
1559
1560" Test for generating a GUI tabline menu event to execute an action
1561func Test_gui_tabmenu_event()
1562 %bw!
1563
1564 " Try to close the last tab page
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001565 call test_gui_event('tabmenu', #{tabnr: 1, item: 1})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001566 call feedkeys("y", "Lx!")
1567
1568 edit Xfile1
1569 tabedit Xfile2
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001570 call test_gui_event('tabmenu', #{tabnr: 1, item: 1})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001571 call feedkeys("y", "Lx!")
1572 call assert_equal(1, tabpagenr('$'))
1573 call assert_equal('Xfile2', bufname())
1574
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001575 eval 'tabmenu'->test_gui_event(#{tabnr: 1, item: 2})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001576 call feedkeys("y", "Lx!")
1577 call assert_equal(2, tabpagenr('$'))
1578
1579 " If tabnr is 0, then the current tabpage should be used.
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001580 call test_gui_event('tabmenu', #{tabnr: 0, item: 2})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001581 call feedkeys("y", "Lx!")
1582 call assert_equal(3, tabpagenr('$'))
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001583 call test_gui_event('tabmenu', #{tabnr: 0, item: 1})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001584 call feedkeys("y", "Lx!")
1585 call assert_equal(2, tabpagenr('$'))
1586
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001587 " Invalid arguments
1588 call assert_false(test_gui_event('tabmenu', {}))
1589 call assert_false(test_gui_event('tabmenu', #{tabnr: 1}))
1590 call assert_false(test_gui_event('tabmenu', #{item: 1}))
1591 call assert_false(test_gui_event('tabmenu', #{abc: 1}))
1592
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001593 %bw!
1594endfunc
1595
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001596" Test for find/replace text dialog event
1597func Test_gui_findrepl()
Yegappan Lakshmanan08238042022-03-07 16:57:22 +00001598 " Find/Replace dialog is supported only on GTK, Motif and MS-Windows.
1599 if !has('gui_gtk') && !has('gui_motif') && !has('gui_win32')
1600 return
1601 endif
1602
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001603 new
1604 call setline(1, ['one two one', 'Twoo One two oneo'])
1605
1606 " Replace all instances of a string with another
1607 let args = #{find_text: 'one', repl_text: 'ONE', flags: 0x4, forward: 1}
1608 call test_gui_event('findrepl', args)
1609 call assert_equal(['ONE two ONE', 'Twoo ONE two ONEo'], getline(1, '$'))
1610
1611 " Replace all instances of a whole string with another
1612 call cursor(1, 1)
1613 let args = #{find_text: 'two', repl_text: 'TWO', flags: 0xC, forward: 1}
1614 call test_gui_event('findrepl', args)
1615 call assert_equal(['ONE TWO ONE', 'Twoo ONE TWO ONEo'], getline(1, '$'))
1616
Dominique Pelle81b573d2022-03-22 21:14:55 +00001617 " Find next occurrence of a string (in a find dialog)
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001618 call cursor(1, 11)
1619 let args = #{find_text: 'TWO', repl_text: '', flags: 0x11, forward: 1}
1620 call test_gui_event('findrepl', args)
1621 call assert_equal([2, 10], [line('.'), col('.')])
1622
Dominique Pelle81b573d2022-03-22 21:14:55 +00001623 " Find previous occurrences of a string (in a find dialog)
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001624 call cursor(1, 11)
1625 let args = #{find_text: 'TWO', repl_text: '', flags: 0x11, forward: 0}
1626 call test_gui_event('findrepl', args)
1627 call assert_equal([1, 5], [line('.'), col('.')])
1628
Dominique Pelle81b573d2022-03-22 21:14:55 +00001629 " Find next occurrence of a string (in a replace dialog)
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001630 call cursor(1, 1)
1631 let args = #{find_text: 'Twoo', repl_text: '', flags: 0x2, forward: 1}
1632 call test_gui_event('findrepl', args)
1633 call assert_equal([2, 1], [line('.'), col('.')])
1634
Dominique Pelle81b573d2022-03-22 21:14:55 +00001635 " Replace only the next occurrence of a string (once)
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001636 call cursor(1, 5)
1637 let args = #{find_text: 'TWO', repl_text: 'two', flags: 0x3, forward: 1}
1638 call test_gui_event('findrepl', args)
1639 call assert_equal(['ONE two ONE', 'Twoo ONE TWO ONEo'], getline(1, '$'))
1640
1641 " Replace all instances of a whole string with another matching case
1642 call cursor(1, 1)
1643 let args = #{find_text: 'TWO', repl_text: 'two', flags: 0x1C, forward: 1}
1644 call test_gui_event('findrepl', args)
1645 call assert_equal(['ONE two ONE', 'Twoo ONE two ONEo'], getline(1, '$'))
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +00001646
matveyt2834ebd2022-09-06 17:00:15 +01001647 " Replace all instances with sub-replace specials
1648 call cursor(1, 1)
1649 let args = #{find_text: 'ONE', repl_text: '&~&', flags: 0x4, forward: 1}
1650 call test_gui_event('findrepl', args)
1651 call assert_equal(['&~& two &~&', 'Twoo &~& two &~&o'], getline(1, '$'))
1652
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +00001653 " Invalid arguments
1654 call assert_false(test_gui_event('findrepl', {}))
1655 let args = #{repl_text: 'a', flags: 1, forward: 1}
1656 call assert_false(test_gui_event('findrepl', args))
1657 let args = #{find_text: 'a', flags: 1, forward: 1}
1658 call assert_false(test_gui_event('findrepl', args))
1659 let args = #{find_text: 'a', repl_text: 'b', forward: 1}
1660 call assert_false(test_gui_event('findrepl', args))
1661 let args = #{find_text: 'a', repl_text: 'b', flags: 1}
1662 call assert_false(test_gui_event('findrepl', args))
1663
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001664 bw!
1665endfunc
1666
zeertzjqbad8a012022-04-29 16:44:00 +01001667func Test_gui_CTRL_SHIFT_V()
1668 call feedkeys(":let g:str = '\<*C-S-V>\<*C-S-I>\<*C-S-V>\<*C-S-@>'\<CR>", 'tx')
zeertzjq758a8d12022-04-29 11:06:34 +01001669 call assert_equal('<C-S-I><C-S-@>', g:str)
1670 unlet g:str
1671endfunc
1672
Bram Moolenaarde8be2b2022-06-16 14:45:41 +01001673func Test_gui_dialog_file()
K.Takatae68f1342022-09-07 13:01:11 +01001674 " make sure the file does not exist, otherwise a dialog makes Vim hang
1675 call delete('Xdialfile')
1676
Bram Moolenaarde8be2b2022-06-16 14:45:41 +01001677 let lines =<< trim END
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001678 file Xdialfile
Bram Moolenaarde8be2b2022-06-16 14:45:41 +01001679 normal axxx
1680 confirm qa
1681 END
K.Takatae68f1342022-09-07 13:01:11 +01001682 call writefile(lines, 'Xlines', 'D')
Bram Moolenaara2726242022-06-16 16:36:43 +01001683 let prefix = '!'
1684 if has('win32')
1685 let prefix = '!start '
1686 endif
1687 execute prefix .. GetVimCommand() .. ' -g -f --clean --gui-dialog-file Xdialog -S Xlines'
Bram Moolenaarde8be2b2022-06-16 14:45:41 +01001688
1689 call WaitForAssert({-> assert_true(filereadable('Xdialog'))})
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001690 call assert_match('Question: Save changes to "Xdialfile"?', readfile('Xdialog')->join('<NL>'))
Bram Moolenaarde8be2b2022-06-16 14:45:41 +01001691
1692 call delete('Xdialog')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001693 call delete('Xdialfile')
Bram Moolenaarde8be2b2022-06-16 14:45:41 +01001694endfunc
1695
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01001696" Test for sending low level key presses
1697func SendKeys(keylist)
1698 for k in a:keylist
Christopher Plewright20b795e2022-12-20 20:01:58 +00001699 call test_gui_event("key", #{event: "keydown", keycode: k})
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01001700 endfor
1701 for k in reverse(a:keylist)
Christopher Plewright20b795e2022-12-20 20:01:58 +00001702 call test_gui_event("key", #{event: "keyup", keycode: k})
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01001703 endfor
1704endfunc
1705
1706func Test_gui_lowlevel_keyevent()
1707 CheckMSWindows
1708 new
1709
1710 " Test for <Ctrl-A> to <Ctrl-Z> keys
Ken Takata490c1da2024-07-15 19:52:25 +02001711 " FIXME: <Ctrl-C> is excluded for now. It makes the test flaky.
1712 for kc in range(65, 66) + range(68, 90)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01001713 call SendKeys([0x11, kc])
Christian Brabandtf6ebaa72024-01-25 20:44:49 +01001714 try
1715 let ch = getcharstr()
1716 catch /^Vim:Interrupt$/
1717 let ch = "\<c-c>"
1718 endtry
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01001719 call assert_equal(nr2char(kc - 64), ch)
1720 endfor
1721
Christopher Plewright566f76e2023-01-10 13:43:04 +00001722 " Testing more extensive windows keyboard handling
1723 " is covered in test_mswin_event.vim
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01001724
1725 bw!
1726endfunc
1727
zeertzjq2cd0f272022-10-04 20:14:28 +01001728func Test_gui_macro_csi()
1729 " Test for issue #11270
1730 nnoremap <C-L> <Cmd>let g:triggered = 1<CR>
1731 let @q = "\x9b\xfc\x04L"
1732 norm @q
1733 call assert_equal(1, g:triggered)
1734 unlet g:triggered
1735 nunmap <C-L>
1736
1737 " Test for issue #11057
1738 inoremap <C-D>t bbb
1739 call setline(1, "\t")
1740 let @q = "i\x9b\xfc\x04D"
1741 " The end of :normal is like a mapping timing out
1742 norm @q
1743 call assert_equal('', getline(1))
1744 iunmap <C-D>t
1745endfunc
1746
zeertzjq1bed9932023-08-30 19:38:24 +02001747func Test_gui_csi_keytrans()
1748 call assert_equal('<C-L>', keytrans("\x9b\xfc\x04L"))
1749 call assert_equal('<C-D>', keytrans("\x9b\xfc\x04D"))
1750endfunc
1751
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001752" vim: shiftwidth=2 sts=2 expandtab