blob: 6b849c747e971bff9d73f8b37b1026542986c457 [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 Moolenaar4e9dbc72017-02-17 13:44:48 +010064 let skipped = ''
65
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +010066 if !g:x11_based_gui
67 let skipped = g:not_implemented
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010068 elseif has('gui_athena') || has('gui_motif')
Bram Moolenaar6f785742017-02-06 22:11:55 +010069 " Invalid font name. The result should be an empty string.
70 call assert_equal('', getfontname('notexist'))
71
72 " Valid font name. This is usually the real name of 7x13 by default.
Bram Moolenaar87748452017-03-12 17:10:33 +010073 let fname = '-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1'
74 call assert_match(fname, getfontname(fname))
Bram Moolenaar6f785742017-02-06 22:11:55 +010075
76 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
77 " Invalid font name. The result should be the name plus the default size.
78 call assert_equal('notexist 10', getfontname('notexist'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010079 call assert_equal('', getfontname('*'))
Bram Moolenaar6f785742017-02-06 22:11:55 +010080
81 " Valid font name. This is usually the real name of Monospace by default.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010082 let fname = 'Bitstream Vera Sans Mono 12'
83 call assert_equal(fname, getfontname(fname))
84 endif
85
86 if !empty(skipped)
87 throw skipped
Bram Moolenaar6f785742017-02-06 22:11:55 +010088 endif
89endfunc
90
91func Test_getfontname_without_arg()
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010092 let skipped = ''
93
94 let fname = getfontname()
95
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +010096 if !g:x11_based_gui
97 let skipped = g:not_implemented
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010098 elseif has('gui_kde')
Bram Moolenaar6f785742017-02-06 22:11:55 +010099 " 'expected' is the value specified by SetUp() above.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100100 call assert_equal('Courier 10 Pitch/8/-1/5/50/0/0/0/0/0', fname)
Bram Moolenaar6f785742017-02-06 22:11:55 +0100101 elseif has('gui_athena') || has('gui_motif')
Bram Moolenaar87748452017-03-12 17:10:33 +0100102 " 'expected' is DFLT_FONT of gui_x11.c or its real name.
103 let pat = '\(7x13\)\|\(\c-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1\)'
104 call assert_match(pat, fname)
Bram Moolenaar6f785742017-02-06 22:11:55 +0100105 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
106 " 'expected' is DEFAULT_FONT of gui_gtk_x11.c.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100107 call assert_equal('Monospace 10', fname)
108 endif
109
110 if !empty(skipped)
111 throw skipped
Bram Moolenaar6f785742017-02-06 22:11:55 +0100112 endif
113endfunc
114
Bram Moolenaar87748452017-03-12 17:10:33 +0100115func Test_getwinpos()
116 call assert_match('Window position: X \d\+, Y \d\+', execute('winpos'))
117 call assert_true(getwinposx() >= 0)
118 call assert_true(getwinposy() >= 0)
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200119 call assert_equal([getwinposx(), getwinposy()], getwinpos())
Bram Moolenaar87748452017-03-12 17:10:33 +0100120endfunc
121
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100122func Test_quoteplus()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100123 let g:test_is_flaky = 1
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100124 let skipped = ''
125
126 if !g:x11_based_gui
127 let skipped = g:not_supported . 'quoteplus'
128 else
129 let quoteplus_saved = @+
130
131 let test_call = 'Can you hear me?'
132 let test_response = 'Yes, I can.'
Bram Moolenaar93344c22019-08-14 21:12:05 +0200133 let vim_exe = GetVimCommand()
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100134 let testee = 'VIMRUNTIME=' . $VIMRUNTIME . '; export VIMRUNTIME;'
Bram Moolenaar93344c22019-08-14 21:12:05 +0200135 \ . vim_exe . ' --noplugin --not-a-term -c ''%s'''
Bram Moolenaarf8ab1b12017-03-01 18:30:34 +0100136 " Ignore the "failed to create input context" error.
Bram Moolenaar24d76362017-03-04 13:32:10 +0100137 let cmd = 'call test_ignore_error("E285") | '
138 \ . 'gui -f | '
139 \ . 'call feedkeys("'
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100140 \ . '\"+p'
141 \ . ':s/' . test_call . '/' . test_response . '/\<CR>'
142 \ . '\"+yis'
143 \ . ':q!\<CR>", "tx")'
Bram Moolenaar24d76362017-03-04 13:32:10 +0100144 let run_vimtest = printf(testee, cmd)
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100145
146 " Set the quoteplus register to test_call, and another gvim will launched.
147 " Then, it first tries to paste the content of its own quotedplus register
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200148 " onto it. Second, it tries to substitute test_response for the pasted
Bram Moolenaar5074a0e2017-02-26 15:08:21 +0100149 " sentence. If the sentence is identical to test_call, the substitution
150 " should succeed. Third, it tries to yank the result of the substitution
151 " to its own quoteplus register, and last it quits. When system()
152 " returns, the content of the quoteplus register should be identical to
153 " test_response if those quoteplus registers are synchronized properly
154 " with/through the X11 clipboard.
155 let @+ = test_call
156 call system(run_vimtest)
157 call assert_equal(test_response, @+)
158
159 let @+ = quoteplus_saved
160 endif
161
162 if !empty(skipped)
163 throw skipped
164 endif
165endfunc
166
Bram Moolenaar87748452017-03-12 17:10:33 +0100167func Test_set_background()
168 let background_saved = &background
169
170 set background&
171 call assert_equal('light', &background)
172
173 set background=dark
174 call assert_equal('dark', &background)
175
176 let &background = background_saved
177endfunc
178
Bram Moolenaard5841f22017-03-05 13:27:25 +0100179func Test_set_balloondelay()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200180 CheckOption balloondelay
Bram Moolenaard5841f22017-03-05 13:27:25 +0100181
182 let balloondelay_saved = &balloondelay
183
184 " Check if the default value is identical to that described in the manual.
185 set balloondelay&
186 call assert_equal(600, &balloondelay)
187
188 " Edge cases
189
190 " XXX This fact should be hidden so that people won't be tempted to write
191 " plugin/TimeMachine.vim. TODO Add reasonable range checks to the source
192 " code.
193 set balloondelay=-1
194 call assert_equal(-1, &balloondelay)
195
196 " Though it's possible to interpret the zero delay to be 'as soon as
197 " possible' or even 'indefinite', its actual meaning depends on the GUI
198 " toolkit in use after all.
199 set balloondelay=0
200 call assert_equal(0, &balloondelay)
201
202 set balloondelay=1
203 call assert_equal(1, &balloondelay)
204
205 " Since p_bdelay is of type long currently, the upper bound can be
206 " impractically huge and machine-dependent. Practically, it's sufficient
Bram Moolenaar0f9ea222017-03-05 13:48:13 +0100207 " to check if balloondelay works with 0x7fffffff (32 bits) for now.
208 set balloondelay=2147483647
209 call assert_equal(2147483647, &balloondelay)
Bram Moolenaard5841f22017-03-05 13:27:25 +0100210
211 let &balloondelay = balloondelay_saved
212endfunc
213
214func Test_set_ballooneval()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200215 CheckOption ballooneval
Bram Moolenaard5841f22017-03-05 13:27:25 +0100216
217 let ballooneval_saved = &ballooneval
218
219 set ballooneval&
220 call assert_equal(0, &ballooneval)
221
222 set ballooneval
223 call assert_notequal(0, &ballooneval)
224
225 set noballooneval
226 call assert_equal(0, &ballooneval)
227
228 let &ballooneval = ballooneval_saved
229endfunc
230
231func Test_set_balloonexpr()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200232 CheckOption balloonexpr
Bram Moolenaard5841f22017-03-05 13:27:25 +0100233
234 let balloonexpr_saved = &balloonexpr
235
236 " Default value
237 set balloonexpr&
238 call assert_equal('', &balloonexpr)
239
240 " User-defined function
241 new
242 func MyBalloonExpr()
243 return 'Cursor is at line ' . v:beval_lnum .
244 \', column ' . v:beval_col .
245 \ ' of file ' . bufname(v:beval_bufnr) .
246 \ ' on word "' . v:beval_text . '"' .
247 \ ' in window ' . v:beval_winid . ' (#' . v:beval_winnr . ')'
248 endfunc
249 setl balloonexpr=MyBalloonExpr()
250 setl ballooneval
251 call assert_equal('MyBalloonExpr()', &balloonexpr)
252 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200253 " and check if the content of the balloon is the same as what is expected.
Bram Moolenaard5841f22017-03-05 13:27:25 +0100254 " Also, check if textlock works as expected.
255 setl balloonexpr&
256 call assert_equal('', &balloonexpr)
257 delfunc MyBalloonExpr
258 bwipe!
259
260 " Multiline support
261 if has('balloon_multiline')
262 " Multiline balloon using NL
263 new
264 func MyBalloonFuncForMultilineUsingNL()
265 return "Multiline\nSuppported\nBalloon\nusing NL"
266 endfunc
267 setl balloonexpr=MyBalloonFuncForMultilineUsingNL()
268 setl ballooneval
269 call assert_equal('MyBalloonFuncForMultilineUsingNL()', &balloonexpr)
270 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200271 " and check if the content of the balloon is the same as what is
Bram Moolenaard5841f22017-03-05 13:27:25 +0100272 " expected. Also, check if textlock works as expected.
273 setl balloonexpr&
274 delfunc MyBalloonFuncForMultilineUsingNL
275 bwipe!
276
277 " Multiline balloon using List
278 new
279 func MyBalloonFuncForMultilineUsingList()
280 return [ 'Multiline', 'Suppported', 'Balloon', 'using List' ]
281 endfunc
282 setl balloonexpr=MyBalloonFuncForMultilineUsingList()
283 setl ballooneval
284 call assert_equal('MyBalloonFuncForMultilineUsingList()', &balloonexpr)
285 " TODO Read non-empty text, place the pointer at a character of a word,
Bram Moolenaar027df2a2018-05-14 21:31:08 +0200286 " and check if the content of the balloon is the same as what is
Bram Moolenaard5841f22017-03-05 13:27:25 +0100287 " expected. Also, check if textlock works as expected.
288 setl balloonexpr&
289 delfunc MyBalloonFuncForMultilineUsingList
290 bwipe!
291 endif
292
293 let &balloonexpr = balloonexpr_saved
294endfunc
295
Bram Moolenaar87748452017-03-12 17:10:33 +0100296" Invalid arguments are tested with test_options in conjunction with segfaults
297" caused by them (Patch 8.0.0357, 24922ec233).
298func Test_set_guicursor()
299 let guicursor_saved = &guicursor
300
301 let default = [
302 \ "n-v-c:block-Cursor/lCursor",
303 \ "ve:ver35-Cursor",
304 \ "o:hor50-Cursor",
305 \ "i-ci:ver25-Cursor/lCursor",
306 \ "r-cr:hor20-Cursor/lCursor",
307 \ "sm:block-Cursor-blinkwait175-blinkoff150-blinkon175"
308 \ ]
309
310 " Default Value
311 set guicursor&
312 call assert_equal(join(default, ','), &guicursor)
313
314 " Argument List Example 1
315 let opt_list = copy(default)
316 let opt_list[0] = "n-c-v:block-nCursor"
317 exec "set guicursor=" . join(opt_list, ',')
318 call assert_equal(join(opt_list, ','), &guicursor)
319 unlet opt_list
320
321 " Argument List Example 2
322 let opt_list = copy(default)
323 let opt_list[3] = "i-ci:ver30-iCursor-blinkwait300-blinkon200-blinkoff150"
324 exec "set guicursor=" . join(opt_list, ',')
325 call assert_equal(join(opt_list, ','), &guicursor)
326 unlet opt_list
327
328 " 'a' Mode
329 set guicursor&
330 let &guicursor .= ',a:blinkon0'
331 call assert_equal(join(default, ',') . ",a:blinkon0", &guicursor)
332
333 let &guicursor = guicursor_saved
334endfunc
335
Bram Moolenaar43dded82017-02-09 16:06:17 +0100336func Test_set_guifont()
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100337 let skipped = ''
338
339 let guifont_saved = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100340 if has('xfontset')
341 " Prevent 'guifontset' from canceling 'guifont'.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100342 let guifontset_saved = &guifontset
Bram Moolenaar43dded82017-02-09 16:06:17 +0100343 set guifontset=
344 endif
345
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100346 if !g:x11_based_gui
347 let skipped = g:not_implemented
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100348 elseif has('gui_athena') || has('gui_motif')
Bram Moolenaar43dded82017-02-09 16:06:17 +0100349 " Non-empty font list with invalid font names.
350 "
351 " This test is twofold: (1) It checks if the command fails as expected
352 " when there are no loadable fonts found in the list. (2) It checks if
353 " 'guifont' remains the same after the command loads none of the fonts
354 " listed.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100355 let flist = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100356 call assert_fails('set guifont=-notexist1-*,-notexist2-*')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100357 call assert_equal(flist, &guifont)
Bram Moolenaar43dded82017-02-09 16:06:17 +0100358
359 " Non-empty font list with a valid font name. Should pick up the first
360 " valid font.
361 set guifont=-notexist1-*,fixed,-notexist2-*
Bram Moolenaar87748452017-03-12 17:10:33 +0100362 let pat = '\(fixed\)\|\(\c-Misc-Fixed-Medium-R-SemiCondensed--13-120-75-75-C-60-ISO8859-1\)'
363 call assert_match(pat, getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100364
365 " Empty list. Should fallback to the built-in default.
366 set guifont=
Bram Moolenaar87748452017-03-12 17:10:33 +0100367 let pat = '\(7x13\)\|\(\c-Misc-Fixed-Medium-R-Normal--13-120-75-75-C-70-ISO8859-1\)'
368 call assert_match(pat, getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100369
370 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
371 " For GTK, what we refer to as 'font names' in our manual are actually
372 " 'initial font patterns'. A valid font which matches the 'canonical font
373 " pattern' constructed from a given 'initial pattern' is to be looked up
374 " and loaded. That explains why the GTK GUIs appear to accept 'invalid
375 " font names'.
376 "
377 " Non-empty list. Should always pick up the first element, no matter how
378 " strange it is, as explained above.
379 set guifont=(´・ω・`)\ 12,Courier\ 12
380 call assert_equal('(´・ω・`) 12', getfontname())
381
382 " Empty list. Should fallback to the built-in default.
383 set guifont=
384 call assert_equal('Monospace 10', getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100385 endif
386
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200387 if has('win32')
388 " Invalid font names are accepted in GTK GUI
389 call assert_fails('set guifont=xa1bc23d7f', 'E596:')
Bram Moolenaar2c78a772020-12-09 12:01:03 +0100390 endif
Bram Moolenaar1b3e0722020-12-08 21:12:40 +0100391
Bram Moolenaar3650fd72021-02-28 23:13:40 +0100392 " This only works if 'renderoptions' exists and does not work for Windows XP
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200393 " and older.
Bram Moolenaar3650fd72021-02-28 23:13:40 +0100394 if exists('+renderoptions') && windowsversion() !~ '^[345]\.'
Bram Moolenaar1b3e0722020-12-08 21:12:40 +0100395 " doing this four times used to cause a crash
Bram Moolenaar2c78a772020-12-09 12:01:03 +0100396 set renderoptions=type:directx
397 for i in range(5)
398 set guifont=
399 endfor
400 set renderoptions=
401 for i in range(5)
402 set guifont=
403 endfor
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200404 endif
405
Bram Moolenaar43dded82017-02-09 16:06:17 +0100406 if has('xfontset')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100407 let &guifontset = guifontset_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100408 endif
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100409 let &guifont = guifont_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100410
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100411 if !empty(skipped)
412 throw skipped
Bram Moolenaar43dded82017-02-09 16:06:17 +0100413 endif
414endfunc
415
Bram Moolenaar10434672017-02-12 19:59:08 +0100416func Test_set_guifontset()
Bram Moolenaarce90e362019-09-08 18:58:44 +0200417 CheckFeature xfontset
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100418 let skipped = ''
Bram Moolenaar10434672017-02-12 19:59:08 +0100419
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200420 call assert_fails('set guifontset=*', 'E597:')
421
Bram Moolenaarce90e362019-09-08 18:58:44 +0200422 let ctype_saved = v:ctype
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100423
Bram Moolenaarce90e362019-09-08 18:58:44 +0200424 " First, since XCreateFontSet(3) is very sensitive to locale, fonts must
425 " be chosen meticulously.
426 let font_head = '-misc-fixed-medium-r-normal--14'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100427
Bram Moolenaarce90e362019-09-08 18:58:44 +0200428 let font_aw70 = font_head . '-130-75-75-c-70'
429 let font_aw140 = font_head . '-130-75-75-c-140'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100430
Bram Moolenaarce90e362019-09-08 18:58:44 +0200431 let font_jisx0201 = font_aw70 . '-jisx0201.1976-0'
432 let font_jisx0208 = font_aw140 . '-jisx0208.1983-0'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100433
Bram Moolenaarce90e362019-09-08 18:58:44 +0200434 let full_XLFDs = join([ font_jisx0208, font_jisx0201 ], ',')
435 let short_XLFDs = join([ font_aw140, font_aw70 ], ',')
436 let singleton = font_head . '-*'
437 let aliases = 'k14,r14'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100438
Bram Moolenaarce90e362019-09-08 18:58:44 +0200439 " Second, among 'locales', look up such a locale that gets 'set
440 " guifontset=' to work successfully with every fontset specified with
441 " 'fontsets'.
442 let locales = [ 'ja_JP.UTF-8', 'ja_JP.eucJP', 'ja_JP.SJIS' ]
443 let fontsets = [ full_XLFDs, short_XLFDs, singleton, aliases ]
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100444
Bram Moolenaarce90e362019-09-08 18:58:44 +0200445 let feasible = 0
446 for locale in locales
447 try
448 exec 'language ctype' locale
449 catch /^Vim\%((\a\+)\)\=:E197/
450 continue
451 endtry
452 let done = 0
453 for fontset in fontsets
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100454 try
Bram Moolenaarce90e362019-09-08 18:58:44 +0200455 exec 'set guifontset=' . fontset
456 catch /^Vim\%((\a\+)\)\=:E\%(250\|252\|234\|597\|598\)/
457 break
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100458 endtry
Bram Moolenaarce90e362019-09-08 18:58:44 +0200459 let done += 1
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100460 endfor
Bram Moolenaarce90e362019-09-08 18:58:44 +0200461 if done == len(fontsets)
462 let feasible = 1
463 break
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100464 endif
Bram Moolenaarce90e362019-09-08 18:58:44 +0200465 endfor
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100466
Bram Moolenaarce90e362019-09-08 18:58:44 +0200467 " Third, give a set of tests if it is found feasible.
468 if !feasible
469 let skipped = g:not_hosted
470 else
471 " N.B. 'v:ctype' has already been set to an appropriate value in the
472 " previous loop.
473 for fontset in fontsets
474 exec 'set guifontset=' . fontset
475 call assert_equal(fontset, &guifontset)
476 endfor
Bram Moolenaar10434672017-02-12 19:59:08 +0100477 endif
478
Bram Moolenaarce90e362019-09-08 18:58:44 +0200479 " Finally, restore ctype.
480 exec 'language ctype' ctype_saved
481
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100482 if !empty(skipped)
483 throw skipped
Bram Moolenaar10434672017-02-12 19:59:08 +0100484 endif
485endfunc
486
487func Test_set_guifontwide()
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200488 call assert_fails('set guifontwide=*', 'E533:')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100489 let skipped = ''
Bram Moolenaar10434672017-02-12 19:59:08 +0100490
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100491 if !g:x11_based_gui
492 let skipped = g:not_implemented
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100493 elseif has('gui_gtk')
494 let guifont_saved = &guifont
495 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100496
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100497 let fc_match = exepath('fc-match')
498 if empty(fc_match)
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100499 let skipped = g:not_hosted
Bram Moolenaar10434672017-02-12 19:59:08 +0100500 else
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100501 let &guifont = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=en')
502 let wide = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=ja')
503 exec 'set guifontwide=' . fnameescape(wide)
504 call assert_equal(wide, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100505 endif
506
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100507 let &guifontwide = guifontwide_saved
508 let &guifont = guifont_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100509
510 elseif has('gui_athena') || has('gui_motif')
511 " guifontwide is premised upon the xfontset feature.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100512 if !has('xfontset')
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100513 let skipped = g:not_supported . 'xfontset'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100514 else
515 let encoding_saved = &encoding
516 let guifont_saved = &guifont
517 let guifontset_saved = &guifontset
518 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100519
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100520 let nfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-90-iso10646-1'
521 let wfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-180-iso10646-1'
Bram Moolenaar10434672017-02-12 19:59:08 +0100522
523 set encoding=utf-8
524
525 " Case 1: guifontset is empty
526 set guifontset=
527
528 " Case 1-1: Automatic selection
529 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100530 exec 'set guifont=' . nfont
531 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100532
533 " Case 1-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100534 exec 'set guifontwide=' . wfont
535 exec 'set guifont=' . nfont
536 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100537
538 " Case 2: guifontset is invalid
539 try
540 set guifontset=-*-notexist-*
Bram Moolenaar37175402017-03-18 20:18:45 +0100541 call assert_report("'set guifontset=-*-notexist-*' should have failed")
Bram Moolenaar10434672017-02-12 19:59:08 +0100542 catch
Bram Moolenaare2e40752020-09-04 21:18:46 +0200543 call assert_exception('E598:')
Bram Moolenaar10434672017-02-12 19:59:08 +0100544 endtry
545 " Set it to an invalid value brutally for preparation.
546 let &guifontset = '-*-notexist-*'
547
548 " Case 2-1: Automatic selection
549 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100550 exec 'set guifont=' . nfont
551 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100552
553 " Case 2-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100554 exec 'set guifontwide=' . wfont
555 exec 'set guifont=' . nfont
556 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100557
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100558 let &guifontwide = guifontwide_saved
559 let &guifontset = guifontset_saved
560 let &guifont = guifont_saved
561 let &encoding = encoding_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100562 endif
Bram Moolenaar10434672017-02-12 19:59:08 +0100563 endif
564
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100565 if !empty(skipped)
566 throw skipped
Bram Moolenaar10434672017-02-12 19:59:08 +0100567 endif
568endfunc
569
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100570func Test_set_guiheadroom()
571 let skipped = ''
572
573 if !g:x11_based_gui
574 let skipped = g:not_supported . 'guiheadroom'
575 else
576 " Since this script is to be read together with '-U NONE', the default
577 " value must be preserved.
578 call assert_equal(50, &guiheadroom)
579 endif
580
581 if !empty(skipped)
582 throw skipped
583 endif
584endfunc
585
Bram Moolenaar87748452017-03-12 17:10:33 +0100586func Test_set_guioptions()
587 let guioptions_saved = &guioptions
588 let duration = '200m'
589
590 if has('win32')
591 " Default Value
592 set guioptions&
593 call assert_equal('egmrLtT', &guioptions)
594
595 else
596 " Default Value
597 set guioptions&
598 call assert_equal('aegimrLtT', &guioptions)
599
600 " To activate scrollbars of type 'L' or 'R'.
601 wincmd v
602 redraw!
603
604 " Remove all default GUI ornaments
605 set guioptions-=T
606 exec 'sleep' . duration
607 call assert_equal('aegimrLt', &guioptions)
608 set guioptions-=t
609 exec 'sleep' . duration
610 call assert_equal('aegimrL', &guioptions)
611 set guioptions-=L
612 exec 'sleep' . duration
613 call assert_equal('aegimr', &guioptions)
614 set guioptions-=r
615 exec 'sleep' . duration
616 call assert_equal('aegim', &guioptions)
617 set guioptions-=m
618 exec 'sleep' . duration
619 call assert_equal('aegi', &guioptions)
620
621 " Try non-default GUI ornaments
622 set guioptions+=l
623 exec 'sleep' . duration
624 call assert_equal('aegil', &guioptions)
625 set guioptions-=l
626 exec 'sleep' . duration
627 call assert_equal('aegi', &guioptions)
628
629 set guioptions+=R
630 exec 'sleep' . duration
631 call assert_equal('aegiR', &guioptions)
632 set guioptions-=R
633 exec 'sleep' . duration
634 call assert_equal('aegi', &guioptions)
635
636 set guioptions+=b
637 exec 'sleep' . duration
638 call assert_equal('aegib', &guioptions)
639 set guioptions+=h
640 exec 'sleep' . duration
641 call assert_equal('aegibh', &guioptions)
642 set guioptions-=h
643 exec 'sleep' . duration
644 call assert_equal('aegib', &guioptions)
645 set guioptions-=b
646 exec 'sleep' . duration
647 call assert_equal('aegi', &guioptions)
648
649 set guioptions+=v
650 exec 'sleep' . duration
651 call assert_equal('aegiv', &guioptions)
652 set guioptions-=v
653 exec 'sleep' . duration
654 call assert_equal('aegi', &guioptions)
655
656 if has('gui_motif')
657 set guioptions+=F
658 exec 'sleep' . duration
659 call assert_equal('aegiF', &guioptions)
660 set guioptions-=F
661 exec 'sleep' . duration
662 call assert_equal('aegi', &guioptions)
663 endif
664
Bram Moolenaar50bf7ce2019-09-15 13:17:00 +0200665 if has('gui_gtk3')
666 set guioptions+=d
667 exec 'sleep' . duration
668 call assert_equal('aegid', &guioptions)
669 set guioptions-=d
670 exec 'sleep' . duration
671 call assert_equal('aegi', &guioptions)
672 endif
673
Bram Moolenaar87748452017-03-12 17:10:33 +0100674 " Restore GUI ornaments to the default state.
675 set guioptions+=m
676 exec 'sleep' . duration
677 call assert_equal('aegim', &guioptions)
678 set guioptions+=r
679 exec 'sleep' . duration
680 call assert_equal('aegimr', &guioptions)
681 set guioptions+=L
682 exec 'sleep' . duration
683 call assert_equal('aegimrL', &guioptions)
684 set guioptions+=t
685 exec 'sleep' . duration
686 call assert_equal('aegimrLt', &guioptions)
687 set guioptions+=T
688 exec 'sleep' . duration
689 call assert_equal("aegimrLtT", &guioptions)
690
691 wincmd o
692 redraw!
693 endif
694
695 let &guioptions = guioptions_saved
696endfunc
697
Bram Moolenaarab186732018-09-14 21:27:06 +0200698func Test_scrollbars()
699 new
700 " buffer with 200 lines
701 call setline(1, repeat(['one', 'two'], 100))
702 set guioptions+=rlb
703
704 " scroll to move line 11 at top, moves the cursor there
Bram Moolenaarce90e362019-09-08 18:58:44 +0200705 eval 10->test_scrollbar('left', 0)
Bram Moolenaarab186732018-09-14 21:27:06 +0200706 redraw
707 call assert_equal(1, winline())
708 call assert_equal(11, line('.'))
709
710 " scroll to move line 1 at top, cursor stays in line 11
711 call test_scrollbar('right', 0, 0)
712 redraw
713 call assert_equal(11, winline())
714 call assert_equal(11, line('.'))
715
716 set nowrap
717 call setline(11, repeat('x', 150))
718 redraw
719 call assert_equal(1, wincol())
Bram Moolenaarf6d50f12019-06-06 15:40:08 +0200720 set number
721 redraw
722 call assert_equal(5, wincol())
723 set nonumber
724 redraw
Bram Moolenaarab186732018-09-14 21:27:06 +0200725 call assert_equal(1, col('.'))
726
727 " scroll to character 11, cursor is moved
728 call test_scrollbar('hor', 10, 0)
729 redraw
730 call assert_equal(1, wincol())
Bram Moolenaarf6d50f12019-06-06 15:40:08 +0200731 set number
732 redraw
733 call assert_equal(5, wincol())
734 set nonumber
735 redraw
Bram Moolenaarab186732018-09-14 21:27:06 +0200736 call assert_equal(11, col('.'))
737
738 set guioptions&
739 set wrap&
740 bwipe!
741endfunc
742
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100743func Test_menu()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100744 CheckFeature quickfix
745
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100746 " Check Help menu exists
747 let help_menu = execute('menu Help')
748 call assert_match('Overview', help_menu)
749
750 " Check Help menu works
751 emenu Help.Overview
752 call assert_equal('help', &buftype)
753 close
754
755 " Check deleting menu doesn't cause trouble.
756 aunmenu Help
Bram Moolenaarb45cd362020-09-28 21:41:49 +0200757 if exists(':tlmenu')
758 tlunmenu Help
759 endif
Bram Moolenaar47cf1cc2019-03-28 22:04:56 +0100760 call assert_fails('menu Help', 'E329:')
761endfunc
762
Bram Moolenaar87748452017-03-12 17:10:33 +0100763func Test_set_guipty()
764 let guipty_saved = &guipty
765
766 " Default Value
767 set guipty&
768 call assert_equal(1, &guipty)
769
770 set noguipty
771 call assert_equal(0, &guipty)
772
773 let &guipty = guipty_saved
Bram Moolenaar6f785742017-02-06 22:11:55 +0100774endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100775
Bram Moolenaarc701f322019-03-28 21:49:21 +0100776func Test_encoding_conversion()
777 " GTK supports conversion between 'encoding' and "utf-8"
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200778 CheckFeature gui_gtk
779 let encoding_saved = &encoding
780 set encoding=latin1
Bram Moolenaarc701f322019-03-28 21:49:21 +0100781
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200782 " would be nice if we could take a screenshot
783 intro
784 " sets the window title
785 edit SomeFile
Bram Moolenaarc701f322019-03-28 21:49:21 +0100786
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200787 let &encoding = encoding_saved
Bram Moolenaarc701f322019-03-28 21:49:21 +0100788endfunc
789
Bram Moolenaar877e9572016-08-04 20:05:50 +0200790func Test_shell_command()
791 new
Bram Moolenaar9d5b8762016-08-04 21:21:13 +0200792 r !echo hello
793 call assert_equal('hello', substitute(getline(2), '\W', '', 'g'))
Bram Moolenaar877e9572016-08-04 20:05:50 +0200794 bwipe!
Bram Moolenaar877e9572016-08-04 20:05:50 +0200795endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100796
Bram Moolenaar87748452017-03-12 17:10:33 +0100797func Test_syntax_colortest()
798 runtime syntax/colortest.vim
799 redraw!
800 sleep 200m
801 bwipe!
802endfunc
803
804func Test_set_term()
805 " It's enough to check the current value since setting 'term' to anything
806 " other than builtin_gui makes no sense at all.
807 call assert_equal('builtin_gui', &term)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200808 call assert_fails('set term=xterm', 'E530:')
Bram Moolenaar87748452017-03-12 17:10:33 +0100809endfunc
810
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100811func Test_windowid_variable()
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100812 if g:x11_based_gui || has('win32')
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100813 call assert_true(v:windowid > 0)
814 else
815 call assert_equal(0, v:windowid)
816 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +0100817endfunc
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200818
819" Test "vim -g" and also the GUIEnter autocommand.
820func Test_gui_dash_g()
821 let cmd = GetVimCommand('Xscriptgui')
822 call writefile([""], "Xtestgui")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200823 let lines =<< trim END
824 au GUIEnter * call writefile(["insertmode: " . &insertmode], "Xtestgui")
825 au GUIEnter * qall
826 END
827 call writefile(lines, 'Xscriptgui')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200828 call system(cmd . ' -g')
829 call WaitForAssert({-> assert_equal(['insertmode: 0'], readfile('Xtestgui'))})
830
831 call delete('Xscriptgui')
832 call delete('Xtestgui')
833endfunc
834
835" Test "vim -7" and also the GUIEnter autocommand.
836func Test_gui_dash_y()
837 let cmd = GetVimCommand('Xscriptgui')
838 call writefile([""], "Xtestgui")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200839 let lines =<< trim END
840 au GUIEnter * call writefile(["insertmode: " . &insertmode], "Xtestgui")
841 au GUIEnter * qall
842 END
843 call writefile(lines, 'Xscriptgui')
Bram Moolenaar248be5c2018-05-05 15:47:19 +0200844 call system(cmd . ' -y')
845 call WaitForAssert({-> assert_equal(['insertmode: 1'], readfile('Xtestgui'))})
846
847 call delete('Xscriptgui')
848 call delete('Xtestgui')
849endfunc
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100850
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200851" Test for "!" option in 'guioptions'. Use a terminal for running external
852" commands
853func Test_gui_run_cmd_in_terminal()
Bram Moolenaar4457e1d2020-05-15 22:46:44 +0200854 CheckFeature terminal
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200855 let save_guioptions = &guioptions
856 set guioptions+=!
857 if has('win32')
858 let cmd = 'type'
859 else
860 " assume all the other systems have a cat command
861 let cmd = 'cat'
862 endif
Bram Moolenaar98f16712020-05-22 13:34:01 +0200863 exe "silent !" . cmd . " test_gui.vim"
Bram Moolenaar91689ea2020-05-11 22:04:53 +0200864 " TODO: how to check that the command ran in a separate terminal?
865 " Maybe check for $TERM (dumb vs xterm) in the spawned shell?
866 let &guioptions = save_guioptions
867endfunc
868
Bram Moolenaar46cd43b2020-06-04 22:22:11 +0200869func Test_gui_recursive_mapping()
870 nmap ' <C-W>
871 nmap <C-W>a :let didit = 1<CR>
872 call feedkeys("'a", 'xt')
873 call assert_equal(1, didit)
874
875 nunmap '
876 nunmap <C-W>a
877endfunc
878
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200879" Test GUI mouse events
880func Test_gui_mouse_event()
881 set mousemodel=extend
882 call test_override('no_query_mouse', 1)
883 new
884 call setline(1, ['one two three', 'four five six'])
885
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +0200886 " place the cursor using left click in normal mode
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200887 call cursor(1, 1)
888 call test_gui_mouse_event(0, 2, 4, 0, 0)
889 call test_gui_mouse_event(3, 2, 4, 0, 0)
890 call feedkeys("\<Esc>", 'Lx!')
891 call assert_equal([0, 2, 4, 0], getpos('.'))
892
893 " select and yank a word
894 let @" = ''
895 call test_gui_mouse_event(0, 1, 9, 0, 0)
896 call test_gui_mouse_event(0, 1, 9, 1, 0)
897 call test_gui_mouse_event(3, 1, 9, 0, 0)
898 call feedkeys("y", 'Lx!')
899 call assert_equal('three', @")
900
901 " create visual selection using right click
902 let @" = ''
903 call test_gui_mouse_event(0, 2, 6, 0, 0)
904 call test_gui_mouse_event(3, 2, 6, 0, 0)
905 call test_gui_mouse_event(2, 2, 13, 0, 0)
906 call test_gui_mouse_event(3, 2, 13, 0, 0)
907 call feedkeys("y", 'Lx!')
908 call assert_equal('five six', @")
909
910 " paste using middle mouse button
911 let @* = 'abc '
912 call feedkeys('""', 'Lx!')
913 call test_gui_mouse_event(1, 1, 9, 0, 0)
914 call test_gui_mouse_event(3, 1, 9, 0, 0)
915 call feedkeys("\<Esc>", 'Lx!')
916 call assert_equal(['one two abc three', 'four five six'], getline(1, '$'))
917
918 " extend visual selection using right click in visual mode
919 let @" = ''
920 call cursor(1, 1)
921 call feedkeys('v', 'Lx!')
922 call test_gui_mouse_event(2, 1, 17, 0, 0)
923 call test_gui_mouse_event(3, 1, 17, 0, 0)
924 call feedkeys("y", 'Lx!')
925 call assert_equal('one two abc three', @")
926
927 " extend visual selection using mouse drag
928 let @" = ''
929 call cursor(1, 1)
930 call test_gui_mouse_event(0, 2, 1, 0, 0)
931 call test_gui_mouse_event(0x43, 2, 9, 0, 0)
932 call test_gui_mouse_event(0x3, 2, 9, 0, 0)
933 call feedkeys("y", 'Lx!')
934 call assert_equal('four five', @")
935
936 " select text by moving the mouse
937 let @" = ''
938 call cursor(1, 1)
939 redraw!
940 call test_gui_mouse_event(0, 1, 4, 0, 0)
941 call test_gui_mouse_event(0x700, 1, 9, 0, 0)
942 call test_gui_mouse_event(0x700, 1, 13, 0, 0)
943 call test_gui_mouse_event(0x3, 1, 13, 0, 0)
944 call feedkeys("y", 'Lx!')
945 call assert_equal(' two abc t', @")
946
947 " Using mouse in insert mode
948 call cursor(1, 1)
949 call feedkeys('i', 't')
950 call test_gui_mouse_event(0, 2, 11, 0, 0)
951 call test_gui_mouse_event(3, 2, 11, 0, 0)
952 call feedkeys("po\<Esc>", 'Lx!')
953 call assert_equal(['one two abc three', 'four five posix'], getline(1, '$'))
954
955 %d _
956 call setline(1, range(1, 100))
957 " scroll up
958 call test_gui_mouse_event(0x200, 2, 1, 0, 0)
959 call test_gui_mouse_event(0x200, 2, 1, 0, 0)
960 call test_gui_mouse_event(0x200, 2, 1, 0, 0)
961 call feedkeys("H", 'Lx!')
962 call assert_equal(10, line('.'))
963
964 " scroll down
965 call test_gui_mouse_event(0x100, 2, 1, 0, 0)
966 call test_gui_mouse_event(0x100, 2, 1, 0, 0)
967 call feedkeys("H", 'Lx!')
968 call assert_equal(4, line('.'))
969
970 %d _
971 set nowrap
972 call setline(1, range(10)->join('')->repeat(10))
973 " scroll left
974 call test_gui_mouse_event(0x500, 1, 5, 0, 0)
975 call test_gui_mouse_event(0x500, 1, 10, 0, 0)
976 call test_gui_mouse_event(0x500, 1, 15, 0, 0)
977 call feedkeys('g0', 'Lx!')
978 call assert_equal(19, col('.'))
979
980 " scroll right
981 call test_gui_mouse_event(0x600, 1, 15, 0, 0)
982 call test_gui_mouse_event(0x600, 1, 10, 0, 0)
983 call feedkeys('g0', 'Lx!')
984 call assert_equal(7, col('.'))
985 set wrap&
986
987 %d _
988 call setline(1, repeat([repeat('a', 60)], 10))
989
990 " record various mouse events
991 let mouseEventNames = [
992 \ 'LeftMouse', 'LeftRelease', '2-LeftMouse', '3-LeftMouse',
993 \ 'S-LeftMouse', 'A-LeftMouse', 'C-LeftMouse', 'MiddleMouse',
994 \ 'MiddleRelease', '2-MiddleMouse', '3-MiddleMouse',
995 \ 'S-MiddleMouse', 'A-MiddleMouse', 'C-MiddleMouse',
996 \ 'RightMouse', 'RightRelease', '2-RightMouse',
997 \ '3-RightMouse', 'S-RightMouse', 'A-RightMouse', 'C-RightMouse',
998 \ 'X1Mouse', 'S-X1Mouse', 'A-X1Mouse', 'C-X1Mouse', 'X2Mouse',
999 \ 'S-X2Mouse', 'A-X2Mouse', 'C-X2Mouse'
1000 \ ]
1001 let mouseEventCodes = map(copy(mouseEventNames), "'<' .. v:val .. '>'")
1002 let g:events = []
1003 for e in mouseEventCodes
1004 exe 'nnoremap ' .. e .. ' <Cmd>call add(g:events, "' ..
1005 \ substitute(e, '[<>]', '', 'g') .. '")<CR>'
1006 endfor
1007
1008 " Test various mouse buttons (0 - Left, 1 - Middle, 2 - Right, 0x300 - X1,
1009 " 0x300- X2)
1010 for button in [0, 1, 2, 0x300, 0x400]
1011 " Single click
1012 call test_gui_mouse_event(button, 2, 5, 0, 0)
1013 call test_gui_mouse_event(3, 2, 5, 0, 0)
1014
1015 " Double/Triple click is supported by only the Left/Middle/Right mouse
1016 " buttons
1017 if button <= 2
1018 " Double Click
1019 call test_gui_mouse_event(button, 2, 5, 0, 0)
1020 call test_gui_mouse_event(button, 2, 5, 1, 0)
1021 call test_gui_mouse_event(3, 2, 5, 0, 0)
1022
1023 " Triple Click
1024 call test_gui_mouse_event(button, 2, 5, 0, 0)
1025 call test_gui_mouse_event(button, 2, 5, 1, 0)
1026 call test_gui_mouse_event(button, 2, 5, 1, 0)
1027 call test_gui_mouse_event(3, 2, 5, 0, 0)
1028 endif
1029
1030 " Shift click
1031 call test_gui_mouse_event(button, 3, 7, 0, 4)
1032 call test_gui_mouse_event(3, 3, 7, 0, 4)
1033
1034 " Alt click
1035 call test_gui_mouse_event(button, 3, 7, 0, 8)
1036 call test_gui_mouse_event(3, 3, 7, 0, 8)
1037
1038 " Ctrl click
1039 call test_gui_mouse_event(button, 3, 7, 0, 16)
1040 call test_gui_mouse_event(3, 3, 7, 0, 16)
1041
1042 call feedkeys("\<Esc>", 'Lx!')
1043 endfor
1044
1045 call assert_equal(['LeftMouse', 'LeftRelease', 'LeftMouse', '2-LeftMouse',
1046 \ 'LeftMouse', '2-LeftMouse', '3-LeftMouse', 'S-LeftMouse',
1047 \ 'A-LeftMouse', 'C-LeftMouse', 'MiddleMouse', 'MiddleRelease',
1048 \ 'MiddleMouse', '2-MiddleMouse', 'MiddleMouse', '2-MiddleMouse',
1049 \ '3-MiddleMouse', 'S-MiddleMouse', 'A-MiddleMouse', 'C-MiddleMouse',
1050 \ 'RightMouse', 'RightRelease', 'RightMouse', '2-RightMouse',
1051 \ 'RightMouse', '2-RightMouse', '3-RightMouse', 'S-RightMouse',
1052 \ 'A-RightMouse', 'C-RightMouse', 'X1Mouse', 'S-X1Mouse', 'A-X1Mouse',
1053 \ 'C-X1Mouse', 'X2Mouse', 'S-X2Mouse', 'A-X2Mouse', 'C-X2Mouse'],
1054 \ g:events)
1055
1056 for e in mouseEventCodes
1057 exe 'nunmap ' .. e
1058 endfor
1059
1060 " modeless selection
1061 set mouse=
1062 let save_guioptions = &guioptions
1063 set guioptions+=A
1064 %d _
1065 call setline(1, ['one two three', 'four five sixteen'])
1066 call cursor(1, 1)
1067 redraw!
1068 " Double click should select the word and copy it to clipboard
1069 let @* = ''
1070 call test_gui_mouse_event(0, 2, 11, 0, 0)
1071 call test_gui_mouse_event(0, 2, 11, 1, 0)
1072 call test_gui_mouse_event(3, 2, 11, 0, 0)
1073 call feedkeys("\<Esc>", 'Lx!')
1074 call assert_equal([0, 1, 1, 0], getpos('.'))
1075 call assert_equal('sixteen', @*)
1076 " Right click should extend the selection from cursor
1077 call cursor(1, 6)
1078 redraw!
1079 let @* = ''
1080 call test_gui_mouse_event(2, 1, 11, 0, 0)
1081 call test_gui_mouse_event(3, 1, 11, 0, 0)
1082 call feedkeys("\<Esc>", 'Lx!')
1083 call assert_equal([0, 1, 6, 0], getpos('.'))
1084 call assert_equal('wo thr', @*)
1085 " Middle click should paste the clipboard contents
1086 call cursor(2, 1)
1087 redraw!
1088 call test_gui_mouse_event(1, 1, 11, 0, 0)
1089 call test_gui_mouse_event(3, 1, 11, 0, 0)
1090 call feedkeys("\<Esc>", 'Lx!')
1091 call assert_equal([0, 2, 7, 0], getpos('.'))
1092 call assert_equal('wo thrfour five sixteen', getline(2))
1093 set mouse&
1094 let &guioptions = save_guioptions
1095
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001096 " Test invalid parameters for test_gui_mouse_event()
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001097 call assert_fails('call test_gui_mouse_event("", 1, 2, 3, 4)', 'E1210:')
1098 call assert_fails('call test_gui_mouse_event(0, "", 2, 3, 4)', 'E1210:')
1099 call assert_fails('call test_gui_mouse_event(0, 1, "", 3, 4)', 'E1210:')
1100 call assert_fails('call test_gui_mouse_event(0, 1, 2, "", 4)', 'E1210:')
1101 call assert_fails('call test_gui_mouse_event(0, 1, 2, 3, "")', 'E1210:')
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001102
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +02001103 bw!
1104 call test_override('no_query_mouse', 0)
1105 set mousemodel&
1106endfunc
1107
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001108" Test for 'guitablabel' and 'guitabtooltip' options
1109func TestGuiTabLabel()
1110 call add(g:TabLabels, v:lnum + 100)
1111 let bufnrlist = tabpagebuflist(v:lnum)
1112 return bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
1113endfunc
1114
1115func TestGuiTabToolTip()
1116 call add(g:TabToolTips, v:lnum + 200)
1117 let bufnrlist = tabpagebuflist(v:lnum)
1118 return bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
1119endfunc
1120
1121func Test_gui_tablabel_tooltip()
Bram Moolenaarfb773a32021-07-03 21:37:59 +02001122 CheckNotFeature gui_athena
1123
Yegappan Lakshmanan7237cab2021-06-22 19:52:27 +02001124 %bw!
1125 " Removing the tabline at the end of this test, reduces the window height by
1126 " one. Save and restore it after the test.
1127 let save_lines = &lines
1128 edit one
1129 set modified
1130 tabnew two
1131 set modified
1132 tabnew three
1133 set modified
1134 let g:TabLabels = []
1135 set guitablabel=%{TestGuiTabLabel()}
1136 call test_override('starting', 1)
1137 redrawtabline
1138 call test_override('starting', 0)
1139 call assert_true(index(g:TabLabels, 101) != -1)
1140 call assert_true(index(g:TabLabels, 102) != -1)
1141 call assert_true(index(g:TabLabels, 103) != -1)
1142 set guitablabel&
1143 unlet g:TabLabels
1144
1145 if has('gui_gtk')
1146 " Only on GTK+, the tooltip function is called even if the mouse is not
1147 " on the tabline. on Win32 and Motif, the tooltip function is called only
1148 " when the mouse pointer is over the tabline.
1149 let g:TabToolTips = []
1150 set guitabtooltip=%{TestGuiTabToolTip()}
1151 call test_override('starting', 1)
1152 redrawtabline
1153 call test_override('starting', 0)
1154 call assert_true(index(g:TabToolTips, 201) != -1)
1155 call assert_true(index(g:TabToolTips, 202) != -1)
1156 call assert_true(index(g:TabToolTips, 203) != -1)
1157 set guitabtooltip&
1158 unlet g:TabToolTips
1159 endif
1160 %bw!
1161 let &lines = save_lines
1162endfunc
1163
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001164" Test for dropping files into a window in GUI
1165func DropFilesInCmdLine()
Bram Moolenaar1d1ce612021-06-27 19:02:52 +02001166 CheckFeature drop_file
1167
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001168 call feedkeys(":\"", 'L')
1169 call test_gui_drop_files(['a.c', 'b.c'], &lines, 1, 0)
1170 call feedkeys("\<CR>", 'L')
1171endfunc
1172
1173func Test_gui_drop_files()
Bram Moolenaar1d1ce612021-06-27 19:02:52 +02001174 CheckFeature drop_file
1175
Yegappan Lakshmanan5bca9062021-07-24 21:33:26 +02001176 call assert_fails('call test_gui_drop_files(1, 1, 1, 0)', 'E1211:')
1177 call assert_fails('call test_gui_drop_files(["x"], "", 1, 0)', 'E1210:')
1178 call assert_fails('call test_gui_drop_files(["x"], 1, "", 0)', 'E1210:')
1179 call assert_fails('call test_gui_drop_files(["x"], 1, 1, "")', 'E1210:')
Yegappan Lakshmanan18d46582021-06-23 20:46:52 +02001180
1181 %bw!
1182 %argdelete
1183 call test_gui_drop_files([], 1, 1, 0)
1184 call assert_equal([], argv())
1185 call test_gui_drop_files([1, 2], 1, 1, 0)
1186 call assert_equal([], argv())
1187
1188 call test_gui_drop_files(['a.c', 'b.c'], 1, 1, 0)
1189 call assert_equal(['a.c', 'b.c'], argv())
1190 %bw!
1191 %argdelete
1192 call test_gui_drop_files([], 1, 1, 0)
1193 call assert_equal([], argv())
1194 %bw!
1195 " if the buffer in the window is modified, then the file should be opened in
1196 " a new window
1197 set modified
1198 call test_gui_drop_files(['x.c', 'y.c'], 1, 1, 0)
1199 call assert_equal(['x.c', 'y.c'], argv())
1200 call assert_equal(2, winnr('$'))
1201 call assert_equal('x.c', bufname(winbufnr(1)))
1202 %bw!
1203 %argdelete
1204 " if Ctrl is pressed, then the file should be opened in a new window
1205 call test_gui_drop_files(['s.py', 't.py'], 1, 1, 0x10)
1206 call assert_equal(['s.py', 't.py'], argv())
1207 call assert_equal(2, winnr('$'))
1208 call assert_equal('s.py', bufname(winbufnr(1)))
1209 %bw!
1210 %argdelete
1211 " drop the files in a non-current window
1212 belowright new
1213 call test_gui_drop_files(['a.py', 'b.py'], 1, 1, 0)
1214 call assert_equal(['a.py', 'b.py'], argv())
1215 call assert_equal(2, winnr('$'))
1216 call assert_equal(1, winnr())
1217 call assert_equal('a.py', bufname(winbufnr(1)))
1218 %bw!
1219 %argdelete
1220 " pressing shift when dropping files should change directory
1221 let save_cwd = getcwd()
1222 call mkdir('Xdir1')
1223 call writefile([], 'Xdir1/Xfile1')
1224 call writefile([], 'Xdir1/Xfile2')
1225 call test_gui_drop_files(['Xdir1/Xfile1', 'Xdir1/Xfile2'], 1, 1, 0x4)
1226 call assert_equal('Xdir1', fnamemodify(getcwd(), ':t'))
1227 call assert_equal('Xfile1', @%)
1228 call chdir(save_cwd)
1229 " pressing shift when dropping directory and files should change directory
1230 call test_gui_drop_files(['Xdir1', 'Xdir1/Xfile2'], 1, 1, 0x4)
1231 call assert_equal('Xdir1', fnamemodify(getcwd(), ':t'))
1232 call assert_equal('Xdir1', fnamemodify(@%, ':t'))
1233 call chdir(save_cwd)
1234 %bw!
1235 %argdelete
1236 " dropping a directory should edit it
1237 call test_gui_drop_files(['Xdir1'], 1, 1, 0)
1238 call assert_equal('Xdir1', @%)
1239 %bw!
1240 %argdelete
1241 " dropping only a directory name with Shift should ignore it
1242 call test_gui_drop_files(['Xdir1'], 1, 1, 0x4)
1243 call assert_equal('', @%)
1244 %bw!
1245 %argdelete
1246 call delete('Xdir1', 'rf')
1247 " drop files in the command line. The GUI drop files adds the file names to
1248 " the low level input buffer. So need to use a cmdline map and feedkeys()
1249 " with 'Lx!' to process it in this function itself.
1250 cnoremap <expr> <buffer> <F4> DropFilesInCmdLine()
1251 call feedkeys(":\"\<F4>\<CR>", 'xt')
1252 call feedkeys('k', 'Lx!')
1253 call assert_equal('"a.c b.c', @:)
1254 cunmap <buffer> <F4>
1255endfunc
1256
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001257" vim: shiftwidth=2 sts=2 expandtab