blob: 9fd840a53a85308ff41d9574a750d0579058f041 [file] [log] [blame]
Bram Moolenaar877e9572016-08-04 20:05:50 +02001" Tests specifically for the GUI
2
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02003CheckCanRunGui
Bram Moolenaar877e9572016-08-04 20:05:50 +02004
Christian Brabandteb380b92025-07-07 20:53:55 +02005source util/setup_gui.vim
Bram Moolenaar13c724f2017-02-05 20:54:26 +01006
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +01007func Setup()
8 call GUISetUpCommon()
Bram Moolenaar50fa8dd2016-08-09 22:58:21 +02009endfunc
10
11func TearDown()
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +010012 call GUITearDownCommon()
Bram Moolenaar877e9572016-08-04 20:05:50 +020013endfunc
14
15" Test for resetting "secure" flag after GUI has started.
Bram Moolenaar5d7ead32018-02-27 17:17:42 +010016" Must be run first, since it starts the GUI on Unix.
Bram Moolenaar877e9572016-08-04 20:05:50 +020017func Test_1_set_secure()
18 set exrc secure
19 gui -f
20 call assert_equal(1, has('gui_running'))
21endfunc
22
Bram Moolenaard5841f22017-03-05 13:27:25 +010023" As for non-GUI, a balloon_show() test was already added with patch 8.0.0401
24func Test_balloon_show()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020025 CheckFeature balloon_eval
26 " This won't do anything but must not crash either.
27 call balloon_show('hi!')
Bram Moolenaard5841f22017-03-05 13:27:25 +010028endfunc
29
Bram Moolenaar87748452017-03-12 17:10:33 +010030func Test_colorscheme()
Bram Moolenaaracc770a2020-04-12 15:11:06 +020031 call assert_equal('16777216', &t_Co)
32
Bram Moolenaar87748452017-03-12 17:10:33 +010033 let colorscheme_saved = exists('g:colors_name') ? g:colors_name : 'default'
Bram Moolenaar60a68362018-04-30 15:40:48 +020034 let g:color_count = 0
35 augroup TestColors
36 au!
Bram Moolenaar5d09a402022-08-31 21:17:10 +010037 au ColorScheme * let g:color_count += 1
38 \ | let g:after_colors = g:color_count
39 \ | let g:color_after = expand('<amatch>')
40 au ColorSchemePre * let g:color_count += 1
41 \ | let g:before_colors = g:color_count
42 \ | let g:color_pre = expand('<amatch>')
Bram Moolenaar60a68362018-04-30 15:40:48 +020043 augroup END
Bram Moolenaar87748452017-03-12 17:10:33 +010044
45 colorscheme torte
46 redraw!
Bram Moolenaar87748452017-03-12 17:10:33 +010047 call assert_equal('dark', &background)
Bram Moolenaar60a68362018-04-30 15:40:48 +020048 call assert_equal(1, g:before_colors)
49 call assert_equal(2, g:after_colors)
Bram Moolenaar5d09a402022-08-31 21:17:10 +010050 call assert_equal('torte', g:color_pre)
51 call assert_equal('torte', g:color_after)
Bram Moolenaar6d4470b2019-01-08 21:05:51 +010052 call assert_equal("\ntorte", execute('colorscheme'))
53
54 let a = substitute(execute('hi Search'), "\n\\s\\+", ' ', 'g')
Bram Moolenaar98feace2022-05-14 12:34:43 +010055 " FIXME: temporarily check less while the colorscheme changes
56 " call assert_match("\nSearch xxx term=reverse cterm=reverse ctermfg=196 ctermbg=16 gui=reverse guifg=#ff0000 guibg=#000000", a)
57 call assert_match("\nSearch xxx term=reverse ", a)
Bram Moolenaar6d4470b2019-01-08 21:05:51 +010058
59 call assert_fails('colorscheme does_not_exist', 'E185:')
Bram Moolenaar5d09a402022-08-31 21:17:10 +010060 call assert_equal('does_not_exist', g:color_pre)
61 call assert_equal('torte', g:color_after)
Bram Moolenaar87748452017-03-12 17:10:33 +010062
63 exec 'colorscheme' colorscheme_saved
Bram Moolenaar60a68362018-04-30 15:40:48 +020064 augroup TestColors
65 au!
66 augroup END
67 unlet g:color_count g:after_colors g:before_colors
Bram Moolenaar87748452017-03-12 17:10:33 +010068 redraw!
69endfunc
70
Bram Moolenaar6f785742017-02-06 22:11:55 +010071func Test_getfontname_with_arg()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +010072 CheckX11BasedGui
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010073
Bram Moolenaar0b962e52022-04-03 18:02:37 +010074 if has('gui_motif')
Bram Moolenaar6f785742017-02-06 22:11:55 +010075 " Invalid font name. The result should be an empty string.
76 call assert_equal('', getfontname('notexist'))
77
78 " Valid font name. This is usually the real name of 7x13 by default.
Bram Moolenaar87748452017-03-12 17:10:33 +010079 let fname = '-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1'
80 call assert_match(fname, getfontname(fname))
Bram Moolenaar6f785742017-02-06 22:11:55 +010081
82 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
83 " Invalid font name. The result should be the name plus the default size.
84 call assert_equal('notexist 10', getfontname('notexist'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010085 call assert_equal('', getfontname('*'))
Bram Moolenaar6f785742017-02-06 22:11:55 +010086
87 " Valid font name. This is usually the real name of Monospace by default.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010088 let fname = 'Bitstream Vera Sans Mono 12'
89 call assert_equal(fname, getfontname(fname))
90 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +010091endfunc
92
93func Test_getfontname_without_arg()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +010094 CheckX11BasedGui
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010095
96 let fname = getfontname()
97
Bram Moolenaar40bd5a12021-10-16 21:58:27 +010098 if has('gui_kde')
Bram Moolenaar6f785742017-02-06 22:11:55 +010099 " 'expected' is the value specified by SetUp() above.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100100 call assert_equal('Courier 10 Pitch/8/-1/5/50/0/0/0/0/0', fname)
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100101 elseif has('gui_motif')
Bram Moolenaar87748452017-03-12 17:10:33 +0100102 " 'expected' is DFLT_FONT of gui_x11.c or its real name.
103 let pat = '\(7x13\)\|\(\c-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1\)'
104 call assert_match(pat, fname)
Bram Moolenaar6f785742017-02-06 22:11:55 +0100105 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
matveytad3b6a32024-12-08 10:26:51 +0100106 " 'expected' is DEFAULT_FONT of gui_gtk_x11.c (any size)
107 call assert_match('^Monospace\>', fname)
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100108 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +0100109endfunc
110
Bram Moolenaar87748452017-03-12 17:10:33 +0100111func Test_getwinpos()
lilydjwg6e0a18f2024-01-29 20:54:28 +0100112 CheckX11
113
Bram Moolenaar87748452017-03-12 17:10:33 +0100114 call assert_match('Window position: X \d\+, Y \d\+', execute('winpos'))
115 call assert_true(getwinposx() >= 0)
116 call assert_true(getwinposy() >= 0)
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200117 call assert_equal([getwinposx(), getwinposy()], getwinpos())
Bram Moolenaar87748452017-03-12 17:10:33 +0100118endfunc
119
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100120func Test_quoteplus()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100121 CheckX11BasedGui
122
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100123 let g:test_is_flaky = 1
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100124
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100125 let quoteplus_saved = @+
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100126
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100127 let test_call = 'Can you hear me?'
128 let test_response = 'Yes, I can.'
Bram Moolenaarde8be2b2022-06-16 14:45:41 +0100129 let testee = 'VIMRUNTIME=' .. $VIMRUNTIME .. '; export VIMRUNTIME;'
130 \ .. GetVimCommand() .. ' --noplugin --not-a-term -c ''%s'''
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100131 " Ignore the "failed to create input context" error.
132 let cmd = 'call test_ignore_error("E285") | '
133 \ . 'gui -f | '
134 \ . 'call feedkeys("'
135 \ . '\"+p'
136 \ . ':s/' . test_call . '/' . test_response . '/\<CR>'
137 \ . '\"+yis'
138 \ . ':q!\<CR>", "tx")'
139 let run_vimtest = printf(testee, cmd)
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100140
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100141 " Set the quoteplus register to test_call, and another gvim will launched.
142 " Then, it first tries to paste the content of its own quotedplus register
143 " onto it. Second, it tries to substitute test_response for the pasted
144 " sentence. If the sentence is identical to test_call, the substitution
145 " should succeed. Third, it tries to yank the result of the substitution
146 " to its own quoteplus register, and last it quits. When system()
147 " returns, the content of the quoteplus register should be identical to
148 " test_response if those quoteplus registers are synchronized properly
149 " with/through the X11 clipboard.
150 let @+ = test_call
151 call system(run_vimtest)
152 call assert_equal(test_response, @+)
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100153
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100154 let @+ = quoteplus_saved
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100155endfunc
156
Bram Moolenaard68a0042021-10-20 23:08:11 +0100157func Test_gui_read_stdin()
158 CheckUnix
159
Bram Moolenaar572a4432022-09-28 21:07:03 +0100160 call writefile(['some', 'lines'], 'Xstdin', 'D')
Bram Moolenaard68a0042021-10-20 23:08:11 +0100161 let script =<< trim END
162 call writefile(getline(1, '$'), 'XstdinOK')
163 qa!
164 END
Bram Moolenaar572a4432022-09-28 21:07:03 +0100165 call writefile(script, 'Xscript', 'D')
Bram Moolenaard68a0042021-10-20 23:08:11 +0100166
167 " Cannot use --not-a-term here, the "reading from stdin" message would not be
168 " displayed.
Bram Moolenaarb5912e02022-05-31 17:03:14 +0100169 " However, when using XIM we might get E285, do use it then.
170 if has('xim')
171 let vimcmd = GetVimCommand()
172 else
173 let vimcmd = substitute(GetVimCommand(), '--not-a-term', '', '')
174 endif
Bram Moolenaard68a0042021-10-20 23:08:11 +0100175
176 call system('cat Xstdin | ' .. vimcmd .. ' -f -g -S Xscript -')
177 call assert_equal(['some', 'lines'], readfile('XstdinOK'))
178
Bram Moolenaard68a0042021-10-20 23:08:11 +0100179 call delete('XstdinOK')
Bram Moolenaard68a0042021-10-20 23:08:11 +0100180endfunc
181
Bram Moolenaar87748452017-03-12 17:10:33 +0100182func Test_set_background()
183 let background_saved = &background
184
185 set background&
186 call assert_equal('light', &background)
187
188 set background=dark
189 call assert_equal('dark', &background)
190
191 let &background = background_saved
192endfunc
193
Bram Moolenaard5841f22017-03-05 13:27:25 +0100194func Test_set_balloondelay()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200195 CheckOption balloondelay
Bram Moolenaard5841f22017-03-05 13:27:25 +0100196
197 let balloondelay_saved = &balloondelay
198
199 " Check if the default value is identical to that described in the manual.
200 set balloondelay&
201 call assert_equal(600, &balloondelay)
202
203 " Edge cases
204
205 " XXX This fact should be hidden so that people won't be tempted to write
206 " plugin/TimeMachine.vim. TODO Add reasonable range checks to the source
207 " code.
208 set balloondelay=-1
209 call assert_equal(-1, &balloondelay)
210
211 " Though it's possible to interpret the zero delay to be 'as soon as
212 " possible' or even 'indefinite', its actual meaning depends on the GUI
213 " toolkit in use after all.
214 set balloondelay=0
215 call assert_equal(0, &balloondelay)
216
217 set balloondelay=1
218 call assert_equal(1, &balloondelay)
219
220 " Since p_bdelay is of type long currently, the upper bound can be
221 " impractically huge and machine-dependent. Practically, it's sufficient
Bram Moolenaar0f9ea222017-03-05 13:48:13 +0100222 " to check if balloondelay works with 0x7fffffff (32 bits) for now.
223 set balloondelay=2147483647
224 call assert_equal(2147483647, &balloondelay)
Bram Moolenaard5841f22017-03-05 13:27:25 +0100225
226 let &balloondelay = balloondelay_saved
227endfunc
228
229func Test_set_ballooneval()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200230 CheckOption ballooneval
Bram Moolenaard5841f22017-03-05 13:27:25 +0100231
232 let ballooneval_saved = &ballooneval
233
234 set ballooneval&
235 call assert_equal(0, &ballooneval)
236
237 set ballooneval
238 call assert_notequal(0, &ballooneval)
239
240 set noballooneval
241 call assert_equal(0, &ballooneval)
242
243 let &ballooneval = ballooneval_saved
244endfunc
245
246func Test_set_balloonexpr()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200247 CheckOption balloonexpr
Bram Moolenaard5841f22017-03-05 13:27:25 +0100248
249 let balloonexpr_saved = &balloonexpr
250
251 " Default value
252 set balloonexpr&
253 call assert_equal('', &balloonexpr)
254
255 " User-defined function
256 new
257 func MyBalloonExpr()
258 return 'Cursor is at line ' . v:beval_lnum .
259 \', column ' . v:beval_col .
260 \ ' of file ' . bufname(v:beval_bufnr) .
261 \ ' on word "' . v:beval_text . '"' .
262 \ ' in window ' . v:beval_winid . ' (#' . v:beval_winnr . ')'
263 endfunc
264 setl balloonexpr=MyBalloonExpr()
265 setl ballooneval
266 call assert_equal('MyBalloonExpr()', &balloonexpr)
267 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200268 " and check if the content of the balloon is the same as what is expected.
Bram Moolenaard5841f22017-03-05 13:27:25 +0100269 " Also, check if textlock works as expected.
270 setl balloonexpr&
271 call assert_equal('', &balloonexpr)
272 delfunc MyBalloonExpr
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +0000273
274 " Using a script-local function
275 func s:NewBalloonExpr()
276 endfunc
277 set balloonexpr=s:NewBalloonExpr()
278 call assert_equal(expand('<SID>') .. 'NewBalloonExpr()', &balloonexpr)
279 set balloonexpr=<SID>NewBalloonExpr()
280 call assert_equal(expand('<SID>') .. 'NewBalloonExpr()', &balloonexpr)
281 delfunc s:NewBalloonExpr
Bram Moolenaard5841f22017-03-05 13:27:25 +0100282 bwipe!
283
284 " Multiline support
285 if has('balloon_multiline')
286 " Multiline balloon using NL
287 new
288 func MyBalloonFuncForMultilineUsingNL()
Dominique Pelle81b573d2022-03-22 21:14:55 +0000289 return "Multiline\nSupported\nBalloon\nusing NL"
Bram Moolenaard5841f22017-03-05 13:27:25 +0100290 endfunc
291 setl balloonexpr=MyBalloonFuncForMultilineUsingNL()
292 setl ballooneval
293 call assert_equal('MyBalloonFuncForMultilineUsingNL()', &balloonexpr)
294 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200295 " and check if the content of the balloon is the same as what is
Bram Moolenaard5841f22017-03-05 13:27:25 +0100296 " expected. Also, check if textlock works as expected.
297 setl balloonexpr&
298 delfunc MyBalloonFuncForMultilineUsingNL
299 bwipe!
300
301 " Multiline balloon using List
302 new
303 func MyBalloonFuncForMultilineUsingList()
Dominique Pelle81b573d2022-03-22 21:14:55 +0000304 return [ 'Multiline', 'Supported', 'Balloon', 'using List' ]
Bram Moolenaard5841f22017-03-05 13:27:25 +0100305 endfunc
306 setl balloonexpr=MyBalloonFuncForMultilineUsingList()
307 setl ballooneval
308 call assert_equal('MyBalloonFuncForMultilineUsingList()', &balloonexpr)
309 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200310 " and check if the content of the balloon is the same as what is
Bram Moolenaard5841f22017-03-05 13:27:25 +0100311 " expected. Also, check if textlock works as expected.
312 setl balloonexpr&
313 delfunc MyBalloonFuncForMultilineUsingList
314 bwipe!
315 endif
316
317 let &balloonexpr = balloonexpr_saved
318endfunc
319
Bram Moolenaar87748452017-03-12 17:10:33 +0100320" Invalid arguments are tested with test_options in conjunction with segfaults
321" caused by them (Patch 8.0.0357, 24922ec233).
322func Test_set_guicursor()
323 let guicursor_saved = &guicursor
324
325 let default = [
326 \ "n-v-c:block-Cursor/lCursor",
327 \ "ve:ver35-Cursor",
328 \ "o:hor50-Cursor",
329 \ "i-ci:ver25-Cursor/lCursor",
330 \ "r-cr:hor20-Cursor/lCursor",
331 \ "sm:block-Cursor-blinkwait175-blinkoff150-blinkon175"
332 \ ]
333
334 " Default Value
335 set guicursor&
336 call assert_equal(join(default, ','), &guicursor)
337
338 " Argument List Example 1
339 let opt_list = copy(default)
340 let opt_list[0] = "n-c-v:block-nCursor"
341 exec "set guicursor=" . join(opt_list, ',')
342 call assert_equal(join(opt_list, ','), &guicursor)
343 unlet opt_list
344
345 " Argument List Example 2
346 let opt_list = copy(default)
347 let opt_list[3] = "i-ci:ver30-iCursor-blinkwait300-blinkon200-blinkoff150"
348 exec "set guicursor=" . join(opt_list, ',')
349 call assert_equal(join(opt_list, ','), &guicursor)
350 unlet opt_list
351
352 " 'a' Mode
353 set guicursor&
354 let &guicursor .= ',a:blinkon0'
355 call assert_equal(join(default, ',') . ",a:blinkon0", &guicursor)
356
357 let &guicursor = guicursor_saved
358endfunc
359
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100360func Test_set_guifont_errors()
361 if has('win32')
362 " Invalid font names are accepted in GTK GUI
363 call assert_fails('set guifont=xa1bc23d7f', 'E596:')
364 endif
365
366 " This only works if 'renderoptions' exists and does not work for Windows XP
367 " and older.
368 if exists('+renderoptions') && windowsversion() !~ '^[345]\.'
369 " doing this four times used to cause a crash
370 set renderoptions=type:directx
371 for i in range(5)
372 set guifont=
373 endfor
374 set renderoptions=
375 for i in range(5)
376 set guifont=
377 endfor
378 endif
379endfunc
380
Bram Moolenaar43dded82017-02-09 16:06:17 +0100381func Test_set_guifont()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100382 CheckX11BasedGui
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100383
384 let guifont_saved = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100385 if has('xfontset')
386 " Prevent 'guifontset' from canceling 'guifont'.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100387 let guifontset_saved = &guifontset
Bram Moolenaar43dded82017-02-09 16:06:17 +0100388 set guifontset=
389 endif
390
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100391 if has('gui_motif')
Bram Moolenaar43dded82017-02-09 16:06:17 +0100392 " Non-empty font list with invalid font names.
393 "
394 " This test is twofold: (1) It checks if the command fails as expected
395 " when there are no loadable fonts found in the list. (2) It checks if
396 " 'guifont' remains the same after the command loads none of the fonts
397 " listed.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100398 let flist = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100399 call assert_fails('set guifont=-notexist1-*,-notexist2-*')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100400 call assert_equal(flist, &guifont)
Bram Moolenaar43dded82017-02-09 16:06:17 +0100401
402 " Non-empty font list with a valid font name. Should pick up the first
403 " valid font.
404 set guifont=-notexist1-*,fixed,-notexist2-*
Bram Moolenaar87748452017-03-12 17:10:33 +0100405 let pat = '\(fixed\)\|\(\c-Misc-Fixed-Medium-R-SemiCondensed--13-120-75-75-C-60-ISO8859-1\)'
406 call assert_match(pat, getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100407
408 " Empty list. Should fallback to the built-in default.
409 set guifont=
Bram Moolenaar87748452017-03-12 17:10:33 +0100410 let pat = '\(7x13\)\|\(\c-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1\)'
411 call assert_match(pat, getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100412
413 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
414 " For GTK, what we refer to as 'font names' in our manual are actually
415 " 'initial font patterns'. A valid font which matches the 'canonical font
416 " pattern' constructed from a given 'initial pattern' is to be looked up
417 " and loaded. That explains why the GTK GUIs appear to accept 'invalid
418 " font names'.
419 "
420 " Non-empty list. Should always pick up the first element, no matter how
421 " strange it is, as explained above.
422 set guifont=(´・ω・`)\ 12,Courier\ 12
423 call assert_equal('(´・ω・`) 12', getfontname())
424
425 " Empty list. Should fallback to the built-in default.
426 set guifont=
matveytad3b6a32024-12-08 10:26:51 +0100427 call assert_match('^Monospace\>', getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100428 endif
429
430 if has('xfontset')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100431 let &guifontset = guifontset_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100432 endif
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100433 let &guifont = guifont_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100434endfunc
435
Bram Moolenaar10434672017-02-12 19:59:08 +0100436func Test_set_guifontset()
Bram Moolenaarce90e362019-09-08 18:58:44 +0200437 CheckFeature xfontset
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100438 let skipped = ''
Bram Moolenaar10434672017-02-12 19:59:08 +0100439
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200440 call assert_fails('set guifontset=*', 'E597:')
441
Bram Moolenaarce90e362019-09-08 18:58:44 +0200442 let ctype_saved = v:ctype
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100443
Bram Moolenaarce90e362019-09-08 18:58:44 +0200444 " First, since XCreateFontSet(3) is very sensitive to locale, fonts must
445 " be chosen meticulously.
446 let font_head = '-misc-fixed-medium-r-normal--14'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100447
Bram Moolenaarce90e362019-09-08 18:58:44 +0200448 let font_aw70 = font_head . '-130-75-75-c-70'
449 let font_aw140 = font_head . '-130-75-75-c-140'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100450
Bram Moolenaarce90e362019-09-08 18:58:44 +0200451 let font_jisx0201 = font_aw70 . '-jisx0201.1976-0'
452 let font_jisx0208 = font_aw140 . '-jisx0208.1983-0'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100453
Bram Moolenaarce90e362019-09-08 18:58:44 +0200454 let full_XLFDs = join([ font_jisx0208, font_jisx0201 ], ',')
455 let short_XLFDs = join([ font_aw140, font_aw70 ], ',')
456 let singleton = font_head . '-*'
457 let aliases = 'k14,r14'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100458
Bram Moolenaarce90e362019-09-08 18:58:44 +0200459 " Second, among 'locales', look up such a locale that gets 'set
460 " guifontset=' to work successfully with every fontset specified with
461 " 'fontsets'.
462 let locales = [ 'ja_JP.UTF-8', 'ja_JP.eucJP', 'ja_JP.SJIS' ]
463 let fontsets = [ full_XLFDs, short_XLFDs, singleton, aliases ]
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100464
Bram Moolenaarce90e362019-09-08 18:58:44 +0200465 let feasible = 0
466 for locale in locales
467 try
468 exec 'language ctype' locale
469 catch /^Vim\%((\a\+)\)\=:E197/
470 continue
471 endtry
472 let done = 0
473 for fontset in fontsets
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100474 try
Bram Moolenaarce90e362019-09-08 18:58:44 +0200475 exec 'set guifontset=' . fontset
476 catch /^Vim\%((\a\+)\)\=:E\%(250\|252\|234\|597\|598\)/
477 break
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100478 endtry
Bram Moolenaarce90e362019-09-08 18:58:44 +0200479 let done += 1
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100480 endfor
Bram Moolenaarce90e362019-09-08 18:58:44 +0200481 if done == len(fontsets)
482 let feasible = 1
483 break
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100484 endif
Bram Moolenaarce90e362019-09-08 18:58:44 +0200485 endfor
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100486
Bram Moolenaarce90e362019-09-08 18:58:44 +0200487 " Third, give a set of tests if it is found feasible.
488 if !feasible
489 let skipped = g:not_hosted
490 else
491 " N.B. 'v:ctype' has already been set to an appropriate value in the
492 " previous loop.
493 for fontset in fontsets
494 exec 'set guifontset=' . fontset
495 call assert_equal(fontset, &guifontset)
496 endfor
Bram Moolenaar10434672017-02-12 19:59:08 +0100497 endif
498
Bram Moolenaarce90e362019-09-08 18:58:44 +0200499 " Finally, restore ctype.
500 exec 'language ctype' ctype_saved
501
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100502 if !empty(skipped)
503 throw skipped
Bram Moolenaar10434672017-02-12 19:59:08 +0100504 endif
505endfunc
506
507func Test_set_guifontwide()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100508 CheckX11BasedGui
Bram Moolenaar10434672017-02-12 19:59:08 +0100509
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100510 call assert_fails('set guifontwide=*', 'E533:')
511
512 if has('gui_gtk')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100513 let guifont_saved = &guifont
514 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100515
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100516 let fc_match = exepath('fc-match')
517 if empty(fc_match)
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100518 let skipped = g:not_hosted
Bram Moolenaar10434672017-02-12 19:59:08 +0100519 else
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100520 let &guifont = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=en')
521 let wide = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=ja')
522 exec 'set guifontwide=' . fnameescape(wide)
523 call assert_equal(wide, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100524 endif
525
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100526 let &guifontwide = guifontwide_saved
527 let &guifont = guifont_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100528
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100529 elseif has('gui_motif')
Bram Moolenaar10434672017-02-12 19:59:08 +0100530 " guifontwide is premised upon the xfontset feature.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100531 if !has('xfontset')
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100532 let skipped = g:not_supported . 'xfontset'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100533 else
534 let encoding_saved = &encoding
535 let guifont_saved = &guifont
536 let guifontset_saved = &guifontset
537 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100538
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100539 let nfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-90-iso10646-1'
540 let wfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-180-iso10646-1'
Bram Moolenaar10434672017-02-12 19:59:08 +0100541
542 set encoding=utf-8
543
544 " Case 1: guifontset is empty
545 set guifontset=
546
547 " Case 1-1: Automatic selection
548 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100549 exec 'set guifont=' . nfont
550 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100551
552 " Case 1-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100553 exec 'set guifontwide=' . wfont
554 exec 'set guifont=' . nfont
555 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100556
557 " Case 2: guifontset is invalid
558 try
559 set guifontset=-*-notexist-*
Bram Moolenaar37175402017-03-18 20:18:45 +0100560 call assert_report("'set guifontset=-*-notexist-*' should have failed")
Bram Moolenaar10434672017-02-12 19:59:08 +0100561 catch
Bram Moolenaare2e40752020-09-04 21:18:46 +0200562 call assert_exception('E598:')
Bram Moolenaar10434672017-02-12 19:59:08 +0100563 endtry
Bram Moolenaar10434672017-02-12 19:59:08 +0100564
565 " Case 2-1: Automatic selection
566 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100567 exec 'set guifont=' . nfont
568 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100569
570 " Case 2-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100571 exec 'set guifontwide=' . wfont
572 exec 'set guifont=' . nfont
573 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100574
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100575 let &guifontwide = guifontwide_saved
576 let &guifontset = guifontset_saved
577 let &guifont = guifont_saved
578 let &encoding = encoding_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100579 endif
Bram Moolenaar10434672017-02-12 19:59:08 +0100580 endif
Bram Moolenaar10434672017-02-12 19:59:08 +0100581endfunc
582
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200583func Test_expand_guifont()
584 if has('gui_win32')
585 let guifont_saved = &guifont
586 let guifontwide_saved = &guifontwide
587
588 " Test recalling existing option, and suggesting current font size
589 set guifont=Courier\ New:h11:cANSI
590 call assert_equal('Courier\ New:h11:cANSI', getcompletion('set guifont=', 'cmdline')[0])
591 call assert_equal('h11', getcompletion('set guifont=Lucida\ Console:', 'cmdline')[0])
592
593 " Test auto-completion working for font names
594 call assert_equal(['Courier\ New'], getcompletion('set guifont=Couri*ew$', 'cmdline'))
595 call assert_equal(['Courier\ New'], getcompletion('set guifontwide=Couri*ew$', 'cmdline'))
596
597 " Make sure non-monospace fonts are filtered out
598 call assert_equal([], getcompletion('set guifont=Arial', 'cmdline'))
599 call assert_equal([], getcompletion('set guifontwide=Arial', 'cmdline'))
600
601 " Test auto-completion working for font options
602 call assert_notequal(-1, index(getcompletion('set guifont=Courier\ New:', 'cmdline'), 'b'))
603 call assert_equal(['cDEFAULT'], getcompletion('set guifont=Courier\ New:cD*T', 'cmdline'))
604 call assert_equal(['qCLEARTYPE'], getcompletion('set guifont=Courier\ New:qC*TYPE', 'cmdline'))
605
606 let &guifontwide = guifontwide_saved
607 let &guifont = guifont_saved
608 elseif has('gui_gtk')
609 let guifont_saved = &guifont
610 let guifontwide_saved = &guifontwide
611
612 " Test recalling default and existing option
613 set guifont=
matveytad3b6a32024-12-08 10:26:51 +0100614 call assert_match('^Monospace\>', getcompletion('set guifont=', 'cmdline')[0])
Yee Cheng Chin290b8872023-10-05 20:54:21 +0200615 set guifont=Monospace\ 9
616 call assert_equal('Monospace\ 9', getcompletion('set guifont=', 'cmdline')[0])
617
618 " Test auto-completion working for font names
619 call assert_equal(['Monospace'], getcompletion('set guifont=Mono*pace$', 'cmdline'))
620 call assert_equal(['Monospace'], getcompletion('set guifontwide=Mono*pace$', 'cmdline'))
621
622 " Make sure non-monospace fonts are filtered out only in 'guifont'
623 call assert_equal([], getcompletion('set guifont=Sans$', 'cmdline'))
624 call assert_equal(['Sans'], getcompletion('set guifontwide=Sans$', 'cmdline'))
625
626 let &guifontwide = guifontwide_saved
627 let &guifont = guifont_saved
628 else
629 call assert_equal([], getcompletion('set guifont=', 'cmdline'))
630 endif
631endfunc
632
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100633func Test_set_guiligatures()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100634 CheckX11BasedGui
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100635
Erik S. V. Jansson8b1e7492024-02-24 14:26:52 +0100636 if has('gui_gtk') || has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3') || has('win32')
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100637 " Try correct value
638 set guiligatures=<>=ab
639 call assert_equal("<>=ab", &guiligatures)
640 " Try to throw error
641 try
642 set guiligatures=<>=šab
643 call assert_report("'set guiligatures=<>=šab should have failed")
644 catch
645 call assert_exception('E1243:')
646 endtry
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100647 endif
648endfunc
649
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100650func Test_set_guiheadroom()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100651 CheckX11BasedGui
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100652
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100653 " Since this script is to be read together with '-U NONE', the default
654 " value must be preserved.
655 call assert_equal(50, &guiheadroom)
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100656endfunc
657
Bram Moolenaar87748452017-03-12 17:10:33 +0100658func Test_set_guioptions()
659 let guioptions_saved = &guioptions
660 let duration = '200m'
661
662 if has('win32')
663 " Default Value
664 set guioptions&
665 call assert_equal('egmrLtT', &guioptions)
666
667 else
668 " Default Value
669 set guioptions&
670 call assert_equal('aegimrLtT', &guioptions)
671
672 " To activate scrollbars of type 'L' or 'R'.
673 wincmd v
674 redraw!
675
676 " Remove all default GUI ornaments
677 set guioptions-=T
678 exec 'sleep' . duration
679 call assert_equal('aegimrLt', &guioptions)
680 set guioptions-=t
681 exec 'sleep' . duration
682 call assert_equal('aegimrL', &guioptions)
683 set guioptions-=L
684 exec 'sleep' . duration
685 call assert_equal('aegimr', &guioptions)
686 set guioptions-=r
687 exec 'sleep' . duration
688 call assert_equal('aegim', &guioptions)
689 set guioptions-=m
690 exec 'sleep' . duration
691 call assert_equal('aegi', &guioptions)
692
693 " Try non-default GUI ornaments
694 set guioptions+=l
695 exec 'sleep' . duration
696 call assert_equal('aegil', &guioptions)
697 set guioptions-=l
698 exec 'sleep' . duration
699 call assert_equal('aegi', &guioptions)
700
701 set guioptions+=R
702 exec 'sleep' . duration
703 call assert_equal('aegiR', &guioptions)
704 set guioptions-=R
705 exec 'sleep' . duration
706 call assert_equal('aegi', &guioptions)
707
708 set guioptions+=b
709 exec 'sleep' . duration
710 call assert_equal('aegib', &guioptions)
711 set guioptions+=h
712 exec 'sleep' . duration
713 call assert_equal('aegibh', &guioptions)
714 set guioptions-=h
715 exec 'sleep' . duration
716 call assert_equal('aegib', &guioptions)
717 set guioptions-=b
718 exec 'sleep' . duration
719 call assert_equal('aegi', &guioptions)
720
721 set guioptions+=v
722 exec 'sleep' . duration
723 call assert_equal('aegiv', &guioptions)
724 set guioptions-=v
725 exec 'sleep' . duration
726 call assert_equal('aegi', &guioptions)
727
728 if has('gui_motif')
729 set guioptions+=F
730 exec 'sleep' . duration
731 call assert_equal('aegiF', &guioptions)
732 set guioptions-=F
733 exec 'sleep' . duration
734 call assert_equal('aegi', &guioptions)
735 endif
736
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +0200737 if has('gui_gtk3')
738 set guioptions+=d
739 exec 'sleep' . duration
740 call assert_equal('aegid', &guioptions)
741 set guioptions-=d
742 exec 'sleep' . duration
743 call assert_equal('aegi', &guioptions)
744 endif
745
Bram Moolenaar87748452017-03-12 17:10:33 +0100746 " Restore GUI ornaments to the default state.
747 set guioptions+=m
748 exec 'sleep' . duration
749 call assert_equal('aegim', &guioptions)
750 set guioptions+=r
751 exec 'sleep' . duration
752 call assert_equal('aegimr', &guioptions)
753 set guioptions+=L
754 exec 'sleep' . duration
755 call assert_equal('aegimrL', &guioptions)
756 set guioptions+=t
757 exec 'sleep' . duration
758 call assert_equal('aegimrLt', &guioptions)
759 set guioptions+=T
760 exec 'sleep' . duration
761 call assert_equal("aegimrLtT", &guioptions)
762
763 wincmd o
764 redraw!
765 endif
766
767 let &guioptions = guioptions_saved
768endfunc
769
Bram Moolenaarab186732018-09-14 21:27:06 +0200770func Test_scrollbars()
Bram Moolenaarab186732018-09-14 21:27:06 +0200771 " buffer with 200 lines
772 call setline(1, repeat(['one', 'two'], 100))
Drew Vogelea67ba72025-05-07 22:05:17 +0200773 set scrolloff=0
774 set guioptions=rlbk
Bram Moolenaarab186732018-09-14 21:27:06 +0200775
776 " scroll to move line 11 at top, moves the cursor there
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +0000777 let args = #{which: 'left', value: 10, dragging: 0}
778 call test_gui_event('scrollbar', args)
Bram Moolenaarab186732018-09-14 21:27:06 +0200779 redraw
780 call assert_equal(1, winline())
781 call assert_equal(11, line('.'))
782
Bram Moolenaar74ccbb12023-05-18 20:07:12 +0100783 " FIXME: This test should also pass with Motif and 24 lines
784 if &lines > 24 || !has('gui_motif')
785 " scroll to move line 1 at top, cursor stays in line 11
786 let args = #{which: 'right', value: 0, dragging: 0}
787 call test_gui_event('scrollbar', args)
788 redraw
789 call assert_equal(11, winline())
790 call assert_equal(11, line('.'))
791 endif
Bram Moolenaarab186732018-09-14 21:27:06 +0200792
793 set nowrap
794 call setline(11, repeat('x', 150))
795 redraw
796 call assert_equal(1, wincol())
Bram Moolenaarf6d50f12019-06-06 15:40:08 +0200797 set number
798 redraw
799 call assert_equal(5, wincol())
800 set nonumber
801 redraw
Bram Moolenaarab186732018-09-14 21:27:06 +0200802 call assert_equal(1, col('.'))
803
804 " scroll to character 11, cursor is moved
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +0000805 let args = #{which: 'hor', value: 10, dragging: 0}
806 call test_gui_event('scrollbar', args)
Bram Moolenaarab186732018-09-14 21:27:06 +0200807 redraw
808 call assert_equal(1, wincol())
Bram Moolenaarf6d50f12019-06-06 15:40:08 +0200809 set number
810 redraw
811 call assert_equal(5, wincol())
812 set nonumber
813 redraw
Bram Moolenaarab186732018-09-14 21:27:06 +0200814 call assert_equal(11, col('.'))
815
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +0000816 " Invalid arguments
817 call assert_false(test_gui_event('scrollbar', {}))
818 call assert_false(test_gui_event('scrollbar', #{value: 10, dragging: 0}))
819 call assert_false(test_gui_event('scrollbar', #{which: 'hor', dragging: 0}))
820 call assert_false(test_gui_event('scrollbar', #{which: 'hor', value: 1}))
821 call assert_fails("call test_gui_event('scrollbar', #{which: 'a', value: 1, dragging: 0})", 'E475:')
822
Bram Moolenaarab186732018-09-14 21:27:06 +0200823 set guioptions&
Drew Vogelea67ba72025-05-07 22:05:17 +0200824 set scrolloff&
Bram Moolenaarab186732018-09-14 21:27:06 +0200825 set wrap&
826 bwipe!
827endfunc
828
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100829func Test_menu()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100830 CheckFeature quickfix
831
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100832 " Check Help menu exists
833 let help_menu = execute('menu Help')
834 call assert_match('Overview', help_menu)
835
836 " Check Help menu works
837 emenu Help.Overview
838 call assert_equal('help', &buftype)
839 close
840
841 " Check deleting menu doesn't cause trouble.
842 aunmenu Help
Bram Moolenaarb45cd362020-09-28 21:41:49 +0200843 if exists(':tlmenu')
844 tlunmenu Help
845 endif
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100846 call assert_fails('menu Help', 'E329:')
847endfunc
848
Bram Moolenaar87748452017-03-12 17:10:33 +0100849func Test_set_guipty()
850 let guipty_saved = &guipty
851
852 " Default Value
853 set guipty&
854 call assert_equal(1, &guipty)
855
856 set noguipty
857 call assert_equal(0, &guipty)
858
859 let &guipty = guipty_saved
Bram Moolenaar6f785742017-02-06 22:11:55 +0100860endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100861
Bram Moolenaarc701f322019-03-28 21:49:21 +0100862func Test_encoding_conversion()
863 " GTK supports conversion between 'encoding' and "utf-8"
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200864 CheckFeature gui_gtk
865 let encoding_saved = &encoding
866 set encoding=latin1
Bram Moolenaarc701f322019-03-28 21:49:21 +0100867
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200868 " would be nice if we could take a screenshot
869 intro
870 " sets the window title
871 edit SomeFile
Bram Moolenaarc701f322019-03-28 21:49:21 +0100872
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200873 let &encoding = encoding_saved
Bram Moolenaarc701f322019-03-28 21:49:21 +0100874endfunc
875
Bram Moolenaar877e9572016-08-04 20:05:50 +0200876func Test_shell_command()
877 new
Bram Moolenaar9d5b8762016-08-04 21:21:13 +0200878 r !echo hello
879 call assert_equal('hello', substitute(getline(2), '\W', '', 'g'))
Bram Moolenaar877e9572016-08-04 20:05:50 +0200880 bwipe!
Bram Moolenaar877e9572016-08-04 20:05:50 +0200881endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100882
Bram Moolenaar87748452017-03-12 17:10:33 +0100883func Test_syntax_colortest()
884 runtime syntax/colortest.vim
885 redraw!
886 sleep 200m
887 bwipe!
888endfunc
889
890func Test_set_term()
891 " It's enough to check the current value since setting 'term' to anything
892 " other than builtin_gui makes no sense at all.
893 call assert_equal('builtin_gui', &term)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200894 call assert_fails('set term=xterm', 'E530:')
Bram Moolenaar87748452017-03-12 17:10:33 +0100895endfunc
896
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100897func Test_windowid_variable()
Christian Brabandte5bc2e42024-06-01 20:55:09 +0200898 if g:x11_based_gui || has('win32')
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100899 call assert_true(v:windowid > 0)
900 else
901 call assert_equal(0, v:windowid)
902 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +0100903endfunc
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200904
905" Test "vim -g" and also the GUIEnter autocommand.
906func Test_gui_dash_g()
907 let cmd = GetVimCommand('Xscriptgui')
Bram Moolenaar572a4432022-09-28 21:07:03 +0100908 call writefile([""], "Xtestgui", 'D')
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200909 let lines =<< trim END
910 au GUIEnter * call writefile(["insertmode: " . &insertmode], "Xtestgui")
911 au GUIEnter * qall
912 END
Bram Moolenaar572a4432022-09-28 21:07:03 +0100913 call writefile(lines, 'Xscriptgui', 'D')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200914 call system(cmd . ' -g')
915 call WaitForAssert({-> assert_equal(['insertmode: 0'], readfile('Xtestgui'))})
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200916endfunc
917
918" Test "vim -7" and also the GUIEnter autocommand.
919func Test_gui_dash_y()
920 let cmd = GetVimCommand('Xscriptgui')
Bram Moolenaar572a4432022-09-28 21:07:03 +0100921 call writefile([""], "Xtestgui", 'D')
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200922 let lines =<< trim END
923 au GUIEnter * call writefile(["insertmode: " . &insertmode], "Xtestgui")
924 au GUIEnter * qall
925 END
Bram Moolenaar572a4432022-09-28 21:07:03 +0100926 call writefile(lines, 'Xscriptgui', 'D')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200927 call system(cmd . ' -y')
928 call WaitForAssert({-> assert_equal(['insertmode: 1'], readfile('Xtestgui'))})
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200929endfunc
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100930
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200931" Test for "!" option in 'guioptions'. Use a terminal for running external
932" commands
933func Test_gui_run_cmd_in_terminal()
Bram Moolenaar4457e1d2020-05-15 22:46:44 +0200934 CheckFeature terminal
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200935 let save_guioptions = &guioptions
936 set guioptions+=!
937 if has('win32')
938 let cmd = 'type'
939 else
940 " assume all the other systems have a cat command
941 let cmd = 'cat'
942 endif
Bram Moolenaar98f16712020-05-22 13:34:01 +0200943 exe "silent !" . cmd . " test_gui.vim"
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200944 " TODO: how to check that the command ran in a separate terminal?
945 " Maybe check for $TERM (dumb vs xterm) in the spawned shell?
946 let &guioptions = save_guioptions
947endfunc
948
Bram Moolenaar46cd43b2020-06-04 22:22:11 +0200949func Test_gui_recursive_mapping()
950 nmap ' <C-W>
951 nmap <C-W>a :let didit = 1<CR>
952 call feedkeys("'a", 'xt')
953 call assert_equal(1, didit)
954
955 nunmap '
956 nunmap <C-W>a
957endfunc
958
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200959" Test GUI mouse events
960func Test_gui_mouse_event()
Bram Moolenaarc13e9982022-11-28 21:20:48 +0000961 " Low level input isn't 100% reliable
962 let g:test_is_flaky = 1
963
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200964 set mousemodel=extend
965 call test_override('no_query_mouse', 1)
966 new
967 call setline(1, ['one two three', 'four five six'])
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200968 call cursor(1, 1)
Bram Moolenaarc13e9982022-11-28 21:20:48 +0000969 redraw!
970
971 " place the cursor using left click and release in normal mode
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000972 let args = #{button: 0, row: 2, col: 4, multiclick: 0, modifiers: 0}
973 call test_gui_event('mouse', args)
974 let args.button = 3
975 eval 'mouse'->test_gui_event(args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200976 call feedkeys("\<Esc>", 'Lx!')
977 call assert_equal([0, 2, 4, 0], getpos('.'))
978
979 " select and yank a word
980 let @" = ''
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000981 let args = #{button: 0, row: 1, col: 9, multiclick: 0, modifiers: 0}
982 call test_gui_event('mouse', args)
983 let args.multiclick = 1
984 call test_gui_event('mouse', args)
985 let args.button = 3
986 let args.multiclick = 0
987 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200988 call feedkeys("y", 'Lx!')
989 call assert_equal('three', @")
990
991 " create visual selection using right click
992 let @" = ''
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000993 let args = #{button: 0, row: 2, col: 6, multiclick: 0, modifiers: 0}
994 call test_gui_event('mouse', args)
995 let args.button = 3
996 call test_gui_event('mouse', args)
997 let args = #{button: 2, row: 2, col: 13, multiclick: 0, modifiers: 0}
998 call test_gui_event('mouse', args)
999 let args.button = 3
1000 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001001 call feedkeys("y", 'Lx!')
1002 call assert_equal('five six', @")
1003
1004 " paste using middle mouse button
1005 let @* = 'abc '
1006 call feedkeys('""', 'Lx!')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001007 let args = #{button: 1, row: 1, col: 9, multiclick: 0, modifiers: 0}
1008 call test_gui_event('mouse', args)
1009 let args.button = 3
1010 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001011 call feedkeys("\<Esc>", 'Lx!')
1012 call assert_equal(['one two abc three', 'four five six'], getline(1, '$'))
1013
1014 " extend visual selection using right click in visual mode
1015 let @" = ''
1016 call cursor(1, 1)
1017 call feedkeys('v', 'Lx!')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001018 let args = #{button: 2, row: 1, col: 17, multiclick: 0, modifiers: 0}
1019 call test_gui_event('mouse', args)
1020 let args.button = 3
1021 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001022 call feedkeys("y", 'Lx!')
1023 call assert_equal('one two abc three', @")
1024
1025 " extend visual selection using mouse drag
1026 let @" = ''
1027 call cursor(1, 1)
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001028 let args = #{button: 0, row: 2, col: 1, multiclick: 0, modifiers: 0}
1029 call test_gui_event('mouse', args)
1030 let args = #{button: 0x43, row: 2, col: 9, multiclick: 0, modifiers: 0}
1031 call test_gui_event('mouse', args)
1032 let args.button = 0x3
1033 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001034 call feedkeys("y", 'Lx!')
1035 call assert_equal('four five', @")
1036
1037 " select text by moving the mouse
1038 let @" = ''
1039 call cursor(1, 1)
1040 redraw!
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001041 let args = #{button: 0, row: 1, col: 4, multiclick: 0, modifiers: 0}
1042 call test_gui_event('mouse', args)
1043 let args.button = 0x700
1044 let args.col = 9
1045 call test_gui_event('mouse', args)
1046 let args.col = 13
1047 call test_gui_event('mouse', args)
1048 let args.button = 3
1049 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001050 call feedkeys("y", 'Lx!')
1051 call assert_equal(' two abc t', @")
1052
1053 " Using mouse in insert mode
1054 call cursor(1, 1)
1055 call feedkeys('i', 't')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001056 let args = #{button: 0, row: 2, col: 11, multiclick: 0, modifiers: 0}
1057 call test_gui_event('mouse', args)
1058 let args.button = 3
1059 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001060 call feedkeys("po\<Esc>", 'Lx!')
1061 call assert_equal(['one two abc three', 'four five posix'], getline(1, '$'))
1062
1063 %d _
LemonBoyc27747e2022-05-07 12:25:40 +01001064 set scrolloff=0
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001065 call setline(1, range(1, 100))
1066 " scroll up
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001067 let args = #{button: 0x200, row: 2, col: 1, multiclick: 0, modifiers: 0}
1068 call test_gui_event('mouse', args)
1069 call test_gui_event('mouse', args)
1070 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001071 call feedkeys("H", 'Lx!')
1072 call assert_equal(10, line('.'))
1073
1074 " scroll down
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001075 let args = #{button: 0x100, row: 2, col: 1, multiclick: 0, modifiers: 0}
1076 call test_gui_event('mouse', args)
1077 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001078 call feedkeys("H", 'Lx!')
1079 call assert_equal(4, line('.'))
LemonBoyc27747e2022-05-07 12:25:40 +01001080 set scrolloff&
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001081
1082 %d _
1083 set nowrap
1084 call setline(1, range(10)->join('')->repeat(10))
1085 " scroll left
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001086 let args = #{button: 0x500, row: 1, col: 5, multiclick: 0, modifiers: 0}
1087 call test_gui_event('mouse', args)
1088 let args.col = 10
1089 call test_gui_event('mouse', args)
1090 let args.col = 15
1091 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001092 call feedkeys('g0', 'Lx!')
1093 call assert_equal(19, col('.'))
1094
1095 " scroll right
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001096 let args = #{button: 0x600, row: 1, col: 15, multiclick: 0, modifiers: 0}
1097 call test_gui_event('mouse', args)
1098 let args.col = 10
1099 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001100 call feedkeys('g0', 'Lx!')
1101 call assert_equal(7, col('.'))
1102 set wrap&
1103
1104 %d _
1105 call setline(1, repeat([repeat('a', 60)], 10))
1106
1107 " record various mouse events
1108 let mouseEventNames = [
1109 \ 'LeftMouse', 'LeftRelease', '2-LeftMouse', '3-LeftMouse',
1110 \ 'S-LeftMouse', 'A-LeftMouse', 'C-LeftMouse', 'MiddleMouse',
1111 \ 'MiddleRelease', '2-MiddleMouse', '3-MiddleMouse',
1112 \ 'S-MiddleMouse', 'A-MiddleMouse', 'C-MiddleMouse',
1113 \ 'RightMouse', 'RightRelease', '2-RightMouse',
1114 \ '3-RightMouse', 'S-RightMouse', 'A-RightMouse', 'C-RightMouse',
1115 \ 'X1Mouse', 'S-X1Mouse', 'A-X1Mouse', 'C-X1Mouse', 'X2Mouse',
1116 \ 'S-X2Mouse', 'A-X2Mouse', 'C-X2Mouse'
1117 \ ]
1118 let mouseEventCodes = map(copy(mouseEventNames), "'<' .. v:val .. '>'")
1119 let g:events = []
1120 for e in mouseEventCodes
1121 exe 'nnoremap ' .. e .. ' <Cmd>call add(g:events, "' ..
1122 \ substitute(e, '[<>]', '', 'g') .. '")<CR>'
1123 endfor
1124
1125 " Test various mouse buttons (0 - Left, 1 - Middle, 2 - Right, 0x300 - X1,
1126 " 0x300- X2)
1127 for button in [0, 1, 2, 0x300, 0x400]
1128 " Single click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001129 let args = #{button: button, row: 2, col: 5, multiclick: 0, modifiers: 0}
1130 call test_gui_event('mouse', args)
1131 let args.button = 3
1132 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001133
1134 " Double/Triple click is supported by only the Left/Middle/Right mouse
1135 " buttons
1136 if button <= 2
1137 " Double Click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001138 let args.button = button
1139 call test_gui_event('mouse', args)
1140 let args.multiclick = 1
1141 call test_gui_event('mouse', args)
1142 let args.button = 3
1143 let args.multiclick = 0
1144 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001145
1146 " Triple Click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001147 let args.button = button
1148 call test_gui_event('mouse', args)
1149 let args.multiclick = 1
1150 call test_gui_event('mouse', args)
1151 call test_gui_event('mouse', args)
1152 let args.button = 3
1153 let args.multiclick = 0
1154 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001155 endif
1156
1157 " Shift click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001158 let args = #{button: button, row: 3, col: 7, multiclick: 0, modifiers: 4}
1159 call test_gui_event('mouse', args)
1160 let args.button = 3
1161 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001162
1163 " Alt click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001164 let args.button = button
1165 let args.modifiers = 8
1166 call test_gui_event('mouse', args)
1167 let args.button = 3
1168 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001169
1170 " Ctrl click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001171 let args.button = button
1172 let args.modifiers = 16
1173 call test_gui_event('mouse', args)
1174 let args.button = 3
1175 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001176
1177 call feedkeys("\<Esc>", 'Lx!')
1178 endfor
1179
1180 call assert_equal(['LeftMouse', 'LeftRelease', 'LeftMouse', '2-LeftMouse',
1181 \ 'LeftMouse', '2-LeftMouse', '3-LeftMouse', 'S-LeftMouse',
1182 \ 'A-LeftMouse', 'C-LeftMouse', 'MiddleMouse', 'MiddleRelease',
1183 \ 'MiddleMouse', '2-MiddleMouse', 'MiddleMouse', '2-MiddleMouse',
1184 \ '3-MiddleMouse', 'S-MiddleMouse', 'A-MiddleMouse', 'C-MiddleMouse',
1185 \ 'RightMouse', 'RightRelease', 'RightMouse', '2-RightMouse',
1186 \ 'RightMouse', '2-RightMouse', '3-RightMouse', 'S-RightMouse',
1187 \ 'A-RightMouse', 'C-RightMouse', 'X1Mouse', 'S-X1Mouse', 'A-X1Mouse',
1188 \ 'C-X1Mouse', 'X2Mouse', 'S-X2Mouse', 'A-X2Mouse', 'C-X2Mouse'],
1189 \ g:events)
1190
1191 for e in mouseEventCodes
1192 exe 'nunmap ' .. e
1193 endfor
1194
1195 " modeless selection
1196 set mouse=
1197 let save_guioptions = &guioptions
1198 set guioptions+=A
1199 %d _
1200 call setline(1, ['one two three', 'four five sixteen'])
1201 call cursor(1, 1)
1202 redraw!
1203 " Double click should select the word and copy it to clipboard
1204 let @* = ''
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001205 let args = #{button: 0, row: 2, col: 11, multiclick: 0, modifiers: 0}
1206 call test_gui_event('mouse', args)
1207 let args.multiclick = 1
1208 call test_gui_event('mouse', args)
1209 let args.button = 3
1210 let args.multiclick = 0
1211 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001212 call feedkeys("\<Esc>", 'Lx!')
1213 call assert_equal([0, 1, 1, 0], getpos('.'))
1214 call assert_equal('sixteen', @*)
1215 " Right click should extend the selection from cursor
1216 call cursor(1, 6)
1217 redraw!
1218 let @* = ''
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001219 let args = #{button: 2, row: 1, col: 11, multiclick: 0, modifiers: 0}
1220 call test_gui_event('mouse', args)
1221 let args.button = 3
1222 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001223 call feedkeys("\<Esc>", 'Lx!')
1224 call assert_equal([0, 1, 6, 0], getpos('.'))
1225 call assert_equal('wo thr', @*)
1226 " Middle click should paste the clipboard contents
1227 call cursor(2, 1)
1228 redraw!
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001229 let args = #{button: 1, row: 1, col: 11, multiclick: 0, modifiers: 0}
1230 call test_gui_event('mouse', args)
1231 let args.button = 3
1232 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001233 call feedkeys("\<Esc>", 'Lx!')
1234 call assert_equal([0, 2, 7, 0], getpos('.'))
1235 call assert_equal('wo thrfour five sixteen', getline(2))
Bram Moolenaarc13e9982022-11-28 21:20:48 +00001236
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001237 set mouse&
1238 let &guioptions = save_guioptions
Bram Moolenaarc13e9982022-11-28 21:20:48 +00001239 bw!
1240 call test_override('no_query_mouse', 0)
1241 set mousemodel&
1242endfunc
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001243
Bram Moolenaarc13e9982022-11-28 21:20:48 +00001244" Test invalid parameters for test_gui_event()
1245func Test_gui_event_mouse_fails()
1246 call test_override('no_query_mouse', 1)
1247 new
1248 call setline(1, ['one two three', 'four five six'])
1249 set mousemodel=extend
1250
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001251 let args = #{row: 2, col: 4, multiclick: 0, modifiers: 0}
1252 call assert_false(test_gui_event('mouse', args))
1253 let args = #{button: 0, col: 4, multiclick: 0, modifiers: 0}
1254 call assert_false(test_gui_event('mouse', args))
1255 let args = #{button: 0, row: 2, multiclick: 0, modifiers: 0}
1256 call assert_false(test_gui_event('mouse', args))
1257 let args = #{button: 0, row: 2, col: 4, modifiers: 0}
1258 call assert_false(test_gui_event('mouse', args))
1259 let args = #{button: 0, row: 2, col: 4, multiclick: 0}
1260 call assert_false(test_gui_event('mouse', args))
1261
1262 " Error cases for test_gui_event()
1263 call assert_fails("call test_gui_event('a1b2c3', args)", 'E475:')
1264 call assert_fails("call test_gui_event([], args)", 'E1174:')
1265 call assert_fails("call test_gui_event('abc', [])", 'E1206:')
1266 call assert_fails("call test_gui_event(test_null_string(), {})", 'E475:')
1267 call assert_false(test_gui_event('mouse', test_null_dict()))
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001268
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001269 bw!
1270 call test_override('no_query_mouse', 0)
1271 set mousemodel&
1272endfunc
1273
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001274" Move the mouse to the top-left in preparation for mouse events
1275func PrepareForMouseEvent(args)
Bram Moolenaar36eb14f2022-09-01 14:38:01 +01001276 call extend(a:args, #{row: 1, col: 1})
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001277 call test_gui_event('mouse', a:args)
Bram Moolenaar36eb14f2022-09-01 14:38:01 +01001278 let g:eventlist = []
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001279 call feedkeys('', 'Lx!')
Bram Moolenaar36eb14f2022-09-01 14:38:01 +01001280
1281 " Wait a bit for the event. I may not come if the mouse didn't move, wait up
1282 " to 100 msec.
1283 for n in range(10)
1284 if len(g:eventlist) > 0
1285 break
1286 endif
1287 sleep 10m
1288 endfor
1289 let g:eventlist = []
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001290endfunc
1291
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001292func MouseWasMoved()
1293 let pos = getmousepos()
1294 call add(g:eventlist, #{row: pos.screenrow, col: pos.screencol})
1295endfunc
1296
Ernie Raelc4cb5442022-04-03 15:47:28 +01001297func Test_gui_mouse_move_event()
1298 let args = #{move: 1, button: 0, multiclick: 0, modifiers: 0}
1299
Bram Moolenaar36eb14f2022-09-01 14:38:01 +01001300 " by default, no mouse move events are generated
Ernie Raelc4cb5442022-04-03 15:47:28 +01001301 set mousemev&
1302 call assert_false(&mousemev)
1303
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001304 let g:eventlist = []
1305 nnoremap <special> <silent> <MouseMove> :call MouseWasMoved()<CR>
Ernie Raelc4cb5442022-04-03 15:47:28 +01001306
1307 " start at mouse pos (1,1), clear counter
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001308 call PrepareForMouseEvent(args)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001309
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001310 call extend(args, #{row: 3, col: 30, cell: v:true})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001311 call test_gui_event('mouse', args)
1312 call feedkeys('', 'Lx!')
1313
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001314 call extend(args, #{row: 10, 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 Moolenaar36eb14f2022-09-01 14:38:01 +01001318 " no events since 'mousemev' is off
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001319 call assert_equal([], g:eventlist)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001320
1321 " turn on mouse events and try the same thing
1322 set mousemev
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001323 call PrepareForMouseEvent(args)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001324
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001325 call extend(args, #{row: 3, col: 30, cell: v:true})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001326 call test_gui_event('mouse', args)
1327 call feedkeys('', 'Lx!')
1328
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001329 call extend(args, #{row: 10, 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 Moolenaar3c25a862022-05-16 17:07:41 +01001333 " FIXME: on MS-Windows we get a stray event first
1334 if has('win32') && len(g:eventlist) == 3
Bram Moolenaare5162e72022-05-16 16:41:35 +01001335 let g:eventlist = g:eventlist[1 : ]
1336 endif
1337
Christopher Plewright20b795e2022-12-20 20:01:58 +00001338 call assert_equal([#{row: 3, col: 30}, #{row: 10, col: 30}], g:eventlist)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001339
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001340 " wiggle the mouse around within a screen cell, shouldn't trigger events
1341 call extend(args, #{cell: v:false})
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001342 call PrepareForMouseEvent(args)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001343
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001344 call extend(args, #{row: 1, col: 2, cell: v:false})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001345 call test_gui_event('mouse', args)
1346 call feedkeys('', 'Lx!')
1347
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001348 call extend(args, #{row: 2, 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: 1, 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 assert_equal([], g:eventlist)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001357
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001358 unlet g:eventlist
Ernie Raelc4cb5442022-04-03 15:47:28 +01001359 unmap <MouseMove>
1360 set mousemev&
1361endfunc
1362
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001363" Test for 'guitablabel' and 'guitabtooltip' options
1364func TestGuiTabLabel()
1365 call add(g:TabLabels, v:lnum + 100)
1366 let bufnrlist = tabpagebuflist(v:lnum)
1367 return bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
1368endfunc
1369
1370func TestGuiTabToolTip()
1371 call add(g:TabToolTips, v:lnum + 200)
1372 let bufnrlist = tabpagebuflist(v:lnum)
1373 return bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
1374endfunc
1375
1376func Test_gui_tablabel_tooltip()
1377 %bw!
1378 " Removing the tabline at the end of this test, reduces the window height by
1379 " one. Save and restore it after the test.
1380 let save_lines = &lines
1381 edit one
1382 set modified
1383 tabnew two
1384 set modified
1385 tabnew three
1386 set modified
1387 let g:TabLabels = []
1388 set guitablabel=%{TestGuiTabLabel()}
1389 call test_override('starting', 1)
1390 redrawtabline
1391 call test_override('starting', 0)
1392 call assert_true(index(g:TabLabels, 101) != -1)
1393 call assert_true(index(g:TabLabels, 102) != -1)
1394 call assert_true(index(g:TabLabels, 103) != -1)
1395 set guitablabel&
1396 unlet g:TabLabels
1397
1398 if has('gui_gtk')
1399 " Only on GTK+, the tooltip function is called even if the mouse is not
1400 " on the tabline. on Win32 and Motif, the tooltip function is called only
1401 " when the mouse pointer is over the tabline.
1402 let g:TabToolTips = []
1403 set guitabtooltip=%{TestGuiTabToolTip()}
1404 call test_override('starting', 1)
1405 redrawtabline
1406 call test_override('starting', 0)
1407 call assert_true(index(g:TabToolTips, 201) != -1)
1408 call assert_true(index(g:TabToolTips, 202) != -1)
1409 call assert_true(index(g:TabToolTips, 203) != -1)
1410 set guitabtooltip&
1411 unlet g:TabToolTips
1412 endif
1413 %bw!
1414 let &lines = save_lines
1415endfunc
1416
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001417" Test for dropping files into a window in GUI
1418func DropFilesInCmdLine()
1419 call feedkeys(":\"", 'L')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001420 let d = #{files: ['a.c', 'b.c'], row: &lines, col: 1, modifiers: 0}
1421 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001422 call feedkeys("\<CR>", 'L')
1423endfunc
1424
1425func Test_gui_drop_files()
Bram Moolenaar1d1ce612021-06-27 19:02:52 +02001426 CheckFeature drop_file
1427
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001428 %bw!
1429 %argdelete
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001430 let d = #{files: [], row: 1, col: 1, modifiers: 0}
1431 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001432 call assert_equal([], argv())
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001433 let d = #{files: [1, 2], row: 1, col: 1, modifiers: 0}
1434 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001435 call assert_equal([], argv())
1436
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001437 let d = #{files: ['a.c', 'b.c'], row: 1, col: 1, modifiers: 0}
1438 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001439 call assert_equal(['a.c', 'b.c'], argv())
1440 %bw!
1441 %argdelete
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001442 let d = #{files: [], row: 1, col: 1, modifiers: 0}
1443 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001444 call assert_equal([], argv())
1445 %bw!
1446 " if the buffer in the window is modified, then the file should be opened in
1447 " a new window
1448 set modified
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001449 let d = #{files: ['x.c', 'y.c'], row: 1, col: 1, modifiers: 0}
1450 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001451 call assert_equal(['x.c', 'y.c'], argv())
1452 call assert_equal(2, winnr('$'))
1453 call assert_equal('x.c', bufname(winbufnr(1)))
1454 %bw!
1455 %argdelete
1456 " if Ctrl is pressed, then the file should be opened in a new window
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001457 let d = #{files: ['s.py', 't.py'], row: 1, col: 1, modifiers: 0x10}
1458 eval 'dropfiles'->test_gui_event(d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001459 call assert_equal(['s.py', 't.py'], argv())
1460 call assert_equal(2, winnr('$'))
1461 call assert_equal('s.py', bufname(winbufnr(1)))
1462 %bw!
1463 %argdelete
1464 " drop the files in a non-current window
1465 belowright new
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001466 let d = #{files: ['a.py', 'b.py'], row: 1, col: 1, modifiers: 0}
1467 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001468 call assert_equal(['a.py', 'b.py'], argv())
1469 call assert_equal(2, winnr('$'))
1470 call assert_equal(1, winnr())
1471 call assert_equal('a.py', bufname(winbufnr(1)))
1472 %bw!
1473 %argdelete
1474 " pressing shift when dropping files should change directory
1475 let save_cwd = getcwd()
Bram Moolenaar572a4432022-09-28 21:07:03 +01001476 call mkdir('Xdropdir1', 'R')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001477 call writefile([], 'Xdropdir1/Xfile1')
1478 call writefile([], 'Xdropdir1/Xfile2')
1479 let d = #{files: ['Xdropdir1/Xfile1', 'Xdropdir1/Xfile2'], row: 1, col: 1,
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001480 \ modifiers: 0x4}
1481 call test_gui_event('dropfiles', d)
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001482 call assert_equal('Xdropdir1', fnamemodify(getcwd(), ':t'))
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001483 call assert_equal('Xfile1', @%)
1484 call chdir(save_cwd)
1485 " pressing shift when dropping directory and files should change directory
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001486 let d = #{files: ['Xdropdir1', 'Xdropdir1/Xfile2'], row: 1, col: 1, modifiers: 0x4}
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001487 call test_gui_event('dropfiles', d)
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001488 call assert_equal('Xdropdir1', fnamemodify(getcwd(), ':t'))
1489 call assert_equal('Xdropdir1', fnamemodify(@%, ':t'))
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001490 call chdir(save_cwd)
1491 %bw!
1492 %argdelete
1493 " dropping a directory should edit it
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001494 let d = #{files: ['Xdropdir1'], row: 1, col: 1, modifiers: 0}
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001495 call test_gui_event('dropfiles', d)
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001496 call assert_equal('Xdropdir1', @%)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001497 %bw!
1498 %argdelete
1499 " dropping only a directory name with Shift should ignore it
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001500 let d = #{files: ['Xdropdir1'], row: 1, col: 1, modifiers: 0x4}
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001501 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001502 call assert_equal('', @%)
1503 %bw!
1504 %argdelete
Bram Moolenaar00d4cee2022-08-28 13:38:43 +01001505
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001506 " drop files in the command line. The GUI drop files adds the file names to
1507 " the low level input buffer. So need to use a cmdline map and feedkeys()
1508 " with 'Lx!' to process it in this function itself.
Bram Moolenaar00d4cee2022-08-28 13:38:43 +01001509 " This sometimes fails, e.g. when using valgrind.
1510 let g:test_is_flaky = 1
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001511 cnoremap <expr> <buffer> <F4> DropFilesInCmdLine()
1512 call feedkeys(":\"\<F4>\<CR>", 'xt')
1513 call feedkeys('k', 'Lx!')
1514 call assert_equal('"a.c b.c', @:)
1515 cunmap <buffer> <F4>
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001516
1517 " Invalid arguments
1518 call assert_false(test_gui_event("dropfiles", {}))
1519 let d = #{row: 1, col: 1, modifiers: 0}
1520 call assert_false(test_gui_event("dropfiles", d))
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +00001521 let d = #{files: 1, row: 1, col: 1, modifiers: 0}
1522 call assert_false(test_gui_event("dropfiles", d))
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001523 let d = #{files: test_null_list(), row: 1, col: 1, modifiers: 0}
1524 call assert_false(test_gui_event("dropfiles", d))
1525 let d = #{files: [test_null_string()], row: 1, col: 1, modifiers: 0}
1526 call assert_true(test_gui_event("dropfiles", d))
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001527endfunc
1528
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001529" Test for generating a GUI tabline event to select a tab page
1530func Test_gui_tabline_event()
1531 %bw!
1532 edit Xfile1
1533 tabedit Xfile2
1534 tabedit Xfile3
1535
1536 tabfirst
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001537 call assert_equal(v:true, test_gui_event('tabline', #{tabnr: 2}))
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001538 call feedkeys("y", "Lx!")
1539 call assert_equal(2, tabpagenr())
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001540 call assert_equal(v:true, test_gui_event('tabline', #{tabnr: 3}))
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001541 call feedkeys("y", "Lx!")
1542 call assert_equal(3, tabpagenr())
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001543 call assert_equal(v:false, 'tabline'->test_gui_event(#{tabnr: 3}))
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001544
1545 " From the cmdline window, tabline event should not be handled
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001546 call feedkeys("q::let t = test_gui_event('tabline', #{tabnr: 2})\<CR>:q\<CR>", 'x!')
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001547 call assert_equal(v:false, t)
1548
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001549 " Invalid arguments
1550 call assert_false(test_gui_event('tabline', {}))
1551 call assert_false(test_gui_event('tabline', #{abc: 1}))
1552
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001553 %bw!
1554endfunc
1555
1556" Test for generating a GUI tabline menu event to execute an action
1557func Test_gui_tabmenu_event()
1558 %bw!
1559
1560 " Try to close the last tab page
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001561 call test_gui_event('tabmenu', #{tabnr: 1, item: 1})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001562 call feedkeys("y", "Lx!")
1563
1564 edit Xfile1
1565 tabedit Xfile2
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001566 call test_gui_event('tabmenu', #{tabnr: 1, item: 1})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001567 call feedkeys("y", "Lx!")
1568 call assert_equal(1, tabpagenr('$'))
1569 call assert_equal('Xfile2', bufname())
1570
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001571 eval 'tabmenu'->test_gui_event(#{tabnr: 1, item: 2})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001572 call feedkeys("y", "Lx!")
1573 call assert_equal(2, tabpagenr('$'))
1574
1575 " If tabnr is 0, then the current tabpage should be used.
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001576 call test_gui_event('tabmenu', #{tabnr: 0, item: 2})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001577 call feedkeys("y", "Lx!")
1578 call assert_equal(3, tabpagenr('$'))
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001579 call test_gui_event('tabmenu', #{tabnr: 0, item: 1})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001580 call feedkeys("y", "Lx!")
1581 call assert_equal(2, tabpagenr('$'))
1582
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001583 " Invalid arguments
1584 call assert_false(test_gui_event('tabmenu', {}))
1585 call assert_false(test_gui_event('tabmenu', #{tabnr: 1}))
1586 call assert_false(test_gui_event('tabmenu', #{item: 1}))
1587 call assert_false(test_gui_event('tabmenu', #{abc: 1}))
1588
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001589 %bw!
1590endfunc
1591
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001592" Test for find/replace text dialog event
1593func Test_gui_findrepl()
Yegappan Lakshmanan08238042022-03-07 16:57:22 +00001594 " Find/Replace dialog is supported only on GTK, Motif and MS-Windows.
1595 if !has('gui_gtk') && !has('gui_motif') && !has('gui_win32')
1596 return
1597 endif
1598
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001599 new
1600 call setline(1, ['one two one', 'Twoo One two oneo'])
1601
1602 " Replace all instances of a string with another
1603 let args = #{find_text: 'one', repl_text: 'ONE', flags: 0x4, forward: 1}
1604 call test_gui_event('findrepl', args)
1605 call assert_equal(['ONE two ONE', 'Twoo ONE two ONEo'], getline(1, '$'))
1606
1607 " Replace all instances of a whole string with another
1608 call cursor(1, 1)
1609 let args = #{find_text: 'two', repl_text: 'TWO', flags: 0xC, forward: 1}
1610 call test_gui_event('findrepl', args)
1611 call assert_equal(['ONE TWO ONE', 'Twoo ONE TWO ONEo'], getline(1, '$'))
1612
Dominique Pelle81b573d2022-03-22 21:14:55 +00001613 " Find next occurrence of a string (in a find dialog)
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001614 call cursor(1, 11)
1615 let args = #{find_text: 'TWO', repl_text: '', flags: 0x11, forward: 1}
1616 call test_gui_event('findrepl', args)
1617 call assert_equal([2, 10], [line('.'), col('.')])
1618
Dominique Pelle81b573d2022-03-22 21:14:55 +00001619 " Find previous occurrences of a string (in a find dialog)
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001620 call cursor(1, 11)
1621 let args = #{find_text: 'TWO', repl_text: '', flags: 0x11, forward: 0}
1622 call test_gui_event('findrepl', args)
1623 call assert_equal([1, 5], [line('.'), col('.')])
1624
Dominique Pelle81b573d2022-03-22 21:14:55 +00001625 " Find next occurrence of a string (in a replace dialog)
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001626 call cursor(1, 1)
1627 let args = #{find_text: 'Twoo', repl_text: '', flags: 0x2, forward: 1}
1628 call test_gui_event('findrepl', args)
1629 call assert_equal([2, 1], [line('.'), col('.')])
1630
Dominique Pelle81b573d2022-03-22 21:14:55 +00001631 " Replace only the next occurrence of a string (once)
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001632 call cursor(1, 5)
1633 let args = #{find_text: 'TWO', repl_text: 'two', flags: 0x3, forward: 1}
1634 call test_gui_event('findrepl', args)
1635 call assert_equal(['ONE two ONE', 'Twoo ONE TWO ONEo'], getline(1, '$'))
1636
1637 " Replace all instances of a whole string with another matching case
1638 call cursor(1, 1)
1639 let args = #{find_text: 'TWO', repl_text: 'two', flags: 0x1C, forward: 1}
1640 call test_gui_event('findrepl', args)
1641 call assert_equal(['ONE two ONE', 'Twoo ONE two ONEo'], getline(1, '$'))
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +00001642
matveyt2834ebd2022-09-06 17:00:15 +01001643 " Replace all instances with sub-replace specials
1644 call cursor(1, 1)
1645 let args = #{find_text: 'ONE', repl_text: '&~&', flags: 0x4, forward: 1}
1646 call test_gui_event('findrepl', args)
1647 call assert_equal(['&~& two &~&', 'Twoo &~& two &~&o'], getline(1, '$'))
1648
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +00001649 " Invalid arguments
1650 call assert_false(test_gui_event('findrepl', {}))
1651 let args = #{repl_text: 'a', flags: 1, forward: 1}
1652 call assert_false(test_gui_event('findrepl', args))
1653 let args = #{find_text: 'a', flags: 1, forward: 1}
1654 call assert_false(test_gui_event('findrepl', args))
1655 let args = #{find_text: 'a', repl_text: 'b', forward: 1}
1656 call assert_false(test_gui_event('findrepl', args))
1657 let args = #{find_text: 'a', repl_text: 'b', flags: 1}
1658 call assert_false(test_gui_event('findrepl', args))
1659
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001660 bw!
1661endfunc
1662
zeertzjqbad8a012022-04-29 16:44:00 +01001663func Test_gui_CTRL_SHIFT_V()
1664 call feedkeys(":let g:str = '\<*C-S-V>\<*C-S-I>\<*C-S-V>\<*C-S-@>'\<CR>", 'tx')
zeertzjq758a8d12022-04-29 11:06:34 +01001665 call assert_equal('<C-S-I><C-S-@>', g:str)
1666 unlet g:str
1667endfunc
1668
Bram Moolenaarde8be2b2022-06-16 14:45:41 +01001669func Test_gui_dialog_file()
K.Takatae68f1342022-09-07 13:01:11 +01001670 " make sure the file does not exist, otherwise a dialog makes Vim hang
1671 call delete('Xdialfile')
1672
Bram Moolenaarde8be2b2022-06-16 14:45:41 +01001673 let lines =<< trim END
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001674 file Xdialfile
Bram Moolenaarde8be2b2022-06-16 14:45:41 +01001675 normal axxx
1676 confirm qa
1677 END
K.Takatae68f1342022-09-07 13:01:11 +01001678 call writefile(lines, 'Xlines', 'D')
Bram Moolenaara2726242022-06-16 16:36:43 +01001679 let prefix = '!'
1680 if has('win32')
1681 let prefix = '!start '
1682 endif
1683 execute prefix .. GetVimCommand() .. ' -g -f --clean --gui-dialog-file Xdialog -S Xlines'
Bram Moolenaarde8be2b2022-06-16 14:45:41 +01001684
1685 call WaitForAssert({-> assert_true(filereadable('Xdialog'))})
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001686 call assert_match('Question: Save changes to "Xdialfile"?', readfile('Xdialog')->join('<NL>'))
Bram Moolenaarde8be2b2022-06-16 14:45:41 +01001687
1688 call delete('Xdialog')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001689 call delete('Xdialfile')
Bram Moolenaarde8be2b2022-06-16 14:45:41 +01001690endfunc
1691
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01001692" Test for sending low level key presses
1693func SendKeys(keylist)
1694 for k in a:keylist
Christopher Plewright20b795e2022-12-20 20:01:58 +00001695 call test_gui_event("key", #{event: "keydown", keycode: k})
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01001696 endfor
1697 for k in reverse(a:keylist)
Christopher Plewright20b795e2022-12-20 20:01:58 +00001698 call test_gui_event("key", #{event: "keyup", keycode: k})
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01001699 endfor
1700endfunc
1701
1702func Test_gui_lowlevel_keyevent()
1703 CheckMSWindows
1704 new
1705
1706 " Test for <Ctrl-A> to <Ctrl-Z> keys
Ken Takata490c1da2024-07-15 19:52:25 +02001707 " FIXME: <Ctrl-C> is excluded for now. It makes the test flaky.
1708 for kc in range(65, 66) + range(68, 90)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01001709 call SendKeys([0x11, kc])
Christian Brabandtf6ebaa72024-01-25 20:44:49 +01001710 try
1711 let ch = getcharstr()
1712 catch /^Vim:Interrupt$/
1713 let ch = "\<c-c>"
1714 endtry
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01001715 call assert_equal(nr2char(kc - 64), ch)
1716 endfor
1717
Christopher Plewright566f76e2023-01-10 13:43:04 +00001718 " Testing more extensive windows keyboard handling
1719 " is covered in test_mswin_event.vim
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01001720
1721 bw!
1722endfunc
1723
zeertzjq2cd0f272022-10-04 20:14:28 +01001724func Test_gui_macro_csi()
1725 " Test for issue #11270
1726 nnoremap <C-L> <Cmd>let g:triggered = 1<CR>
1727 let @q = "\x9b\xfc\x04L"
1728 norm @q
1729 call assert_equal(1, g:triggered)
1730 unlet g:triggered
1731 nunmap <C-L>
1732
1733 " Test for issue #11057
1734 inoremap <C-D>t bbb
1735 call setline(1, "\t")
1736 let @q = "i\x9b\xfc\x04D"
1737 " The end of :normal is like a mapping timing out
1738 norm @q
1739 call assert_equal('', getline(1))
1740 iunmap <C-D>t
1741endfunc
1742
zeertzjq1bed9932023-08-30 19:38:24 +02001743func Test_gui_csi_keytrans()
1744 call assert_equal('<C-L>', keytrans("\x9b\xfc\x04L"))
1745 call assert_equal('<C-D>', keytrans("\x9b\xfc\x04D"))
1746endfunc
1747
Gary Johnson318ff9c2025-05-26 20:10:25 +02001748" Test that CursorHold is NOT triggered at startup before a keypress
1749func Test_CursorHold_not_triggered_at_startup()
1750 defer delete('Xcursorhold.log')
1751 defer delete('Xcursorhold_test.vim')
1752 call writefile([
1753 \ 'set updatetime=300',
1754 \ 'let g:cursorhold_triggered = 0',
1755 \ 'autocmd CursorHold * let g:cursorhold_triggered += 1 | call writefile(["CursorHold triggered"], "Xcursorhold.log", "a")',
1756 \ 'call timer_start(400, {-> execute(''call writefile(["g:cursorhold_triggered=" . g:cursorhold_triggered], "Xcursorhold.log", "a") | qa!'')})',
1757 \ ], 'Xcursorhold_test.vim')
1758
1759 let vimcmd = v:progpath . ' -g -f -N -u NONE -i NONE -S Xcursorhold_test.vim'
1760 call system(vimcmd)
1761
1762 let lines = filereadable('Xcursorhold.log') ? readfile('Xcursorhold.log') : []
1763
1764 " Assert that CursorHold did NOT trigger at startup
1765 call assert_false(index(lines, 'CursorHold triggered') != -1)
1766 let found = filter(copy(lines), 'v:val =~ "^g:cursorhold_triggered="')
1767 call assert_equal(['g:cursorhold_triggered=0'], found)
1768endfunc
1769
SUN Haitao08896dd2025-06-04 21:25:31 +02001770" Test that buffer names are shown at the end in the :Buffers menu
1771func Test_Buffers_Menu()
1772 doautocmd LoadBufferMenu VimEnter
1773
1774 let name = '天'
1775 exe ':badd ' .. name
1776 let nr = bufnr('$')
1777
1778 let cmd = printf(':amenu Buffers.%s\ (%d)', name, nr)
1779 let menu = split(execute(cmd), '\n')[1]
1780 call assert_match('^9999 '.. name, menu)
1781endfunc
1782
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001783" vim: shiftwidth=2 sts=2 expandtab