blob: 5561d8417fdd6d8dfbbc23f6c539ab7b28860892 [file] [log] [blame]
Bram Moolenaar877e9572016-08-04 20:05:50 +02001" Tests specifically for the GUI
2
Bram Moolenaar9f0139a2017-08-13 20:26:20 +02003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
5CheckCanRunGui
Bram Moolenaar877e9572016-08-04 20:05:50 +02006
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +01007source setup_gui.vim
Bram Moolenaar13c724f2017-02-05 20:54:26 +01008
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +01009func Setup()
10 call GUISetUpCommon()
Bram Moolenaar50fa8dd2016-08-09 22:58:21 +020011endfunc
12
13func TearDown()
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +010014 call GUITearDownCommon()
Bram Moolenaar877e9572016-08-04 20:05:50 +020015endfunc
16
17" Test for resetting "secure" flag after GUI has started.
Bram Moolenaar5d7ead32018-02-27 17:17:42 +010018" Must be run first, since it starts the GUI on Unix.
Bram Moolenaar877e9572016-08-04 20:05:50 +020019func Test_1_set_secure()
20 set exrc secure
21 gui -f
22 call assert_equal(1, has('gui_running'))
23endfunc
24
Bram Moolenaard5841f22017-03-05 13:27:25 +010025" As for non-GUI, a balloon_show() test was already added with patch 8.0.0401
26func Test_balloon_show()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020027 CheckFeature balloon_eval
28 " This won't do anything but must not crash either.
29 call balloon_show('hi!')
Bram Moolenaard5841f22017-03-05 13:27:25 +010030endfunc
31
Bram Moolenaar87748452017-03-12 17:10:33 +010032func Test_colorscheme()
Bram Moolenaaracc770a2020-04-12 15:11:06 +020033 call assert_equal('16777216', &t_Co)
34
Bram Moolenaar87748452017-03-12 17:10:33 +010035 let colorscheme_saved = exists('g:colors_name') ? g:colors_name : 'default'
Bram Moolenaar60a68362018-04-30 15:40:48 +020036 let g:color_count = 0
37 augroup TestColors
38 au!
39 au ColorScheme * let g:color_count += 1| let g:after_colors = g:color_count
40 au ColorSchemePre * let g:color_count += 1 |let g:before_colors = g:color_count
41 augroup END
Bram Moolenaar87748452017-03-12 17:10:33 +010042
43 colorscheme torte
44 redraw!
Bram Moolenaar87748452017-03-12 17:10:33 +010045 call assert_equal('dark', &background)
Bram Moolenaar60a68362018-04-30 15:40:48 +020046 call assert_equal(1, g:before_colors)
47 call assert_equal(2, g:after_colors)
Bram Moolenaar6d4470b2019-01-08 21:05:51 +010048 call assert_equal("\ntorte", execute('colorscheme'))
49
50 let a = substitute(execute('hi Search'), "\n\\s\\+", ' ', 'g')
Bram Moolenaar98feace2022-05-14 12:34:43 +010051 " FIXME: temporarily check less while the colorscheme changes
52 " call assert_match("\nSearch xxx term=reverse cterm=reverse ctermfg=196 ctermbg=16 gui=reverse guifg=#ff0000 guibg=#000000", a)
53 call assert_match("\nSearch xxx term=reverse ", a)
Bram Moolenaar6d4470b2019-01-08 21:05:51 +010054
55 call assert_fails('colorscheme does_not_exist', 'E185:')
Bram Moolenaar87748452017-03-12 17:10:33 +010056
57 exec 'colorscheme' colorscheme_saved
Bram Moolenaar60a68362018-04-30 15:40:48 +020058 augroup TestColors
59 au!
60 augroup END
61 unlet g:color_count g:after_colors g:before_colors
Bram Moolenaar87748452017-03-12 17:10:33 +010062 redraw!
63endfunc
64
Bram Moolenaar6f785742017-02-06 22:11:55 +010065func Test_getfontname_with_arg()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +010066 CheckX11BasedGui
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010067
Bram Moolenaar0b962e52022-04-03 18:02:37 +010068 if has('gui_motif')
Bram Moolenaar6f785742017-02-06 22:11:55 +010069 " Invalid font name. The result should be an empty string.
70 call assert_equal('', getfontname('notexist'))
71
72 " Valid font name. This is usually the real name of 7x13 by default.
Bram Moolenaar87748452017-03-12 17:10:33 +010073 let fname = '-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1'
74 call assert_match(fname, getfontname(fname))
Bram Moolenaar6f785742017-02-06 22:11:55 +010075
76 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
77 " Invalid font name. The result should be the name plus the default size.
78 call assert_equal('notexist 10', getfontname('notexist'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010079 call assert_equal('', getfontname('*'))
Bram Moolenaar6f785742017-02-06 22:11:55 +010080
81 " Valid font name. This is usually the real name of Monospace by default.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010082 let fname = 'Bitstream Vera Sans Mono 12'
83 call assert_equal(fname, getfontname(fname))
84 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +010085endfunc
86
87func Test_getfontname_without_arg()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +010088 CheckX11BasedGui
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010089
90 let fname = getfontname()
91
Bram Moolenaar40bd5a12021-10-16 21:58:27 +010092 if has('gui_kde')
Bram Moolenaar6f785742017-02-06 22:11:55 +010093 " 'expected' is the value specified by SetUp() above.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010094 call assert_equal('Courier 10 Pitch/8/-1/5/50/0/0/0/0/0', fname)
Bram Moolenaar0b962e52022-04-03 18:02:37 +010095 elseif has('gui_motif')
Bram Moolenaar87748452017-03-12 17:10:33 +010096 " 'expected' is DFLT_FONT of gui_x11.c or its real name.
97 let pat = '\(7x13\)\|\(\c-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1\)'
98 call assert_match(pat, fname)
Bram Moolenaar6f785742017-02-06 22:11:55 +010099 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
100 " 'expected' is DEFAULT_FONT of gui_gtk_x11.c.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100101 call assert_equal('Monospace 10', fname)
102 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +0100103endfunc
104
Bram Moolenaar87748452017-03-12 17:10:33 +0100105func Test_getwinpos()
106 call assert_match('Window position: X \d\+, Y \d\+', execute('winpos'))
107 call assert_true(getwinposx() >= 0)
108 call assert_true(getwinposy() >= 0)
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200109 call assert_equal([getwinposx(), getwinposy()], getwinpos())
Bram Moolenaar87748452017-03-12 17:10:33 +0100110endfunc
111
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100112func Test_quoteplus()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100113 CheckX11BasedGui
114
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100115 let g:test_is_flaky = 1
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100116
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100117 let quoteplus_saved = @+
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100118
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100119 let test_call = 'Can you hear me?'
120 let test_response = 'Yes, I can.'
121 let vim_exe = GetVimCommand()
122 let testee = 'VIMRUNTIME=' . $VIMRUNTIME . '; export VIMRUNTIME;'
123 \ . vim_exe . ' --noplugin --not-a-term -c ''%s'''
124 " Ignore the "failed to create input context" error.
125 let cmd = 'call test_ignore_error("E285") | '
126 \ . 'gui -f | '
127 \ . 'call feedkeys("'
128 \ . '\"+p'
129 \ . ':s/' . test_call . '/' . test_response . '/\<CR>'
130 \ . '\"+yis'
131 \ . ':q!\<CR>", "tx")'
132 let run_vimtest = printf(testee, cmd)
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100133
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100134 " Set the quoteplus register to test_call, and another gvim will launched.
135 " Then, it first tries to paste the content of its own quotedplus register
136 " onto it. Second, it tries to substitute test_response for the pasted
137 " sentence. If the sentence is identical to test_call, the substitution
138 " should succeed. Third, it tries to yank the result of the substitution
139 " to its own quoteplus register, and last it quits. When system()
140 " returns, the content of the quoteplus register should be identical to
141 " test_response if those quoteplus registers are synchronized properly
142 " with/through the X11 clipboard.
143 let @+ = test_call
144 call system(run_vimtest)
145 call assert_equal(test_response, @+)
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100146
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100147 let @+ = quoteplus_saved
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100148endfunc
149
Bram Moolenaard68a0042021-10-20 23:08:11 +0100150func Test_gui_read_stdin()
151 CheckUnix
152
153 call writefile(['some', 'lines'], 'Xstdin')
154 let script =<< trim END
155 call writefile(getline(1, '$'), 'XstdinOK')
156 qa!
157 END
158 call writefile(script, 'Xscript')
159
160 " Cannot use --not-a-term here, the "reading from stdin" message would not be
161 " displayed.
162 let vimcmd = substitute(GetVimCommand(), '--not-a-term', '', '')
163
164 call system('cat Xstdin | ' .. vimcmd .. ' -f -g -S Xscript -')
165 call assert_equal(['some', 'lines'], readfile('XstdinOK'))
166
167 call delete('Xstdin')
168 call delete('XstdinOK')
169 call delete('Xscript')
170endfunc
171
Bram Moolenaar87748452017-03-12 17:10:33 +0100172func Test_set_background()
173 let background_saved = &background
174
175 set background&
176 call assert_equal('light', &background)
177
178 set background=dark
179 call assert_equal('dark', &background)
180
181 let &background = background_saved
182endfunc
183
Bram Moolenaard5841f22017-03-05 13:27:25 +0100184func Test_set_balloondelay()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200185 CheckOption balloondelay
Bram Moolenaard5841f22017-03-05 13:27:25 +0100186
187 let balloondelay_saved = &balloondelay
188
189 " Check if the default value is identical to that described in the manual.
190 set balloondelay&
191 call assert_equal(600, &balloondelay)
192
193 " Edge cases
194
195 " XXX This fact should be hidden so that people won't be tempted to write
196 " plugin/TimeMachine.vim. TODO Add reasonable range checks to the source
197 " code.
198 set balloondelay=-1
199 call assert_equal(-1, &balloondelay)
200
201 " Though it's possible to interpret the zero delay to be 'as soon as
202 " possible' or even 'indefinite', its actual meaning depends on the GUI
203 " toolkit in use after all.
204 set balloondelay=0
205 call assert_equal(0, &balloondelay)
206
207 set balloondelay=1
208 call assert_equal(1, &balloondelay)
209
210 " Since p_bdelay is of type long currently, the upper bound can be
211 " impractically huge and machine-dependent. Practically, it's sufficient
Bram Moolenaar0f9ea222017-03-05 13:48:13 +0100212 " to check if balloondelay works with 0x7fffffff (32 bits) for now.
213 set balloondelay=2147483647
214 call assert_equal(2147483647, &balloondelay)
Bram Moolenaard5841f22017-03-05 13:27:25 +0100215
216 let &balloondelay = balloondelay_saved
217endfunc
218
219func Test_set_ballooneval()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200220 CheckOption ballooneval
Bram Moolenaard5841f22017-03-05 13:27:25 +0100221
222 let ballooneval_saved = &ballooneval
223
224 set ballooneval&
225 call assert_equal(0, &ballooneval)
226
227 set ballooneval
228 call assert_notequal(0, &ballooneval)
229
230 set noballooneval
231 call assert_equal(0, &ballooneval)
232
233 let &ballooneval = ballooneval_saved
234endfunc
235
236func Test_set_balloonexpr()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200237 CheckOption balloonexpr
Bram Moolenaard5841f22017-03-05 13:27:25 +0100238
239 let balloonexpr_saved = &balloonexpr
240
241 " Default value
242 set balloonexpr&
243 call assert_equal('', &balloonexpr)
244
245 " User-defined function
246 new
247 func MyBalloonExpr()
248 return 'Cursor is at line ' . v:beval_lnum .
249 \', column ' . v:beval_col .
250 \ ' of file ' . bufname(v:beval_bufnr) .
251 \ ' on word "' . v:beval_text . '"' .
252 \ ' in window ' . v:beval_winid . ' (#' . v:beval_winnr . ')'
253 endfunc
254 setl balloonexpr=MyBalloonExpr()
255 setl ballooneval
256 call assert_equal('MyBalloonExpr()', &balloonexpr)
257 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200258 " and check if the content of the balloon is the same as what is expected.
Bram Moolenaard5841f22017-03-05 13:27:25 +0100259 " Also, check if textlock works as expected.
260 setl balloonexpr&
261 call assert_equal('', &balloonexpr)
262 delfunc MyBalloonExpr
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +0000263
264 " Using a script-local function
265 func s:NewBalloonExpr()
266 endfunc
267 set balloonexpr=s:NewBalloonExpr()
268 call assert_equal(expand('<SID>') .. 'NewBalloonExpr()', &balloonexpr)
269 set balloonexpr=<SID>NewBalloonExpr()
270 call assert_equal(expand('<SID>') .. 'NewBalloonExpr()', &balloonexpr)
271 delfunc s:NewBalloonExpr
Bram Moolenaard5841f22017-03-05 13:27:25 +0100272 bwipe!
273
274 " Multiline support
275 if has('balloon_multiline')
276 " Multiline balloon using NL
277 new
278 func MyBalloonFuncForMultilineUsingNL()
Dominique Pelle81b573d2022-03-22 21:14:55 +0000279 return "Multiline\nSupported\nBalloon\nusing NL"
Bram Moolenaard5841f22017-03-05 13:27:25 +0100280 endfunc
281 setl balloonexpr=MyBalloonFuncForMultilineUsingNL()
282 setl ballooneval
283 call assert_equal('MyBalloonFuncForMultilineUsingNL()', &balloonexpr)
284 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200285 " and check if the content of the balloon is the same as what is
Bram Moolenaard5841f22017-03-05 13:27:25 +0100286 " expected. Also, check if textlock works as expected.
287 setl balloonexpr&
288 delfunc MyBalloonFuncForMultilineUsingNL
289 bwipe!
290
291 " Multiline balloon using List
292 new
293 func MyBalloonFuncForMultilineUsingList()
Dominique Pelle81b573d2022-03-22 21:14:55 +0000294 return [ 'Multiline', 'Supported', 'Balloon', 'using List' ]
Bram Moolenaard5841f22017-03-05 13:27:25 +0100295 endfunc
296 setl balloonexpr=MyBalloonFuncForMultilineUsingList()
297 setl ballooneval
298 call assert_equal('MyBalloonFuncForMultilineUsingList()', &balloonexpr)
299 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200300 " and check if the content of the balloon is the same as what is
Bram Moolenaard5841f22017-03-05 13:27:25 +0100301 " expected. Also, check if textlock works as expected.
302 setl balloonexpr&
303 delfunc MyBalloonFuncForMultilineUsingList
304 bwipe!
305 endif
306
307 let &balloonexpr = balloonexpr_saved
308endfunc
309
Bram Moolenaar87748452017-03-12 17:10:33 +0100310" Invalid arguments are tested with test_options in conjunction with segfaults
311" caused by them (Patch 8.0.0357, 24922ec233).
312func Test_set_guicursor()
313 let guicursor_saved = &guicursor
314
315 let default = [
316 \ "n-v-c:block-Cursor/lCursor",
317 \ "ve:ver35-Cursor",
318 \ "o:hor50-Cursor",
319 \ "i-ci:ver25-Cursor/lCursor",
320 \ "r-cr:hor20-Cursor/lCursor",
321 \ "sm:block-Cursor-blinkwait175-blinkoff150-blinkon175"
322 \ ]
323
324 " Default Value
325 set guicursor&
326 call assert_equal(join(default, ','), &guicursor)
327
328 " Argument List Example 1
329 let opt_list = copy(default)
330 let opt_list[0] = "n-c-v:block-nCursor"
331 exec "set guicursor=" . join(opt_list, ',')
332 call assert_equal(join(opt_list, ','), &guicursor)
333 unlet opt_list
334
335 " Argument List Example 2
336 let opt_list = copy(default)
337 let opt_list[3] = "i-ci:ver30-iCursor-blinkwait300-blinkon200-blinkoff150"
338 exec "set guicursor=" . join(opt_list, ',')
339 call assert_equal(join(opt_list, ','), &guicursor)
340 unlet opt_list
341
342 " 'a' Mode
343 set guicursor&
344 let &guicursor .= ',a:blinkon0'
345 call assert_equal(join(default, ',') . ",a:blinkon0", &guicursor)
346
347 let &guicursor = guicursor_saved
348endfunc
349
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100350func Test_set_guifont_errors()
351 if has('win32')
352 " Invalid font names are accepted in GTK GUI
353 call assert_fails('set guifont=xa1bc23d7f', 'E596:')
354 endif
355
356 " This only works if 'renderoptions' exists and does not work for Windows XP
357 " and older.
358 if exists('+renderoptions') && windowsversion() !~ '^[345]\.'
359 " doing this four times used to cause a crash
360 set renderoptions=type:directx
361 for i in range(5)
362 set guifont=
363 endfor
364 set renderoptions=
365 for i in range(5)
366 set guifont=
367 endfor
368 endif
369endfunc
370
Bram Moolenaar43dded82017-02-09 16:06:17 +0100371func Test_set_guifont()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100372 CheckX11BasedGui
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100373
374 let guifont_saved = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100375 if has('xfontset')
376 " Prevent 'guifontset' from canceling 'guifont'.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100377 let guifontset_saved = &guifontset
Bram Moolenaar43dded82017-02-09 16:06:17 +0100378 set guifontset=
379 endif
380
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100381 if has('gui_motif')
Bram Moolenaar43dded82017-02-09 16:06:17 +0100382 " Non-empty font list with invalid font names.
383 "
384 " This test is twofold: (1) It checks if the command fails as expected
385 " when there are no loadable fonts found in the list. (2) It checks if
386 " 'guifont' remains the same after the command loads none of the fonts
387 " listed.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100388 let flist = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100389 call assert_fails('set guifont=-notexist1-*,-notexist2-*')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100390 call assert_equal(flist, &guifont)
Bram Moolenaar43dded82017-02-09 16:06:17 +0100391
392 " Non-empty font list with a valid font name. Should pick up the first
393 " valid font.
394 set guifont=-notexist1-*,fixed,-notexist2-*
Bram Moolenaar87748452017-03-12 17:10:33 +0100395 let pat = '\(fixed\)\|\(\c-Misc-Fixed-Medium-R-SemiCondensed--13-120-75-75-C-60-ISO8859-1\)'
396 call assert_match(pat, getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100397
398 " Empty list. Should fallback to the built-in default.
399 set guifont=
Bram Moolenaar87748452017-03-12 17:10:33 +0100400 let pat = '\(7x13\)\|\(\c-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1\)'
401 call assert_match(pat, getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100402
403 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
404 " For GTK, what we refer to as 'font names' in our manual are actually
405 " 'initial font patterns'. A valid font which matches the 'canonical font
406 " pattern' constructed from a given 'initial pattern' is to be looked up
407 " and loaded. That explains why the GTK GUIs appear to accept 'invalid
408 " font names'.
409 "
410 " Non-empty list. Should always pick up the first element, no matter how
411 " strange it is, as explained above.
412 set guifont=(´・ω・`)\ 12,Courier\ 12
413 call assert_equal('(´・ω・`) 12', getfontname())
414
415 " Empty list. Should fallback to the built-in default.
416 set guifont=
417 call assert_equal('Monospace 10', getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100418 endif
419
420 if has('xfontset')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100421 let &guifontset = guifontset_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100422 endif
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100423 let &guifont = guifont_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100424endfunc
425
Bram Moolenaar10434672017-02-12 19:59:08 +0100426func Test_set_guifontset()
Bram Moolenaarce90e362019-09-08 18:58:44 +0200427 CheckFeature xfontset
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100428 let skipped = ''
Bram Moolenaar10434672017-02-12 19:59:08 +0100429
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200430 call assert_fails('set guifontset=*', 'E597:')
431
Bram Moolenaarce90e362019-09-08 18:58:44 +0200432 let ctype_saved = v:ctype
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100433
Bram Moolenaarce90e362019-09-08 18:58:44 +0200434 " First, since XCreateFontSet(3) is very sensitive to locale, fonts must
435 " be chosen meticulously.
436 let font_head = '-misc-fixed-medium-r-normal--14'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100437
Bram Moolenaarce90e362019-09-08 18:58:44 +0200438 let font_aw70 = font_head . '-130-75-75-c-70'
439 let font_aw140 = font_head . '-130-75-75-c-140'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100440
Bram Moolenaarce90e362019-09-08 18:58:44 +0200441 let font_jisx0201 = font_aw70 . '-jisx0201.1976-0'
442 let font_jisx0208 = font_aw140 . '-jisx0208.1983-0'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100443
Bram Moolenaarce90e362019-09-08 18:58:44 +0200444 let full_XLFDs = join([ font_jisx0208, font_jisx0201 ], ',')
445 let short_XLFDs = join([ font_aw140, font_aw70 ], ',')
446 let singleton = font_head . '-*'
447 let aliases = 'k14,r14'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100448
Bram Moolenaarce90e362019-09-08 18:58:44 +0200449 " Second, among 'locales', look up such a locale that gets 'set
450 " guifontset=' to work successfully with every fontset specified with
451 " 'fontsets'.
452 let locales = [ 'ja_JP.UTF-8', 'ja_JP.eucJP', 'ja_JP.SJIS' ]
453 let fontsets = [ full_XLFDs, short_XLFDs, singleton, aliases ]
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100454
Bram Moolenaarce90e362019-09-08 18:58:44 +0200455 let feasible = 0
456 for locale in locales
457 try
458 exec 'language ctype' locale
459 catch /^Vim\%((\a\+)\)\=:E197/
460 continue
461 endtry
462 let done = 0
463 for fontset in fontsets
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100464 try
Bram Moolenaarce90e362019-09-08 18:58:44 +0200465 exec 'set guifontset=' . fontset
466 catch /^Vim\%((\a\+)\)\=:E\%(250\|252\|234\|597\|598\)/
467 break
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100468 endtry
Bram Moolenaarce90e362019-09-08 18:58:44 +0200469 let done += 1
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100470 endfor
Bram Moolenaarce90e362019-09-08 18:58:44 +0200471 if done == len(fontsets)
472 let feasible = 1
473 break
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100474 endif
Bram Moolenaarce90e362019-09-08 18:58:44 +0200475 endfor
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100476
Bram Moolenaarce90e362019-09-08 18:58:44 +0200477 " Third, give a set of tests if it is found feasible.
478 if !feasible
479 let skipped = g:not_hosted
480 else
481 " N.B. 'v:ctype' has already been set to an appropriate value in the
482 " previous loop.
483 for fontset in fontsets
484 exec 'set guifontset=' . fontset
485 call assert_equal(fontset, &guifontset)
486 endfor
Bram Moolenaar10434672017-02-12 19:59:08 +0100487 endif
488
Bram Moolenaarce90e362019-09-08 18:58:44 +0200489 " Finally, restore ctype.
490 exec 'language ctype' ctype_saved
491
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100492 if !empty(skipped)
493 throw skipped
Bram Moolenaar10434672017-02-12 19:59:08 +0100494 endif
495endfunc
496
497func Test_set_guifontwide()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100498 CheckX11BasedGui
Bram Moolenaar10434672017-02-12 19:59:08 +0100499
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100500 call assert_fails('set guifontwide=*', 'E533:')
501
502 if has('gui_gtk')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100503 let guifont_saved = &guifont
504 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100505
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100506 let fc_match = exepath('fc-match')
507 if empty(fc_match)
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100508 let skipped = g:not_hosted
Bram Moolenaar10434672017-02-12 19:59:08 +0100509 else
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100510 let &guifont = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=en')
511 let wide = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=ja')
512 exec 'set guifontwide=' . fnameescape(wide)
513 call assert_equal(wide, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100514 endif
515
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100516 let &guifontwide = guifontwide_saved
517 let &guifont = guifont_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100518
Bram Moolenaar0b962e52022-04-03 18:02:37 +0100519 elseif has('gui_motif')
Bram Moolenaar10434672017-02-12 19:59:08 +0100520 " guifontwide is premised upon the xfontset feature.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100521 if !has('xfontset')
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100522 let skipped = g:not_supported . 'xfontset'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100523 else
524 let encoding_saved = &encoding
525 let guifont_saved = &guifont
526 let guifontset_saved = &guifontset
527 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100528
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100529 let nfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-90-iso10646-1'
530 let wfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-180-iso10646-1'
Bram Moolenaar10434672017-02-12 19:59:08 +0100531
532 set encoding=utf-8
533
534 " Case 1: guifontset is empty
535 set guifontset=
536
537 " Case 1-1: Automatic selection
538 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100539 exec 'set guifont=' . nfont
540 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100541
542 " Case 1-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100543 exec 'set guifontwide=' . wfont
544 exec 'set guifont=' . nfont
545 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100546
547 " Case 2: guifontset is invalid
548 try
549 set guifontset=-*-notexist-*
Bram Moolenaar37175402017-03-18 20:18:45 +0100550 call assert_report("'set guifontset=-*-notexist-*' should have failed")
Bram Moolenaar10434672017-02-12 19:59:08 +0100551 catch
Bram Moolenaare2e40752020-09-04 21:18:46 +0200552 call assert_exception('E598:')
Bram Moolenaar10434672017-02-12 19:59:08 +0100553 endtry
Bram Moolenaar10434672017-02-12 19:59:08 +0100554
555 " Case 2-1: Automatic selection
556 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100557 exec 'set guifont=' . nfont
558 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100559
560 " Case 2-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100561 exec 'set guifontwide=' . wfont
562 exec 'set guifont=' . nfont
563 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100564
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100565 let &guifontwide = guifontwide_saved
566 let &guifontset = guifontset_saved
567 let &guifont = guifont_saved
568 let &encoding = encoding_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100569 endif
Bram Moolenaar10434672017-02-12 19:59:08 +0100570 endif
Bram Moolenaar10434672017-02-12 19:59:08 +0100571endfunc
572
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100573func Test_set_guiligatures()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100574 CheckX11BasedGui
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100575
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100576 if has('gui_gtk') || has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
577 " Try correct value
578 set guiligatures=<>=ab
579 call assert_equal("<>=ab", &guiligatures)
580 " Try to throw error
581 try
582 set guiligatures=<>=šab
583 call assert_report("'set guiligatures=<>=šab should have failed")
584 catch
585 call assert_exception('E1243:')
586 endtry
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100587 endif
588endfunc
589
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100590func Test_set_guiheadroom()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100591 CheckX11BasedGui
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100592
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100593 " Since this script is to be read together with '-U NONE', the default
594 " value must be preserved.
595 call assert_equal(50, &guiheadroom)
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100596endfunc
597
Bram Moolenaar87748452017-03-12 17:10:33 +0100598func Test_set_guioptions()
599 let guioptions_saved = &guioptions
600 let duration = '200m'
601
602 if has('win32')
603 " Default Value
604 set guioptions&
605 call assert_equal('egmrLtT', &guioptions)
606
607 else
608 " Default Value
609 set guioptions&
610 call assert_equal('aegimrLtT', &guioptions)
611
612 " To activate scrollbars of type 'L' or 'R'.
613 wincmd v
614 redraw!
615
616 " Remove all default GUI ornaments
617 set guioptions-=T
618 exec 'sleep' . duration
619 call assert_equal('aegimrLt', &guioptions)
620 set guioptions-=t
621 exec 'sleep' . duration
622 call assert_equal('aegimrL', &guioptions)
623 set guioptions-=L
624 exec 'sleep' . duration
625 call assert_equal('aegimr', &guioptions)
626 set guioptions-=r
627 exec 'sleep' . duration
628 call assert_equal('aegim', &guioptions)
629 set guioptions-=m
630 exec 'sleep' . duration
631 call assert_equal('aegi', &guioptions)
632
633 " Try non-default GUI ornaments
634 set guioptions+=l
635 exec 'sleep' . duration
636 call assert_equal('aegil', &guioptions)
637 set guioptions-=l
638 exec 'sleep' . duration
639 call assert_equal('aegi', &guioptions)
640
641 set guioptions+=R
642 exec 'sleep' . duration
643 call assert_equal('aegiR', &guioptions)
644 set guioptions-=R
645 exec 'sleep' . duration
646 call assert_equal('aegi', &guioptions)
647
648 set guioptions+=b
649 exec 'sleep' . duration
650 call assert_equal('aegib', &guioptions)
651 set guioptions+=h
652 exec 'sleep' . duration
653 call assert_equal('aegibh', &guioptions)
654 set guioptions-=h
655 exec 'sleep' . duration
656 call assert_equal('aegib', &guioptions)
657 set guioptions-=b
658 exec 'sleep' . duration
659 call assert_equal('aegi', &guioptions)
660
661 set guioptions+=v
662 exec 'sleep' . duration
663 call assert_equal('aegiv', &guioptions)
664 set guioptions-=v
665 exec 'sleep' . duration
666 call assert_equal('aegi', &guioptions)
667
668 if has('gui_motif')
669 set guioptions+=F
670 exec 'sleep' . duration
671 call assert_equal('aegiF', &guioptions)
672 set guioptions-=F
673 exec 'sleep' . duration
674 call assert_equal('aegi', &guioptions)
675 endif
676
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +0200677 if has('gui_gtk3')
678 set guioptions+=d
679 exec 'sleep' . duration
680 call assert_equal('aegid', &guioptions)
681 set guioptions-=d
682 exec 'sleep' . duration
683 call assert_equal('aegi', &guioptions)
684 endif
685
Bram Moolenaar87748452017-03-12 17:10:33 +0100686 " Restore GUI ornaments to the default state.
687 set guioptions+=m
688 exec 'sleep' . duration
689 call assert_equal('aegim', &guioptions)
690 set guioptions+=r
691 exec 'sleep' . duration
692 call assert_equal('aegimr', &guioptions)
693 set guioptions+=L
694 exec 'sleep' . duration
695 call assert_equal('aegimrL', &guioptions)
696 set guioptions+=t
697 exec 'sleep' . duration
698 call assert_equal('aegimrLt', &guioptions)
699 set guioptions+=T
700 exec 'sleep' . duration
701 call assert_equal("aegimrLtT", &guioptions)
702
703 wincmd o
704 redraw!
705 endif
706
707 let &guioptions = guioptions_saved
708endfunc
709
Bram Moolenaarab186732018-09-14 21:27:06 +0200710func Test_scrollbars()
711 new
712 " buffer with 200 lines
713 call setline(1, repeat(['one', 'two'], 100))
714 set guioptions+=rlb
715
716 " scroll to move line 11 at top, moves the cursor there
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +0000717 let args = #{which: 'left', value: 10, dragging: 0}
718 call test_gui_event('scrollbar', args)
Bram Moolenaarab186732018-09-14 21:27:06 +0200719 redraw
720 call assert_equal(1, winline())
721 call assert_equal(11, line('.'))
722
723 " scroll to move line 1 at top, cursor stays in line 11
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +0000724 let args = #{which: 'right', value: 0, dragging: 0}
725 call test_gui_event('scrollbar', args)
Bram Moolenaarab186732018-09-14 21:27:06 +0200726 redraw
727 call assert_equal(11, winline())
728 call assert_equal(11, line('.'))
729
730 set nowrap
731 call setline(11, repeat('x', 150))
732 redraw
733 call assert_equal(1, wincol())
Bram Moolenaarf6d50f12019-06-06 15:40:08 +0200734 set number
735 redraw
736 call assert_equal(5, wincol())
737 set nonumber
738 redraw
Bram Moolenaarab186732018-09-14 21:27:06 +0200739 call assert_equal(1, col('.'))
740
741 " scroll to character 11, cursor is moved
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +0000742 let args = #{which: 'hor', value: 10, dragging: 0}
743 call test_gui_event('scrollbar', args)
Bram Moolenaarab186732018-09-14 21:27:06 +0200744 redraw
745 call assert_equal(1, wincol())
Bram Moolenaarf6d50f12019-06-06 15:40:08 +0200746 set number
747 redraw
748 call assert_equal(5, wincol())
749 set nonumber
750 redraw
Bram Moolenaarab186732018-09-14 21:27:06 +0200751 call assert_equal(11, col('.'))
752
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +0000753 " Invalid arguments
754 call assert_false(test_gui_event('scrollbar', {}))
755 call assert_false(test_gui_event('scrollbar', #{value: 10, dragging: 0}))
756 call assert_false(test_gui_event('scrollbar', #{which: 'hor', dragging: 0}))
757 call assert_false(test_gui_event('scrollbar', #{which: 'hor', value: 1}))
758 call assert_fails("call test_gui_event('scrollbar', #{which: 'a', value: 1, dragging: 0})", 'E475:')
759
Bram Moolenaarab186732018-09-14 21:27:06 +0200760 set guioptions&
761 set wrap&
762 bwipe!
763endfunc
764
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100765func Test_menu()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100766 CheckFeature quickfix
767
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100768 " Check Help menu exists
769 let help_menu = execute('menu Help')
770 call assert_match('Overview', help_menu)
771
772 " Check Help menu works
773 emenu Help.Overview
774 call assert_equal('help', &buftype)
775 close
776
777 " Check deleting menu doesn't cause trouble.
778 aunmenu Help
Bram Moolenaarb45cd362020-09-28 21:41:49 +0200779 if exists(':tlmenu')
780 tlunmenu Help
781 endif
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100782 call assert_fails('menu Help', 'E329:')
783endfunc
784
Bram Moolenaar87748452017-03-12 17:10:33 +0100785func Test_set_guipty()
786 let guipty_saved = &guipty
787
788 " Default Value
789 set guipty&
790 call assert_equal(1, &guipty)
791
792 set noguipty
793 call assert_equal(0, &guipty)
794
795 let &guipty = guipty_saved
Bram Moolenaar6f785742017-02-06 22:11:55 +0100796endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100797
Bram Moolenaarc701f322019-03-28 21:49:21 +0100798func Test_encoding_conversion()
799 " GTK supports conversion between 'encoding' and "utf-8"
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200800 CheckFeature gui_gtk
801 let encoding_saved = &encoding
802 set encoding=latin1
Bram Moolenaarc701f322019-03-28 21:49:21 +0100803
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200804 " would be nice if we could take a screenshot
805 intro
806 " sets the window title
807 edit SomeFile
Bram Moolenaarc701f322019-03-28 21:49:21 +0100808
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200809 let &encoding = encoding_saved
Bram Moolenaarc701f322019-03-28 21:49:21 +0100810endfunc
811
Bram Moolenaar877e9572016-08-04 20:05:50 +0200812func Test_shell_command()
813 new
Bram Moolenaar9d5b8762016-08-04 21:21:13 +0200814 r !echo hello
815 call assert_equal('hello', substitute(getline(2), '\W', '', 'g'))
Bram Moolenaar877e9572016-08-04 20:05:50 +0200816 bwipe!
Bram Moolenaar877e9572016-08-04 20:05:50 +0200817endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100818
Bram Moolenaar87748452017-03-12 17:10:33 +0100819func Test_syntax_colortest()
820 runtime syntax/colortest.vim
821 redraw!
822 sleep 200m
823 bwipe!
824endfunc
825
826func Test_set_term()
827 " It's enough to check the current value since setting 'term' to anything
828 " other than builtin_gui makes no sense at all.
829 call assert_equal('builtin_gui', &term)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200830 call assert_fails('set term=xterm', 'E530:')
Bram Moolenaar87748452017-03-12 17:10:33 +0100831endfunc
832
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100833func Test_windowid_variable()
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100834 if g:x11_based_gui || has('win32')
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100835 call assert_true(v:windowid > 0)
836 else
837 call assert_equal(0, v:windowid)
838 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +0100839endfunc
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200840
841" Test "vim -g" and also the GUIEnter autocommand.
842func Test_gui_dash_g()
843 let cmd = GetVimCommand('Xscriptgui')
844 call writefile([""], "Xtestgui")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200845 let lines =<< trim END
846 au GUIEnter * call writefile(["insertmode: " . &insertmode], "Xtestgui")
847 au GUIEnter * qall
848 END
849 call writefile(lines, 'Xscriptgui')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200850 call system(cmd . ' -g')
851 call WaitForAssert({-> assert_equal(['insertmode: 0'], readfile('Xtestgui'))})
852
853 call delete('Xscriptgui')
854 call delete('Xtestgui')
855endfunc
856
857" Test "vim -7" and also the GUIEnter autocommand.
858func Test_gui_dash_y()
859 let cmd = GetVimCommand('Xscriptgui')
860 call writefile([""], "Xtestgui")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200861 let lines =<< trim END
862 au GUIEnter * call writefile(["insertmode: " . &insertmode], "Xtestgui")
863 au GUIEnter * qall
864 END
865 call writefile(lines, 'Xscriptgui')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200866 call system(cmd . ' -y')
867 call WaitForAssert({-> assert_equal(['insertmode: 1'], readfile('Xtestgui'))})
868
869 call delete('Xscriptgui')
870 call delete('Xtestgui')
871endfunc
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100872
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200873" Test for "!" option in 'guioptions'. Use a terminal for running external
874" commands
875func Test_gui_run_cmd_in_terminal()
Bram Moolenaar4457e1d2020-05-15 22:46:44 +0200876 CheckFeature terminal
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200877 let save_guioptions = &guioptions
878 set guioptions+=!
879 if has('win32')
880 let cmd = 'type'
881 else
882 " assume all the other systems have a cat command
883 let cmd = 'cat'
884 endif
Bram Moolenaar98f16712020-05-22 13:34:01 +0200885 exe "silent !" . cmd . " test_gui.vim"
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200886 " TODO: how to check that the command ran in a separate terminal?
887 " Maybe check for $TERM (dumb vs xterm) in the spawned shell?
888 let &guioptions = save_guioptions
889endfunc
890
Bram Moolenaar46cd43b2020-06-04 22:22:11 +0200891func Test_gui_recursive_mapping()
892 nmap ' <C-W>
893 nmap <C-W>a :let didit = 1<CR>
894 call feedkeys("'a", 'xt')
895 call assert_equal(1, didit)
896
897 nunmap '
898 nunmap <C-W>a
899endfunc
900
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200901" Test GUI mouse events
902func Test_gui_mouse_event()
903 set mousemodel=extend
904 call test_override('no_query_mouse', 1)
905 new
906 call setline(1, ['one two three', 'four five six'])
907
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200908 " place the cursor using left click in normal mode
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200909 call cursor(1, 1)
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000910 let args = #{button: 0, row: 2, col: 4, multiclick: 0, modifiers: 0}
911 call test_gui_event('mouse', args)
912 let args.button = 3
913 eval 'mouse'->test_gui_event(args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200914 call feedkeys("\<Esc>", 'Lx!')
915 call assert_equal([0, 2, 4, 0], getpos('.'))
916
917 " select and yank a word
918 let @" = ''
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000919 let args = #{button: 0, row: 1, col: 9, multiclick: 0, modifiers: 0}
920 call test_gui_event('mouse', args)
921 let args.multiclick = 1
922 call test_gui_event('mouse', args)
923 let args.button = 3
924 let args.multiclick = 0
925 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200926 call feedkeys("y", 'Lx!')
927 call assert_equal('three', @")
928
929 " create visual selection using right click
930 let @" = ''
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000931 let args = #{button: 0, row: 2, col: 6, multiclick: 0, modifiers: 0}
932 call test_gui_event('mouse', args)
933 let args.button = 3
934 call test_gui_event('mouse', args)
935 let args = #{button: 2, row: 2, col: 13, multiclick: 0, modifiers: 0}
936 call test_gui_event('mouse', args)
937 let args.button = 3
938 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200939 call feedkeys("y", 'Lx!')
940 call assert_equal('five six', @")
941
942 " paste using middle mouse button
943 let @* = 'abc '
944 call feedkeys('""', 'Lx!')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000945 let args = #{button: 1, row: 1, col: 9, multiclick: 0, modifiers: 0}
946 call test_gui_event('mouse', args)
947 let args.button = 3
948 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200949 call feedkeys("\<Esc>", 'Lx!')
950 call assert_equal(['one two abc three', 'four five six'], getline(1, '$'))
951
952 " extend visual selection using right click in visual mode
953 let @" = ''
954 call cursor(1, 1)
955 call feedkeys('v', 'Lx!')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000956 let args = #{button: 2, row: 1, col: 17, multiclick: 0, modifiers: 0}
957 call test_gui_event('mouse', args)
958 let args.button = 3
959 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200960 call feedkeys("y", 'Lx!')
961 call assert_equal('one two abc three', @")
962
963 " extend visual selection using mouse drag
964 let @" = ''
965 call cursor(1, 1)
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000966 let args = #{button: 0, row: 2, col: 1, multiclick: 0, modifiers: 0}
967 call test_gui_event('mouse', args)
968 let args = #{button: 0x43, row: 2, col: 9, multiclick: 0, modifiers: 0}
969 call test_gui_event('mouse', args)
970 let args.button = 0x3
971 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200972 call feedkeys("y", 'Lx!')
973 call assert_equal('four five', @")
974
975 " select text by moving the mouse
976 let @" = ''
977 call cursor(1, 1)
978 redraw!
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000979 let args = #{button: 0, row: 1, col: 4, multiclick: 0, modifiers: 0}
980 call test_gui_event('mouse', args)
981 let args.button = 0x700
982 let args.col = 9
983 call test_gui_event('mouse', args)
984 let args.col = 13
985 call test_gui_event('mouse', args)
986 let args.button = 3
987 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200988 call feedkeys("y", 'Lx!')
989 call assert_equal(' two abc t', @")
990
991 " Using mouse in insert mode
992 call cursor(1, 1)
993 call feedkeys('i', 't')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +0000994 let args = #{button: 0, row: 2, col: 11, multiclick: 0, modifiers: 0}
995 call test_gui_event('mouse', args)
996 let args.button = 3
997 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200998 call feedkeys("po\<Esc>", 'Lx!')
999 call assert_equal(['one two abc three', 'four five posix'], getline(1, '$'))
1000
1001 %d _
LemonBoyc27747e2022-05-07 12:25:40 +01001002 set scrolloff=0
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001003 call setline(1, range(1, 100))
1004 " scroll up
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001005 let args = #{button: 0x200, row: 2, col: 1, multiclick: 0, modifiers: 0}
1006 call test_gui_event('mouse', args)
1007 call test_gui_event('mouse', args)
1008 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001009 call feedkeys("H", 'Lx!')
1010 call assert_equal(10, line('.'))
1011
1012 " scroll down
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001013 let args = #{button: 0x100, row: 2, col: 1, multiclick: 0, modifiers: 0}
1014 call test_gui_event('mouse', args)
1015 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001016 call feedkeys("H", 'Lx!')
1017 call assert_equal(4, line('.'))
LemonBoyc27747e2022-05-07 12:25:40 +01001018 set scrolloff&
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001019
1020 %d _
1021 set nowrap
1022 call setline(1, range(10)->join('')->repeat(10))
1023 " scroll left
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001024 let args = #{button: 0x500, row: 1, col: 5, multiclick: 0, modifiers: 0}
1025 call test_gui_event('mouse', args)
1026 let args.col = 10
1027 call test_gui_event('mouse', args)
1028 let args.col = 15
1029 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001030 call feedkeys('g0', 'Lx!')
1031 call assert_equal(19, col('.'))
1032
1033 " scroll right
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001034 let args = #{button: 0x600, row: 1, col: 15, multiclick: 0, modifiers: 0}
1035 call test_gui_event('mouse', args)
1036 let args.col = 10
1037 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001038 call feedkeys('g0', 'Lx!')
1039 call assert_equal(7, col('.'))
1040 set wrap&
1041
1042 %d _
1043 call setline(1, repeat([repeat('a', 60)], 10))
1044
1045 " record various mouse events
1046 let mouseEventNames = [
1047 \ 'LeftMouse', 'LeftRelease', '2-LeftMouse', '3-LeftMouse',
1048 \ 'S-LeftMouse', 'A-LeftMouse', 'C-LeftMouse', 'MiddleMouse',
1049 \ 'MiddleRelease', '2-MiddleMouse', '3-MiddleMouse',
1050 \ 'S-MiddleMouse', 'A-MiddleMouse', 'C-MiddleMouse',
1051 \ 'RightMouse', 'RightRelease', '2-RightMouse',
1052 \ '3-RightMouse', 'S-RightMouse', 'A-RightMouse', 'C-RightMouse',
1053 \ 'X1Mouse', 'S-X1Mouse', 'A-X1Mouse', 'C-X1Mouse', 'X2Mouse',
1054 \ 'S-X2Mouse', 'A-X2Mouse', 'C-X2Mouse'
1055 \ ]
1056 let mouseEventCodes = map(copy(mouseEventNames), "'<' .. v:val .. '>'")
1057 let g:events = []
1058 for e in mouseEventCodes
1059 exe 'nnoremap ' .. e .. ' <Cmd>call add(g:events, "' ..
1060 \ substitute(e, '[<>]', '', 'g') .. '")<CR>'
1061 endfor
1062
1063 " Test various mouse buttons (0 - Left, 1 - Middle, 2 - Right, 0x300 - X1,
1064 " 0x300- X2)
1065 for button in [0, 1, 2, 0x300, 0x400]
1066 " Single click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001067 let args = #{button: button, row: 2, col: 5, multiclick: 0, modifiers: 0}
1068 call test_gui_event('mouse', args)
1069 let args.button = 3
1070 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001071
1072 " Double/Triple click is supported by only the Left/Middle/Right mouse
1073 " buttons
1074 if button <= 2
1075 " Double Click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001076 let args.button = button
1077 call test_gui_event('mouse', args)
1078 let args.multiclick = 1
1079 call test_gui_event('mouse', args)
1080 let args.button = 3
1081 let args.multiclick = 0
1082 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001083
1084 " Triple Click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001085 let args.button = button
1086 call test_gui_event('mouse', args)
1087 let args.multiclick = 1
1088 call test_gui_event('mouse', args)
1089 call test_gui_event('mouse', args)
1090 let args.button = 3
1091 let args.multiclick = 0
1092 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001093 endif
1094
1095 " Shift click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001096 let args = #{button: button, row: 3, col: 7, multiclick: 0, modifiers: 4}
1097 call test_gui_event('mouse', args)
1098 let args.button = 3
1099 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001100
1101 " Alt click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001102 let args.button = button
1103 let args.modifiers = 8
1104 call test_gui_event('mouse', args)
1105 let args.button = 3
1106 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001107
1108 " Ctrl click
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001109 let args.button = button
1110 let args.modifiers = 16
1111 call test_gui_event('mouse', args)
1112 let args.button = 3
1113 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001114
1115 call feedkeys("\<Esc>", 'Lx!')
1116 endfor
1117
1118 call assert_equal(['LeftMouse', 'LeftRelease', 'LeftMouse', '2-LeftMouse',
1119 \ 'LeftMouse', '2-LeftMouse', '3-LeftMouse', 'S-LeftMouse',
1120 \ 'A-LeftMouse', 'C-LeftMouse', 'MiddleMouse', 'MiddleRelease',
1121 \ 'MiddleMouse', '2-MiddleMouse', 'MiddleMouse', '2-MiddleMouse',
1122 \ '3-MiddleMouse', 'S-MiddleMouse', 'A-MiddleMouse', 'C-MiddleMouse',
1123 \ 'RightMouse', 'RightRelease', 'RightMouse', '2-RightMouse',
1124 \ 'RightMouse', '2-RightMouse', '3-RightMouse', 'S-RightMouse',
1125 \ 'A-RightMouse', 'C-RightMouse', 'X1Mouse', 'S-X1Mouse', 'A-X1Mouse',
1126 \ 'C-X1Mouse', 'X2Mouse', 'S-X2Mouse', 'A-X2Mouse', 'C-X2Mouse'],
1127 \ g:events)
1128
1129 for e in mouseEventCodes
1130 exe 'nunmap ' .. e
1131 endfor
1132
1133 " modeless selection
1134 set mouse=
1135 let save_guioptions = &guioptions
1136 set guioptions+=A
1137 %d _
1138 call setline(1, ['one two three', 'four five sixteen'])
1139 call cursor(1, 1)
1140 redraw!
1141 " Double click should select the word and copy it to clipboard
1142 let @* = ''
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001143 let args = #{button: 0, row: 2, col: 11, multiclick: 0, modifiers: 0}
1144 call test_gui_event('mouse', args)
1145 let args.multiclick = 1
1146 call test_gui_event('mouse', args)
1147 let args.button = 3
1148 let args.multiclick = 0
1149 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001150 call feedkeys("\<Esc>", 'Lx!')
1151 call assert_equal([0, 1, 1, 0], getpos('.'))
1152 call assert_equal('sixteen', @*)
1153 " Right click should extend the selection from cursor
1154 call cursor(1, 6)
1155 redraw!
1156 let @* = ''
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001157 let args = #{button: 2, row: 1, col: 11, multiclick: 0, modifiers: 0}
1158 call test_gui_event('mouse', args)
1159 let args.button = 3
1160 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001161 call feedkeys("\<Esc>", 'Lx!')
1162 call assert_equal([0, 1, 6, 0], getpos('.'))
1163 call assert_equal('wo thr', @*)
1164 " Middle click should paste the clipboard contents
1165 call cursor(2, 1)
1166 redraw!
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001167 let args = #{button: 1, row: 1, col: 11, multiclick: 0, modifiers: 0}
1168 call test_gui_event('mouse', args)
1169 let args.button = 3
1170 call test_gui_event('mouse', args)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001171 call feedkeys("\<Esc>", 'Lx!')
1172 call assert_equal([0, 2, 7, 0], getpos('.'))
1173 call assert_equal('wo thrfour five sixteen', getline(2))
1174 set mouse&
1175 let &guioptions = save_guioptions
1176
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001177 " Test invalid parameters for test_gui_event()
1178 let args = #{row: 2, col: 4, multiclick: 0, modifiers: 0}
1179 call assert_false(test_gui_event('mouse', args))
1180 let args = #{button: 0, col: 4, multiclick: 0, modifiers: 0}
1181 call assert_false(test_gui_event('mouse', args))
1182 let args = #{button: 0, row: 2, multiclick: 0, modifiers: 0}
1183 call assert_false(test_gui_event('mouse', args))
1184 let args = #{button: 0, row: 2, col: 4, modifiers: 0}
1185 call assert_false(test_gui_event('mouse', args))
1186 let args = #{button: 0, row: 2, col: 4, multiclick: 0}
1187 call assert_false(test_gui_event('mouse', args))
1188
1189 " Error cases for test_gui_event()
1190 call assert_fails("call test_gui_event('a1b2c3', args)", 'E475:')
1191 call assert_fails("call test_gui_event([], args)", 'E1174:')
1192 call assert_fails("call test_gui_event('abc', [])", 'E1206:')
1193 call assert_fails("call test_gui_event(test_null_string(), {})", 'E475:')
1194 call assert_false(test_gui_event('mouse', test_null_dict()))
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001195
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001196 bw!
1197 call test_override('no_query_mouse', 0)
1198 set mousemodel&
1199endfunc
1200
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001201" Move the mouse to the top-left in preparation for mouse events
1202func PrepareForMouseEvent(args)
1203 call extend(a:args, #{row: 1, col:1})
1204 call test_gui_event('mouse', a:args)
1205 call feedkeys('', 'Lx!')
1206 " on MS-Windows the event may have a slight delay
1207 if has('win32')
1208 sleep 20m
1209 endif
1210endfunc
1211
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001212func MouseWasMoved()
1213 let pos = getmousepos()
1214 call add(g:eventlist, #{row: pos.screenrow, col: pos.screencol})
1215endfunc
1216
Ernie Raelc4cb5442022-04-03 15:47:28 +01001217func Test_gui_mouse_move_event()
1218 let args = #{move: 1, button: 0, multiclick: 0, modifiers: 0}
1219
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001220 " by default, does not generate mouse move events
Ernie Raelc4cb5442022-04-03 15:47:28 +01001221 set mousemev&
1222 call assert_false(&mousemev)
1223
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001224 let g:eventlist = []
1225 nnoremap <special> <silent> <MouseMove> :call MouseWasMoved()<CR>
Ernie Raelc4cb5442022-04-03 15:47:28 +01001226
1227 " start at mouse pos (1,1), clear counter
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001228 call PrepareForMouseEvent(args)
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001229 let g:eventlist = []
Ernie Raelc4cb5442022-04-03 15:47:28 +01001230
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001231 call extend(args, #{row: 3, col: 30, cell: v:true})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001232 call test_gui_event('mouse', args)
1233 call feedkeys('', 'Lx!')
1234
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001235 call extend(args, #{row: 10, col: 30, cell: v:true})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001236 call test_gui_event('mouse', args)
1237 call feedkeys('', 'Lx!')
1238
1239 " no events since mousemev off
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001240 call assert_equal([], g:eventlist)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001241
1242 " turn on mouse events and try the same thing
1243 set mousemev
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001244 call PrepareForMouseEvent(args)
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001245 let g:eventlist = []
Ernie Raelc4cb5442022-04-03 15:47:28 +01001246
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001247 call extend(args, #{row: 3, col: 30, cell: v:true})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001248 call test_gui_event('mouse', args)
1249 call feedkeys('', 'Lx!')
1250
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001251 call extend(args, #{row: 10, col: 30, cell: v:true})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001252 call test_gui_event('mouse', args)
1253 call feedkeys('', 'Lx!')
1254
Bram Moolenaare5162e72022-05-16 16:41:35 +01001255 if has('win32')
1256 " FIXME: on MS-Windows we get a stray event first
1257 let g:eventlist = g:eventlist[1 : ]
1258 endif
1259
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001260 call assert_equal([#{row: 4, col: 31}, #{row: 11, col: 31}], g:eventlist)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001261
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001262 " wiggle the mouse around within a screen cell, shouldn't trigger events
1263 call extend(args, #{cell: v:false})
Bram Moolenaar3b675c22022-05-16 13:34:44 +01001264 call PrepareForMouseEvent(args)
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001265 let g:eventlist = []
Ernie Raelc4cb5442022-04-03 15:47:28 +01001266
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001267 call extend(args, #{row: 1, col: 2, cell: v:false})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001268 call test_gui_event('mouse', args)
1269 call feedkeys('', 'Lx!')
1270
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001271 call extend(args, #{row: 2, col: 2, cell: v:false})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001272 call test_gui_event('mouse', args)
1273 call feedkeys('', 'Lx!')
1274
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001275 call extend(args, #{row: 2, col: 1, cell: v:false})
Ernie Raelc4cb5442022-04-03 15:47:28 +01001276 call test_gui_event('mouse', args)
1277 call feedkeys('', 'Lx!')
1278
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001279 call assert_equal([], g:eventlist)
Ernie Raelc4cb5442022-04-03 15:47:28 +01001280
Bram Moolenaar7add8d32022-05-16 15:27:46 +01001281 unlet g:eventlist
Ernie Raelc4cb5442022-04-03 15:47:28 +01001282 unmap <MouseMove>
1283 set mousemev&
1284endfunc
1285
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001286" Test for 'guitablabel' and 'guitabtooltip' options
1287func TestGuiTabLabel()
1288 call add(g:TabLabels, v:lnum + 100)
1289 let bufnrlist = tabpagebuflist(v:lnum)
1290 return bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
1291endfunc
1292
1293func TestGuiTabToolTip()
1294 call add(g:TabToolTips, v:lnum + 200)
1295 let bufnrlist = tabpagebuflist(v:lnum)
1296 return bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
1297endfunc
1298
1299func Test_gui_tablabel_tooltip()
1300 %bw!
1301 " Removing the tabline at the end of this test, reduces the window height by
1302 " one. Save and restore it after the test.
1303 let save_lines = &lines
1304 edit one
1305 set modified
1306 tabnew two
1307 set modified
1308 tabnew three
1309 set modified
1310 let g:TabLabels = []
1311 set guitablabel=%{TestGuiTabLabel()}
1312 call test_override('starting', 1)
1313 redrawtabline
1314 call test_override('starting', 0)
1315 call assert_true(index(g:TabLabels, 101) != -1)
1316 call assert_true(index(g:TabLabels, 102) != -1)
1317 call assert_true(index(g:TabLabels, 103) != -1)
1318 set guitablabel&
1319 unlet g:TabLabels
1320
1321 if has('gui_gtk')
1322 " Only on GTK+, the tooltip function is called even if the mouse is not
1323 " on the tabline. on Win32 and Motif, the tooltip function is called only
1324 " when the mouse pointer is over the tabline.
1325 let g:TabToolTips = []
1326 set guitabtooltip=%{TestGuiTabToolTip()}
1327 call test_override('starting', 1)
1328 redrawtabline
1329 call test_override('starting', 0)
1330 call assert_true(index(g:TabToolTips, 201) != -1)
1331 call assert_true(index(g:TabToolTips, 202) != -1)
1332 call assert_true(index(g:TabToolTips, 203) != -1)
1333 set guitabtooltip&
1334 unlet g:TabToolTips
1335 endif
1336 %bw!
1337 let &lines = save_lines
1338endfunc
1339
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001340" Test for dropping files into a window in GUI
1341func DropFilesInCmdLine()
Bram Moolenaar1d1ce612021-06-27 19:02:52 +02001342 CheckFeature drop_file
1343
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001344 call feedkeys(":\"", 'L')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001345 let d = #{files: ['a.c', 'b.c'], row: &lines, col: 1, modifiers: 0}
1346 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001347 call feedkeys("\<CR>", 'L')
1348endfunc
1349
1350func Test_gui_drop_files()
Bram Moolenaar1d1ce612021-06-27 19:02:52 +02001351 CheckFeature drop_file
1352
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001353 %bw!
1354 %argdelete
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001355 let d = #{files: [], row: 1, col: 1, modifiers: 0}
1356 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001357 call assert_equal([], argv())
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001358 let d = #{files: [1, 2], row: 1, col: 1, modifiers: 0}
1359 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001360 call assert_equal([], argv())
1361
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001362 let d = #{files: ['a.c', 'b.c'], row: 1, col: 1, modifiers: 0}
1363 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001364 call assert_equal(['a.c', 'b.c'], argv())
1365 %bw!
1366 %argdelete
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001367 let d = #{files: [], row: 1, col: 1, modifiers: 0}
1368 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001369 call assert_equal([], argv())
1370 %bw!
1371 " if the buffer in the window is modified, then the file should be opened in
1372 " a new window
1373 set modified
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001374 let d = #{files: ['x.c', 'y.c'], row: 1, col: 1, modifiers: 0}
1375 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001376 call assert_equal(['x.c', 'y.c'], argv())
1377 call assert_equal(2, winnr('$'))
1378 call assert_equal('x.c', bufname(winbufnr(1)))
1379 %bw!
1380 %argdelete
1381 " if Ctrl is pressed, then the file should be opened in a new window
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001382 let d = #{files: ['s.py', 't.py'], row: 1, col: 1, modifiers: 0x10}
1383 eval 'dropfiles'->test_gui_event(d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001384 call assert_equal(['s.py', 't.py'], argv())
1385 call assert_equal(2, winnr('$'))
1386 call assert_equal('s.py', bufname(winbufnr(1)))
1387 %bw!
1388 %argdelete
1389 " drop the files in a non-current window
1390 belowright new
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001391 let d = #{files: ['a.py', 'b.py'], row: 1, col: 1, modifiers: 0}
1392 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001393 call assert_equal(['a.py', 'b.py'], argv())
1394 call assert_equal(2, winnr('$'))
1395 call assert_equal(1, winnr())
1396 call assert_equal('a.py', bufname(winbufnr(1)))
1397 %bw!
1398 %argdelete
1399 " pressing shift when dropping files should change directory
1400 let save_cwd = getcwd()
1401 call mkdir('Xdir1')
1402 call writefile([], 'Xdir1/Xfile1')
1403 call writefile([], 'Xdir1/Xfile2')
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001404 let d = #{files: ['Xdir1/Xfile1', 'Xdir1/Xfile2'], row: 1, col: 1,
1405 \ modifiers: 0x4}
1406 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001407 call assert_equal('Xdir1', fnamemodify(getcwd(), ':t'))
1408 call assert_equal('Xfile1', @%)
1409 call chdir(save_cwd)
1410 " pressing shift when dropping directory and files should change directory
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001411 let d = #{files: ['Xdir1', 'Xdir1/Xfile2'], row: 1, col: 1, modifiers: 0x4}
1412 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001413 call assert_equal('Xdir1', fnamemodify(getcwd(), ':t'))
1414 call assert_equal('Xdir1', fnamemodify(@%, ':t'))
1415 call chdir(save_cwd)
1416 %bw!
1417 %argdelete
1418 " dropping a directory should edit it
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001419 let d = #{files: ['Xdir1'], row: 1, col: 1, modifiers: 0}
1420 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001421 call assert_equal('Xdir1', @%)
1422 %bw!
1423 %argdelete
1424 " dropping only a directory name with Shift should ignore it
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001425 let d = #{files: ['Xdir1'], row: 1, col: 1, modifiers: 0x4}
1426 call test_gui_event('dropfiles', d)
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001427 call assert_equal('', @%)
1428 %bw!
1429 %argdelete
1430 call delete('Xdir1', 'rf')
1431 " drop files in the command line. The GUI drop files adds the file names to
1432 " the low level input buffer. So need to use a cmdline map and feedkeys()
1433 " with 'Lx!' to process it in this function itself.
1434 cnoremap <expr> <buffer> <F4> DropFilesInCmdLine()
1435 call feedkeys(":\"\<F4>\<CR>", 'xt')
1436 call feedkeys('k', 'Lx!')
1437 call assert_equal('"a.c b.c', @:)
1438 cunmap <buffer> <F4>
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001439
1440 " Invalid arguments
1441 call assert_false(test_gui_event("dropfiles", {}))
1442 let d = #{row: 1, col: 1, modifiers: 0}
1443 call assert_false(test_gui_event("dropfiles", d))
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +00001444 let d = #{files: 1, row: 1, col: 1, modifiers: 0}
1445 call assert_false(test_gui_event("dropfiles", d))
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001446 let d = #{files: test_null_list(), row: 1, col: 1, modifiers: 0}
1447 call assert_false(test_gui_event("dropfiles", d))
1448 let d = #{files: [test_null_string()], row: 1, col: 1, modifiers: 0}
1449 call assert_true(test_gui_event("dropfiles", d))
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001450endfunc
1451
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001452" Test for generating a GUI tabline event to select a tab page
1453func Test_gui_tabline_event()
1454 %bw!
1455 edit Xfile1
1456 tabedit Xfile2
1457 tabedit Xfile3
1458
1459 tabfirst
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001460 call assert_equal(v:true, test_gui_event('tabline', #{tabnr: 2}))
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001461 call feedkeys("y", "Lx!")
1462 call assert_equal(2, tabpagenr())
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001463 call assert_equal(v:true, test_gui_event('tabline', #{tabnr: 3}))
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001464 call feedkeys("y", "Lx!")
1465 call assert_equal(3, tabpagenr())
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001466 call assert_equal(v:false, 'tabline'->test_gui_event(#{tabnr: 3}))
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001467
1468 " From the cmdline window, tabline event should not be handled
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001469 call feedkeys("q::let t = test_gui_event('tabline', #{tabnr: 2})\<CR>:q\<CR>", 'x!')
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001470 call assert_equal(v:false, t)
1471
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001472 " Invalid arguments
1473 call assert_false(test_gui_event('tabline', {}))
1474 call assert_false(test_gui_event('tabline', #{abc: 1}))
1475
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001476 %bw!
1477endfunc
1478
1479" Test for generating a GUI tabline menu event to execute an action
1480func Test_gui_tabmenu_event()
1481 %bw!
1482
1483 " Try to close the last tab page
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001484 call test_gui_event('tabmenu', #{tabnr: 1, item: 1})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001485 call feedkeys("y", "Lx!")
1486
1487 edit Xfile1
1488 tabedit Xfile2
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001489 call test_gui_event('tabmenu', #{tabnr: 1, item: 1})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001490 call feedkeys("y", "Lx!")
1491 call assert_equal(1, tabpagenr('$'))
1492 call assert_equal('Xfile2', bufname())
1493
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001494 eval 'tabmenu'->test_gui_event(#{tabnr: 1, item: 2})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001495 call feedkeys("y", "Lx!")
1496 call assert_equal(2, tabpagenr('$'))
1497
1498 " If tabnr is 0, then the current tabpage should be used.
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001499 call test_gui_event('tabmenu', #{tabnr: 0, item: 2})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001500 call feedkeys("y", "Lx!")
1501 call assert_equal(3, tabpagenr('$'))
Yegappan Lakshmanan06011e12022-01-30 12:37:29 +00001502 call test_gui_event('tabmenu', #{tabnr: 0, item: 1})
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001503 call feedkeys("y", "Lx!")
1504 call assert_equal(2, tabpagenr('$'))
1505
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001506 " Invalid arguments
1507 call assert_false(test_gui_event('tabmenu', {}))
1508 call assert_false(test_gui_event('tabmenu', #{tabnr: 1}))
1509 call assert_false(test_gui_event('tabmenu', #{item: 1}))
1510 call assert_false(test_gui_event('tabmenu', #{abc: 1}))
1511
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001512 %bw!
1513endfunc
1514
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001515" Test for find/replace text dialog event
1516func Test_gui_findrepl()
Yegappan Lakshmanan08238042022-03-07 16:57:22 +00001517 " Find/Replace dialog is supported only on GTK, Motif and MS-Windows.
1518 if !has('gui_gtk') && !has('gui_motif') && !has('gui_win32')
1519 return
1520 endif
1521
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001522 new
1523 call setline(1, ['one two one', 'Twoo One two oneo'])
1524
1525 " Replace all instances of a string with another
1526 let args = #{find_text: 'one', repl_text: 'ONE', flags: 0x4, forward: 1}
1527 call test_gui_event('findrepl', args)
1528 call assert_equal(['ONE two ONE', 'Twoo ONE two ONEo'], getline(1, '$'))
1529
1530 " Replace all instances of a whole string with another
1531 call cursor(1, 1)
1532 let args = #{find_text: 'two', repl_text: 'TWO', flags: 0xC, forward: 1}
1533 call test_gui_event('findrepl', args)
1534 call assert_equal(['ONE TWO ONE', 'Twoo ONE TWO ONEo'], getline(1, '$'))
1535
Dominique Pelle81b573d2022-03-22 21:14:55 +00001536 " Find next occurrence of a string (in a find dialog)
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001537 call cursor(1, 11)
1538 let args = #{find_text: 'TWO', repl_text: '', flags: 0x11, forward: 1}
1539 call test_gui_event('findrepl', args)
1540 call assert_equal([2, 10], [line('.'), col('.')])
1541
Dominique Pelle81b573d2022-03-22 21:14:55 +00001542 " Find previous occurrences of a string (in a find dialog)
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001543 call cursor(1, 11)
1544 let args = #{find_text: 'TWO', repl_text: '', flags: 0x11, forward: 0}
1545 call test_gui_event('findrepl', args)
1546 call assert_equal([1, 5], [line('.'), col('.')])
1547
Dominique Pelle81b573d2022-03-22 21:14:55 +00001548 " Find next occurrence of a string (in a replace dialog)
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001549 call cursor(1, 1)
1550 let args = #{find_text: 'Twoo', repl_text: '', flags: 0x2, forward: 1}
1551 call test_gui_event('findrepl', args)
1552 call assert_equal([2, 1], [line('.'), col('.')])
1553
Dominique Pelle81b573d2022-03-22 21:14:55 +00001554 " Replace only the next occurrence of a string (once)
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001555 call cursor(1, 5)
1556 let args = #{find_text: 'TWO', repl_text: 'two', flags: 0x3, forward: 1}
1557 call test_gui_event('findrepl', args)
1558 call assert_equal(['ONE two ONE', 'Twoo ONE TWO ONEo'], getline(1, '$'))
1559
1560 " Replace all instances of a whole string with another matching case
1561 call cursor(1, 1)
1562 let args = #{find_text: 'TWO', repl_text: 'two', flags: 0x1C, forward: 1}
1563 call test_gui_event('findrepl', args)
1564 call assert_equal(['ONE two ONE', 'Twoo ONE two ONEo'], getline(1, '$'))
Yegappan Lakshmanan9e0208f2022-01-31 17:40:55 +00001565
1566 " Invalid arguments
1567 call assert_false(test_gui_event('findrepl', {}))
1568 let args = #{repl_text: 'a', flags: 1, forward: 1}
1569 call assert_false(test_gui_event('findrepl', args))
1570 let args = #{find_text: 'a', flags: 1, forward: 1}
1571 call assert_false(test_gui_event('findrepl', args))
1572 let args = #{find_text: 'a', repl_text: 'b', forward: 1}
1573 call assert_false(test_gui_event('findrepl', args))
1574 let args = #{find_text: 'a', repl_text: 'b', flags: 1}
1575 call assert_false(test_gui_event('findrepl', args))
1576
Yegappan Lakshmananec3637c2022-01-30 18:01:24 +00001577 bw!
1578endfunc
1579
zeertzjqbad8a012022-04-29 16:44:00 +01001580func Test_gui_CTRL_SHIFT_V()
1581 call feedkeys(":let g:str = '\<*C-S-V>\<*C-S-I>\<*C-S-V>\<*C-S-@>'\<CR>", 'tx')
zeertzjq758a8d12022-04-29 11:06:34 +01001582 call assert_equal('<C-S-I><C-S-@>', g:str)
1583 unlet g:str
1584endfunc
1585
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001586" vim: shiftwidth=2 sts=2 expandtab