blob: be3e52deb973dabd19b7979121663ceda351ea80 [file] [log] [blame]
Bram Moolenaar877e9572016-08-04 20:05:50 +02001" Tests specifically for the GUI
2
3if !has('gui') || ($DISPLAY == "" && !has('gui_running'))
4 finish
5endif
6
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.
18" Must be run first.
19func Test_1_set_secure()
20 set exrc secure
21 gui -f
22 call assert_equal(1, has('gui_running'))
23endfunc
24
Bram Moolenaar6f785742017-02-06 22:11:55 +010025func Test_getfontname_with_arg()
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010026 let skipped = ''
27
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +010028 if !g:x11_based_gui
29 let skipped = g:not_implemented
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010030 elseif has('gui_athena') || has('gui_motif')
Bram Moolenaar6f785742017-02-06 22:11:55 +010031 " Invalid font name. The result should be an empty string.
32 call assert_equal('', getfontname('notexist'))
33
34 " Valid font name. This is usually the real name of 7x13 by default.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010035 let fname = '-misc-fixed-medium-r-normal--13-120-75-75-c-70-iso8859-1'
36 call assert_equal(fname, getfontname(fname))
Bram Moolenaar6f785742017-02-06 22:11:55 +010037
38 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
39 " Invalid font name. The result should be the name plus the default size.
40 call assert_equal('notexist 10', getfontname('notexist'))
41
42 " Valid font name. This is usually the real name of Monospace by default.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010043 let fname = 'Bitstream Vera Sans Mono 12'
44 call assert_equal(fname, getfontname(fname))
45 endif
46
47 if !empty(skipped)
48 throw skipped
Bram Moolenaar6f785742017-02-06 22:11:55 +010049 endif
50endfunc
51
52func Test_getfontname_without_arg()
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010053 let skipped = ''
54
55 let fname = getfontname()
56
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +010057 if !g:x11_based_gui
58 let skipped = g:not_implemented
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010059 elseif has('gui_kde')
Bram Moolenaar6f785742017-02-06 22:11:55 +010060 " 'expected' is the value specified by SetUp() above.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010061 call assert_equal('Courier 10 Pitch/8/-1/5/50/0/0/0/0/0', fname)
Bram Moolenaar6f785742017-02-06 22:11:55 +010062 elseif has('gui_athena') || has('gui_motif')
63 " 'expected' is DFLT_FONT of gui_x11.c.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010064 call assert_equal('7x13', fname)
Bram Moolenaar6f785742017-02-06 22:11:55 +010065 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
66 " 'expected' is DEFAULT_FONT of gui_gtk_x11.c.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010067 call assert_equal('Monospace 10', fname)
68 endif
69
70 if !empty(skipped)
71 throw skipped
Bram Moolenaar6f785742017-02-06 22:11:55 +010072 endif
73endfunc
74
Bram Moolenaar5074a0e2017-02-26 15:08:21 +010075func Test_quoteplus()
76 let skipped = ''
77
78 if !g:x11_based_gui
79 let skipped = g:not_supported . 'quoteplus'
80 else
81 let quoteplus_saved = @+
82
83 let test_call = 'Can you hear me?'
84 let test_response = 'Yes, I can.'
85 let vim_exe = exepath(v:progpath)
86 let testee = 'VIMRUNTIME=' . $VIMRUNTIME . '; export VIMRUNTIME;'
Bram Moolenaarf8ab1b12017-03-01 18:30:34 +010087 \ . vim_exe
88 \ . ' -f -g -u NONE -U NONE --noplugin --cmd ''%s'' -c ''%s'''
89 " Ignore the "failed to create input context" error.
90 let cmd1 = 'call test_ignore_error("E285")'
91 let cmd2 = 'call feedkeys("'
Bram Moolenaar5074a0e2017-02-26 15:08:21 +010092 \ . '\"+p'
93 \ . ':s/' . test_call . '/' . test_response . '/\<CR>'
94 \ . '\"+yis'
95 \ . ':q!\<CR>", "tx")'
Bram Moolenaarf8ab1b12017-03-01 18:30:34 +010096 let run_vimtest = printf(testee, cmd1, cmd2)
Bram Moolenaar5074a0e2017-02-26 15:08:21 +010097
98 " Set the quoteplus register to test_call, and another gvim will launched.
99 " Then, it first tries to paste the content of its own quotedplus register
100 " onto it. Second, it tries to substitute test_responce for the pasted
101 " sentence. If the sentence is identical to test_call, the substitution
102 " should succeed. Third, it tries to yank the result of the substitution
103 " to its own quoteplus register, and last it quits. When system()
104 " returns, the content of the quoteplus register should be identical to
105 " test_response if those quoteplus registers are synchronized properly
106 " with/through the X11 clipboard.
107 let @+ = test_call
108 call system(run_vimtest)
109 call assert_equal(test_response, @+)
110
111 let @+ = quoteplus_saved
112 endif
113
114 if !empty(skipped)
115 throw skipped
116 endif
117endfunc
118
Bram Moolenaar43dded82017-02-09 16:06:17 +0100119func Test_set_guifont()
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100120 let skipped = ''
121
122 let guifont_saved = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100123 if has('xfontset')
124 " Prevent 'guifontset' from canceling 'guifont'.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100125 let guifontset_saved = &guifontset
Bram Moolenaar43dded82017-02-09 16:06:17 +0100126 set guifontset=
127 endif
128
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100129 if !g:x11_based_gui
130 let skipped = g:not_implemented
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100131 elseif has('gui_athena') || has('gui_motif')
Bram Moolenaar43dded82017-02-09 16:06:17 +0100132 " Non-empty font list with invalid font names.
133 "
134 " This test is twofold: (1) It checks if the command fails as expected
135 " when there are no loadable fonts found in the list. (2) It checks if
136 " 'guifont' remains the same after the command loads none of the fonts
137 " listed.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100138 let flist = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100139 call assert_fails('set guifont=-notexist1-*,-notexist2-*')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100140 call assert_equal(flist, &guifont)
Bram Moolenaar43dded82017-02-09 16:06:17 +0100141
142 " Non-empty font list with a valid font name. Should pick up the first
143 " valid font.
144 set guifont=-notexist1-*,fixed,-notexist2-*
145 call assert_equal('fixed', getfontname())
146
147 " Empty list. Should fallback to the built-in default.
148 set guifont=
149 call assert_equal('7x13', getfontname())
150
151 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
152 " For GTK, what we refer to as 'font names' in our manual are actually
153 " 'initial font patterns'. A valid font which matches the 'canonical font
154 " pattern' constructed from a given 'initial pattern' is to be looked up
155 " and loaded. That explains why the GTK GUIs appear to accept 'invalid
156 " font names'.
157 "
158 " Non-empty list. Should always pick up the first element, no matter how
159 " strange it is, as explained above.
160 set guifont=(´・ω・`)\ 12,Courier\ 12
161 call assert_equal('(´・ω・`) 12', getfontname())
162
163 " Empty list. Should fallback to the built-in default.
164 set guifont=
165 call assert_equal('Monospace 10', getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100166 endif
167
168 if has('xfontset')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100169 let &guifontset = guifontset_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100170 endif
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100171 let &guifont = guifont_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100172
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100173 if !empty(skipped)
174 throw skipped
Bram Moolenaar43dded82017-02-09 16:06:17 +0100175 endif
176endfunc
177
Bram Moolenaar10434672017-02-12 19:59:08 +0100178func Test_set_guifontset()
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100179 let skipped = ''
Bram Moolenaar10434672017-02-12 19:59:08 +0100180
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100181 if !has('xfontset')
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100182 let skipped = g:not_supported . 'xfontset'
Bram Moolenaar10434672017-02-12 19:59:08 +0100183 else
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100184 let ctype_saved = v:ctype
185
186 " First, since XCreateFontSet(3) is very sensitive to locale, fonts must
187 " be chosen meticulously.
188 let font_head = '-misc-fixed-medium-r-normal--14'
189
190 let font_aw70 = font_head . '-130-75-75-c-70'
191 let font_aw140 = font_head . '-130-75-75-c-140'
192
193 let font_jisx0201 = font_aw70 . '-jisx0201.1976-0'
194 let font_jisx0208 = font_aw140 . '-jisx0208.1983-0'
195
196 let full_XLFDs = join([ font_jisx0208, font_jisx0201 ], ',')
197 let short_XLFDs = join([ font_aw140, font_aw70 ], ',')
198 let singleton = font_head . '-*'
199 let aliases = 'k14,r14'
200
201 " Second, among 'locales', look up such a locale that gets 'set
202 " guifontset=' to work successfully with every fontset specified with
203 " 'fontsets'.
204 let locales = [ 'ja_JP.UTF-8', 'ja_JP.eucJP', 'ja_JP.SJIS' ]
205 let fontsets = [ full_XLFDs, short_XLFDs, singleton, aliases ]
206
207 let feasible = 0
208 for locale in locales
209 try
210 exec 'language ctype' locale
211 catch /^Vim\%((\a\+)\)\=:E197/
212 continue
213 endtry
214 let done = 0
215 for fontset in fontsets
216 try
217 exec 'set guifontset=' . fontset
218 catch /^Vim\%((\a\+)\)\=:E\%(250\|252\|234\|597\|598\)/
219 break
220 endtry
221 let done += 1
222 endfor
223 if done == len(fontsets)
224 let feasible = 1
225 break
226 endif
227 endfor
228
229 " Third, give a set of tests if it is found feasible.
230 if !feasible
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100231 let skipped = g:not_hosted
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100232 else
233 " N.B. 'v:ctype' has already been set to an appropriate value in the
234 " previous loop.
235 for fontset in fontsets
236 exec 'set guifontset=' . fontset
237 call assert_equal(fontset, &guifontset)
238 endfor
239 endif
240
241 " Finally, restore ctype.
242 exec 'language ctype' ctype_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100243 endif
244
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100245 if !empty(skipped)
246 throw skipped
Bram Moolenaar10434672017-02-12 19:59:08 +0100247 endif
248endfunc
249
250func Test_set_guifontwide()
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100251 let skipped = ''
Bram Moolenaar10434672017-02-12 19:59:08 +0100252
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100253 if !g:x11_based_gui
254 let skipped = g:not_implemented
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100255 elseif has('gui_gtk')
256 let guifont_saved = &guifont
257 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100258
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100259 let fc_match = exepath('fc-match')
260 if empty(fc_match)
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100261 let skipped = g:not_hosted
Bram Moolenaar10434672017-02-12 19:59:08 +0100262 else
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100263 let &guifont = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=en')
264 let wide = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=ja')
265 exec 'set guifontwide=' . fnameescape(wide)
266 call assert_equal(wide, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100267 endif
268
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100269 let &guifontwide = guifontwide_saved
270 let &guifont = guifont_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100271
272 elseif has('gui_athena') || has('gui_motif')
273 " guifontwide is premised upon the xfontset feature.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100274 if !has('xfontset')
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100275 let skipped = g:not_supported . 'xfontset'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100276 else
277 let encoding_saved = &encoding
278 let guifont_saved = &guifont
279 let guifontset_saved = &guifontset
280 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100281
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100282 let nfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-90-iso10646-1'
283 let wfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-180-iso10646-1'
Bram Moolenaar10434672017-02-12 19:59:08 +0100284
285 set encoding=utf-8
286
287 " Case 1: guifontset is empty
288 set guifontset=
289
290 " Case 1-1: Automatic selection
291 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100292 exec 'set guifont=' . nfont
293 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100294
295 " Case 1-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100296 exec 'set guifontwide=' . wfont
297 exec 'set guifont=' . nfont
298 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100299
300 " Case 2: guifontset is invalid
301 try
302 set guifontset=-*-notexist-*
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100303 call assert_false(1, "'set guifontset=-*-notexist-*' should have failed")
Bram Moolenaar10434672017-02-12 19:59:08 +0100304 catch
305 call assert_exception('E598')
306 endtry
307 " Set it to an invalid value brutally for preparation.
308 let &guifontset = '-*-notexist-*'
309
310 " Case 2-1: Automatic selection
311 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100312 exec 'set guifont=' . nfont
313 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100314
315 " Case 2-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100316 exec 'set guifontwide=' . wfont
317 exec 'set guifont=' . nfont
318 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100319
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100320 let &guifontwide = guifontwide_saved
321 let &guifontset = guifontset_saved
322 let &guifont = guifont_saved
323 let &encoding = encoding_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100324 endif
Bram Moolenaar10434672017-02-12 19:59:08 +0100325 endif
326
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100327 if !empty(skipped)
328 throw skipped
Bram Moolenaar10434672017-02-12 19:59:08 +0100329 endif
330endfunc
331
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100332func Test_set_guiheadroom()
333 let skipped = ''
334
335 if !g:x11_based_gui
336 let skipped = g:not_supported . 'guiheadroom'
337 else
338 " Since this script is to be read together with '-U NONE', the default
339 " value must be preserved.
340 call assert_equal(50, &guiheadroom)
341 endif
342
343 if !empty(skipped)
344 throw skipped
345 endif
346endfunc
347
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100348func Test_getwinpos()
349 call assert_match('Window position: X \d\+, Y \d\+', execute('winpos'))
350 call assert_true(getwinposx() >= 0)
351 call assert_true(getwinposy() >= 0)
Bram Moolenaar6f785742017-02-06 22:11:55 +0100352endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100353
Bram Moolenaar877e9572016-08-04 20:05:50 +0200354func Test_shell_command()
355 new
Bram Moolenaar9d5b8762016-08-04 21:21:13 +0200356 r !echo hello
357 call assert_equal('hello', substitute(getline(2), '\W', '', 'g'))
Bram Moolenaar877e9572016-08-04 20:05:50 +0200358 bwipe!
Bram Moolenaar877e9572016-08-04 20:05:50 +0200359endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100360
361func Test_windowid_variable()
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100362 if g:x11_based_gui || has('win32')
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100363 call assert_true(v:windowid > 0)
364 else
365 call assert_equal(0, v:windowid)
366 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +0100367endfunc