blob: 133298db3ae16120a1f94302043d58a4b4415e13 [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
261 bwipe!
262
263 " Multiline support
264 if has('balloon_multiline')
265 " Multiline balloon using NL
266 new
267 func MyBalloonFuncForMultilineUsingNL()
268 return "Multiline\nSuppported\nBalloon\nusing NL"
269 endfunc
270 setl balloonexpr=MyBalloonFuncForMultilineUsingNL()
271 setl ballooneval
272 call assert_equal('MyBalloonFuncForMultilineUsingNL()', &balloonexpr)
273 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200274 " and check if the content of the balloon is the same as what is
Bram Moolenaard5841f22017-03-05 13:27:25 +0100275 " expected. Also, check if textlock works as expected.
276 setl balloonexpr&
277 delfunc MyBalloonFuncForMultilineUsingNL
278 bwipe!
279
280 " Multiline balloon using List
281 new
282 func MyBalloonFuncForMultilineUsingList()
283 return [ 'Multiline', 'Suppported', 'Balloon', 'using List' ]
284 endfunc
285 setl balloonexpr=MyBalloonFuncForMultilineUsingList()
286 setl ballooneval
287 call assert_equal('MyBalloonFuncForMultilineUsingList()', &balloonexpr)
288 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200289 " and check if the content of the balloon is the same as what is
Bram Moolenaard5841f22017-03-05 13:27:25 +0100290 " expected. Also, check if textlock works as expected.
291 setl balloonexpr&
292 delfunc MyBalloonFuncForMultilineUsingList
293 bwipe!
294 endif
295
296 let &balloonexpr = balloonexpr_saved
297endfunc
298
Bram Moolenaar87748452017-03-12 17:10:33 +0100299" Invalid arguments are tested with test_options in conjunction with segfaults
300" caused by them (Patch 8.0.0357, 24922ec233).
301func Test_set_guicursor()
302 let guicursor_saved = &guicursor
303
304 let default = [
305 \ "n-v-c:block-Cursor/lCursor",
306 \ "ve:ver35-Cursor",
307 \ "o:hor50-Cursor",
308 \ "i-ci:ver25-Cursor/lCursor",
309 \ "r-cr:hor20-Cursor/lCursor",
310 \ "sm:block-Cursor-blinkwait175-blinkoff150-blinkon175"
311 \ ]
312
313 " Default Value
314 set guicursor&
315 call assert_equal(join(default, ','), &guicursor)
316
317 " Argument List Example 1
318 let opt_list = copy(default)
319 let opt_list[0] = "n-c-v:block-nCursor"
320 exec "set guicursor=" . join(opt_list, ',')
321 call assert_equal(join(opt_list, ','), &guicursor)
322 unlet opt_list
323
324 " Argument List Example 2
325 let opt_list = copy(default)
326 let opt_list[3] = "i-ci:ver30-iCursor-blinkwait300-blinkon200-blinkoff150"
327 exec "set guicursor=" . join(opt_list, ',')
328 call assert_equal(join(opt_list, ','), &guicursor)
329 unlet opt_list
330
331 " 'a' Mode
332 set guicursor&
333 let &guicursor .= ',a:blinkon0'
334 call assert_equal(join(default, ',') . ",a:blinkon0", &guicursor)
335
336 let &guicursor = guicursor_saved
337endfunc
338
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100339func Test_set_guifont_errors()
340 if has('win32')
341 " Invalid font names are accepted in GTK GUI
342 call assert_fails('set guifont=xa1bc23d7f', 'E596:')
343 endif
344
345 " This only works if 'renderoptions' exists and does not work for Windows XP
346 " and older.
347 if exists('+renderoptions') && windowsversion() !~ '^[345]\.'
348 " doing this four times used to cause a crash
349 set renderoptions=type:directx
350 for i in range(5)
351 set guifont=
352 endfor
353 set renderoptions=
354 for i in range(5)
355 set guifont=
356 endfor
357 endif
358endfunc
359
Bram Moolenaar43dded82017-02-09 16:06:17 +0100360func Test_set_guifont()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100361 CheckX11BasedGui
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100362
363 let guifont_saved = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100364 if has('xfontset')
365 " Prevent 'guifontset' from canceling 'guifont'.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100366 let guifontset_saved = &guifontset
Bram Moolenaar43dded82017-02-09 16:06:17 +0100367 set guifontset=
368 endif
369
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100370 if has('gui_athena') || has('gui_motif')
Bram Moolenaar43dded82017-02-09 16:06:17 +0100371 " Non-empty font list with invalid font names.
372 "
373 " This test is twofold: (1) It checks if the command fails as expected
374 " when there are no loadable fonts found in the list. (2) It checks if
375 " 'guifont' remains the same after the command loads none of the fonts
376 " listed.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100377 let flist = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100378 call assert_fails('set guifont=-notexist1-*,-notexist2-*')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100379 call assert_equal(flist, &guifont)
Bram Moolenaar43dded82017-02-09 16:06:17 +0100380
381 " Non-empty font list with a valid font name. Should pick up the first
382 " valid font.
383 set guifont=-notexist1-*,fixed,-notexist2-*
Bram Moolenaar87748452017-03-12 17:10:33 +0100384 let pat = '\(fixed\)\|\(\c-Misc-Fixed-Medium-R-SemiCondensed--13-120-75-75-C-60-ISO8859-1\)'
385 call assert_match(pat, getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100386
387 " Empty list. Should fallback to the built-in default.
388 set guifont=
Bram Moolenaar87748452017-03-12 17:10:33 +0100389 let pat = '\(7x13\)\|\(\c-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1\)'
390 call assert_match(pat, getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100391
392 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
393 " For GTK, what we refer to as 'font names' in our manual are actually
394 " 'initial font patterns'. A valid font which matches the 'canonical font
395 " pattern' constructed from a given 'initial pattern' is to be looked up
396 " and loaded. That explains why the GTK GUIs appear to accept 'invalid
397 " font names'.
398 "
399 " Non-empty list. Should always pick up the first element, no matter how
400 " strange it is, as explained above.
401 set guifont=(´・ω・`)\ 12,Courier\ 12
402 call assert_equal('(´・ω・`) 12', getfontname())
403
404 " Empty list. Should fallback to the built-in default.
405 set guifont=
406 call assert_equal('Monospace 10', getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100407 endif
408
409 if has('xfontset')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100410 let &guifontset = guifontset_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100411 endif
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100412 let &guifont = guifont_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100413endfunc
414
Bram Moolenaar10434672017-02-12 19:59:08 +0100415func Test_set_guifontset()
Bram Moolenaarce90e362019-09-08 18:58:44 +0200416 CheckFeature xfontset
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100417 let skipped = ''
Bram Moolenaar10434672017-02-12 19:59:08 +0100418
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200419 call assert_fails('set guifontset=*', 'E597:')
420
Bram Moolenaarce90e362019-09-08 18:58:44 +0200421 let ctype_saved = v:ctype
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100422
Bram Moolenaarce90e362019-09-08 18:58:44 +0200423 " First, since XCreateFontSet(3) is very sensitive to locale, fonts must
424 " be chosen meticulously.
425 let font_head = '-misc-fixed-medium-r-normal--14'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100426
Bram Moolenaarce90e362019-09-08 18:58:44 +0200427 let font_aw70 = font_head . '-130-75-75-c-70'
428 let font_aw140 = font_head . '-130-75-75-c-140'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100429
Bram Moolenaarce90e362019-09-08 18:58:44 +0200430 let font_jisx0201 = font_aw70 . '-jisx0201.1976-0'
431 let font_jisx0208 = font_aw140 . '-jisx0208.1983-0'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100432
Bram Moolenaarce90e362019-09-08 18:58:44 +0200433 let full_XLFDs = join([ font_jisx0208, font_jisx0201 ], ',')
434 let short_XLFDs = join([ font_aw140, font_aw70 ], ',')
435 let singleton = font_head . '-*'
436 let aliases = 'k14,r14'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100437
Bram Moolenaarce90e362019-09-08 18:58:44 +0200438 " Second, among 'locales', look up such a locale that gets 'set
439 " guifontset=' to work successfully with every fontset specified with
440 " 'fontsets'.
441 let locales = [ 'ja_JP.UTF-8', 'ja_JP.eucJP', 'ja_JP.SJIS' ]
442 let fontsets = [ full_XLFDs, short_XLFDs, singleton, aliases ]
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100443
Bram Moolenaarce90e362019-09-08 18:58:44 +0200444 let feasible = 0
445 for locale in locales
446 try
447 exec 'language ctype' locale
448 catch /^Vim\%((\a\+)\)\=:E197/
449 continue
450 endtry
451 let done = 0
452 for fontset in fontsets
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100453 try
Bram Moolenaarce90e362019-09-08 18:58:44 +0200454 exec 'set guifontset=' . fontset
455 catch /^Vim\%((\a\+)\)\=:E\%(250\|252\|234\|597\|598\)/
456 break
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100457 endtry
Bram Moolenaarce90e362019-09-08 18:58:44 +0200458 let done += 1
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100459 endfor
Bram Moolenaarce90e362019-09-08 18:58:44 +0200460 if done == len(fontsets)
461 let feasible = 1
462 break
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100463 endif
Bram Moolenaarce90e362019-09-08 18:58:44 +0200464 endfor
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100465
Bram Moolenaarce90e362019-09-08 18:58:44 +0200466 " Third, give a set of tests if it is found feasible.
467 if !feasible
468 let skipped = g:not_hosted
469 else
470 " N.B. 'v:ctype' has already been set to an appropriate value in the
471 " previous loop.
472 for fontset in fontsets
473 exec 'set guifontset=' . fontset
474 call assert_equal(fontset, &guifontset)
475 endfor
Bram Moolenaar10434672017-02-12 19:59:08 +0100476 endif
477
Bram Moolenaarce90e362019-09-08 18:58:44 +0200478 " Finally, restore ctype.
479 exec 'language ctype' ctype_saved
480
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100481 if !empty(skipped)
482 throw skipped
Bram Moolenaar10434672017-02-12 19:59:08 +0100483 endif
484endfunc
485
486func Test_set_guifontwide()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100487 CheckX11BasedGui
Bram Moolenaar10434672017-02-12 19:59:08 +0100488
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100489 call assert_fails('set guifontwide=*', 'E533:')
490
491 if has('gui_gtk')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100492 let guifont_saved = &guifont
493 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100494
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100495 let fc_match = exepath('fc-match')
496 if empty(fc_match)
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100497 let skipped = g:not_hosted
Bram Moolenaar10434672017-02-12 19:59:08 +0100498 else
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100499 let &guifont = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=en')
500 let wide = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=ja')
501 exec 'set guifontwide=' . fnameescape(wide)
502 call assert_equal(wide, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100503 endif
504
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100505 let &guifontwide = guifontwide_saved
506 let &guifont = guifont_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100507
508 elseif has('gui_athena') || has('gui_motif')
509 " guifontwide is premised upon the xfontset feature.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100510 if !has('xfontset')
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100511 let skipped = g:not_supported . 'xfontset'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100512 else
513 let encoding_saved = &encoding
514 let guifont_saved = &guifont
515 let guifontset_saved = &guifontset
516 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100517
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100518 let nfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-90-iso10646-1'
519 let wfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-180-iso10646-1'
Bram Moolenaar10434672017-02-12 19:59:08 +0100520
521 set encoding=utf-8
522
523 " Case 1: guifontset is empty
524 set guifontset=
525
526 " Case 1-1: Automatic selection
527 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100528 exec 'set guifont=' . nfont
529 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100530
531 " Case 1-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100532 exec 'set guifontwide=' . wfont
533 exec 'set guifont=' . nfont
534 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100535
536 " Case 2: guifontset is invalid
537 try
538 set guifontset=-*-notexist-*
Bram Moolenaar37175402017-03-18 20:18:45 +0100539 call assert_report("'set guifontset=-*-notexist-*' should have failed")
Bram Moolenaar10434672017-02-12 19:59:08 +0100540 catch
Bram Moolenaare2e40752020-09-04 21:18:46 +0200541 call assert_exception('E598:')
Bram Moolenaar10434672017-02-12 19:59:08 +0100542 endtry
543 " Set it to an invalid value brutally for preparation.
544 let &guifontset = '-*-notexist-*'
545
546 " Case 2-1: Automatic selection
547 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100548 exec 'set guifont=' . nfont
549 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100550
551 " Case 2-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100552 exec 'set guifontwide=' . wfont
553 exec 'set guifont=' . nfont
554 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100555
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100556 let &guifontwide = guifontwide_saved
557 let &guifontset = guifontset_saved
558 let &guifont = guifont_saved
559 let &encoding = encoding_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100560 endif
Bram Moolenaar10434672017-02-12 19:59:08 +0100561 endif
Bram Moolenaar10434672017-02-12 19:59:08 +0100562endfunc
563
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100564func Test_set_guiligatures()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100565 CheckX11BasedGui
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100566
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100567 if has('gui_gtk') || has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
568 " Try correct value
569 set guiligatures=<>=ab
570 call assert_equal("<>=ab", &guiligatures)
571 " Try to throw error
572 try
573 set guiligatures=<>=šab
574 call assert_report("'set guiligatures=<>=šab should have failed")
575 catch
576 call assert_exception('E1243:')
577 endtry
Dusan Popovic4eeedc02021-10-16 20:52:05 +0100578 endif
579endfunc
580
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100581func Test_set_guiheadroom()
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100582 CheckX11BasedGui
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100583
Bram Moolenaar40bd5a12021-10-16 21:58:27 +0100584 " Since this script is to be read together with '-U NONE', the default
585 " value must be preserved.
586 call assert_equal(50, &guiheadroom)
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100587endfunc
588
Bram Moolenaar87748452017-03-12 17:10:33 +0100589func Test_set_guioptions()
590 let guioptions_saved = &guioptions
591 let duration = '200m'
592
593 if has('win32')
594 " Default Value
595 set guioptions&
596 call assert_equal('egmrLtT', &guioptions)
597
598 else
599 " Default Value
600 set guioptions&
601 call assert_equal('aegimrLtT', &guioptions)
602
603 " To activate scrollbars of type 'L' or 'R'.
604 wincmd v
605 redraw!
606
607 " Remove all default GUI ornaments
608 set guioptions-=T
609 exec 'sleep' . duration
610 call assert_equal('aegimrLt', &guioptions)
611 set guioptions-=t
612 exec 'sleep' . duration
613 call assert_equal('aegimrL', &guioptions)
614 set guioptions-=L
615 exec 'sleep' . duration
616 call assert_equal('aegimr', &guioptions)
617 set guioptions-=r
618 exec 'sleep' . duration
619 call assert_equal('aegim', &guioptions)
620 set guioptions-=m
621 exec 'sleep' . duration
622 call assert_equal('aegi', &guioptions)
623
624 " Try non-default GUI ornaments
625 set guioptions+=l
626 exec 'sleep' . duration
627 call assert_equal('aegil', &guioptions)
628 set guioptions-=l
629 exec 'sleep' . duration
630 call assert_equal('aegi', &guioptions)
631
632 set guioptions+=R
633 exec 'sleep' . duration
634 call assert_equal('aegiR', &guioptions)
635 set guioptions-=R
636 exec 'sleep' . duration
637 call assert_equal('aegi', &guioptions)
638
639 set guioptions+=b
640 exec 'sleep' . duration
641 call assert_equal('aegib', &guioptions)
642 set guioptions+=h
643 exec 'sleep' . duration
644 call assert_equal('aegibh', &guioptions)
645 set guioptions-=h
646 exec 'sleep' . duration
647 call assert_equal('aegib', &guioptions)
648 set guioptions-=b
649 exec 'sleep' . duration
650 call assert_equal('aegi', &guioptions)
651
652 set guioptions+=v
653 exec 'sleep' . duration
654 call assert_equal('aegiv', &guioptions)
655 set guioptions-=v
656 exec 'sleep' . duration
657 call assert_equal('aegi', &guioptions)
658
659 if has('gui_motif')
660 set guioptions+=F
661 exec 'sleep' . duration
662 call assert_equal('aegiF', &guioptions)
663 set guioptions-=F
664 exec 'sleep' . duration
665 call assert_equal('aegi', &guioptions)
666 endif
667
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +0200668 if has('gui_gtk3')
669 set guioptions+=d
670 exec 'sleep' . duration
671 call assert_equal('aegid', &guioptions)
672 set guioptions-=d
673 exec 'sleep' . duration
674 call assert_equal('aegi', &guioptions)
675 endif
676
Bram Moolenaar87748452017-03-12 17:10:33 +0100677 " Restore GUI ornaments to the default state.
678 set guioptions+=m
679 exec 'sleep' . duration
680 call assert_equal('aegim', &guioptions)
681 set guioptions+=r
682 exec 'sleep' . duration
683 call assert_equal('aegimr', &guioptions)
684 set guioptions+=L
685 exec 'sleep' . duration
686 call assert_equal('aegimrL', &guioptions)
687 set guioptions+=t
688 exec 'sleep' . duration
689 call assert_equal('aegimrLt', &guioptions)
690 set guioptions+=T
691 exec 'sleep' . duration
692 call assert_equal("aegimrLtT", &guioptions)
693
694 wincmd o
695 redraw!
696 endif
697
698 let &guioptions = guioptions_saved
699endfunc
700
Bram Moolenaarab186732018-09-14 21:27:06 +0200701func Test_scrollbars()
702 new
703 " buffer with 200 lines
704 call setline(1, repeat(['one', 'two'], 100))
705 set guioptions+=rlb
706
707 " scroll to move line 11 at top, moves the cursor there
Bram Moolenaarce90e362019-09-08 18:58:44 +0200708 eval 10->test_scrollbar('left', 0)
Bram Moolenaarab186732018-09-14 21:27:06 +0200709 redraw
710 call assert_equal(1, winline())
711 call assert_equal(11, line('.'))
712
713 " scroll to move line 1 at top, cursor stays in line 11
714 call test_scrollbar('right', 0, 0)
715 redraw
716 call assert_equal(11, winline())
717 call assert_equal(11, line('.'))
718
719 set nowrap
720 call setline(11, repeat('x', 150))
721 redraw
722 call assert_equal(1, wincol())
Bram Moolenaarf6d50f12019-06-06 15:40:08 +0200723 set number
724 redraw
725 call assert_equal(5, wincol())
726 set nonumber
727 redraw
Bram Moolenaarab186732018-09-14 21:27:06 +0200728 call assert_equal(1, col('.'))
729
730 " scroll to character 11, cursor is moved
731 call test_scrollbar('hor', 10, 0)
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(11, col('.'))
740
741 set guioptions&
742 set wrap&
743 bwipe!
744endfunc
745
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100746func Test_menu()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100747 CheckFeature quickfix
748
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100749 " Check Help menu exists
750 let help_menu = execute('menu Help')
751 call assert_match('Overview', help_menu)
752
753 " Check Help menu works
754 emenu Help.Overview
755 call assert_equal('help', &buftype)
756 close
757
758 " Check deleting menu doesn't cause trouble.
759 aunmenu Help
Bram Moolenaarb45cd362020-09-28 21:41:49 +0200760 if exists(':tlmenu')
761 tlunmenu Help
762 endif
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100763 call assert_fails('menu Help', 'E329:')
764endfunc
765
Bram Moolenaar87748452017-03-12 17:10:33 +0100766func Test_set_guipty()
767 let guipty_saved = &guipty
768
769 " Default Value
770 set guipty&
771 call assert_equal(1, &guipty)
772
773 set noguipty
774 call assert_equal(0, &guipty)
775
776 let &guipty = guipty_saved
Bram Moolenaar6f785742017-02-06 22:11:55 +0100777endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100778
Bram Moolenaarc701f322019-03-28 21:49:21 +0100779func Test_encoding_conversion()
780 " GTK supports conversion between 'encoding' and "utf-8"
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200781 CheckFeature gui_gtk
782 let encoding_saved = &encoding
783 set encoding=latin1
Bram Moolenaarc701f322019-03-28 21:49:21 +0100784
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200785 " would be nice if we could take a screenshot
786 intro
787 " sets the window title
788 edit SomeFile
Bram Moolenaarc701f322019-03-28 21:49:21 +0100789
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200790 let &encoding = encoding_saved
Bram Moolenaarc701f322019-03-28 21:49:21 +0100791endfunc
792
Bram Moolenaar877e9572016-08-04 20:05:50 +0200793func Test_shell_command()
794 new
Bram Moolenaar9d5b8762016-08-04 21:21:13 +0200795 r !echo hello
796 call assert_equal('hello', substitute(getline(2), '\W', '', 'g'))
Bram Moolenaar877e9572016-08-04 20:05:50 +0200797 bwipe!
Bram Moolenaar877e9572016-08-04 20:05:50 +0200798endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100799
Bram Moolenaar87748452017-03-12 17:10:33 +0100800func Test_syntax_colortest()
801 runtime syntax/colortest.vim
802 redraw!
803 sleep 200m
804 bwipe!
805endfunc
806
807func Test_set_term()
808 " It's enough to check the current value since setting 'term' to anything
809 " other than builtin_gui makes no sense at all.
810 call assert_equal('builtin_gui', &term)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200811 call assert_fails('set term=xterm', 'E530:')
Bram Moolenaar87748452017-03-12 17:10:33 +0100812endfunc
813
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100814func Test_windowid_variable()
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100815 if g:x11_based_gui || has('win32')
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100816 call assert_true(v:windowid > 0)
817 else
818 call assert_equal(0, v:windowid)
819 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +0100820endfunc
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200821
822" Test "vim -g" and also the GUIEnter autocommand.
823func Test_gui_dash_g()
824 let cmd = GetVimCommand('Xscriptgui')
825 call writefile([""], "Xtestgui")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200826 let lines =<< trim END
827 au GUIEnter * call writefile(["insertmode: " . &insertmode], "Xtestgui")
828 au GUIEnter * qall
829 END
830 call writefile(lines, 'Xscriptgui')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200831 call system(cmd . ' -g')
832 call WaitForAssert({-> assert_equal(['insertmode: 0'], readfile('Xtestgui'))})
833
834 call delete('Xscriptgui')
835 call delete('Xtestgui')
836endfunc
837
838" Test "vim -7" and also the GUIEnter autocommand.
839func Test_gui_dash_y()
840 let cmd = GetVimCommand('Xscriptgui')
841 call writefile([""], "Xtestgui")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200842 let lines =<< trim END
843 au GUIEnter * call writefile(["insertmode: " . &insertmode], "Xtestgui")
844 au GUIEnter * qall
845 END
846 call writefile(lines, 'Xscriptgui')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200847 call system(cmd . ' -y')
848 call WaitForAssert({-> assert_equal(['insertmode: 1'], readfile('Xtestgui'))})
849
850 call delete('Xscriptgui')
851 call delete('Xtestgui')
852endfunc
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100853
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200854" Test for "!" option in 'guioptions'. Use a terminal for running external
855" commands
856func Test_gui_run_cmd_in_terminal()
Bram Moolenaar4457e1d2020-05-15 22:46:44 +0200857 CheckFeature terminal
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200858 let save_guioptions = &guioptions
859 set guioptions+=!
860 if has('win32')
861 let cmd = 'type'
862 else
863 " assume all the other systems have a cat command
864 let cmd = 'cat'
865 endif
Bram Moolenaar98f16712020-05-22 13:34:01 +0200866 exe "silent !" . cmd . " test_gui.vim"
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200867 " TODO: how to check that the command ran in a separate terminal?
868 " Maybe check for $TERM (dumb vs xterm) in the spawned shell?
869 let &guioptions = save_guioptions
870endfunc
871
Bram Moolenaar46cd43b2020-06-04 22:22:11 +0200872func Test_gui_recursive_mapping()
873 nmap ' <C-W>
874 nmap <C-W>a :let didit = 1<CR>
875 call feedkeys("'a", 'xt')
876 call assert_equal(1, didit)
877
878 nunmap '
879 nunmap <C-W>a
880endfunc
881
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200882" Test GUI mouse events
883func Test_gui_mouse_event()
884 set mousemodel=extend
885 call test_override('no_query_mouse', 1)
886 new
887 call setline(1, ['one two three', 'four five six'])
888
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200889 " place the cursor using left click in normal mode
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200890 call cursor(1, 1)
891 call test_gui_mouse_event(0, 2, 4, 0, 0)
892 call test_gui_mouse_event(3, 2, 4, 0, 0)
893 call feedkeys("\<Esc>", 'Lx!')
894 call assert_equal([0, 2, 4, 0], getpos('.'))
895
896 " select and yank a word
897 let @" = ''
898 call test_gui_mouse_event(0, 1, 9, 0, 0)
899 call test_gui_mouse_event(0, 1, 9, 1, 0)
900 call test_gui_mouse_event(3, 1, 9, 0, 0)
901 call feedkeys("y", 'Lx!')
902 call assert_equal('three', @")
903
904 " create visual selection using right click
905 let @" = ''
906 call test_gui_mouse_event(0, 2, 6, 0, 0)
907 call test_gui_mouse_event(3, 2, 6, 0, 0)
908 call test_gui_mouse_event(2, 2, 13, 0, 0)
909 call test_gui_mouse_event(3, 2, 13, 0, 0)
910 call feedkeys("y", 'Lx!')
911 call assert_equal('five six', @")
912
913 " paste using middle mouse button
914 let @* = 'abc '
915 call feedkeys('""', 'Lx!')
916 call test_gui_mouse_event(1, 1, 9, 0, 0)
917 call test_gui_mouse_event(3, 1, 9, 0, 0)
918 call feedkeys("\<Esc>", 'Lx!')
919 call assert_equal(['one two abc three', 'four five six'], getline(1, '$'))
920
921 " extend visual selection using right click in visual mode
922 let @" = ''
923 call cursor(1, 1)
924 call feedkeys('v', 'Lx!')
925 call test_gui_mouse_event(2, 1, 17, 0, 0)
926 call test_gui_mouse_event(3, 1, 17, 0, 0)
927 call feedkeys("y", 'Lx!')
928 call assert_equal('one two abc three', @")
929
930 " extend visual selection using mouse drag
931 let @" = ''
932 call cursor(1, 1)
933 call test_gui_mouse_event(0, 2, 1, 0, 0)
934 call test_gui_mouse_event(0x43, 2, 9, 0, 0)
935 call test_gui_mouse_event(0x3, 2, 9, 0, 0)
936 call feedkeys("y", 'Lx!')
937 call assert_equal('four five', @")
938
939 " select text by moving the mouse
940 let @" = ''
941 call cursor(1, 1)
942 redraw!
943 call test_gui_mouse_event(0, 1, 4, 0, 0)
944 call test_gui_mouse_event(0x700, 1, 9, 0, 0)
945 call test_gui_mouse_event(0x700, 1, 13, 0, 0)
946 call test_gui_mouse_event(0x3, 1, 13, 0, 0)
947 call feedkeys("y", 'Lx!')
948 call assert_equal(' two abc t', @")
949
950 " Using mouse in insert mode
951 call cursor(1, 1)
952 call feedkeys('i', 't')
953 call test_gui_mouse_event(0, 2, 11, 0, 0)
954 call test_gui_mouse_event(3, 2, 11, 0, 0)
955 call feedkeys("po\<Esc>", 'Lx!')
956 call assert_equal(['one two abc three', 'four five posix'], getline(1, '$'))
957
958 %d _
959 call setline(1, range(1, 100))
960 " scroll up
961 call test_gui_mouse_event(0x200, 2, 1, 0, 0)
962 call test_gui_mouse_event(0x200, 2, 1, 0, 0)
963 call test_gui_mouse_event(0x200, 2, 1, 0, 0)
964 call feedkeys("H", 'Lx!')
965 call assert_equal(10, line('.'))
966
967 " scroll down
968 call test_gui_mouse_event(0x100, 2, 1, 0, 0)
969 call test_gui_mouse_event(0x100, 2, 1, 0, 0)
970 call feedkeys("H", 'Lx!')
971 call assert_equal(4, line('.'))
972
973 %d _
974 set nowrap
975 call setline(1, range(10)->join('')->repeat(10))
976 " scroll left
977 call test_gui_mouse_event(0x500, 1, 5, 0, 0)
978 call test_gui_mouse_event(0x500, 1, 10, 0, 0)
979 call test_gui_mouse_event(0x500, 1, 15, 0, 0)
980 call feedkeys('g0', 'Lx!')
981 call assert_equal(19, col('.'))
982
983 " scroll right
984 call test_gui_mouse_event(0x600, 1, 15, 0, 0)
985 call test_gui_mouse_event(0x600, 1, 10, 0, 0)
986 call feedkeys('g0', 'Lx!')
987 call assert_equal(7, col('.'))
988 set wrap&
989
990 %d _
991 call setline(1, repeat([repeat('a', 60)], 10))
992
993 " record various mouse events
994 let mouseEventNames = [
995 \ 'LeftMouse', 'LeftRelease', '2-LeftMouse', '3-LeftMouse',
996 \ 'S-LeftMouse', 'A-LeftMouse', 'C-LeftMouse', 'MiddleMouse',
997 \ 'MiddleRelease', '2-MiddleMouse', '3-MiddleMouse',
998 \ 'S-MiddleMouse', 'A-MiddleMouse', 'C-MiddleMouse',
999 \ 'RightMouse', 'RightRelease', '2-RightMouse',
1000 \ '3-RightMouse', 'S-RightMouse', 'A-RightMouse', 'C-RightMouse',
1001 \ 'X1Mouse', 'S-X1Mouse', 'A-X1Mouse', 'C-X1Mouse', 'X2Mouse',
1002 \ 'S-X2Mouse', 'A-X2Mouse', 'C-X2Mouse'
1003 \ ]
1004 let mouseEventCodes = map(copy(mouseEventNames), "'<' .. v:val .. '>'")
1005 let g:events = []
1006 for e in mouseEventCodes
1007 exe 'nnoremap ' .. e .. ' <Cmd>call add(g:events, "' ..
1008 \ substitute(e, '[<>]', '', 'g') .. '")<CR>'
1009 endfor
1010
1011 " Test various mouse buttons (0 - Left, 1 - Middle, 2 - Right, 0x300 - X1,
1012 " 0x300- X2)
1013 for button in [0, 1, 2, 0x300, 0x400]
1014 " Single click
1015 call test_gui_mouse_event(button, 2, 5, 0, 0)
1016 call test_gui_mouse_event(3, 2, 5, 0, 0)
1017
1018 " Double/Triple click is supported by only the Left/Middle/Right mouse
1019 " buttons
1020 if button <= 2
1021 " Double Click
1022 call test_gui_mouse_event(button, 2, 5, 0, 0)
1023 call test_gui_mouse_event(button, 2, 5, 1, 0)
1024 call test_gui_mouse_event(3, 2, 5, 0, 0)
1025
1026 " Triple Click
1027 call test_gui_mouse_event(button, 2, 5, 0, 0)
1028 call test_gui_mouse_event(button, 2, 5, 1, 0)
1029 call test_gui_mouse_event(button, 2, 5, 1, 0)
1030 call test_gui_mouse_event(3, 2, 5, 0, 0)
1031 endif
1032
1033 " Shift click
1034 call test_gui_mouse_event(button, 3, 7, 0, 4)
1035 call test_gui_mouse_event(3, 3, 7, 0, 4)
1036
1037 " Alt click
1038 call test_gui_mouse_event(button, 3, 7, 0, 8)
1039 call test_gui_mouse_event(3, 3, 7, 0, 8)
1040
1041 " Ctrl click
1042 call test_gui_mouse_event(button, 3, 7, 0, 16)
1043 call test_gui_mouse_event(3, 3, 7, 0, 16)
1044
1045 call feedkeys("\<Esc>", 'Lx!')
1046 endfor
1047
1048 call assert_equal(['LeftMouse', 'LeftRelease', 'LeftMouse', '2-LeftMouse',
1049 \ 'LeftMouse', '2-LeftMouse', '3-LeftMouse', 'S-LeftMouse',
1050 \ 'A-LeftMouse', 'C-LeftMouse', 'MiddleMouse', 'MiddleRelease',
1051 \ 'MiddleMouse', '2-MiddleMouse', 'MiddleMouse', '2-MiddleMouse',
1052 \ '3-MiddleMouse', 'S-MiddleMouse', 'A-MiddleMouse', 'C-MiddleMouse',
1053 \ 'RightMouse', 'RightRelease', 'RightMouse', '2-RightMouse',
1054 \ 'RightMouse', '2-RightMouse', '3-RightMouse', 'S-RightMouse',
1055 \ 'A-RightMouse', 'C-RightMouse', 'X1Mouse', 'S-X1Mouse', 'A-X1Mouse',
1056 \ 'C-X1Mouse', 'X2Mouse', 'S-X2Mouse', 'A-X2Mouse', 'C-X2Mouse'],
1057 \ g:events)
1058
1059 for e in mouseEventCodes
1060 exe 'nunmap ' .. e
1061 endfor
1062
1063 " modeless selection
1064 set mouse=
1065 let save_guioptions = &guioptions
1066 set guioptions+=A
1067 %d _
1068 call setline(1, ['one two three', 'four five sixteen'])
1069 call cursor(1, 1)
1070 redraw!
1071 " Double click should select the word and copy it to clipboard
1072 let @* = ''
1073 call test_gui_mouse_event(0, 2, 11, 0, 0)
1074 call test_gui_mouse_event(0, 2, 11, 1, 0)
1075 call test_gui_mouse_event(3, 2, 11, 0, 0)
1076 call feedkeys("\<Esc>", 'Lx!')
1077 call assert_equal([0, 1, 1, 0], getpos('.'))
1078 call assert_equal('sixteen', @*)
1079 " Right click should extend the selection from cursor
1080 call cursor(1, 6)
1081 redraw!
1082 let @* = ''
1083 call test_gui_mouse_event(2, 1, 11, 0, 0)
1084 call test_gui_mouse_event(3, 1, 11, 0, 0)
1085 call feedkeys("\<Esc>", 'Lx!')
1086 call assert_equal([0, 1, 6, 0], getpos('.'))
1087 call assert_equal('wo thr', @*)
1088 " Middle click should paste the clipboard contents
1089 call cursor(2, 1)
1090 redraw!
1091 call test_gui_mouse_event(1, 1, 11, 0, 0)
1092 call test_gui_mouse_event(3, 1, 11, 0, 0)
1093 call feedkeys("\<Esc>", 'Lx!')
1094 call assert_equal([0, 2, 7, 0], getpos('.'))
1095 call assert_equal('wo thrfour five sixteen', getline(2))
1096 set mouse&
1097 let &guioptions = save_guioptions
1098
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001099 " Test invalid parameters for test_gui_mouse_event()
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001100 call assert_fails('call test_gui_mouse_event("", 1, 2, 3, 4)', 'E1210:')
1101 call assert_fails('call test_gui_mouse_event(0, "", 2, 3, 4)', 'E1210:')
1102 call assert_fails('call test_gui_mouse_event(0, 1, "", 3, 4)', 'E1210:')
1103 call assert_fails('call test_gui_mouse_event(0, 1, 2, "", 4)', 'E1210:')
1104 call assert_fails('call test_gui_mouse_event(0, 1, 2, 3, "")', 'E1210:')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001105
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001106 bw!
1107 call test_override('no_query_mouse', 0)
1108 set mousemodel&
1109endfunc
1110
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001111" Test for 'guitablabel' and 'guitabtooltip' options
1112func TestGuiTabLabel()
1113 call add(g:TabLabels, v:lnum + 100)
1114 let bufnrlist = tabpagebuflist(v:lnum)
1115 return bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
1116endfunc
1117
1118func TestGuiTabToolTip()
1119 call add(g:TabToolTips, v:lnum + 200)
1120 let bufnrlist = tabpagebuflist(v:lnum)
1121 return bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
1122endfunc
1123
1124func Test_gui_tablabel_tooltip()
Bram Moolenaarfb773a32021-07-03 21:37:59 +02001125 CheckNotFeature gui_athena
1126
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001127 %bw!
1128 " Removing the tabline at the end of this test, reduces the window height by
1129 " one. Save and restore it after the test.
1130 let save_lines = &lines
1131 edit one
1132 set modified
1133 tabnew two
1134 set modified
1135 tabnew three
1136 set modified
1137 let g:TabLabels = []
1138 set guitablabel=%{TestGuiTabLabel()}
1139 call test_override('starting', 1)
1140 redrawtabline
1141 call test_override('starting', 0)
1142 call assert_true(index(g:TabLabels, 101) != -1)
1143 call assert_true(index(g:TabLabels, 102) != -1)
1144 call assert_true(index(g:TabLabels, 103) != -1)
1145 set guitablabel&
1146 unlet g:TabLabels
1147
1148 if has('gui_gtk')
1149 " Only on GTK+, the tooltip function is called even if the mouse is not
1150 " on the tabline. on Win32 and Motif, the tooltip function is called only
1151 " when the mouse pointer is over the tabline.
1152 let g:TabToolTips = []
1153 set guitabtooltip=%{TestGuiTabToolTip()}
1154 call test_override('starting', 1)
1155 redrawtabline
1156 call test_override('starting', 0)
1157 call assert_true(index(g:TabToolTips, 201) != -1)
1158 call assert_true(index(g:TabToolTips, 202) != -1)
1159 call assert_true(index(g:TabToolTips, 203) != -1)
1160 set guitabtooltip&
1161 unlet g:TabToolTips
1162 endif
1163 %bw!
1164 let &lines = save_lines
1165endfunc
1166
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001167" Test for dropping files into a window in GUI
1168func DropFilesInCmdLine()
Bram Moolenaar1d1ce612021-06-27 19:02:52 +02001169 CheckFeature drop_file
1170
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001171 call feedkeys(":\"", 'L')
1172 call test_gui_drop_files(['a.c', 'b.c'], &lines, 1, 0)
1173 call feedkeys("\<CR>", 'L')
1174endfunc
1175
1176func Test_gui_drop_files()
Bram Moolenaar1d1ce612021-06-27 19:02:52 +02001177 CheckFeature drop_file
1178
Yegappan Lakshmanan5bca9062021-07-24 21:33:26 +02001179 call assert_fails('call test_gui_drop_files(1, 1, 1, 0)', 'E1211:')
1180 call assert_fails('call test_gui_drop_files(["x"], "", 1, 0)', 'E1210:')
1181 call assert_fails('call test_gui_drop_files(["x"], 1, "", 0)', 'E1210:')
1182 call assert_fails('call test_gui_drop_files(["x"], 1, 1, "")', 'E1210:')
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001183
1184 %bw!
1185 %argdelete
1186 call test_gui_drop_files([], 1, 1, 0)
1187 call assert_equal([], argv())
1188 call test_gui_drop_files([1, 2], 1, 1, 0)
1189 call assert_equal([], argv())
1190
1191 call test_gui_drop_files(['a.c', 'b.c'], 1, 1, 0)
1192 call assert_equal(['a.c', 'b.c'], argv())
1193 %bw!
1194 %argdelete
1195 call test_gui_drop_files([], 1, 1, 0)
1196 call assert_equal([], argv())
1197 %bw!
1198 " if the buffer in the window is modified, then the file should be opened in
1199 " a new window
1200 set modified
1201 call test_gui_drop_files(['x.c', 'y.c'], 1, 1, 0)
1202 call assert_equal(['x.c', 'y.c'], argv())
1203 call assert_equal(2, winnr('$'))
1204 call assert_equal('x.c', bufname(winbufnr(1)))
1205 %bw!
1206 %argdelete
1207 " if Ctrl is pressed, then the file should be opened in a new window
1208 call test_gui_drop_files(['s.py', 't.py'], 1, 1, 0x10)
1209 call assert_equal(['s.py', 't.py'], argv())
1210 call assert_equal(2, winnr('$'))
1211 call assert_equal('s.py', bufname(winbufnr(1)))
1212 %bw!
1213 %argdelete
1214 " drop the files in a non-current window
1215 belowright new
1216 call test_gui_drop_files(['a.py', 'b.py'], 1, 1, 0)
1217 call assert_equal(['a.py', 'b.py'], argv())
1218 call assert_equal(2, winnr('$'))
1219 call assert_equal(1, winnr())
1220 call assert_equal('a.py', bufname(winbufnr(1)))
1221 %bw!
1222 %argdelete
1223 " pressing shift when dropping files should change directory
1224 let save_cwd = getcwd()
1225 call mkdir('Xdir1')
1226 call writefile([], 'Xdir1/Xfile1')
1227 call writefile([], 'Xdir1/Xfile2')
1228 call test_gui_drop_files(['Xdir1/Xfile1', 'Xdir1/Xfile2'], 1, 1, 0x4)
1229 call assert_equal('Xdir1', fnamemodify(getcwd(), ':t'))
1230 call assert_equal('Xfile1', @%)
1231 call chdir(save_cwd)
1232 " pressing shift when dropping directory and files should change directory
1233 call test_gui_drop_files(['Xdir1', 'Xdir1/Xfile2'], 1, 1, 0x4)
1234 call assert_equal('Xdir1', fnamemodify(getcwd(), ':t'))
1235 call assert_equal('Xdir1', fnamemodify(@%, ':t'))
1236 call chdir(save_cwd)
1237 %bw!
1238 %argdelete
1239 " dropping a directory should edit it
1240 call test_gui_drop_files(['Xdir1'], 1, 1, 0)
1241 call assert_equal('Xdir1', @%)
1242 %bw!
1243 %argdelete
1244 " dropping only a directory name with Shift should ignore it
1245 call test_gui_drop_files(['Xdir1'], 1, 1, 0x4)
1246 call assert_equal('', @%)
1247 %bw!
1248 %argdelete
1249 call delete('Xdir1', 'rf')
1250 " drop files in the command line. The GUI drop files adds the file names to
1251 " the low level input buffer. So need to use a cmdline map and feedkeys()
1252 " with 'Lx!' to process it in this function itself.
1253 cnoremap <expr> <buffer> <F4> DropFilesInCmdLine()
1254 call feedkeys(":\"\<F4>\<CR>", 'xt')
1255 call feedkeys('k', 'Lx!')
1256 call assert_equal('"a.c b.c', @:)
1257 cunmap <buffer> <F4>
1258endfunc
1259
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001260" vim: shiftwidth=2 sts=2 expandtab