xref: /vim-8.2.3635/src/testdir/test_tabpage.vim (revision 8ea05de6)
1" Tests for tabpage
2
3source screendump.vim
4source check.vim
5
6function Test_tabpage()
7  CheckFeature quickfix
8
9  bw!
10  " Simple test for opening and closing a tab page
11  tabnew
12  call assert_equal(2, tabpagenr())
13  quit
14
15  " Open three tab pages and use ":tabdo"
16  0tabnew
17  1tabnew
18  $tabnew
19  %del
20  tabdo call append(line('$'), tabpagenr())
21  tabclose! 2
22  tabrewind
23  let line1 = getline('$')
24  undo
25  q
26  tablast
27  let line2 = getline('$')
28  q!
29  call append(line('$'), line1)
30  call append(line('$'), line2)
31  unlet line1 line2
32  call assert_equal(['', '3', '1', '4'], getline(1, '$'))
33  "
34  " Test for settabvar() and gettabvar() functions. Open a new tab page and
35  " set 3 variables to a number, string and a list. Verify that the variables
36  " are correctly set.
37  tabnew
38  tabfirst
39  call settabvar(2, 'val_num', 100)
40  eval 'SetTabVar test'->settabvar(2, 'val_str')
41  call settabvar(2, 'val_list', ['red', 'blue', 'green'])
42  "
43  call assert_true(gettabvar(2, 'val_num') == 100 && gettabvar(2, 'val_str') == 'SetTabVar test' && gettabvar(2, 'val_list') == ['red', 'blue', 'green'])
44
45  tabnext 2
46  call assert_true(t:val_num == 100 && t:val_str == 'SetTabVar test'  && t:val_list == ['red', 'blue', 'green'])
47  tabclose
48
49  " Test for ":tab drop exist-file" to keep current window.
50  sp test1
51  tab drop test1
52  call assert_true(tabpagenr('$') == 1 && winnr('$') == 2 && winnr() == 1)
53  close
54  "
55  "
56  " Test for ":tab drop new-file" to keep current window of tabpage 1.
57  split
58  tab drop newfile
59  call assert_true(tabpagenr('$') == 2 && tabpagewinnr(1, '$') == 2 && tabpagewinnr(1) == 1)
60  tabclose
61  q
62  "
63  "
64  " Test for ":tab drop multi-opened-file" to keep current tabpage and window.
65  new test1
66  tabnew
67  new test1
68  tab drop test1
69  call assert_true(tabpagenr() == 2 && tabpagewinnr(2, '$') == 2 && tabpagewinnr(2) == 1)
70  tabclose
71  q
72  "
73  "
74  " Test for ":tab drop vertical-split-window" to jump test1 buffer
75  tabedit test1
76  vnew
77  tabfirst
78  tab drop test1
79  call assert_equal([2, 2, 2, 2], [tabpagenr('$'), tabpagenr(), tabpagewinnr(2, '$'), tabpagewinnr(2)])
80  1tabonly
81  "
82  "
83  for i in range(9) | tabnew | endfor
84  normal! 1gt
85  call assert_equal(1, tabpagenr())
86  tabmove 5
87  call assert_equal(5, tabpagenr())
88  .tabmove
89  call assert_equal(5, tabpagenr())
90  tabmove -
91  call assert_equal(4, tabpagenr())
92  tabmove +
93  call assert_equal(5, tabpagenr())
94  tabmove -2
95  call assert_equal(3, tabpagenr())
96  tabmove +4
97  call assert_equal(7, tabpagenr())
98  tabmove
99  call assert_equal(10, tabpagenr())
100  0tabmove
101  call assert_equal(1, tabpagenr())
102  $tabmove
103  call assert_equal(10, tabpagenr())
104  tabmove 0
105  call assert_equal(1, tabpagenr())
106  tabmove $
107  call assert_equal(10, tabpagenr())
108  3tabmove
109  call assert_equal(4, tabpagenr())
110  7tabmove 5
111  call assert_equal(5, tabpagenr())
112  -tabmove
113  call assert_equal(4, tabpagenr())
114  +tabmove
115  call assert_equal(5, tabpagenr())
116  -2tabmove
117  call assert_equal(3, tabpagenr())
118  +3tabmove
119  call assert_equal(6, tabpagenr())
120
121  " The following are a no-op
122  norm! 2gt
123  call assert_equal(2, tabpagenr())
124  tabmove 2
125  call assert_equal(2, tabpagenr())
126  2tabmove
127  call assert_equal(2, tabpagenr())
128  tabmove 1
129  call assert_equal(2, tabpagenr())
130  1tabmove
131  call assert_equal(2, tabpagenr())
132
133  call assert_fails('let t = tabpagenr("@")', 'E15:')
134  call assert_equal(0, tabpagewinnr(-1))
135  call assert_fails("99tabmove", 'E16:')
136  call assert_fails("+99tabmove", 'E16:')
137  call assert_fails("-99tabmove", 'E16:')
138  call assert_fails("tabmove foo", 'E475:')
139  call assert_fails("tabmove 99", 'E475:')
140  call assert_fails("tabmove +99", 'E475:')
141  call assert_fails("tabmove -99", 'E475:')
142  call assert_fails("tabmove -3+", 'E475:')
143  call assert_fails("tabmove $3", 'E475:')
144  call assert_fails("%tabonly", 'E16:')
145  1tabonly!
146  tabmove 1
147  call assert_equal(1, tabpagenr())
148  tabnew
149  call assert_fails("-2tabmove", 'E16:')
150  tabonly!
151endfunc
152
153" Test autocommands
154function Test_tabpage_with_autocmd()
155  command -nargs=1 -bar C :call add(s:li, '=== ' . <q-args> . ' ===')|<args>
156  augroup TestTabpageGroup
157    au!
158    autocmd TabEnter * call add(s:li, 'TabEnter')
159    autocmd WinEnter * call add(s:li, 'WinEnter')
160    autocmd BufEnter * call add(s:li, 'BufEnter')
161    autocmd TabLeave * call add(s:li, 'TabLeave')
162    autocmd WinLeave * call add(s:li, 'WinLeave')
163    autocmd BufLeave * call add(s:li, 'BufLeave')
164  augroup END
165
166  let s:li = []
167  let t:a='a'
168  C tab split
169  call assert_equal(['=== tab split ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter'], s:li)
170  let s:li = []
171  let t:a='b'
172  C tabnew
173  call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
174  let t:a='c'
175  let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
176  call assert_equal(['a', 'b', 'c'], s:li)
177
178  let s:li = []
179  C call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)')
180  call assert_equal(["=== call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)') ==="], s:li)
181  let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
182  call assert_equal(['2', '4', '6'], s:li)
183
184  let s:li = []
185  let w:a='a'
186  C vsplit
187  call assert_equal(['=== vsplit ===', 'WinLeave', 'WinEnter'], s:li)
188  let s:li = []
189  let w:a='a'
190  let tabn=tabpagenr()
191  let winr=range(1, winnr('$'))
192  C tabnext 1
193  call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
194  let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
195  call assert_equal(['a', 'a'], s:li)
196  let s:li = []
197  C call map(copy(winr), '(v:val*2)->settabwinvar(' .. tabn .. ', v:val, ''a'')')
198  let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
199  call assert_equal(['2', '4'], s:li)
200
201  augroup TabDestructive
202    autocmd TabEnter * :C tabnext 2 | C tabclose 3
203  augroup END
204  let s:li = []
205  C tabnext 3
206  call assert_equal(['=== tabnext 3 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
207  call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
208
209  autocmd! TabDestructive TabEnter
210  let s:li = []
211  C tabnew
212  call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
213  let s:li = []
214  C tabnext 1
215  call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
216
217  autocmd TabDestructive TabEnter * nested :C tabnext 2 | C tabclose 3
218  let s:li = []
219  call assert_equal(3, tabpagenr('$'))
220  C tabnext 2
221  call assert_equal(2, tabpagenr('$'))
222  call assert_equal(['=== tabnext 2 ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
223  call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
224
225  delcommand C
226  autocmd! TabDestructive
227  augroup! TabDestructive
228  autocmd! TestTabpageGroup
229  augroup! TestTabpageGroup
230  1tabonly!
231endfunction
232
233" Test autocommands on tab drop
234function Test_tabpage_with_autocmd_tab_drop()
235  augroup TestTabpageGroup
236    au!
237    autocmd TabEnter * call add(s:li, 'TabEnter')
238    autocmd WinEnter * call add(s:li, 'WinEnter')
239    autocmd BufEnter * call add(s:li, 'BufEnter')
240    autocmd TabLeave * call add(s:li, 'TabLeave')
241    autocmd WinLeave * call add(s:li, 'WinLeave')
242    autocmd BufLeave * call add(s:li, 'BufLeave')
243  augroup END
244
245  let s:li = []
246  tab drop test1
247  call assert_equal(['BufLeave', 'BufEnter'], s:li)
248
249  let s:li = []
250  tab drop test2 test3
251  call assert_equal([
252        \ 'TabLeave', 'TabEnter', 'TabLeave', 'TabEnter',
253        \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter',
254        \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
255
256  autocmd! TestTabpageGroup
257  augroup! TestTabpageGroup
258  1tabonly!
259endfunction
260
261function Test_tabpage_with_tab_modifier()
262  CheckFeature quickfix
263
264  for n in range(4)
265    tabedit
266  endfor
267
268  function s:check_tab(pre_nr, cmd, post_nr)
269    exec 'tabnext ' . a:pre_nr
270    exec a:cmd
271    call assert_equal(a:post_nr, tabpagenr())
272    call assert_equal('help', &buftype)
273    helpclose
274  endfunc
275
276  call s:check_tab(1, 'tab help', 2)
277  call s:check_tab(1, '3tab help', 4)
278  call s:check_tab(1, '.tab help', 2)
279  call s:check_tab(1, '.+1tab help', 3)
280  call s:check_tab(1, '0tab help', 1)
281  call s:check_tab(2, '+tab help', 4)
282  call s:check_tab(2, '+2tab help', 5)
283  call s:check_tab(4, '-tab help', 4)
284  call s:check_tab(4, '-2tab help', 3)
285  call s:check_tab(3, '$tab help', 6)
286  call assert_fails('99tab help', 'E16:')
287  call assert_fails('+99tab help', 'E16:')
288  call assert_fails('-99tab help', 'E16:')
289
290  delfunction s:check_tab
291  1tabonly!
292endfunction
293
294function Check_tab_count(pre_nr, cmd, post_nr)
295  exec 'tabnext' a:pre_nr
296  normal! G
297  exec a:cmd
298  call assert_equal(a:post_nr, tabpagenr(), a:cmd)
299endfunc
300
301" Test for [count] of tabnext
302function Test_tabpage_with_tabnext()
303  for n in range(4)
304    tabedit
305    call setline(1, ['', '', '3'])
306  endfor
307
308  call Check_tab_count(1, 'tabnext', 2)
309  call Check_tab_count(1, '3tabnext', 3)
310  call Check_tab_count(1, '.tabnext', 1)
311  call Check_tab_count(1, '.+1tabnext', 2)
312  call Check_tab_count(2, '+tabnext', 3)
313  call Check_tab_count(2, '+2tabnext', 4)
314  call Check_tab_count(4, '-tabnext', 3)
315  call Check_tab_count(4, '-2tabnext', 2)
316  call Check_tab_count(3, '$tabnext', 5)
317  call assert_fails('0tabnext', 'E16:')
318  call assert_fails('99tabnext', 'E16:')
319  call assert_fails('+99tabnext', 'E16:')
320  call assert_fails('-99tabnext', 'E16:')
321  call Check_tab_count(1, 'tabnext 3', 3)
322  call Check_tab_count(2, 'tabnext +', 3)
323  call Check_tab_count(2, 'tabnext +2', 4)
324  call Check_tab_count(4, 'tabnext -', 3)
325  call Check_tab_count(4, 'tabnext -2', 2)
326  call Check_tab_count(3, 'tabnext $', 5)
327  call assert_fails('tabnext 0', 'E475:')
328  call assert_fails('tabnext .', 'E475:')
329  call assert_fails('tabnext -+', 'E475:')
330  call assert_fails('tabnext +2-', 'E475:')
331  call assert_fails('tabnext $3', 'E475:')
332  call assert_fails('tabnext 99', 'E475:')
333  call assert_fails('tabnext +99', 'E475:')
334  call assert_fails('tabnext -99', 'E475:')
335
336  1tabonly!
337endfunction
338
339" Test for [count] of tabprevious
340function Test_tabpage_with_tabprevious()
341  for n in range(5)
342    tabedit
343    call setline(1, ['', '', '3'])
344  endfor
345
346  for cmd in ['tabNext', 'tabprevious']
347    call Check_tab_count(6, cmd, 5)
348    call Check_tab_count(6, '3' . cmd, 3)
349    call Check_tab_count(6, '8' . cmd, 4)
350    call Check_tab_count(6, cmd . ' 3', 3)
351    call Check_tab_count(6, cmd . ' 8', 4)
352    for n in range(2)
353      for c in ['0', '.+3', '+', '+2', '-', '-2', '$', '+99', '-99']
354        if n == 0 " pre count
355          let entire_cmd = c . cmd
356          let err_code = 'E16:'
357        else
358          let entire_cmd = cmd . ' ' . c
359          let err_code = 'E475:'
360        endif
361        call assert_fails(entire_cmd, err_code)
362      endfor
363    endfor
364  endfor
365
366  1tabonly!
367endfunction
368
369function s:reconstruct_tabpage_for_test(nr)
370  let n = (a:nr > 2) ? a:nr - 2 : 1
371  1tabonly!
372  0tabedit n0
373  for n in range(1, n)
374    exec '$tabedit n' . n
375    if n == 1
376      call setline(1, ['', '', '3'])
377    endif
378  endfor
379endfunc
380
381func Test_tabpage_ctrl_pgup_pgdown()
382  enew!
383  tabnew tab1
384  tabnew tab2
385
386  call assert_equal(3, tabpagenr())
387  exe "norm! \<C-PageUp>"
388  call assert_equal(2, tabpagenr())
389  exe "norm! \<C-PageDown>"
390  call assert_equal(3, tabpagenr())
391
392  " Check wrapping at last or first page.
393  exe "norm! \<C-PageDown>"
394  call assert_equal(1, tabpagenr())
395  exe "norm! \<C-PageUp>"
396  call assert_equal(3, tabpagenr())
397
398 " With a count, <C-PageUp> and <C-PageDown> are not symmetrical somehow:
399 " - {count}<C-PageUp> goes {count} pages downward (relative count)
400 " - {count}<C-PageDown> goes to page number {count} (absolute count)
401  exe "norm! 2\<C-PageUp>"
402  call assert_equal(1, tabpagenr())
403  exe "norm! 2\<C-PageDown>"
404  call assert_equal(2, tabpagenr())
405
406  1tabonly!
407endfunc
408
409" Test for [count] of tabclose
410function Test_tabpage_with_tabclose()
411
412  " pre count
413  call s:reconstruct_tabpage_for_test(6)
414  call Check_tab_count(3, 'tabclose!', 3)
415  call Check_tab_count(1, '3tabclose', 1)
416  call Check_tab_count(4, '4tabclose', 3)
417  call Check_tab_count(3, '1tabclose', 2)
418  call Check_tab_count(2, 'tabclose', 1)
419  call assert_equal(1, tabpagenr('$'))
420  call assert_equal('', bufname(''))
421
422  call s:reconstruct_tabpage_for_test(6)
423  call Check_tab_count(2, '$tabclose', 2)
424  call Check_tab_count(4, '.tabclose', 4)
425  call Check_tab_count(3, '.+tabclose', 3)
426  call Check_tab_count(3, '.-2tabclose', 2)
427  call Check_tab_count(1, '.+1tabclose!', 1)
428  call assert_equal(1, tabpagenr('$'))
429  call assert_equal('', bufname(''))
430
431  " post count
432  call s:reconstruct_tabpage_for_test(6)
433  call Check_tab_count(3, 'tabclose!', 3)
434  call Check_tab_count(1, 'tabclose 3', 1)
435  call Check_tab_count(4, 'tabclose 4', 3)
436  call Check_tab_count(3, 'tabclose 1', 2)
437  call Check_tab_count(2, 'tabclose', 1)
438  call assert_equal(1, tabpagenr('$'))
439  call assert_equal('', bufname(''))
440
441  call s:reconstruct_tabpage_for_test(6)
442  call Check_tab_count(2, 'tabclose $', 2)
443  call Check_tab_count(4, 'tabclose', 4)
444  call Check_tab_count(3, 'tabclose +', 3)
445  call Check_tab_count(3, 'tabclose -2', 2)
446  call Check_tab_count(1, 'tabclose! +1', 1)
447  call assert_equal(1, tabpagenr('$'))
448  call assert_equal('', bufname(''))
449
450  call s:reconstruct_tabpage_for_test(6)
451  for n in range(2)
452    for c in ['0', '$3', '99', '+99', '-99']
453      if n == 0 " pre count
454        let entire_cmd = c . 'tabclose'
455        let err_code = 'E16:'
456      else
457        let entire_cmd = 'tabclose ' . c
458        let err_code = 'E475:'
459      endif
460      call assert_fails(entire_cmd, err_code)
461      call assert_equal(6, tabpagenr('$'))
462    endfor
463  endfor
464
465  call assert_fails('3tabclose', 'E37:')
466  call assert_fails('tabclose 3', 'E37:')
467  call assert_fails('tabclose -+', 'E475:')
468  call assert_fails('tabclose +2-', 'E475:')
469  call assert_equal(6, tabpagenr('$'))
470
471  1tabonly!
472endfunction
473
474" Test for [count] of tabonly
475function Test_tabpage_with_tabonly()
476
477  " Test for the normal behavior (pre count only)
478  let tc = [ [4, '.', '!'], [2, '.+', ''], [3, '.-2', '!'], [1, '.+1', '!'] ]
479  for c in tc
480    call s:reconstruct_tabpage_for_test(6)
481    let entire_cmd = c[1] . 'tabonly' . c[2]
482    call Check_tab_count(c[0], entire_cmd, 1)
483    call assert_equal(1, tabpagenr('$'))
484  endfor
485
486  " Test for the normal behavior
487  let tc2 = [ [3, '', ''], [1, '3', ''], [4, '4', '!'], [3, '1', '!'],
488        \    [2, '', '!'],
489        \    [2, '$', '!'], [3, '+', '!'], [3, '-2', '!'], [3, '+1', '!']
490        \  ]
491  for n in range(2)
492    for c in tc2
493      call s:reconstruct_tabpage_for_test(6)
494      if n == 0 " pre count
495        let entire_cmd = c[1] . 'tabonly' . c[2]
496      else
497        let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
498      endif
499      call Check_tab_count(c[0], entire_cmd, 1)
500      call assert_equal(1, tabpagenr('$'))
501    endfor
502  endfor
503
504  " Test for the error behavior
505  for n in range(2)
506    for c in ['0', '$3', '99', '+99', '-99']
507      call s:reconstruct_tabpage_for_test(6)
508      if n == 0 " pre count
509        let entire_cmd = c . 'tabonly'
510        let err_code = 'E16:'
511      else
512        let entire_cmd = 'tabonly ' . c
513        let err_code = 'E475:'
514      endif
515      call assert_fails(entire_cmd, err_code)
516      call assert_equal(6, tabpagenr('$'))
517    endfor
518  endfor
519
520  " Test for the error behavior (post count only)
521  for c in tc
522    call s:reconstruct_tabpage_for_test(6)
523    let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
524    let err_code = 'E475:'
525    call assert_fails(entire_cmd, err_code)
526    call assert_equal(6, tabpagenr('$'))
527  endfor
528
529  call assert_fails('tabonly -+', 'E475:')
530  call assert_fails('tabonly +2-', 'E475:')
531  call assert_equal(6, tabpagenr('$'))
532
533  1tabonly!
534  new
535  only!
536endfunction
537
538func Test_tabnext_on_buf_unload1()
539  " This once caused a crash
540  new
541  tabedit
542  tabfirst
543  au BufUnload <buffer> tabnext
544  q
545
546  while tabpagenr('$') > 1
547    bwipe!
548  endwhile
549endfunc
550
551func Test_tabnext_on_buf_unload2()
552  " This once caused a crash
553  tabedit
554  autocmd BufUnload <buffer> tabnext
555  file x
556  edit y
557
558  while tabpagenr('$') > 1
559    bwipe!
560  endwhile
561endfunc
562
563func Test_close_on_quitpre()
564  " This once caused a crash
565  edit Xtest
566  new
567  only
568  set bufhidden=delete
569  au QuitPre <buffer> close
570  tabnew tab1
571  tabnew tab2
572  1tabn
573  q!
574  call assert_equal(1, tabpagenr())
575  call assert_equal(2, tabpagenr('$'))
576  " clean up
577  while tabpagenr('$') > 1
578    bwipe!
579  endwhile
580  buf Xtest
581endfunc
582
583func Test_tabs()
584  enew!
585  tabnew tab1
586  norm ixxx
587  let a=split(execute(':tabs'), "\n")
588  call assert_equal(['Tab page 1',
589      \              '    [No Name]',
590      \              'Tab page 2',
591      \              '> + tab1'], a)
592
593  1tabonly!
594  bw!
595endfunc
596
597func Test_tabpage_cmdheight()
598  CheckRunVimInTerminal
599  call writefile([
600        \ 'set laststatus=2',
601        \ 'set cmdheight=2',
602        \ 'tabnew',
603        \ 'set cmdheight=3',
604        \ 'tabnext',
605        \ 'redraw!',
606        \ 'echo "hello\nthere"',
607        \ 'tabnext',
608        \ 'redraw',
609	\ ], 'XTest_tabpage_cmdheight')
610  " Check that cursor line is concealed
611  let buf = RunVimInTerminal('-S XTest_tabpage_cmdheight', {'statusoff': 3})
612  call VerifyScreenDump(buf, 'Test_tabpage_cmdheight', {})
613
614  call StopVimInTerminal(buf)
615  call delete('XTest_tabpage_cmdheight')
616endfunc
617
618" Test for closing the tab page from a command window
619func Test_tabpage_close_cmdwin()
620  tabnew
621  call feedkeys("q/:tabclose\<CR>\<Esc>", 'xt')
622  call assert_equal(2, tabpagenr('$'))
623  call feedkeys("q/:tabonly\<CR>\<Esc>", 'xt')
624  call assert_equal(2, tabpagenr('$'))
625  tabonly
626endfunc
627
628" Pressing <C-PageUp> in insert mode should go to the previous tab page
629" and <C-PageDown> should go to the next tab page
630func Test_tabpage_Ctrl_Pageup()
631  tabnew
632  call feedkeys("i\<C-PageUp>", 'xt')
633  call assert_equal(1, tabpagenr())
634  call feedkeys("i\<C-PageDown>", 'xt')
635  call assert_equal(2, tabpagenr())
636  %bw!
637endfunc
638
639" Return the terminal key code for selecting a tab page from the tabline. This
640" sequence contains the following codes: a CSI (0x9b), KS_TABLINE (0xf0),
641" KS_FILLER (0x58) and then the tab page number.
642func TabLineSelectPageCode(tabnr)
643  return "\x9b\xf0\x58" ..  nr2char(a:tabnr)
644endfunc
645
646" Return the terminal key code for opening a new tabpage from the tabpage
647" menu. This sequence consists of the following codes: a CSI (0x9b),
648" KS_TABMENU (0xef), KS_FILLER (0x58), the tab page number and
649" TABLINE_MENU_NEW (2).
650func TabMenuNewItemCode(tabnr)
651  return "\x9b\xef\x58" .. nr2char(a:tabnr) .. nr2char(2)
652endfunc
653
654" Return the terminal key code for closing a tabpage from the tabpage menu.
655" This sequence consists of the following codes: a CSI (0x9b), KS_TABMENU
656" (0xef), KS_FILLER (0x58), the tab page number and TABLINE_MENU_CLOSE (1).
657func TabMenuCloseItemCode(tabnr)
658  return "\x9b\xef\x58" .. nr2char(a:tabnr) .. nr2char(1)
659endfunc
660
661" Test for using the tabpage menu from the insert and normal modes
662func Test_tabline_tabmenu()
663  " only works in GUI
664  CheckGui
665
666  %bw!
667  tabnew
668  tabnew
669  call assert_equal(3, tabpagenr())
670
671  " go to tab page 2 in normal mode
672  call feedkeys(TabLineSelectPageCode(2), "Lx!")
673  call assert_equal(2, tabpagenr())
674
675  " close tab page 3 in normal mode
676  call feedkeys(TabMenuCloseItemCode(3), "Lx!")
677  call assert_equal(2, tabpagenr('$'))
678  call assert_equal(2, tabpagenr())
679
680  " open new tab page before tab page 1 in normal mode
681  call feedkeys(TabMenuNewItemCode(1), "Lx!")
682  call assert_equal(1, tabpagenr())
683  call assert_equal(3, tabpagenr('$'))
684
685  " go to tab page 2 in operator-pending mode (should beep)
686  call assert_beeps('call feedkeys("c" .. TabLineSelectPageCode(2), "Lx!")')
687  call assert_equal(2, tabpagenr())
688  call assert_equal(3, tabpagenr('$'))
689
690  " open new tab page before tab page 1 in operator-pending mode (should beep)
691  call assert_beeps('call feedkeys("c" .. TabMenuNewItemCode(1), "Lx!")')
692  call assert_equal(1, tabpagenr())
693  call assert_equal(4, tabpagenr('$'))
694
695  " open new tab page after tab page 3 in normal mode
696  call feedkeys(TabMenuNewItemCode(4), "Lx!")
697  call assert_equal(4, tabpagenr())
698  call assert_equal(5, tabpagenr('$'))
699
700  " go to tab page 2 in insert mode
701  call feedkeys("i" .. TabLineSelectPageCode(2) .. "\<C-C>", "Lx!")
702  call assert_equal(2, tabpagenr())
703
704  " close tab page 2 in insert mode
705  call feedkeys("i" .. TabMenuCloseItemCode(2) .. "\<C-C>", "Lx!")
706  call assert_equal(4, tabpagenr('$'))
707
708  " open new tab page before tab page 3 in insert mode
709  call feedkeys("i" .. TabMenuNewItemCode(3) .. "\<C-C>", "Lx!")
710  call assert_equal(3, tabpagenr())
711  call assert_equal(5, tabpagenr('$'))
712
713  " open new tab page after tab page 4 in insert mode
714  call feedkeys("i" .. TabMenuNewItemCode(5) .. "\<C-C>", "Lx!")
715  call assert_equal(5, tabpagenr())
716  call assert_equal(6, tabpagenr('$'))
717
718  %bw!
719endfunc
720
721" Test for changing the current tab page from an autocmd when closing a tab
722" page.
723func Test_tabpage_switchtab_on_close()
724  only
725  tabnew
726  tabnew
727  " Test for BufLeave
728  augroup T1
729    au!
730    au BufLeave * tabfirst
731  augroup END
732  tabclose
733  call assert_equal(1, tabpagenr())
734  augroup T1
735    au!
736  augroup END
737
738  " Test for WinLeave
739  $tabnew
740  augroup T1
741    au!
742    au WinLeave * tabfirst
743  augroup END
744  tabclose
745  call assert_equal(1, tabpagenr())
746  augroup T1
747    au!
748  augroup END
749
750  " Test for TabLeave
751  $tabnew
752  augroup T1
753    au!
754    au TabLeave * tabfirst
755  augroup END
756  tabclose
757  call assert_equal(1, tabpagenr())
758  augroup T1
759    au!
760  augroup END
761  augroup! T1
762  tabonly
763endfunc
764
765" Test for closing the destination tabpage when jumping from one to another.
766func Test_tabpage_close_on_switch()
767  tabnew
768  tabnew
769  edit Xfile
770  augroup T2
771    au!
772    au BufLeave Xfile 1tabclose
773  augroup END
774  tabfirst
775  call assert_equal(2, tabpagenr())
776  call assert_equal('Xfile', @%)
777  augroup T2
778    au!
779  augroup END
780  augroup! T2
781  %bw!
782endfunc
783
784" Test for jumping to last accessed tabpage
785func Test_lastused_tabpage()
786  tabonly!
787  call assert_equal(0, tabpagenr('#'))
788  call assert_beeps('call feedkeys("g\<Tab>", "xt")')
789  call assert_beeps('call feedkeys("\<C-Tab>", "xt")')
790  call assert_beeps('call feedkeys("\<C-W>g\<Tab>", "xt")')
791  call assert_fails('tabnext #', 'E475:')
792
793  " open four tab pages
794  tabnew
795  tabnew
796  tabnew
797
798  2tabnext
799
800  " Test for g<Tab>
801  call assert_equal(4, tabpagenr('#'))
802  call feedkeys("g\<Tab>", "xt")
803  call assert_equal(4, tabpagenr())
804  call assert_equal(2, tabpagenr('#'))
805
806  " Test for <C-Tab>
807  call feedkeys("\<C-Tab>", "xt")
808  call assert_equal(2, tabpagenr())
809  call assert_equal(4, tabpagenr('#'))
810
811  " Test for <C-W>g<Tab>
812  call feedkeys("\<C-W>g\<Tab>", "xt")
813  call assert_equal(4, tabpagenr())
814  call assert_equal(2, tabpagenr('#'))
815
816  " Test for :tabnext #
817  tabnext #
818  call assert_equal(2, tabpagenr())
819  call assert_equal(4, tabpagenr('#'))
820
821  " Try to jump to a closed tab page
822  tabclose #
823  call assert_equal(0, tabpagenr('#'))
824  call feedkeys("g\<Tab>", "xt")
825  call assert_equal(2, tabpagenr())
826  call feedkeys("\<C-Tab>", "xt")
827  call assert_equal(2, tabpagenr())
828  call feedkeys("\<C-W>g\<Tab>", "xt")
829  call assert_equal(2, tabpagenr())
830  call assert_fails('tabnext #', 'E475:')
831  call assert_equal(2, tabpagenr())
832
833  " Test for :tabonly #
834  let wnum = win_getid()
835  $tabnew
836  tabonly #
837  call assert_equal(wnum, win_getid())
838  call assert_equal(1, tabpagenr('$'))
839
840  " Test for :tabmove #
841  tabnew
842  let wnum = win_getid()
843  tabnew
844  tabnew
845  tabnext 2
846  tabmove #
847  call assert_equal(4, tabpagenr())
848  call assert_equal(wnum, win_getid())
849
850  tabonly!
851endfunc
852
853" vim: shiftwidth=2 sts=2 expandtab
854