patch 9.1.1211: TabClosedPre is triggered just before the tab is being freed

Problem:  TabClosedPre is triggered just before the tab is being freed,
          which limited its functionality.
Solution: Trigger it a bit earlier and also on :tabclose and :tabonly
          (Jim Zhou)

closes: #16890

Signed-off-by: Jim Zhou <jimzhouzzy@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/testdir/test_autocmd.vim b/src/testdir/test_autocmd.vim
index 138b852..2519e60 100644
--- a/src/testdir/test_autocmd.vim
+++ b/src/testdir/test_autocmd.vim
@@ -5109,7 +5109,7 @@
   call ClearAutomcdAndCreateTabs()
   au TabClosedPre * tabmove 0
   tabclose
-  call assert_equal('1Z2A3>B', GetTabs())
+  call assert_equal('1>Z2A3B', GetTabs())
   call ClearAutomcdAndCreateTabs()
   au TabClosedPre * tabmove 0
   tabclose 1
@@ -5137,7 +5137,33 @@
   au TabClosedPre * new X | new Y | new Z
   call assert_fails('tabclose 1', 'E242')
 
+  " Test directly closing the tab page with ':tabclose'
+  au!
+  tabonly
+  bw!
+  e Z
+  au TabClosedPre * mksession!
+  tabnew A
+  sp
+  tabclose
+  source Session.vim
+  call assert_equal('1Z2>AA', GetTabs())
+
+  " Test directly closing the tab page with ':tabonly'
+  " Z is closed before A. Hence A overwrites the session.
+  au!
+  tabonly
+  bw!
+  e Z
+  au TabClosedPre * mksession!
+  tabnew A
+  tabnew B
+  tabonly
+  source Session.vim
+  call assert_equal('1>A2B', GetTabs())
+
   " Clean up
+  call delete('Session.vim')
   au!
   only
   tabonly