blob: aad56904152b9bec3618b6f9fa2dfec0f367440d [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 Moolenaar13c724f2017-02-05 20:54:26 +01007let s:x11_based_gui = has('gui_athena') || has('gui_motif')
8 \ || has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
9
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010010" Reasons for 'skipped'.
11let s:not_supported = "Skipped: Feature/Option not supported by this GUI: "
12let s:not_implemented = "Skipped: Test not implemented yet for this GUI"
13let s:not_hosted = "Skipped: Test not hosted by the system/environment"
14
Bram Moolenaar877e9572016-08-04 20:05:50 +020015" For KDE set a font, empty 'guifont' may cause a hang.
16func SetUp()
17 if has("gui_kde")
18 set guifont=Courier\ 10\ Pitch/8/-1/5/50/0/0/0/0/0
19 endif
Bram Moolenaar50fa8dd2016-08-09 22:58:21 +020020
Bram Moolenaarf1ab9c12017-02-01 12:32:58 +010021 " Gnome insists on creating $HOME/.gnome2/, set $HOME to avoid changing the
22 " actual home directory. But avoid triggering fontconfig by setting the
23 " cache directory. Only needed for Unix.
24 if $XDG_CACHE_HOME == '' && exists('g:tester_HOME')
Bram Moolenaar56e6bd72017-02-01 12:08:47 +010025 let $XDG_CACHE_HOME = g:tester_HOME . '/.cache'
26 endif
Bram Moolenaar50fa8dd2016-08-09 22:58:21 +020027 call mkdir('Xhome')
28 let $HOME = fnamemodify('Xhome', ':p')
29endfunc
30
31func TearDown()
32 call delete('Xhome', 'rf')
Bram Moolenaar877e9572016-08-04 20:05:50 +020033endfunc
34
35" Test for resetting "secure" flag after GUI has started.
36" Must be run first.
37func Test_1_set_secure()
38 set exrc secure
39 gui -f
40 call assert_equal(1, has('gui_running'))
41endfunc
42
Bram Moolenaar6f785742017-02-06 22:11:55 +010043func Test_getfontname_with_arg()
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010044 let skipped = ''
45
46 if !s:x11_based_gui
47 let skipped = s:not_implemented
48 elseif has('gui_athena') || has('gui_motif')
Bram Moolenaar6f785742017-02-06 22:11:55 +010049 " Invalid font name. The result should be an empty string.
50 call assert_equal('', getfontname('notexist'))
51
52 " Valid font name. This is usually the real name of 7x13 by default.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010053 let fname = '-misc-fixed-medium-r-normal--13-120-75-75-c-70-iso8859-1'
54 call assert_equal(fname, getfontname(fname))
Bram Moolenaar6f785742017-02-06 22:11:55 +010055
56 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
57 " Invalid font name. The result should be the name plus the default size.
58 call assert_equal('notexist 10', getfontname('notexist'))
59
60 " Valid font name. This is usually the real name of Monospace by default.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010061 let fname = 'Bitstream Vera Sans Mono 12'
62 call assert_equal(fname, getfontname(fname))
63 endif
64
65 if !empty(skipped)
66 throw skipped
Bram Moolenaar6f785742017-02-06 22:11:55 +010067 endif
68endfunc
69
70func Test_getfontname_without_arg()
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010071 let skipped = ''
72
73 let fname = getfontname()
74
75 if !s:x11_based_gui
76 let skipped = s:not_implemented
77 elseif has('gui_kde')
Bram Moolenaar6f785742017-02-06 22:11:55 +010078 " 'expected' is the value specified by SetUp() above.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010079 call assert_equal('Courier 10 Pitch/8/-1/5/50/0/0/0/0/0', fname)
Bram Moolenaar6f785742017-02-06 22:11:55 +010080 elseif has('gui_athena') || has('gui_motif')
81 " 'expected' is DFLT_FONT of gui_x11.c.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010082 call assert_equal('7x13', fname)
Bram Moolenaar6f785742017-02-06 22:11:55 +010083 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
84 " 'expected' is DEFAULT_FONT of gui_gtk_x11.c.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010085 call assert_equal('Monospace 10', fname)
86 endif
87
88 if !empty(skipped)
89 throw skipped
Bram Moolenaar6f785742017-02-06 22:11:55 +010090 endif
91endfunc
92
Bram Moolenaar43dded82017-02-09 16:06:17 +010093func Test_set_guifont()
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010094 let skipped = ''
95
96 let guifont_saved = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +010097 if has('xfontset')
98 " Prevent 'guifontset' from canceling 'guifont'.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +010099 let guifontset_saved = &guifontset
Bram Moolenaar43dded82017-02-09 16:06:17 +0100100 set guifontset=
101 endif
102
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100103 if !s:x11_based_gui
104 let skipped = s:not_implemented
105 elseif has('gui_athena') || has('gui_motif')
Bram Moolenaar43dded82017-02-09 16:06:17 +0100106 " Non-empty font list with invalid font names.
107 "
108 " This test is twofold: (1) It checks if the command fails as expected
109 " when there are no loadable fonts found in the list. (2) It checks if
110 " 'guifont' remains the same after the command loads none of the fonts
111 " listed.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100112 let flist = &guifont
Bram Moolenaar43dded82017-02-09 16:06:17 +0100113 call assert_fails('set guifont=-notexist1-*,-notexist2-*')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100114 call assert_equal(flist, &guifont)
Bram Moolenaar43dded82017-02-09 16:06:17 +0100115
116 " Non-empty font list with a valid font name. Should pick up the first
117 " valid font.
118 set guifont=-notexist1-*,fixed,-notexist2-*
119 call assert_equal('fixed', getfontname())
120
121 " Empty list. Should fallback to the built-in default.
122 set guifont=
123 call assert_equal('7x13', getfontname())
124
125 elseif has('gui_gtk2') || has('gui_gnome') || has('gui_gtk3')
126 " For GTK, what we refer to as 'font names' in our manual are actually
127 " 'initial font patterns'. A valid font which matches the 'canonical font
128 " pattern' constructed from a given 'initial pattern' is to be looked up
129 " and loaded. That explains why the GTK GUIs appear to accept 'invalid
130 " font names'.
131 "
132 " Non-empty list. Should always pick up the first element, no matter how
133 " strange it is, as explained above.
134 set guifont=(´・ω・`)\ 12,Courier\ 12
135 call assert_equal('(´・ω・`) 12', getfontname())
136
137 " Empty list. Should fallback to the built-in default.
138 set guifont=
139 call assert_equal('Monospace 10', getfontname())
Bram Moolenaar43dded82017-02-09 16:06:17 +0100140 endif
141
142 if has('xfontset')
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100143 let &guifontset = guifontset_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100144 endif
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100145 let &guifont = guifont_saved
Bram Moolenaar43dded82017-02-09 16:06:17 +0100146
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100147 if !empty(skipped)
148 throw skipped
Bram Moolenaar43dded82017-02-09 16:06:17 +0100149 endif
150endfunc
151
Bram Moolenaar10434672017-02-12 19:59:08 +0100152func Test_set_guifontset()
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100153 let skipped = ''
Bram Moolenaar10434672017-02-12 19:59:08 +0100154
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100155 if !has('xfontset')
156 let skipped = s:not_supported . 'xfontset'
Bram Moolenaar10434672017-02-12 19:59:08 +0100157 else
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100158 let ctype_saved = v:ctype
159
160 " First, since XCreateFontSet(3) is very sensitive to locale, fonts must
161 " be chosen meticulously.
162 let font_head = '-misc-fixed-medium-r-normal--14'
163
164 let font_aw70 = font_head . '-130-75-75-c-70'
165 let font_aw140 = font_head . '-130-75-75-c-140'
166
167 let font_jisx0201 = font_aw70 . '-jisx0201.1976-0'
168 let font_jisx0208 = font_aw140 . '-jisx0208.1983-0'
169
170 let full_XLFDs = join([ font_jisx0208, font_jisx0201 ], ',')
171 let short_XLFDs = join([ font_aw140, font_aw70 ], ',')
172 let singleton = font_head . '-*'
173 let aliases = 'k14,r14'
174
175 " Second, among 'locales', look up such a locale that gets 'set
176 " guifontset=' to work successfully with every fontset specified with
177 " 'fontsets'.
178 let locales = [ 'ja_JP.UTF-8', 'ja_JP.eucJP', 'ja_JP.SJIS' ]
179 let fontsets = [ full_XLFDs, short_XLFDs, singleton, aliases ]
180
181 let feasible = 0
182 for locale in locales
183 try
184 exec 'language ctype' locale
185 catch /^Vim\%((\a\+)\)\=:E197/
186 continue
187 endtry
188 let done = 0
189 for fontset in fontsets
190 try
191 exec 'set guifontset=' . fontset
192 catch /^Vim\%((\a\+)\)\=:E\%(250\|252\|234\|597\|598\)/
193 break
194 endtry
195 let done += 1
196 endfor
197 if done == len(fontsets)
198 let feasible = 1
199 break
200 endif
201 endfor
202
203 " Third, give a set of tests if it is found feasible.
204 if !feasible
205 let skipped = s:not_hosted
206 else
207 " N.B. 'v:ctype' has already been set to an appropriate value in the
208 " previous loop.
209 for fontset in fontsets
210 exec 'set guifontset=' . fontset
211 call assert_equal(fontset, &guifontset)
212 endfor
213 endif
214
215 " Finally, restore ctype.
216 exec 'language ctype' ctype_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100217 endif
218
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100219 if !empty(skipped)
220 throw skipped
Bram Moolenaar10434672017-02-12 19:59:08 +0100221 endif
222endfunc
223
224func Test_set_guifontwide()
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100225 let skipped = ''
Bram Moolenaar10434672017-02-12 19:59:08 +0100226
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100227 if !s:x11_based_gui
228 let skipped = s:not_implemented
229 elseif has('gui_gtk')
230 let guifont_saved = &guifont
231 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100232
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100233 let fc_match = exepath('fc-match')
234 if empty(fc_match)
235 let skipped = s:not_hosted
Bram Moolenaar10434672017-02-12 19:59:08 +0100236 else
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100237 let &guifont = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=en')
238 let wide = system('fc-match -f "%{family[0]} %{size}" monospace:size=10:lang=ja')
239 exec 'set guifontwide=' . fnameescape(wide)
240 call assert_equal(wide, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100241 endif
242
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100243 let &guifontwide = guifontwide_saved
244 let &guifont = guifont_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100245
246 elseif has('gui_athena') || has('gui_motif')
247 " guifontwide is premised upon the xfontset feature.
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100248 if !has('xfontset')
249 let skipped = s:not_supported . 'xfontset'
250 else
251 let encoding_saved = &encoding
252 let guifont_saved = &guifont
253 let guifontset_saved = &guifontset
254 let guifontwide_saved = &guifontwide
Bram Moolenaar10434672017-02-12 19:59:08 +0100255
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100256 let nfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-90-iso10646-1'
257 let wfont = '-misc-fixed-medium-r-normal-*-18-120-100-100-c-180-iso10646-1'
Bram Moolenaar10434672017-02-12 19:59:08 +0100258
259 set encoding=utf-8
260
261 " Case 1: guifontset is empty
262 set guifontset=
263
264 " Case 1-1: Automatic selection
265 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100266 exec 'set guifont=' . nfont
267 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100268
269 " Case 1-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100270 exec 'set guifontwide=' . wfont
271 exec 'set guifont=' . nfont
272 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100273
274 " Case 2: guifontset is invalid
275 try
276 set guifontset=-*-notexist-*
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100277 call assert_false(1, "'set guifontset=-*-notexist-*' should have failed")
Bram Moolenaar10434672017-02-12 19:59:08 +0100278 catch
279 call assert_exception('E598')
280 endtry
281 " Set it to an invalid value brutally for preparation.
282 let &guifontset = '-*-notexist-*'
283
284 " Case 2-1: Automatic selection
285 set guifontwide=
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100286 exec 'set guifont=' . nfont
287 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100288
289 " Case 2-2: Manual selection
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100290 exec 'set guifontwide=' . wfont
291 exec 'set guifont=' . nfont
292 call assert_equal(wfont, &guifontwide)
Bram Moolenaar10434672017-02-12 19:59:08 +0100293
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100294 let &guifontwide = guifontwide_saved
295 let &guifontset = guifontset_saved
296 let &guifont = guifont_saved
297 let &encoding = encoding_saved
Bram Moolenaar10434672017-02-12 19:59:08 +0100298 endif
Bram Moolenaar10434672017-02-12 19:59:08 +0100299 endif
300
Bram Moolenaar4e9dbc72017-02-17 13:44:48 +0100301 if !empty(skipped)
302 throw skipped
Bram Moolenaar10434672017-02-12 19:59:08 +0100303 endif
304endfunc
305
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100306func Test_getwinpos()
307 call assert_match('Window position: X \d\+, Y \d\+', execute('winpos'))
308 call assert_true(getwinposx() >= 0)
309 call assert_true(getwinposy() >= 0)
Bram Moolenaar6f785742017-02-06 22:11:55 +0100310endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100311
Bram Moolenaar877e9572016-08-04 20:05:50 +0200312func Test_shell_command()
313 new
Bram Moolenaar9d5b8762016-08-04 21:21:13 +0200314 r !echo hello
315 call assert_equal('hello', substitute(getline(2), '\W', '', 'g'))
Bram Moolenaar877e9572016-08-04 20:05:50 +0200316 bwipe!
Bram Moolenaar877e9572016-08-04 20:05:50 +0200317endfunc
Bram Moolenaar13c724f2017-02-05 20:54:26 +0100318
319func Test_windowid_variable()
320 if s:x11_based_gui || has('win32')
321 call assert_true(v:windowid > 0)
322 else
323 call assert_equal(0, v:windowid)
324 endif
Bram Moolenaar6f785742017-02-06 22:11:55 +0100325endfunc