tdf#118207: tdf#118189: sc: Move UItest to CppUnitTest

Change-Id: I1084aaf66d0c1943fa1457de7bb58c2b0b1b1189
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/109481
Tested-by: Jenkins
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
diff --git a/sc/qa/uitest/calc_tests2/tdf118189.py b/sc/qa/uitest/calc_tests2/tdf118189.py
deleted file mode 100644
index bd5bd41..0000000
--- a/sc/qa/uitest/calc_tests2/tdf118189.py
+++ /dev/null
@@ -1,47 +0,0 @@
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
from uitest.framework import UITestCase
from uitest.uihelper.common import get_state_as_dict, get_url_for_data_file
from uitest.uihelper.common import select_pos
from uitest.uihelper.calc import enter_text_to_cell
from libreoffice.calc.document import get_cell_by_position
from libreoffice.uno.propertyvalue import mkPropertyValues

#Bug 118189 - EDITING Crashes on Undo after Cut conditional formatting data

class tdf118189(UITestCase):
    def test_tdf118189(self):
        calc_doc = self.ui_test.load_file(get_url_for_data_file("tdf118189.xlsx"))
        xCalcDoc = self.xUITest.getTopFocusWindow()
        gridwin = xCalcDoc.getChild("grid_window")
        document = self.ui_test.get_component()
        #1. Open attached document
        #2. Copy Column A
        gridwin.executeAction("SELECT", mkPropertyValues({"CELL": "A1"}))
        self.xUITest.executeCommand(".uno:SelectColumn")
        self.xUITest.executeCommand(".uno:Copy")
        #3. New Spreadsheet
        calc_doc2 = self.ui_test.load_file(get_url_for_data_file("emptyFile.ods"))
        gridwin2 = xCalcDoc.getChild("grid_window")
        document2 = self.ui_test.get_component()

        frames = self.ui_test.get_frames()
        frames[1].activate()
        #4. Paste it
        gridwin2.executeAction("SELECT", mkPropertyValues({"CELL": "A1"}))
        self.xUITest.executeCommand(".uno:Paste")
        #5. Cut it
        self.xUITest.executeCommand(".uno:Cut")
        #6. Undo
        self.xUITest.executeCommand(".uno:Undo")

        #-> CRASH
        self.assertEqual(get_cell_by_position(document2, 0, 0, 0).getString(), "On Back Order")

        self.ui_test.close_doc()

# vim: set shiftwidth=4 softtabstop=4 expandtab:
\ No newline at end of file
diff --git a/sc/qa/uitest/calc_tests4/tdf118207.py b/sc/qa/uitest/calc_tests4/tdf118207.py
deleted file mode 100644
index 82e821c..0000000
--- a/sc/qa/uitest/calc_tests4/tdf118207.py
+++ /dev/null
@@ -1,55 +0,0 @@
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
from uitest.framework import UITestCase
from uitest.uihelper.common import get_state_as_dict, get_url_for_data_file
from uitest.uihelper.common import select_pos
from uitest.uihelper.calc import enter_text_to_cell
from libreoffice.calc.document import get_cell_by_position
from libreoffice.uno.propertyvalue import mkPropertyValues

#Bug 118207 - CRASH: Cutting and pasting a conditional format column and undoing

class tdf118207(UITestCase):
    def test_tdf118207(self):
        calc_doc = self.ui_test.load_file(get_url_for_data_file("tdf118189.xlsx"))
        xCalcDoc = self.xUITest.getTopFocusWindow()
        gridwin = xCalcDoc.getChild("grid_window")
        document = self.ui_test.get_component()
        #Make sure that tools-options-LibreOffice Calc-General-Input settings-Show overwrite warning when pasting data is NOT tagged.
        self.ui_test.execute_dialog_through_command(".uno:OptionsTreeDialog")  #optionsdialog
        xDialogOpt = self.xUITest.getTopFocusWindow()

        xPages = xDialogOpt.getChild("pages")
        xCalcEntry = xPages.getChild('3')                 # Calc
        xCalcEntry.executeAction("EXPAND", tuple())
        xCalcGeneralEntry = xCalcEntry.getChild('0')
        xCalcGeneralEntry.executeAction("SELECT", tuple())          #General / replwarncb
        xreplwarncb = xDialogOpt.getChild("replwarncb")
        if (get_state_as_dict(xreplwarncb)["Selected"]) == "true":
            xreplwarncb.executeAction("CLICK", tuple())
        xOKBtn = xDialogOpt.getChild("ok")
        self.ui_test.close_dialog_through_button(xOKBtn)
        #Select Column A
        #Cut it
        gridwin.executeAction("SELECT", mkPropertyValues({"CELL": "A1"}))
        self.xUITest.executeCommand(".uno:SelectColumn")
        self.xUITest.executeCommand(".uno:Cut")
        #Paste it in Column B , Paste it in Column B again
        gridwin.executeAction("SELECT", mkPropertyValues({"CELL": "B1"}))
        self.xUITest.executeCommand(".uno:Paste")
        self.xUITest.executeCommand(".uno:Paste")
        #Undo 3 times
        self.xUITest.executeCommand(".uno:Undo")
        self.xUITest.executeCommand(".uno:Undo")
        self.xUITest.executeCommand(".uno:Undo")
        #-> CRASH
        self.assertEqual(get_cell_by_position(document, 0, 0, 0).getString() , "On Back Order")
        #verify

        self.ui_test.close_doc()

# vim: set shiftwidth=4 softtabstop=4 expandtab:
\ No newline at end of file
diff --git a/sc/qa/uitest/data/emptyFile.ods b/sc/qa/uitest/data/emptyFile.ods
deleted file mode 100644
index cd2454d..0000000
--- a/sc/qa/uitest/data/emptyFile.ods
+++ /dev/null
Binary files differ
diff --git a/sc/qa/uitest/data/tdf118189.xlsx b/sc/qa/unit/uicalc/data/tdf118189.xlsx
similarity index 100%
rename from sc/qa/uitest/data/tdf118189.xlsx
rename to sc/qa/unit/uicalc/data/tdf118189.xlsx
Binary files differ
diff --git a/sc/qa/unit/uicalc/uicalc.cxx b/sc/qa/unit/uicalc/uicalc.cxx
index efdcd7c..3b93128 100644
--- a/sc/qa/unit/uicalc/uicalc.cxx
+++ b/sc/qa/unit/uicalc/uicalc.cxx
@@ -432,9 +432,10 @@ CPPUNIT_TEST_FIXTURE(ScUiCalcTest, testTdf124822)
    CPPUNIT_ASSERT_EQUAL(OUString("X"), pDoc->GetString(ScAddress(0, 0, 2)));

    dispatchCommand(mxComponent, ".uno:SelectAll", {});
    dispatchCommand(mxComponent, ".uno:Cut", {});
    Scheduler::ProcessEventsToIdle();

    ScDocShell::GetViewData()->GetView()->CutToClip();

    CPPUNIT_ASSERT_EQUAL(OUString(""), pDoc->GetString(ScAddress(0, 0, 2)));

    dispatchCommand(mxComponent, ".uno:Undo", {});
@@ -443,6 +444,115 @@ CPPUNIT_TEST_FIXTURE(ScUiCalcTest, testTdf124822)
    CPPUNIT_ASSERT_EQUAL(OUString("X"), pDoc->GetString(ScAddress(0, 0, 2)));
}

CPPUNIT_TEST_FIXTURE(ScUiCalcTest, testTdf118189)
{
    ScModelObj* pModelObj = createDoc("tdf118189.xlsx");

    ScDocument* pDoc = pModelObj->GetDocument();
    CPPUNIT_ASSERT(pDoc);

    // Select column A
    lcl_SelectRangeFromString(*pDoc, "A:A");

    ScDocument aClipDoc(SCDOCMODE_CLIP);
    ScDocShell::GetViewData()->GetView()->CopyToClip(&aClipDoc, false, false, false, false);

    mxComponent->dispose();

    // Open a new document
    mxComponent = loadFromDesktop("private:factory/scalc");
    pModelObj = dynamic_cast<ScModelObj*>(mxComponent.get());
    CPPUNIT_ASSERT(pModelObj);
    pDoc = pModelObj->GetDocument();
    CPPUNIT_ASSERT(pDoc);

    ScDocShell::GetViewData()->GetView()->PasteFromClip(InsertDeleteFlags::ALL, &aClipDoc);
    Scheduler::ProcessEventsToIdle();

    OUString aFormula;
    pDoc->GetFormula(0, 77, 0, aFormula);
    CPPUNIT_ASSERT_EQUAL(OUString("=FALSE()"), aFormula);

    ScDocShell::GetViewData()->GetView()->CutToClip();

    pDoc->GetFormula(0, 77, 0, aFormula);
    CPPUNIT_ASSERT_EQUAL(OUString(""), aFormula);

    // Without the fix in place, this test would have crashed here
    dispatchCommand(mxComponent, ".uno:Undo", {});
    Scheduler::ProcessEventsToIdle();

    pDoc->GetFormula(0, 77, 0, aFormula);
    CPPUNIT_ASSERT_EQUAL(OUString("=FALSE()"), aFormula);
}

CPPUNIT_TEST_FIXTURE(ScUiCalcTest, testTdf118207)
{
    ScModelObj* pModelObj = createDoc("tdf118189.xlsx");

    ScDocument* pDoc = pModelObj->GetDocument();
    CPPUNIT_ASSERT(pDoc);

    // Disable replace cell warning
    ScModule* pMod = SC_MOD();
    ScInputOptions aInputOption = pMod->GetInputOptions();
    bool bOldStatus = aInputOption.GetReplaceCellsWarn();
    aInputOption.SetReplaceCellsWarn(false);
    pMod->SetInputOptions(aInputOption);

    // Select column A
    lcl_SelectRangeFromString(*pDoc, "A:A");

    OUString aFormula;
    pDoc->GetFormula(0, 77, 0, aFormula);
    CPPUNIT_ASSERT_EQUAL(OUString("=FALSE()"), aFormula);

    ScDocument aClipDoc(SCDOCMODE_CLIP);
    ScDocShell::GetViewData()->GetView()->CutToClip(&aClipDoc);

    pDoc->GetFormula(0, 77, 0, aFormula);
    CPPUNIT_ASSERT_EQUAL(OUString(""), aFormula);

    // Select column B
    lcl_SelectRangeFromString(*pDoc, "B:B");

    ScDocShell::GetViewData()->GetView()->PasteFromClip(InsertDeleteFlags::ALL, &aClipDoc);
    Scheduler::ProcessEventsToIdle();

    pDoc->GetFormula(1, 77, 0, aFormula);
    CPPUNIT_ASSERT_EQUAL(OUString("=FALSE()"), aFormula);

    ScDocShell::GetViewData()->GetView()->PasteFromClip(InsertDeleteFlags::ALL, &aClipDoc);
    Scheduler::ProcessEventsToIdle();

    pDoc->GetFormula(1, 77, 0, aFormula);
    CPPUNIT_ASSERT_EQUAL(OUString("=FALSE()"), aFormula);

    dispatchCommand(mxComponent, ".uno:Undo", {});
    Scheduler::ProcessEventsToIdle();

    pDoc->GetFormula(1, 77, 0, aFormula);
    CPPUNIT_ASSERT_EQUAL(OUString("=FALSE()"), aFormula);

    dispatchCommand(mxComponent, ".uno:Undo", {});
    Scheduler::ProcessEventsToIdle();

    pDoc->GetFormula(1, 77, 0, aFormula);
    CPPUNIT_ASSERT_EQUAL(OUString(""), aFormula);
    pDoc->GetFormula(0, 77, 0, aFormula);
    CPPUNIT_ASSERT_EQUAL(OUString(""), aFormula);

    dispatchCommand(mxComponent, ".uno:Undo", {});
    Scheduler::ProcessEventsToIdle();

    pDoc->GetFormula(0, 77, 0, aFormula);
    CPPUNIT_ASSERT_EQUAL(OUString("=FALSE()"), aFormula);

    // Restore previous status
    aInputOption.SetReplaceCellsWarn(bOldStatus);
    pMod->SetInputOptions(aInputOption);
}

CPPUNIT_TEST_FIXTURE(ScUiCalcTest, testTdf138428)
{
    mxComponent = loadFromDesktop("private:factory/scalc");
@@ -509,7 +619,6 @@ CPPUNIT_TEST_FIXTURE(ScUiCalcTest, testTdf133342)
    ScDocument* pDoc = pModelObj->GetDocument();
    CPPUNIT_ASSERT(pDoc);

    //Select cell A1
    CPPUNIT_ASSERT_EQUAL(OUString("12,35 %"), pDoc->GetString(ScAddress(0, 0, 0)));
    //Add decimals
    dispatchCommand(mxComponent, ".uno:NumberFormatIncDecimals", {});
diff --git a/sc/source/ui/inc/viewfunc.hxx b/sc/source/ui/inc/viewfunc.hxx
index 497c097..457f599 100644
--- a/sc/source/ui/inc/viewfunc.hxx
+++ b/sc/source/ui/inc/viewfunc.hxx
@@ -107,7 +107,7 @@ public:

    void            EnterDataAtCursor( const OUString& rString );         //! Not used?

    SC_DLLPUBLIC void           CutToClip();
    SC_DLLPUBLIC void           CutToClip(ScDocument* pClipDoc = nullptr);
    SC_DLLPUBLIC bool           CopyToClip( ScDocument* pClipDoc, bool bCut, bool bApi = false,
                                            bool bIncludeObjects = false, bool bStopEdit = true );
    SC_DLLPUBLIC bool           CopyToClip( ScDocument* pClipDoc, const ScRangeList& rRange, bool bCut,
diff --git a/sc/source/ui/view/viewfun3.cxx b/sc/source/ui/view/viewfun3.cxx
index 11f03b0..e8389d4 100644
--- a/sc/source/ui/view/viewfun3.cxx
+++ b/sc/source/ui/view/viewfun3.cxx
@@ -85,7 +85,7 @@ void collectUIInformation(const std::map<OUString, OUString>& aParameters, const

//      C U T

void ScViewFunc::CutToClip()
void ScViewFunc::CutToClip(ScDocument* pClipDoc)
{
    UpdateInputLine();

@@ -114,7 +114,7 @@ void ScViewFunc::CutToClip()
            MarkDataChanged();
        }

        CopyToClip( nullptr, true, false, true/*bIncludeObjects*/ );           // copy to clipboard
        CopyToClip( pClipDoc, true, false, true/*bIncludeObjects*/ );           // copy to clipboard

        ScAddress aOldEnd( aRange.aEnd );       //  combined cells in this range?
        rDoc.ExtendMerge( aRange, true );