blob: aeacb31aa5ad62b983e2a3ad3d2ff26600d81aab [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')
51 call assert_match("\nSearch xxx term=reverse ctermfg=0 ctermbg=12 gui=bold guifg=Black guibg=Red", a)
52
53 call assert_fails('colorscheme does_not_exist', 'E185:')
Bram Moolenaar87748452017-03-12 17:10:33 +010054
55 exec 'colorscheme' colorscheme_saved
Bram Moolenaar60a68362018-04-30 15:40:48 +020056 augroup TestColors
57 au!
58 augroup END
59 unlet g:color_count g:after_colors g:before_colors
Bram Moolenaar87748452017-03-12 17:10:33 +010060 redraw!
61endfunc
62
Bram Moolenaar6f785742017-02-06 22:11:55 +010063func Test_getfontname_with_arg()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +010064 CheckX11BasedGui
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010065
Bram Moolenaar40bd5a12021-10-16 21:58:27 +010066 if has('gui_athena') || has('gui_motif')
Bram Moolenaar6f785742017-02-06 22:11:55 +010067 " Invalid font name. The result should be an empty string.
68 call assert_equal('', getfontname('notexist'))
69
70 " Valid font name. This is usually the real name of 7x13 by default.
Bram Moolenaar87748452017-03-12 17:10:33 +010071 let fname = '-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1'
72 call assert_match(fname, getfontname(fname))
Bram Moolenaar6f785742017-02-06 22:11:55 +010073
74 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
75 " Invalid font name. The result should be the name plus the default size.
76 call assert_equal('notexist 10', getfontname('notexist'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010077 call assert_equal('', getfontname('*'))
Bram Moolenaar6f785742017-02-06 22:11:55 +010078
79 " Valid font name. This is usually the real name of Monospace by default.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010080 let fname = 'Bitstream Vera Sans Mono 12'
81 call assert_equal(fname, getfontname(fname))
82 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +010083endfunc
84
85func Test_getfontname_without_arg()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +010086 CheckX11BasedGui
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010087
88 let fname = getfontname()
89
Bram Moolenaar40bd5a12021-10-16 21:58:27 +010090 if has('gui_kde')
Bram Moolenaar6f785742017-02-06 22:11:55 +010091 " 'expected' is the value specified by SetUp() above.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010092 call assert_equal('Courier 10 Pitch/8/-1/5/50/0/0/0/0/0', fname)
Bram Moolenaar6f785742017-02-06 22:11:55 +010093 elseif has('gui_athena') || has('gui_motif')
Bram Moolenaar87748452017-03-12 17:10:33 +010094 " 'expected' is DFLT_FONT of gui_x11.c or its real name.
95 let pat = '\(7x13\)\|\(\c-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1\)'
96 call assert_match(pat, fname)
Bram Moolenaar6f785742017-02-06 22:11:55 +010097 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
98 " 'expected' is DEFAULT_FONT of gui_gtk_x11.c.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010099 call assert_equal('Monospace 10', fname)
100 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +0100101endfunc
102
Bram Moolenaar87748452017-03-12 17:10:33 +0100103func Test_getwinpos()
104 call assert_match('Window position: X \d\+, Y \d\+', execute('winpos'))
105 call assert_true(getwinposx() >= 0)
106 call assert_true(getwinposy() >= 0)
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200107 call assert_equal([getwinposx(), getwinposy()], getwinpos())
Bram Moolenaar87748452017-03-12 17:10:33 +0100108endfunc
109
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100110func Test_quoteplus()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100111 CheckX11BasedGui
112
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100113 let g:test_is_flaky = 1
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100114
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100115 let quoteplus_saved = @+
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100116
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100117 let test_call = 'Can you hear me?'
118 let test_response = 'Yes, I can.'
119 let vim_exe = GetVimCommand()
120 let testee = 'VIMRUNTIME=' . $VIMRUNTIME . '; export VIMRUNTIME;'
121 \ . vim_exe . ' --noplugin --not-a-term -c ''%s'''
122 " Ignore the "failed to create input context" error.
123 let cmd = 'call test_ignore_error("E285") | '
124 \ . 'gui -f | '
125 \ . 'call feedkeys("'
126 \ . '\"+p'
127 \ . ':s/' . test_call . '/' . test_response . '/\<CR>'
128 \ . '\"+yis'
129 \ . ':q!\<CR>", "tx")'
130 let run_vimtest = printf(testee, cmd)
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100131
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100132 " Set the quoteplus register to test_call, and another gvim will launched.
133 " Then, it first tries to paste the content of its own quotedplus register
134 " onto it. Second, it tries to substitute test_response for the pasted
135 " sentence. If the sentence is identical to test_call, the substitution
136 " should succeed. Third, it tries to yank the result of the substitution
137 " to its own quoteplus register, and last it quits. When system()
138 " returns, the content of the quoteplus register should be identical to
139 " test_response if those quoteplus registers are synchronized properly
140 " with/through the X11 clipboard.
141 let @+ = test_call
142 call system(run_vimtest)
143 call assert_equal(test_response, @+)
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100144
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100145 let @+ = quoteplus_saved
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100146endfunc
147
Bram Moolenaard68a0042021-10-20 23:08:11 +0100148func Test_gui_read_stdin()
149 CheckUnix
150
151 call writefile(['some', 'lines'], 'Xstdin')
152 let script =<< trim END
153 call writefile(getline(1, '$'), 'XstdinOK')
154 qa!
155 END
156 call writefile(script, 'Xscript')
157
158 " Cannot use --not-a-term here, the "reading from stdin" message would not be
159 " displayed.
160 let vimcmd = substitute(GetVimCommand(), '--not-a-term', '', '')
161
162 call system('cat Xstdin | ' .. vimcmd .. ' -f -g -S Xscript -')
163 call assert_equal(['some', 'lines'], readfile('XstdinOK'))
164
165 call delete('Xstdin')
166 call delete('XstdinOK')
167 call delete('Xscript')
168endfunc
169
Bram Moolenaar87748452017-03-12 17:10:33 +0100170func Test_set_background()
171 let background_saved = &background
172
173 set background&
174 call assert_equal('light', &background)
175
176 set background=dark
177 call assert_equal('dark', &background)
178
179 let &background = background_saved
180endfunc
181
Bram Moolenaard5841f22017-03-05 13:27:25 +0100182func Test_set_balloondelay()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200183 CheckOption balloondelay
Bram Moolenaard5841f22017-03-05 13:27:25 +0100184
185 let balloondelay_saved = &balloondelay
186
187 " Check if the default value is identical to that described in the manual.
188 set balloondelay&
189 call assert_equal(600, &balloondelay)
190
191 " Edge cases
192
193 " XXX This fact should be hidden so that people won't be tempted to write
194 " plugin/TimeMachine.vim. TODO Add reasonable range checks to the source
195 " code.
196 set balloondelay=-1
197 call assert_equal(-1, &balloondelay)
198
199 " Though it's possible to interpret the zero delay to be 'as soon as
200 " possible' or even 'indefinite', its actual meaning depends on the GUI
201 " toolkit in use after all.
202 set balloondelay=0
203 call assert_equal(0, &balloondelay)
204
205 set balloondelay=1
206 call assert_equal(1, &balloondelay)
207
208 " Since p_bdelay is of type long currently, the upper bound can be
209 " impractically huge and machine-dependent. Practically, it's sufficient
Bram Moolenaar0f9ea222017-03-05 13:48:13 +0100210 " to check if balloondelay works with 0x7fffffff (32 bits) for now.
211 set balloondelay=2147483647
212 call assert_equal(2147483647, &balloondelay)
Bram Moolenaard5841f22017-03-05 13:27:25 +0100213
214 let &balloondelay = balloondelay_saved
215endfunc
216
217func Test_set_ballooneval()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200218 CheckOption ballooneval
Bram Moolenaard5841f22017-03-05 13:27:25 +0100219
220 let ballooneval_saved = &ballooneval
221
222 set ballooneval&
223 call assert_equal(0, &ballooneval)
224
225 set ballooneval
226 call assert_notequal(0, &ballooneval)
227
228 set noballooneval
229 call assert_equal(0, &ballooneval)
230
231 let &ballooneval = ballooneval_saved
232endfunc
233
234func Test_set_balloonexpr()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200235 CheckOption balloonexpr
Bram Moolenaard5841f22017-03-05 13:27:25 +0100236
237 let balloonexpr_saved = &balloonexpr
238
239 " Default value
240 set balloonexpr&
241 call assert_equal('', &balloonexpr)
242
243 " User-defined function
244 new
245 func MyBalloonExpr()
246 return 'Cursor is at line ' . v:beval_lnum .
247 \', column ' . v:beval_col .
248 \ ' of file ' . bufname(v:beval_bufnr) .
249 \ ' on word "' . v:beval_text . '"' .
250 \ ' in window ' . v:beval_winid . ' (#' . v:beval_winnr . ')'
251 endfunc
252 setl balloonexpr=MyBalloonExpr()
253 setl ballooneval
254 call assert_equal('MyBalloonExpr()', &balloonexpr)
255 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200256 " and check if the content of the balloon is the same as what is expected.
Bram Moolenaard5841f22017-03-05 13:27:25 +0100257 " Also, check if textlock works as expected.
258 setl balloonexpr&
259 call assert_equal('', &balloonexpr)
260 delfunc MyBalloonExpr
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +0000261
262 " Using a script-local function
263 func s:NewBalloonExpr()
264 endfunc
265 set balloonexpr=s:NewBalloonExpr()
266 call assert_equal(expand('<SID>') .. 'NewBalloonExpr()', &balloonexpr)
267 set balloonexpr=<SID>NewBalloonExpr()
268 call assert_equal(expand('<SID>') .. 'NewBalloonExpr()', &balloonexpr)
269 delfunc s:NewBalloonExpr
Bram Moolenaard5841f22017-03-05 13:27:25 +0100270 bwipe!
271
272 " Multiline support
273 if has('balloon_multiline')
274 " Multiline balloon using NL
275 new
276 func MyBalloonFuncForMultilineUsingNL()
277 return "Multiline\nSuppported\nBalloon\nusing NL"
278 endfunc
279 setl balloonexpr=MyBalloonFuncForMultilineUsingNL()
280 setl ballooneval
281 call assert_equal('MyBalloonFuncForMultilineUsingNL()', &balloonexpr)
282 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200283 " and check if the content of the balloon is the same as what is
Bram Moolenaard5841f22017-03-05 13:27:25 +0100284 " expected. Also, check if textlock works as expected.
285 setl balloonexpr&
286 delfunc MyBalloonFuncForMultilineUsingNL
287 bwipe!
288
289 " Multiline balloon using List
290 new
291 func MyBalloonFuncForMultilineUsingList()
292 return [ 'Multiline', 'Suppported', 'Balloon', 'using List' ]
293 endfunc
294 setl balloonexpr=MyBalloonFuncForMultilineUsingList()
295 setl ballooneval
296 call assert_equal('MyBalloonFuncForMultilineUsingList()', &balloonexpr)
297 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200298 " and check if the content of the balloon is the same as what is
Bram Moolenaard5841f22017-03-05 13:27:25 +0100299 " expected. Also, check if textlock works as expected.
300 setl balloonexpr&
301 delfunc MyBalloonFuncForMultilineUsingList
302 bwipe!
303 endif
304
305 let &balloonexpr = balloonexpr_saved
306endfunc
307
Bram Moolenaar87748452017-03-12 17:10:33 +0100308" Invalid arguments are tested with test_options in conjunction with segfaults
309" caused by them (Patch 8.0.0357, 24922ec233).
310func Test_set_guicursor()
311 let guicursor_saved = &guicursor
312
313 let default = [
314 \ "n-v-c:block-Cursor/lCursor",
315 \ "ve:ver35-Cursor",
316 \ "o:hor50-Cursor",
317 \ "i-ci:ver25-Cursor/lCursor",
318 \ "r-cr:hor20-Cursor/lCursor",
319 \ "sm:block-Cursor-blinkwait175-blinkoff150-blinkon175"
320 \ ]
321
322 " Default Value
323 set guicursor&
324 call assert_equal(join(default, ','), &guicursor)
325
326 " Argument List Example 1
327 let opt_list = copy(default)
328 let opt_list[0] = "n-c-v:block-nCursor"
329 exec "set guicursor=" . join(opt_list, ',')
330 call assert_equal(join(opt_list, ','), &guicursor)
331 unlet opt_list
332
333 " Argument List Example 2
334 let opt_list = copy(default)
335 let opt_list[3] = "i-ci:ver30-iCursor-blinkwait300-blinkon200-blinkoff150"
336 exec "set guicursor=" . join(opt_list, ',')
337 call assert_equal(join(opt_list, ','), &guicursor)
338 unlet opt_list
339
340 " 'a' Mode
341 set guicursor&
342 let &guicursor .= ',a:blinkon0'
343 call assert_equal(join(default, ',') . ",a:blinkon0", &guicursor)
344
345 let &guicursor = guicursor_saved
346endfunc
347
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100348func Test_set_guifont_errors()
349 if has('win32')
350 " Invalid font names are accepted in GTK GUI
351 call assert_fails('set guifont=xa1bc23d7f', 'E596:')
352 endif
353
354 " This only works if 'renderoptions' exists and does not work for Windows XP
355 " and older.
356 if exists('+renderoptions') && windowsversion() !~ '^[345]\.'
357 " doing this four times used to cause a crash
358 set renderoptions=type:directx
359 for i in range(5)
360 set guifont=
361 endfor
362 set renderoptions=
363 for i in range(5)
364 set guifont=
365 endfor
366 endif
367endfunc
368
Bram Moolenaar43dded82017-02-09 16:06:17 +0100369func Test_set_guifont()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100370 CheckX11BasedGui
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100371
372 let guifont_saved = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100373 if has('xfontset')
374 " Prevent 'guifontset' from canceling 'guifont'.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100375 let guifontset_saved = &guifontset
Bram Moolenaar43dded82017-02-09 16:06:17 +0100376 set guifontset=
377 endif
378
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100379 if has('gui_athena') || has('gui_motif')
Bram Moolenaar43dded82017-02-09 16:06:17 +0100380 " Non-empty font list with invalid font names.
381 "
382 " This test is twofold: (1) It checks if the command fails as expected
383 " when there are no loadable fonts found in the list. (2) It checks if
384 " 'guifont' remains the same after the command loads none of the fonts
385 " listed.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100386 let flist = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100387 call assert_fails('set guifont=-notexist1-*,-notexist2-*')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100388 call assert_equal(flist, &guifont)
Bram Moolenaar43dded82017-02-09 16:06:17 +0100389
390 " Non-empty font list with a valid font name. Should pick up the first
391 " valid font.
392 set guifont=-notexist1-*,fixed,-notexist2-*
Bram Moolenaar87748452017-03-12 17:10:33 +0100393 let pat = '\(fixed\)\|\(\c-Misc-Fixed-Medium-R-SemiCondensed--13-120-75-75-C-60-ISO8859-1\)'
394 call assert_match(pat, getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100395
396 " Empty list. Should fallback to the built-in default.
397 set guifont=
Bram Moolenaar87748452017-03-12 17:10:33 +0100398 let pat = '\(7x13\)\|\(\c-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1\)'
399 call assert_match(pat, getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100400
401 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
402 " For GTK, what we refer to as 'font names' in our manual are actually
403 " 'initial font patterns'. A valid font which matches the 'canonical font
404 " pattern' constructed from a given 'initial pattern' is to be looked up
405 " and loaded. That explains why the GTK GUIs appear to accept 'invalid
406 " font names'.
407 "
408 " Non-empty list. Should always pick up the first element, no matter how
409 " strange it is, as explained above.
410 set guifont=(´・ω・`)\ 12,Courier\ 12
411 call assert_equal('(´・ω・`) 12', getfontname())
412
413 " Empty list. Should fallback to the built-in default.
414 set guifont=
415 call assert_equal('Monospace 10', getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100416 endif
417
418 if has('xfontset')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100419 let &guifontset = guifontset_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100420 endif
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100421 let &guifont = guifont_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100422endfunc
423
Bram Moolenaar10434672017-02-12 19:59:08 +0100424func Test_set_guifontset()
Bram Moolenaarce90e362019-09-08 18:58:44 +0200425 CheckFeature xfontset
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100426 let skipped = ''
Bram Moolenaar10434672017-02-12 19:59:08 +0100427
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200428 call assert_fails('set guifontset=*', 'E597:')
429
Bram Moolenaarce90e362019-09-08 18:58:44 +0200430 let ctype_saved = v:ctype
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100431
Bram Moolenaarce90e362019-09-08 18:58:44 +0200432 " First, since XCreateFontSet(3) is very sensitive to locale, fonts must
433 " be chosen meticulously.
434 let font_head = '-misc-fixed-medium-r-normal--14'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100435
Bram Moolenaarce90e362019-09-08 18:58:44 +0200436 let font_aw70 = font_head . '-130-75-75-c-70'
437 let font_aw140 = font_head . '-130-75-75-c-140'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100438
Bram Moolenaarce90e362019-09-08 18:58:44 +0200439 let font_jisx0201 = font_aw70 . '-jisx0201.1976-0'
440 let font_jisx0208 = font_aw140 . '-jisx0208.1983-0'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100441
Bram Moolenaarce90e362019-09-08 18:58:44 +0200442 let full_XLFDs = join([ font_jisx0208, font_jisx0201 ], ',')
443 let short_XLFDs = join([ font_aw140, font_aw70 ], ',')
444 let singleton = font_head . '-*'
445 let aliases = 'k14,r14'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100446
Bram Moolenaarce90e362019-09-08 18:58:44 +0200447 " Second, among 'locales', look up such a locale that gets 'set
448 " guifontset=' to work successfully with every fontset specified with
449 " 'fontsets'.
450 let locales = [ 'ja_JP.UTF-8', 'ja_JP.eucJP', 'ja_JP.SJIS' ]
451 let fontsets = [ full_XLFDs, short_XLFDs, singleton, aliases ]
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100452
Bram Moolenaarce90e362019-09-08 18:58:44 +0200453 let feasible = 0
454 for locale in locales
455 try
456 exec 'language ctype' locale
457 catch /^Vim\%((\a\+)\)\=:E197/
458 continue
459 endtry
460 let done = 0
461 for fontset in fontsets
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100462 try
Bram Moolenaarce90e362019-09-08 18:58:44 +0200463 exec 'set guifontset=' . fontset
464 catch /^Vim\%((\a\+)\)\=:E\%(250\|252\|234\|597\|598\)/
465 break
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100466 endtry
Bram Moolenaarce90e362019-09-08 18:58:44 +0200467 let done += 1
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100468 endfor
Bram Moolenaarce90e362019-09-08 18:58:44 +0200469 if done == len(fontsets)
470 let feasible = 1
471 break
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100472 endif
Bram Moolenaarce90e362019-09-08 18:58:44 +0200473 endfor
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100474
Bram Moolenaarce90e362019-09-08 18:58:44 +0200475 " Third, give a set of tests if it is found feasible.
476 if !feasible
477 let skipped = g:not_hosted
478 else
479 " N.B. 'v:ctype' has already been set to an appropriate value in the
480 " previous loop.
481 for fontset in fontsets
482 exec 'set guifontset=' . fontset
483 call assert_equal(fontset, &guifontset)
484 endfor
Bram Moolenaar10434672017-02-12 19:59:08 +0100485 endif
486
Bram Moolenaarce90e362019-09-08 18:58:44 +0200487 " Finally, restore ctype.
488 exec 'language ctype' ctype_saved
489
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100490 if !empty(skipped)
491 throw skipped
Bram Moolenaar10434672017-02-12 19:59:08 +0100492 endif
493endfunc
494
495func Test_set_guifontwide()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100496 CheckX11BasedGui
Bram Moolenaar10434672017-02-12 19:59:08 +0100497
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100498 call assert_fails('set guifontwide=*', 'E533:')
499
500 if has('gui_gtk')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100501 let guifont_saved = &guifont
502 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100503
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100504 let fc_match = exepath('fc-match')
505 if empty(fc_match)
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100506 let skipped = g:not_hosted
Bram Moolenaar10434672017-02-12 19:59:08 +0100507 else
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100508 let &guifont = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=en')
509 let wide = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=ja')
510 exec 'set guifontwide=' . fnameescape(wide)
511 call assert_equal(wide, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100512 endif
513
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100514 let &guifontwide = guifontwide_saved
515 let &guifont = guifont_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100516
517 elseif has('gui_athena') || has('gui_motif')
518 " guifontwide is premised upon the xfontset feature.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100519 if !has('xfontset')
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100520 let skipped = g:not_supported . 'xfontset'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100521 else
522 let encoding_saved = &encoding
523 let guifont_saved = &guifont
524 let guifontset_saved = &guifontset
525 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100526
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100527 let nfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-90-iso10646-1'
528 let wfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-180-iso10646-1'
Bram Moolenaar10434672017-02-12 19:59:08 +0100529
530 set encoding=utf-8
531
532 " Case 1: guifontset is empty
533 set guifontset=
534
535 " Case 1-1: Automatic selection
536 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100537 exec 'set guifont=' . nfont
538 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100539
540 " Case 1-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100541 exec 'set guifontwide=' . wfont
542 exec 'set guifont=' . nfont
543 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100544
545 " Case 2: guifontset is invalid
546 try
547 set guifontset=-*-notexist-*
Bram Moolenaar37175402017-03-18 20:18:45 +0100548 call assert_report("'set guifontset=-*-notexist-*' should have failed")
Bram Moolenaar10434672017-02-12 19:59:08 +0100549 catch
Bram Moolenaare2e40752020-09-04 21:18:46 +0200550 call assert_exception('E598:')
Bram Moolenaar10434672017-02-12 19:59:08 +0100551 endtry
552 " Set it to an invalid value brutally for preparation.
553 let &guifontset = '-*-notexist-*'
554
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
Bram Moolenaarce90e362019-09-08 18:58:44 +0200717 eval 10->test_scrollbar('left', 0)
Bram Moolenaarab186732018-09-14 21:27:06 +0200718 redraw
719 call assert_equal(1, winline())
720 call assert_equal(11, line('.'))
721
722 " scroll to move line 1 at top, cursor stays in line 11
723 call test_scrollbar('right', 0, 0)
724 redraw
725 call assert_equal(11, winline())
726 call assert_equal(11, line('.'))
727
728 set nowrap
729 call setline(11, repeat('x', 150))
730 redraw
731 call assert_equal(1, wincol())
Bram Moolenaarf6d50f12019-06-06 15:40:08 +0200732 set number
733 redraw
734 call assert_equal(5, wincol())
735 set nonumber
736 redraw
Bram Moolenaarab186732018-09-14 21:27:06 +0200737 call assert_equal(1, col('.'))
738
739 " scroll to character 11, cursor is moved
740 call test_scrollbar('hor', 10, 0)
741 redraw
742 call assert_equal(1, wincol())
Bram Moolenaarf6d50f12019-06-06 15:40:08 +0200743 set number
744 redraw
745 call assert_equal(5, wincol())
746 set nonumber
747 redraw
Bram Moolenaarab186732018-09-14 21:27:06 +0200748 call assert_equal(11, col('.'))
749
750 set guioptions&
751 set wrap&
752 bwipe!
753endfunc
754
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100755func Test_menu()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100756 CheckFeature quickfix
757
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100758 " Check Help menu exists
759 let help_menu = execute('menu Help')
760 call assert_match('Overview', help_menu)
761
762 " Check Help menu works
763 emenu Help.Overview
764 call assert_equal('help', &buftype)
765 close
766
767 " Check deleting menu doesn't cause trouble.
768 aunmenu Help
Bram Moolenaarb45cd362020-09-28 21:41:49 +0200769 if exists(':tlmenu')
770 tlunmenu Help
771 endif
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100772 call assert_fails('menu Help', 'E329:')
773endfunc
774
Bram Moolenaar87748452017-03-12 17:10:33 +0100775func Test_set_guipty()
776 let guipty_saved = &guipty
777
778 " Default Value
779 set guipty&
780 call assert_equal(1, &guipty)
781
782 set noguipty
783 call assert_equal(0, &guipty)
784
785 let &guipty = guipty_saved
Bram Moolenaar6f785742017-02-06 22:11:55 +0100786endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100787
Bram Moolenaarc701f322019-03-28 21:49:21 +0100788func Test_encoding_conversion()
789 " GTK supports conversion between 'encoding' and "utf-8"
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200790 CheckFeature gui_gtk
791 let encoding_saved = &encoding
792 set encoding=latin1
Bram Moolenaarc701f322019-03-28 21:49:21 +0100793
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200794 " would be nice if we could take a screenshot
795 intro
796 " sets the window title
797 edit SomeFile
Bram Moolenaarc701f322019-03-28 21:49:21 +0100798
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200799 let &encoding = encoding_saved
Bram Moolenaarc701f322019-03-28 21:49:21 +0100800endfunc
801
Bram Moolenaar877e9572016-08-04 20:05:50 +0200802func Test_shell_command()
803 new
Bram Moolenaar9d5b8762016-08-04 21:21:13 +0200804 r !echo hello
805 call assert_equal('hello', substitute(getline(2), '\W', '', 'g'))
Bram Moolenaar877e9572016-08-04 20:05:50 +0200806 bwipe!
Bram Moolenaar877e9572016-08-04 20:05:50 +0200807endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100808
Bram Moolenaar87748452017-03-12 17:10:33 +0100809func Test_syntax_colortest()
810 runtime syntax/colortest.vim
811 redraw!
812 sleep 200m
813 bwipe!
814endfunc
815
816func Test_set_term()
817 " It's enough to check the current value since setting 'term' to anything
818 " other than builtin_gui makes no sense at all.
819 call assert_equal('builtin_gui', &term)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200820 call assert_fails('set term=xterm', 'E530:')
Bram Moolenaar87748452017-03-12 17:10:33 +0100821endfunc
822
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100823func Test_windowid_variable()
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100824 if g:x11_based_gui || has('win32')
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100825 call assert_true(v:windowid > 0)
826 else
827 call assert_equal(0, v:windowid)
828 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +0100829endfunc
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200830
831" Test "vim -g" and also the GUIEnter autocommand.
832func Test_gui_dash_g()
833 let cmd = GetVimCommand('Xscriptgui')
834 call writefile([""], "Xtestgui")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200835 let lines =<< trim END
836 au GUIEnter * call writefile(["insertmode: " . &insertmode], "Xtestgui")
837 au GUIEnter * qall
838 END
839 call writefile(lines, 'Xscriptgui')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200840 call system(cmd . ' -g')
841 call WaitForAssert({-> assert_equal(['insertmode: 0'], readfile('Xtestgui'))})
842
843 call delete('Xscriptgui')
844 call delete('Xtestgui')
845endfunc
846
847" Test "vim -7" and also the GUIEnter autocommand.
848func Test_gui_dash_y()
849 let cmd = GetVimCommand('Xscriptgui')
850 call writefile([""], "Xtestgui")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200851 let lines =<< trim END
852 au GUIEnter * call writefile(["insertmode: " . &insertmode], "Xtestgui")
853 au GUIEnter * qall
854 END
855 call writefile(lines, 'Xscriptgui')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200856 call system(cmd . ' -y')
857 call WaitForAssert({-> assert_equal(['insertmode: 1'], readfile('Xtestgui'))})
858
859 call delete('Xscriptgui')
860 call delete('Xtestgui')
861endfunc
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100862
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200863" Test for "!" option in 'guioptions'. Use a terminal for running external
864" commands
865func Test_gui_run_cmd_in_terminal()
Bram Moolenaar4457e1d2020-05-15 22:46:44 +0200866 CheckFeature terminal
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200867 let save_guioptions = &guioptions
868 set guioptions+=!
869 if has('win32')
870 let cmd = 'type'
871 else
872 " assume all the other systems have a cat command
873 let cmd = 'cat'
874 endif
Bram Moolenaar98f16712020-05-22 13:34:01 +0200875 exe "silent !" . cmd . " test_gui.vim"
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200876 " TODO: how to check that the command ran in a separate terminal?
877 " Maybe check for $TERM (dumb vs xterm) in the spawned shell?
878 let &guioptions = save_guioptions
879endfunc
880
Bram Moolenaar46cd43b2020-06-04 22:22:11 +0200881func Test_gui_recursive_mapping()
882 nmap ' <C-W>
883 nmap <C-W>a :let didit = 1<CR>
884 call feedkeys("'a", 'xt')
885 call assert_equal(1, didit)
886
887 nunmap '
888 nunmap <C-W>a
889endfunc
890
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200891" Test GUI mouse events
892func Test_gui_mouse_event()
893 set mousemodel=extend
894 call test_override('no_query_mouse', 1)
895 new
896 call setline(1, ['one two three', 'four five six'])
897
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200898 " place the cursor using left click in normal mode
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200899 call cursor(1, 1)
900 call test_gui_mouse_event(0, 2, 4, 0, 0)
901 call test_gui_mouse_event(3, 2, 4, 0, 0)
902 call feedkeys("\<Esc>", 'Lx!')
903 call assert_equal([0, 2, 4, 0], getpos('.'))
904
905 " select and yank a word
906 let @" = ''
907 call test_gui_mouse_event(0, 1, 9, 0, 0)
908 call test_gui_mouse_event(0, 1, 9, 1, 0)
909 call test_gui_mouse_event(3, 1, 9, 0, 0)
910 call feedkeys("y", 'Lx!')
911 call assert_equal('three', @")
912
913 " create visual selection using right click
914 let @" = ''
915 call test_gui_mouse_event(0, 2, 6, 0, 0)
916 call test_gui_mouse_event(3, 2, 6, 0, 0)
917 call test_gui_mouse_event(2, 2, 13, 0, 0)
918 call test_gui_mouse_event(3, 2, 13, 0, 0)
919 call feedkeys("y", 'Lx!')
920 call assert_equal('five six', @")
921
922 " paste using middle mouse button
923 let @* = 'abc '
924 call feedkeys('""', 'Lx!')
925 call test_gui_mouse_event(1, 1, 9, 0, 0)
926 call test_gui_mouse_event(3, 1, 9, 0, 0)
927 call feedkeys("\<Esc>", 'Lx!')
928 call assert_equal(['one two abc three', 'four five six'], getline(1, '$'))
929
930 " extend visual selection using right click in visual mode
931 let @" = ''
932 call cursor(1, 1)
933 call feedkeys('v', 'Lx!')
934 call test_gui_mouse_event(2, 1, 17, 0, 0)
935 call test_gui_mouse_event(3, 1, 17, 0, 0)
936 call feedkeys("y", 'Lx!')
937 call assert_equal('one two abc three', @")
938
939 " extend visual selection using mouse drag
940 let @" = ''
941 call cursor(1, 1)
942 call test_gui_mouse_event(0, 2, 1, 0, 0)
943 call test_gui_mouse_event(0x43, 2, 9, 0, 0)
944 call test_gui_mouse_event(0x3, 2, 9, 0, 0)
945 call feedkeys("y", 'Lx!')
946 call assert_equal('four five', @")
947
948 " select text by moving the mouse
949 let @" = ''
950 call cursor(1, 1)
951 redraw!
952 call test_gui_mouse_event(0, 1, 4, 0, 0)
953 call test_gui_mouse_event(0x700, 1, 9, 0, 0)
954 call test_gui_mouse_event(0x700, 1, 13, 0, 0)
955 call test_gui_mouse_event(0x3, 1, 13, 0, 0)
956 call feedkeys("y", 'Lx!')
957 call assert_equal(' two abc t', @")
958
959 " Using mouse in insert mode
960 call cursor(1, 1)
961 call feedkeys('i', 't')
962 call test_gui_mouse_event(0, 2, 11, 0, 0)
963 call test_gui_mouse_event(3, 2, 11, 0, 0)
964 call feedkeys("po\<Esc>", 'Lx!')
965 call assert_equal(['one two abc three', 'four five posix'], getline(1, '$'))
966
967 %d _
968 call setline(1, range(1, 100))
969 " scroll up
970 call test_gui_mouse_event(0x200, 2, 1, 0, 0)
971 call test_gui_mouse_event(0x200, 2, 1, 0, 0)
972 call test_gui_mouse_event(0x200, 2, 1, 0, 0)
973 call feedkeys("H", 'Lx!')
974 call assert_equal(10, line('.'))
975
976 " scroll down
977 call test_gui_mouse_event(0x100, 2, 1, 0, 0)
978 call test_gui_mouse_event(0x100, 2, 1, 0, 0)
979 call feedkeys("H", 'Lx!')
980 call assert_equal(4, line('.'))
981
982 %d _
983 set nowrap
984 call setline(1, range(10)->join('')->repeat(10))
985 " scroll left
986 call test_gui_mouse_event(0x500, 1, 5, 0, 0)
987 call test_gui_mouse_event(0x500, 1, 10, 0, 0)
988 call test_gui_mouse_event(0x500, 1, 15, 0, 0)
989 call feedkeys('g0', 'Lx!')
990 call assert_equal(19, col('.'))
991
992 " scroll right
993 call test_gui_mouse_event(0x600, 1, 15, 0, 0)
994 call test_gui_mouse_event(0x600, 1, 10, 0, 0)
995 call feedkeys('g0', 'Lx!')
996 call assert_equal(7, col('.'))
997 set wrap&
998
999 %d _
1000 call setline(1, repeat([repeat('a', 60)], 10))
1001
1002 " record various mouse events
1003 let mouseEventNames = [
1004 \ 'LeftMouse', 'LeftRelease', '2-LeftMouse', '3-LeftMouse',
1005 \ 'S-LeftMouse', 'A-LeftMouse', 'C-LeftMouse', 'MiddleMouse',
1006 \ 'MiddleRelease', '2-MiddleMouse', '3-MiddleMouse',
1007 \ 'S-MiddleMouse', 'A-MiddleMouse', 'C-MiddleMouse',
1008 \ 'RightMouse', 'RightRelease', '2-RightMouse',
1009 \ '3-RightMouse', 'S-RightMouse', 'A-RightMouse', 'C-RightMouse',
1010 \ 'X1Mouse', 'S-X1Mouse', 'A-X1Mouse', 'C-X1Mouse', 'X2Mouse',
1011 \ 'S-X2Mouse', 'A-X2Mouse', 'C-X2Mouse'
1012 \ ]
1013 let mouseEventCodes = map(copy(mouseEventNames), "'<' .. v:val .. '>'")
1014 let g:events = []
1015 for e in mouseEventCodes
1016 exe 'nnoremap ' .. e .. ' <Cmd>call add(g:events, "' ..
1017 \ substitute(e, '[<>]', '', 'g') .. '")<CR>'
1018 endfor
1019
1020 " Test various mouse buttons (0 - Left, 1 - Middle, 2 - Right, 0x300 - X1,
1021 " 0x300- X2)
1022 for button in [0, 1, 2, 0x300, 0x400]
1023 " Single click
1024 call test_gui_mouse_event(button, 2, 5, 0, 0)
1025 call test_gui_mouse_event(3, 2, 5, 0, 0)
1026
1027 " Double/Triple click is supported by only the Left/Middle/Right mouse
1028 " buttons
1029 if button <= 2
1030 " Double Click
1031 call test_gui_mouse_event(button, 2, 5, 0, 0)
1032 call test_gui_mouse_event(button, 2, 5, 1, 0)
1033 call test_gui_mouse_event(3, 2, 5, 0, 0)
1034
1035 " Triple Click
1036 call test_gui_mouse_event(button, 2, 5, 0, 0)
1037 call test_gui_mouse_event(button, 2, 5, 1, 0)
1038 call test_gui_mouse_event(button, 2, 5, 1, 0)
1039 call test_gui_mouse_event(3, 2, 5, 0, 0)
1040 endif
1041
1042 " Shift click
1043 call test_gui_mouse_event(button, 3, 7, 0, 4)
1044 call test_gui_mouse_event(3, 3, 7, 0, 4)
1045
1046 " Alt click
1047 call test_gui_mouse_event(button, 3, 7, 0, 8)
1048 call test_gui_mouse_event(3, 3, 7, 0, 8)
1049
1050 " Ctrl click
1051 call test_gui_mouse_event(button, 3, 7, 0, 16)
1052 call test_gui_mouse_event(3, 3, 7, 0, 16)
1053
1054 call feedkeys("\<Esc>", 'Lx!')
1055 endfor
1056
1057 call assert_equal(['LeftMouse', 'LeftRelease', 'LeftMouse', '2-LeftMouse',
1058 \ 'LeftMouse', '2-LeftMouse', '3-LeftMouse', 'S-LeftMouse',
1059 \ 'A-LeftMouse', 'C-LeftMouse', 'MiddleMouse', 'MiddleRelease',
1060 \ 'MiddleMouse', '2-MiddleMouse', 'MiddleMouse', '2-MiddleMouse',
1061 \ '3-MiddleMouse', 'S-MiddleMouse', 'A-MiddleMouse', 'C-MiddleMouse',
1062 \ 'RightMouse', 'RightRelease', 'RightMouse', '2-RightMouse',
1063 \ 'RightMouse', '2-RightMouse', '3-RightMouse', 'S-RightMouse',
1064 \ 'A-RightMouse', 'C-RightMouse', 'X1Mouse', 'S-X1Mouse', 'A-X1Mouse',
1065 \ 'C-X1Mouse', 'X2Mouse', 'S-X2Mouse', 'A-X2Mouse', 'C-X2Mouse'],
1066 \ g:events)
1067
1068 for e in mouseEventCodes
1069 exe 'nunmap ' .. e
1070 endfor
1071
1072 " modeless selection
1073 set mouse=
1074 let save_guioptions = &guioptions
1075 set guioptions+=A
1076 %d _
1077 call setline(1, ['one two three', 'four five sixteen'])
1078 call cursor(1, 1)
1079 redraw!
1080 " Double click should select the word and copy it to clipboard
1081 let @* = ''
1082 call test_gui_mouse_event(0, 2, 11, 0, 0)
1083 call test_gui_mouse_event(0, 2, 11, 1, 0)
1084 call test_gui_mouse_event(3, 2, 11, 0, 0)
1085 call feedkeys("\<Esc>", 'Lx!')
1086 call assert_equal([0, 1, 1, 0], getpos('.'))
1087 call assert_equal('sixteen', @*)
1088 " Right click should extend the selection from cursor
1089 call cursor(1, 6)
1090 redraw!
1091 let @* = ''
1092 call test_gui_mouse_event(2, 1, 11, 0, 0)
1093 call test_gui_mouse_event(3, 1, 11, 0, 0)
1094 call feedkeys("\<Esc>", 'Lx!')
1095 call assert_equal([0, 1, 6, 0], getpos('.'))
1096 call assert_equal('wo thr', @*)
1097 " Middle click should paste the clipboard contents
1098 call cursor(2, 1)
1099 redraw!
1100 call test_gui_mouse_event(1, 1, 11, 0, 0)
1101 call test_gui_mouse_event(3, 1, 11, 0, 0)
1102 call feedkeys("\<Esc>", 'Lx!')
1103 call assert_equal([0, 2, 7, 0], getpos('.'))
1104 call assert_equal('wo thrfour five sixteen', getline(2))
1105 set mouse&
1106 let &guioptions = save_guioptions
1107
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001108 " Test invalid parameters for test_gui_mouse_event()
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001109 call assert_fails('call test_gui_mouse_event("", 1, 2, 3, 4)', 'E1210:')
1110 call assert_fails('call test_gui_mouse_event(0, "", 2, 3, 4)', 'E1210:')
1111 call assert_fails('call test_gui_mouse_event(0, 1, "", 3, 4)', 'E1210:')
1112 call assert_fails('call test_gui_mouse_event(0, 1, 2, "", 4)', 'E1210:')
1113 call assert_fails('call test_gui_mouse_event(0, 1, 2, 3, "")', 'E1210:')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001114
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001115 bw!
1116 call test_override('no_query_mouse', 0)
1117 set mousemodel&
1118endfunc
1119
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001120" Test for 'guitablabel' and 'guitabtooltip' options
1121func TestGuiTabLabel()
1122 call add(g:TabLabels, v:lnum + 100)
1123 let bufnrlist = tabpagebuflist(v:lnum)
1124 return bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
1125endfunc
1126
1127func TestGuiTabToolTip()
1128 call add(g:TabToolTips, v:lnum + 200)
1129 let bufnrlist = tabpagebuflist(v:lnum)
1130 return bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
1131endfunc
1132
1133func Test_gui_tablabel_tooltip()
Bram Moolenaarfb773a32021-07-03 21:37:59 +02001134 CheckNotFeature gui_athena
1135
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001136 %bw!
1137 " Removing the tabline at the end of this test, reduces the window height by
1138 " one. Save and restore it after the test.
1139 let save_lines = &lines
1140 edit one
1141 set modified
1142 tabnew two
1143 set modified
1144 tabnew three
1145 set modified
1146 let g:TabLabels = []
1147 set guitablabel=%{TestGuiTabLabel()}
1148 call test_override('starting', 1)
1149 redrawtabline
1150 call test_override('starting', 0)
1151 call assert_true(index(g:TabLabels, 101) != -1)
1152 call assert_true(index(g:TabLabels, 102) != -1)
1153 call assert_true(index(g:TabLabels, 103) != -1)
1154 set guitablabel&
1155 unlet g:TabLabels
1156
1157 if has('gui_gtk')
1158 " Only on GTK+, the tooltip function is called even if the mouse is not
1159 " on the tabline. on Win32 and Motif, the tooltip function is called only
1160 " when the mouse pointer is over the tabline.
1161 let g:TabToolTips = []
1162 set guitabtooltip=%{TestGuiTabToolTip()}
1163 call test_override('starting', 1)
1164 redrawtabline
1165 call test_override('starting', 0)
1166 call assert_true(index(g:TabToolTips, 201) != -1)
1167 call assert_true(index(g:TabToolTips, 202) != -1)
1168 call assert_true(index(g:TabToolTips, 203) != -1)
1169 set guitabtooltip&
1170 unlet g:TabToolTips
1171 endif
1172 %bw!
1173 let &lines = save_lines
1174endfunc
1175
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001176" Test for dropping files into a window in GUI
1177func DropFilesInCmdLine()
Bram Moolenaar1d1ce612021-06-27 19:02:52 +02001178 CheckFeature drop_file
1179
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001180 call feedkeys(":\"", 'L')
1181 call test_gui_drop_files(['a.c', 'b.c'], &lines, 1, 0)
1182 call feedkeys("\<CR>", 'L')
1183endfunc
1184
1185func Test_gui_drop_files()
Bram Moolenaar1d1ce612021-06-27 19:02:52 +02001186 CheckFeature drop_file
1187
Yegappan Lakshmanan5bca9062021-07-24 21:33:26 +02001188 call assert_fails('call test_gui_drop_files(1, 1, 1, 0)', 'E1211:')
1189 call assert_fails('call test_gui_drop_files(["x"], "", 1, 0)', 'E1210:')
1190 call assert_fails('call test_gui_drop_files(["x"], 1, "", 0)', 'E1210:')
1191 call assert_fails('call test_gui_drop_files(["x"], 1, 1, "")', 'E1210:')
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001192
1193 %bw!
1194 %argdelete
1195 call test_gui_drop_files([], 1, 1, 0)
1196 call assert_equal([], argv())
1197 call test_gui_drop_files([1, 2], 1, 1, 0)
1198 call assert_equal([], argv())
1199
1200 call test_gui_drop_files(['a.c', 'b.c'], 1, 1, 0)
1201 call assert_equal(['a.c', 'b.c'], argv())
1202 %bw!
1203 %argdelete
1204 call test_gui_drop_files([], 1, 1, 0)
1205 call assert_equal([], argv())
1206 %bw!
1207 " if the buffer in the window is modified, then the file should be opened in
1208 " a new window
1209 set modified
1210 call test_gui_drop_files(['x.c', 'y.c'], 1, 1, 0)
1211 call assert_equal(['x.c', 'y.c'], argv())
1212 call assert_equal(2, winnr('$'))
1213 call assert_equal('x.c', bufname(winbufnr(1)))
1214 %bw!
1215 %argdelete
1216 " if Ctrl is pressed, then the file should be opened in a new window
1217 call test_gui_drop_files(['s.py', 't.py'], 1, 1, 0x10)
1218 call assert_equal(['s.py', 't.py'], argv())
1219 call assert_equal(2, winnr('$'))
1220 call assert_equal('s.py', bufname(winbufnr(1)))
1221 %bw!
1222 %argdelete
1223 " drop the files in a non-current window
1224 belowright new
1225 call test_gui_drop_files(['a.py', 'b.py'], 1, 1, 0)
1226 call assert_equal(['a.py', 'b.py'], argv())
1227 call assert_equal(2, winnr('$'))
1228 call assert_equal(1, winnr())
1229 call assert_equal('a.py', bufname(winbufnr(1)))
1230 %bw!
1231 %argdelete
1232 " pressing shift when dropping files should change directory
1233 let save_cwd = getcwd()
1234 call mkdir('Xdir1')
1235 call writefile([], 'Xdir1/Xfile1')
1236 call writefile([], 'Xdir1/Xfile2')
1237 call test_gui_drop_files(['Xdir1/Xfile1', 'Xdir1/Xfile2'], 1, 1, 0x4)
1238 call assert_equal('Xdir1', fnamemodify(getcwd(), ':t'))
1239 call assert_equal('Xfile1', @%)
1240 call chdir(save_cwd)
1241 " pressing shift when dropping directory and files should change directory
1242 call test_gui_drop_files(['Xdir1', 'Xdir1/Xfile2'], 1, 1, 0x4)
1243 call assert_equal('Xdir1', fnamemodify(getcwd(), ':t'))
1244 call assert_equal('Xdir1', fnamemodify(@%, ':t'))
1245 call chdir(save_cwd)
1246 %bw!
1247 %argdelete
1248 " dropping a directory should edit it
1249 call test_gui_drop_files(['Xdir1'], 1, 1, 0)
1250 call assert_equal('Xdir1', @%)
1251 %bw!
1252 %argdelete
1253 " dropping only a directory name with Shift should ignore it
1254 call test_gui_drop_files(['Xdir1'], 1, 1, 0x4)
1255 call assert_equal('', @%)
1256 %bw!
1257 %argdelete
1258 call delete('Xdir1', 'rf')
1259 " drop files in the command line. The GUI drop files adds the file names to
1260 " the low level input buffer. So need to use a cmdline map and feedkeys()
1261 " with 'Lx!' to process it in this function itself.
1262 cnoremap <expr> <buffer> <F4> DropFilesInCmdLine()
1263 call feedkeys(":\"\<F4>\<CR>", 'xt')
1264 call feedkeys('k', 'Lx!')
1265 call assert_equal('"a.c b.c', @:)
1266 cunmap <buffer> <F4>
1267endfunc
1268
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001269" Test for generating a GUI tabline event to select a tab page
1270func Test_gui_tabline_event()
1271 %bw!
1272 edit Xfile1
1273 tabedit Xfile2
1274 tabedit Xfile3
1275
1276 tabfirst
1277 call assert_equal(v:true, test_gui_tabline_event(2))
1278 call feedkeys("y", "Lx!")
1279 call assert_equal(2, tabpagenr())
1280 call assert_equal(v:true, test_gui_tabline_event(3))
1281 call feedkeys("y", "Lx!")
1282 call assert_equal(3, tabpagenr())
1283 call assert_equal(v:false, test_gui_tabline_event(3))
1284
1285 " From the cmdline window, tabline event should not be handled
1286 call feedkeys("q::let t = test_gui_tabline_event(2)\<CR>:q\<CR>", 'x!')
1287 call assert_equal(v:false, t)
1288
1289 %bw!
1290endfunc
1291
1292" Test for generating a GUI tabline menu event to execute an action
1293func Test_gui_tabmenu_event()
1294 %bw!
1295
1296 " Try to close the last tab page
1297 call test_gui_tabmenu_event(1, 1)
1298 call feedkeys("y", "Lx!")
1299
1300 edit Xfile1
1301 tabedit Xfile2
1302 call test_gui_tabmenu_event(1, 1)
1303 call feedkeys("y", "Lx!")
1304 call assert_equal(1, tabpagenr('$'))
1305 call assert_equal('Xfile2', bufname())
1306
1307 call test_gui_tabmenu_event(1, 2)
1308 call feedkeys("y", "Lx!")
1309 call assert_equal(2, tabpagenr('$'))
1310
1311 " If tabnr is 0, then the current tabpage should be used.
1312 call test_gui_tabmenu_event(0, 2)
1313 call feedkeys("y", "Lx!")
1314 call assert_equal(3, tabpagenr('$'))
1315 call test_gui_tabmenu_event(0, 1)
1316 call feedkeys("y", "Lx!")
1317 call assert_equal(2, tabpagenr('$'))
1318
1319 %bw!
1320endfunc
1321
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001322" vim: shiftwidth=2 sts=2 expandtab