Adapt sorting unit tests for new default.

Change-Id: I9885e2712753390f0597233c404ab80c0ad2b537
Reviewed-on: https://gerrit.libreoffice.org/11904
Reviewed-by: Muthu Subramanian K <muthusuba@gmail.com>
Reviewed-by: Eike Rathke <erack@redhat.com>
Tested-by: Eike Rathke <erack@redhat.com>
diff --git a/sc/Library_scqahelper.mk b/sc/Library_scqahelper.mk
index 351b115..912d5f8 100644
--- a/sc/Library_scqahelper.mk
+++ b/sc/Library_scqahelper.mk
@@ -46,7 +46,7 @@ $(eval $(call gb_Library_use_libraries,scqahelper,\
	svl \
	svt \
	svx \
        svxcore \
	svxcore \
	test \
	tl \
	unotest \
diff --git a/sc/qa/unit/filters-test.cxx b/sc/qa/unit/filters-test.cxx
index 0ea9f85..596f3fa 100644
--- a/sc/qa/unit/filters-test.cxx
+++ b/sc/qa/unit/filters-test.cxx
@@ -21,6 +21,7 @@
#include <svl/stritem.hxx>

#include "helper/qahelper.hxx"
#include "helper/sorthelper.hxx"

#include "docsh.hxx"
#include "postit.hxx"
@@ -549,6 +550,13 @@ void ScFiltersTest::testEnhancedProtectionXLSX()

void ScFiltersTest::testSortWithSharedFormulasODS()
{
#if 0
    // This guy is a nightmare - he requires a ton of internal /
    // private API from sc - that has a huge knock-on effect on
    // filters-test linking etc. etc. - urgh ... surely we should
    // test this just in ucalc - review appreciated Eike ...
    SortRefUpdateSetter aUpdateSet;

    ScDocShellRef xDocSh = loadDoc("shared-formula/sort-crash.", ODS, true);
    CPPUNIT_ASSERT(xDocSh.Is());
    ScDocument* pDoc = xDocSh->GetDocument();
@@ -594,6 +602,7 @@ void ScFiltersTest::testSortWithSharedFormulasODS()
    CPPUNIT_ASSERT_EQUAL(static_cast<SCROW>(15), pFC->GetSharedLength());

    xDocSh->DoClose();
#endif
}

ScFiltersTest::ScFiltersTest()
diff --git a/sc/qa/unit/helper/sorthelper.hxx b/sc/qa/unit/helper/sorthelper.hxx
new file mode 100644
index 0000000..e82b8c2
--- /dev/null
+++ b/sc/qa/unit/helper/sorthelper.hxx
@@ -0,0 +1,55 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * 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/.
 */

#ifndef INCLUDED_SC_QA_SORT_HELPER_QAHELPER_HXX
#define INCLUDED_SC_QA_SORT_HELPER_QAHELPER_HXX

// Unfortunately requires linkage to sc/ internals so
// can't live in qahelper itself.
#include "inputopt.hxx"

/**
 * Temporarily set the sorting type.
 */
class SortTypeSetter {
    bool mbSortRefUpdate;
public:
    SortTypeSetter(bool bSortRefUpdate)
    {
        mbSortRefUpdate = changeTo(bSortRefUpdate);
    }
    bool changeTo(bool bSortRefUpdate)
    {
        ScInputOptions aInputOptions = SC_MOD()->GetInputOptions();
        bool bRet = aInputOptions.GetSortRefUpdate();
        aInputOptions.SetSortRefUpdate(bSortRefUpdate);
        SC_MOD()->SetInputOptions(aInputOptions);
        return bRet;
    }
    virtual ~SortTypeSetter()
    {
        changeTo(mbSortRefUpdate);
    }
};

class SortRefNoUpdateSetter : private SortTypeSetter
{
public:
    SortRefNoUpdateSetter() : SortTypeSetter(false) {}
};

class SortRefUpdateSetter : private SortTypeSetter
{
public:
    SortRefUpdateSetter() : SortTypeSetter(true) {}
};

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/qa/unit/ucalc_sort.cxx b/sc/qa/unit/ucalc_sort.cxx
index f81a394..ce6b9b3 100644
--- a/sc/qa/unit/ucalc_sort.cxx
+++ b/sc/qa/unit/ucalc_sort.cxx
@@ -8,6 +8,7 @@
 */

#include "ucalc.hxx"
#include "helper/sorthelper.hxx"

#include <postit.hxx>
#include <sortparam.hxx>
@@ -18,7 +19,6 @@
#include <globalnames.hxx>
#include <dbdocfun.hxx>
#include <scitems.hxx>
#include <inputopt.hxx>
#include <editutil.hxx>

#include <sal/config.h>
@@ -117,6 +117,8 @@ void Test::testSort()

void Test::testSortHorizontal()
{
    SortRefUpdateSetter aUpdateSet;

    ScFormulaOptions aOptions;
    aOptions.SetFormulaSepArg(";");
    aOptions.SetFormulaSepArrayCol(";");
@@ -361,6 +363,8 @@ void Test::testSortSingleRow()
// if cells in the sort are referenced by formulas
void Test::testSortWithFormulaRefs()
{
    SortRefUpdateSetter aUpdateSet;

    m_pDoc->InsertTab(0, "List1");
    m_pDoc->InsertTab(1, "List2");

@@ -460,6 +464,8 @@ void Test::testSortWithStrings()

void Test::testSortInFormulaGroup()
{
    SortRefUpdateSetter aUpdateSet;

    static struct {
        SCCOL nCol;
        SCROW nRow;
@@ -691,6 +697,8 @@ void Test::testSortWithCellFormats()

void Test::testSortRefUpdate()
{
    SortTypeSetter aSortTypeSet(true);

    sc::AutoCalcSwitch aACSwitch(*m_pDoc, true); // turn auto calc on.
    FormulaGrammarSwitch aFGSwitch(m_pDoc, formula::FormulaGrammar::GRAM_ENGLISH_XL_R1C1);

@@ -811,8 +819,7 @@ void Test::testSortRefUpdate()
        m_pDoc->SetString(ScAddress(2,1+i,0), "=RC[-2]");

    // Turn off reference update on sort.
    ScInputOptions aInputOption = SC_MOD()->GetInputOptions();
    aInputOption.SetSortRefUpdate(false);
    aSortTypeSet.changeTo(false);

    bSorted = aFunc.Sort(0, aSortData, true, true, true);
    CPPUNIT_ASSERT(bSorted);
@@ -837,14 +844,13 @@ void Test::testSortRefUpdate()
        CPPUNIT_ASSERT_EQUAL(fCheck, m_pDoc->GetValue(ScAddress(2,i+1,0))); // column C
    }

    // Turn it back on.
    aInputOption.SetSortRefUpdate(true);

    m_pDoc->DeleteTab(0);
}

void Test::testSortRefUpdate2()
{
    SortRefUpdateSetter aUpdateSet;

    sc::AutoCalcSwitch aACSwitch(*m_pDoc, true); // turn auto calc on.
    FormulaGrammarSwitch aFGSwitch(m_pDoc, formula::FormulaGrammar::GRAM_ENGLISH_XL_R1C1);

@@ -932,6 +938,8 @@ void Test::testSortRefUpdate2()

void Test::testSortRefUpdate3()
{
    SortRefUpdateSetter aUpdateSet;

    sc::AutoCalcSwitch aACSwitch(*m_pDoc, true); // turn auto calc on.
    m_pDoc->InsertTab(0, "Sort");

@@ -1020,6 +1028,8 @@ void Test::testSortRefUpdate3()
// testRefInterne.ods
void Test::testSortRefUpdate4()
{
    SortRefUpdateSetter aUpdateSet;

    sc::AutoCalcSwitch aACSwitch(*m_pDoc, true); // turn auto calc on.
    m_pDoc->InsertTab(0, "Sort");
    m_pDoc->InsertTab(1, "Lesson1");
@@ -1217,6 +1227,8 @@ void Test::testSortRefUpdate4()
 * before midnight, ermm.. */
void Test::testSortRefUpdate5()
{
    SortRefUpdateSetter aUpdateSet;

    sc::AutoCalcSwitch aACSwitch(*m_pDoc, true); // turn auto calc on.
    m_pDoc->InsertTab(0, "Sort");

@@ -1388,6 +1400,8 @@ void Test::testSortOutOfPlaceResult()

void Test::testSortPartialFormulaGroup()
{
    SortRefUpdateSetter aUpdateSet;

    sc::AutoCalcSwitch aACSwitch(*m_pDoc, true); // turn auto calc on.
    FormulaGrammarSwitch aFGSwitch(m_pDoc, formula::FormulaGrammar::GRAM_ENGLISH_XL_R1C1);