blob: 8817d8f2c7f9b36986c58fb1d08e541c79e7d613 [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
Bram Moolenaar24d76362017-03-04 13:32:10 +010088 \ . ' -u NONE -U NONE --noplugin --not-a-term -c ''%s'''
Bram Moolenaarf8ab1b12017-03-01 18:30:34 +010089 " Ignore the "failed to create input context" error.
Bram Moolenaar24d76362017-03-04 13:32:10 +010090 let cmd = 'call test_ignore_error("E285") | '
91 \ . 'gui -f | '
92 \ . 'call feedkeys("'
Bram Moolenaar5074a0e2017-02-26 15:08:21 +010093 \ . '\"+p'
94 \ . ':s/' . test_call . '/' . test_response . '/\<CR>'
95 \ . '\"+yis'
96 \ . ':q!\<CR>", "tx")'
Bram Moolenaar24d76362017-03-04 13:32:10 +010097 let run_vimtest = printf(testee, cmd)
Bram Moolenaar5074a0e2017-02-26 15:08:21 +010098
99 " Set the quoteplus register to test_call, and another gvim will launched.
100 " Then, it first tries to paste the content of its own quotedplus register
101 " onto it. Second, it tries to substitute test_responce for the pasted
102 " sentence. If the sentence is identical to test_call, the substitution
103 " should succeed. Third, it tries to yank the result of the substitution
104 " to its own quoteplus register, and last it quits. When system()
105 " returns, the content of the quoteplus register should be identical to
106 " test_response if those quoteplus registers are synchronized properly
107 " with/through the X11 clipboard.
108 let @+ = test_call
109 call system(run_vimtest)
110 call assert_equal(test_response, @+)
111
112 let @+ = quoteplus_saved
113 endif
114
115 if !empty(skipped)
116 throw skipped
117 endif
118endfunc
119
Bram Moolenaar43dded82017-02-09 16:06:17 +0100120func Test_set_guifont()
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100121 let skipped = ''
122
123 let guifont_saved = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100124 if has('xfontset')
125 " Prevent 'guifontset' from canceling 'guifont'.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100126 let guifontset_saved = &guifontset
Bram Moolenaar43dded82017-02-09 16:06:17 +0100127 set guifontset=
128 endif
129
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100130 if !g:x11_based_gui
131 let skipped = g:not_implemented
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100132 elseif has('gui_athena') || has('gui_motif')
Bram Moolenaar43dded82017-02-09 16:06:17 +0100133 " Non-empty font list with invalid font names.
134 "
135 " This test is twofold: (1) It checks if the command fails as expected
136 " when there are no loadable fonts found in the list. (2) It checks if
137 " 'guifont' remains the same after the command loads none of the fonts
138 " listed.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100139 let flist = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100140 call assert_fails('set guifont=-notexist1-*,-notexist2-*')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100141 call assert_equal(flist, &guifont)
Bram Moolenaar43dded82017-02-09 16:06:17 +0100142
143 " Non-empty font list with a valid font name. Should pick up the first
144 " valid font.
145 set guifont=-notexist1-*,fixed,-notexist2-*
146 call assert_equal('fixed', getfontname())
147
148 " Empty list. Should fallback to the built-in default.
149 set guifont=
150 call assert_equal('7x13', getfontname())
151
152 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
153 " For GTK, what we refer to as 'font names' in our manual are actually
154 " 'initial font patterns'. A valid font which matches the 'canonical font
155 " pattern' constructed from a given 'initial pattern' is to be looked up
156 " and loaded. That explains why the GTK GUIs appear to accept 'invalid
157 " font names'.
158 "
159 " Non-empty list. Should always pick up the first element, no matter how
160 " strange it is, as explained above.
161 set guifont=(´・ω・`)\ 12,Courier\ 12
162 call assert_equal('(´・ω・`) 12', getfontname())
163
164 " Empty list. Should fallback to the built-in default.
165 set guifont=
166 call assert_equal('Monospace 10', getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100167 endif
168
169 if has('xfontset')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100170 let &guifontset = guifontset_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100171 endif
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100172 let &guifont = guifont_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100173
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100174 if !empty(skipped)
175 throw skipped
Bram Moolenaar43dded82017-02-09 16:06:17 +0100176 endif
177endfunc
178
Bram Moolenaar10434672017-02-12 19:59:08 +0100179func Test_set_guifontset()
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100180 let skipped = ''
Bram Moolenaar10434672017-02-12 19:59:08 +0100181
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100182 if !has('xfontset')
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100183 let skipped = g:not_supported . 'xfontset'
Bram Moolenaar10434672017-02-12 19:59:08 +0100184 else
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100185 let ctype_saved = v:ctype
186
187 " First, since XCreateFontSet(3) is very sensitive to locale, fonts must
188 " be chosen meticulously.
189 let font_head = '-misc-fixed-medium-r-normal--14'
190
191 let font_aw70 = font_head . '-130-75-75-c-70'
192 let font_aw140 = font_head . '-130-75-75-c-140'
193
194 let font_jisx0201 = font_aw70 . '-jisx0201.1976-0'
195 let font_jisx0208 = font_aw140 . '-jisx0208.1983-0'
196
197 let full_XLFDs = join([ font_jisx0208, font_jisx0201 ], ',')
198 let short_XLFDs = join([ font_aw140, font_aw70 ], ',')
199 let singleton = font_head . '-*'
200 let aliases = 'k14,r14'
201
202 " Second, among 'locales', look up such a locale that gets 'set
203 " guifontset=' to work successfully with every fontset specified with
204 " 'fontsets'.
205 let locales = [ 'ja_JP.UTF-8', 'ja_JP.eucJP', 'ja_JP.SJIS' ]
206 let fontsets = [ full_XLFDs, short_XLFDs, singleton, aliases ]
207
208 let feasible = 0
209 for locale in locales
210 try
211 exec 'language ctype' locale
212 catch /^Vim\%((\a\+)\)\=:E197/
213 continue
214 endtry
215 let done = 0
216 for fontset in fontsets
217 try
218 exec 'set guifontset=' . fontset
219 catch /^Vim\%((\a\+)\)\=:E\%(250\|252\|234\|597\|598\)/
220 break
221 endtry
222 let done += 1
223 endfor
224 if done == len(fontsets)
225 let feasible = 1
226 break
227 endif
228 endfor
229
230 " Third, give a set of tests if it is found feasible.
231 if !feasible
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100232 let skipped = g:not_hosted
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100233 else
234 " N.B. 'v:ctype' has already been set to an appropriate value in the
235 " previous loop.
236 for fontset in fontsets
237 exec 'set guifontset=' . fontset
238 call assert_equal(fontset, &guifontset)
239 endfor
240 endif
241
242 " Finally, restore ctype.
243 exec 'language ctype' ctype_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100244 endif
245
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100246 if !empty(skipped)
247 throw skipped
Bram Moolenaar10434672017-02-12 19:59:08 +0100248 endif
249endfunc
250
251func Test_set_guifontwide()
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100252 let skipped = ''
Bram Moolenaar10434672017-02-12 19:59:08 +0100253
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100254 if !g:x11_based_gui
255 let skipped = g:not_implemented
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100256 elseif has('gui_gtk')
257 let guifont_saved = &guifont
258 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100259
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100260 let fc_match = exepath('fc-match')
261 if empty(fc_match)
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100262 let skipped = g:not_hosted
Bram Moolenaar10434672017-02-12 19:59:08 +0100263 else
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100264 let &guifont = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=en')
265 let wide = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=ja')
266 exec 'set guifontwide=' . fnameescape(wide)
267 call assert_equal(wide, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100268 endif
269
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100270 let &guifontwide = guifontwide_saved
271 let &guifont = guifont_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100272
273 elseif has('gui_athena') || has('gui_motif')
274 " guifontwide is premised upon the xfontset feature.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100275 if !has('xfontset')
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100276 let skipped = g:not_supported . 'xfontset'
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100277 else
278 let encoding_saved = &encoding
279 let guifont_saved = &guifont
280 let guifontset_saved = &guifontset
281 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100282
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100283 let nfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-90-iso10646-1'
284 let wfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-180-iso10646-1'
Bram Moolenaar10434672017-02-12 19:59:08 +0100285
286 set encoding=utf-8
287
288 " Case 1: guifontset is empty
289 set guifontset=
290
291 " Case 1-1: Automatic selection
292 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100293 exec 'set guifont=' . nfont
294 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100295
296 " Case 1-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100297 exec 'set guifontwide=' . wfont
298 exec 'set guifont=' . nfont
299 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100300
301 " Case 2: guifontset is invalid
302 try
303 set guifontset=-*-notexist-*
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100304 call assert_false(1, "'set guifontset=-*-notexist-*' should have failed")
Bram Moolenaar10434672017-02-12 19:59:08 +0100305 catch
306 call assert_exception('E598')
307 endtry
308 " Set it to an invalid value brutally for preparation.
309 let &guifontset = '-*-notexist-*'
310
311 " Case 2-1: Automatic selection
312 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100313 exec 'set guifont=' . nfont
314 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100315
316 " Case 2-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100317 exec 'set guifontwide=' . wfont
318 exec 'set guifont=' . nfont
319 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100320
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100321 let &guifontwide = guifontwide_saved
322 let &guifontset = guifontset_saved
323 let &guifont = guifont_saved
324 let &encoding = encoding_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100325 endif
Bram Moolenaar10434672017-02-12 19:59:08 +0100326 endif
327
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100328 if !empty(skipped)
329 throw skipped
Bram Moolenaar10434672017-02-12 19:59:08 +0100330 endif
331endfunc
332
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100333func Test_set_guiheadroom()
334 let skipped = ''
335
336 if !g:x11_based_gui
337 let skipped = g:not_supported . 'guiheadroom'
338 else
339 " Since this script is to be read together with '-U NONE', the default
340 " value must be preserved.
341 call assert_equal(50, &guiheadroom)
342 endif
343
344 if !empty(skipped)
345 throw skipped
346 endif
347endfunc
348
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100349func Test_getwinpos()
350 call assert_match('Window position: X \d\+, Y \d\+', execute('winpos'))
351 call assert_true(getwinposx() >= 0)
352 call assert_true(getwinposy() >= 0)
Bram Moolenaar6f785742017-02-06 22:11:55 +0100353endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100354
Bram Moolenaar877e9572016-08-04 20:05:50 +0200355func Test_shell_command()
356 new
Bram Moolenaar9d5b8762016-08-04 21:21:13 +0200357 r !echo hello
358 call assert_equal('hello', substitute(getline(2), '\W', '', 'g'))
Bram Moolenaar877e9572016-08-04 20:05:50 +0200359 bwipe!
Bram Moolenaar877e9572016-08-04 20:05:50 +0200360endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100361
362func Test_windowid_variable()
Bram Moolenaar8be2fbb2017-02-23 19:32:47 +0100363 if g:x11_based_gui || has('win32')
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100364 call assert_true(v:windowid > 0)
365 else
366 call assert_equal(0, v:windowid)
367 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +0100368endfunc