sc/qa/unit/tiledrendering/SheetViewTest.cxx |   72 ++++++++++++++++++++++++++++
 sc/source/core/data/SheetViewManager.cxx    |    1 
 sc/source/ui/view/viewfun3.cxx              |   18 +++++--
 3 files changed, 86 insertions(+), 5 deletions(-)

New commits:
commit 0a894bac0df4e2b2654bf6d25d0dd7e3128949ef
Author:     Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
AuthorDate: Mon Aug 11 17:27:18 2025 +0200
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Tue Sep 16 09:30:18 2025 +0200

    sc: test ".uno:RemoveSheetView" to remove current sheet view
    
    Also fix issues found with the test.
    
    We need to broadcast that the table was inserted or it won't
    properly be accounted for ViewData, which will cause an assert
    to fail.
    
    Change-Id: I0c086e16090d40e8a663217b36d04d4de99966cb
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/189386
    Tested-by: Jenkins
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>
    (cherry picked from commit e6cf2c90f14097a1c2ebbd013ccb06276054ad17)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/190954
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>

diff --git a/sc/qa/unit/tiledrendering/SheetViewTest.cxx 
b/sc/qa/unit/tiledrendering/SheetViewTest.cxx
index 621fa86e29cb..6363034b6bfb 100644
--- a/sc/qa/unit/tiledrendering/SheetViewTest.cxx
+++ b/sc/qa/unit/tiledrendering/SheetViewTest.cxx
@@ -186,6 +186,78 @@ CPPUNIT_TEST_FIXTURE(SheetViewTest, 
testSyncValuesBetweenMainSheetAndSheetView)
     CPPUNIT_ASSERT_EQUAL(u"ABC123"_ustr, pDocument->GetString(aA2SheetView));
 }
 
+CPPUNIT_TEST_FIXTURE(SheetViewTest, testRemoveSheetView)
+{
+    // Create two views, and leave the second one current.
+    ScModelObj* pModelObj = createDoc("SheetView_AutoFilter.ods");
+    
pModelObj->initializeForTiledRendering(uno::Sequence<beans::PropertyValue>());
+    ScDocument* pDocument = pModelObj->GetDocument();
+
+    // Setup views
+    ScTestViewCallback aView1;
+    ScTabViewShell* pTabView1 = aView1.getTabViewShell();
+    SfxLokHelper::createView();
+    Scheduler::ProcessEventsToIdle();
+    ScTestViewCallback aView2;
+    ScTabViewShell* pTabView2 = aView2.getTabViewShell();
+    CPPUNIT_ASSERT(pTabView1 != pTabView2);
+    CPPUNIT_ASSERT(aView1.getViewID() != aView2.getViewID());
+
+    // Switch to View1
+    SfxLokHelper::setView(aView1.getViewID());
+    Scheduler::ProcessEventsToIdle();
+
+    // Create a new sheet view for view 1
+    dispatchCommand(mxComponent, u".uno:NewSheetView"_ustr, {});
+    Scheduler::ProcessEventsToIdle();
+
+    // Check AutoFilter values for each view
+    CPPUNIT_ASSERT_EQUAL(u"4"_ustr, pTabView1->GetCurrentString(0, 1));
+    CPPUNIT_ASSERT_EQUAL(u"5"_ustr, pTabView1->GetCurrentString(0, 2));
+    CPPUNIT_ASSERT_EQUAL(u"3"_ustr, pTabView1->GetCurrentString(0, 3));
+    CPPUNIT_ASSERT_EQUAL(u"7"_ustr, pTabView1->GetCurrentString(0, 4));
+
+    CPPUNIT_ASSERT_EQUAL(u"4"_ustr, pTabView2->GetCurrentString(0, 1));
+    CPPUNIT_ASSERT_EQUAL(u"5"_ustr, pTabView2->GetCurrentString(0, 2));
+    CPPUNIT_ASSERT_EQUAL(u"3"_ustr, pTabView2->GetCurrentString(0, 3));
+    CPPUNIT_ASSERT_EQUAL(u"7"_ustr, pTabView2->GetCurrentString(0, 4));
+
+    // Switch to View2
+    SfxLokHelper::setView(aView2.getViewID());
+    Scheduler::ProcessEventsToIdle();
+
+    // Sort AutoFilter descending
+    dispatchCommand(mxComponent, u".uno:SortDescending"_ustr, {});
+    Scheduler::ProcessEventsToIdle();
+
+    // Check values are sorted for view 2
+    CPPUNIT_ASSERT_EQUAL(u"7"_ustr, pTabView2->GetCurrentString(0, 1));
+    CPPUNIT_ASSERT_EQUAL(u"5"_ustr, pTabView2->GetCurrentString(0, 2));
+    CPPUNIT_ASSERT_EQUAL(u"4"_ustr, pTabView2->GetCurrentString(0, 3));
+    CPPUNIT_ASSERT_EQUAL(u"3"_ustr, pTabView2->GetCurrentString(0, 4));
+
+    // Sheet view must be present
+    auto pSheetViewManager = pDocument->GetSheetViewManager(0);
+    CPPUNIT_ASSERT_EQUAL(size_t(1), pSheetViewManager->getSheetViews().size());
+
+    // There should be 2 tables
+    CPPUNIT_ASSERT_EQUAL(SCTAB(2), pDocument->GetTableCount());
+
+    // Switch to View1
+    SfxLokHelper::setView(aView1.getViewID());
+    Scheduler::ProcessEventsToIdle();
+
+    // We remove the current sheet view
+    dispatchCommand(mxComponent, u".uno:RemoveSheetView"_ustr, {});
+    Scheduler::ProcessEventsToIdle();
+
+    // No more sheet view
+    CPPUNIT_ASSERT_EQUAL(size_t(0), pSheetViewManager->getSheetViews().size());
+
+    // Only 1 table left
+    CPPUNIT_ASSERT_EQUAL(SCTAB(1), pDocument->GetTableCount());
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/core/data/SheetViewManager.cxx 
b/sc/source/core/data/SheetViewManager.cxx
index e4e3858fbcdc..a57ea9666025 100644
--- a/sc/source/core/data/SheetViewManager.cxx
+++ b/sc/source/core/data/SheetViewManager.cxx
@@ -26,6 +26,7 @@ bool SheetViewManager::remove(SheetViewID nID)
     if (nID >= 0 && o3tl::make_unsigned(nID) < maViews.size())
     {
         maViews.erase(maViews.begin() + nID);
+        return true;
     }
     return false;
 }
diff --git a/sc/source/ui/view/viewfun3.cxx b/sc/source/ui/view/viewfun3.cxx
index a7d233825ff9..85622824f067 100644
--- a/sc/source/ui/view/viewfun3.cxx
+++ b/sc/source/ui/view/viewfun3.cxx
@@ -64,6 +64,7 @@
 #include <clipoptions.hxx>
 #include <gridwin.hxx>
 #include <SheetView.hxx>
+#include <uiitems.hxx>
 #include <com/sun/star/util/XCloneable.hpp>
 
 using namespace com::sun::star;
@@ -2070,6 +2071,9 @@ void ScViewFunc::MakeNewSheetView()
     SCTAB nSheetViewTab = nTab + 1;
     if (rDocument.CopyTab(nTab, nSheetViewTab))
     {
+        GetViewData().GetDocShell()->Broadcast(ScTablesHint(SC_TAB_INSERTED, 
nSheetViewTab));
+        SfxGetpApp()->Broadcast(SfxHint(SfxHintId::ScTablesChanged));
+
         // Add and register the created sheet view
         rDocument.SetSheetView(nSheetViewTab, true);
         sc::SheetViewID nSheetViewID = rDocument.CreateNewSheetView(nTab, 
nSheetViewTab);
@@ -2078,6 +2082,9 @@ void ScViewFunc::MakeNewSheetView()
         // Update
         GetViewData().SetTabNo(nSheetViewTab); // force add the sheet view tab
         GetViewData().SetTabNo(nTab); // then change back to the current tab
+
+        ScDocShell& rDocSh = *GetViewData().GetDocShell();
+        rDocSh.PostPaintGridAll();
         PaintExtras(); // update Tab Control
     }
 }
@@ -2088,18 +2095,19 @@ void ScViewFunc::RemoveCurrentSheetView()
     if (nSheetViewID == sc::DefaultSheetViewID)
         return;
 
-    SCTAB nTab = GetViewData().GetTabNumber();
     ScDocument& rDocument = GetViewData().GetDocument();
-    SCTAB nSheetViewTab = rDocument.GetSheetViewNumber(nTab, nSheetViewID);
-
+    SCTAB nTab = GetViewData().GetTabNumber();
     auto pSheetManager = rDocument.GetSheetViewManager(nTab);
+    if (!pSheetManager)
+        return;
+
+    SCTAB nSheetViewTab = rDocument.GetSheetViewNumber(nTab, nSheetViewID);
     pSheetManager->remove(nSheetViewID);
     GetViewData().SetSheetViewID(sc::DefaultSheetViewID);
     GetViewData().SetTabNo(nTab);
     GetViewData().GetDocFunc().DeleteTable(nSheetViewTab, true);
 
-    ScDocShell& rDocSh = *GetViewData().GetDocShell();
-    rDocSh.PostPaintGridAll();
+    GetViewData().GetDocShell()->PostPaintGridAll();
     PaintExtras();
 }
 

Reply via email to