tdf#130770 apply autofilter to data regardless of autofilter changes

Add a unit test

Apply autofilter to data if there are no changes from the user for
AutoFilterMode::Normal mode

The condition
    eMode == AutoFilterMode::Normal
at that position is equivalent to
    eMode != AutoFilterMode::Top10
        && eMode != AutoFilterMode::Empty
        && eMode != AutoFilterMode::NonEmpty
because
- Top10: Excluded in if()
- Custom:  Already handled before
- Empty: Excluded in if()
- NonEmpty: Excluded in if()
- SortAscending:  Already handled before
- SortDescending:  Already handled before
- Normal: The remaining condition

Delete dead code:
The condition
    if (mpAutoFilterPopup->isAllSelected())
will never be true since
    eMode == AutoFilterMode::Normal && mpAutoFilterPopup->isAllSelected()
is excluded in the "outer if condition"
    !(eMode == AutoFilterMode::Normal && mpAutoFilterPopup->isAllSelected())
Moreover aParam.RemoveAllEntriesByField(rPos.Col()); has already been called
before.

Change-Id: I1a5362f6edf6d28b3e049977d761cef83897e63e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/91645
Tested-by: Jenkins
Tested-by: Serge Krot (CIB) <Serge.Krot@cib.de>
Reviewed-by: Serge Krot (CIB) <Serge.Krot@cib.de>
(cherry picked from commit e9c04fdc6d91a152c9197bda3b07316bed1dc4f3)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/94457
Reviewed-by: Eike Rathke <erack@redhat.com>
diff --git a/sc/qa/uitest/autofilter/data/tdf130770.ods b/sc/qa/uitest/autofilter/data/tdf130770.ods
new file mode 100644
index 0000000..952cf38
--- /dev/null
+++ b/sc/qa/uitest/autofilter/data/tdf130770.ods
Binary files differ
diff --git a/sc/qa/uitest/autofilter/tdf130070.py b/sc/qa/uitest/autofilter/tdf130070.py
new file mode 100644
index 0000000..77c5548
--- /dev/null
+++ b/sc/qa/uitest/autofilter/tdf130070.py
@@ -0,0 +1,73 @@
# -*- 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
from uitest.uihelper.common import select_pos
from uitest.uihelper.calc import enter_text_to_cell
from libreoffice.calc.document import get_sheet_from_doc
from libreoffice.calc.conditional_format import get_conditional_format_from_sheet
from uitest.debug import sleep
from libreoffice.calc.document import get_cell_by_position
from libreoffice.uno.propertyvalue import mkPropertyValues
from libreoffice.calc.document import get_row
# import org.libreoffice.unotest
# import pathlib
from uitest.path import get_srcdir_url

def get_url_for_data_file(file_name):
#    return pathlib.Path(org.libreoffice.unotest.makeCopyFromTDOC(file_name)).as_uri()
    return get_srcdir_url() + "/sc/qa/uitest/autofilter/data/" + file_name

def is_row_hidden(doc, index):
    row = get_row(doc, index)
    val = row.getPropertyValue("IsVisible")
    return not val

#Bug 130770 - Autofilter not updated on data change (automatically or manually)

class tdf130770(UITestCase):
    def test_tdf130770_autofilter(self):
        calc_doc = self.ui_test.load_file(get_url_for_data_file("tdf130770.ods"))
        xCalcDoc = self.xUITest.getTopFocusWindow()
        gridwin = xCalcDoc.getChild("grid_window")
        document = self.ui_test.get_component()

        # 1. open attached file and check initial state
        self.assertFalse(is_row_hidden(calc_doc, 0))
        self.assertFalse(is_row_hidden(calc_doc, 1))
        self.assertTrue(is_row_hidden(calc_doc, 2))
        self.assertFalse(is_row_hidden(calc_doc, 3))
        self.assertFalse(is_row_hidden(calc_doc, 4))

        # 2. open filter of column A  and cancel
        gridwin.executeAction("LAUNCH", mkPropertyValues({"AUTOFILTER": "", "COL": "0", "ROW": "0"}))
        xFloatWindow = self.xUITest.getFloatWindow()

        xOkBtn = xFloatWindow.getChild("cancel")
        xOkBtn.executeAction("CLICK", tuple())

        self.assertFalse(is_row_hidden(calc_doc, 0))
        self.assertFalse(is_row_hidden(calc_doc, 1))
        self.assertTrue(is_row_hidden(calc_doc, 2))
        self.assertFalse(is_row_hidden(calc_doc, 3))
        self.assertFalse(is_row_hidden(calc_doc, 4))

        # 3. open filter of column A  and just click OK
        gridwin.executeAction("LAUNCH", mkPropertyValues({"AUTOFILTER": "", "COL": "0", "ROW": "0"}))
        xFloatWindow = self.xUITest.getFloatWindow()

        xOkBtn = xFloatWindow.getChild("ok")
        xOkBtn.executeAction("CLICK", tuple())

        self.assertFalse(is_row_hidden(calc_doc, 0))
        self.assertFalse(is_row_hidden(calc_doc, 1))
        self.assertTrue(is_row_hidden(calc_doc, 2))
        self.assertFalse(is_row_hidden(calc_doc, 3))
        self.assertTrue(is_row_hidden(calc_doc, 4)) # filtered out

        self.ui_test.close_doc()
# vim: set shiftwidth=4 softtabstop=4 expandtab:
diff --git a/sc/source/ui/view/gridwin.cxx b/sc/source/ui/view/gridwin.cxx
index ee8f8a9..efe3054 100644
--- a/sc/source/ui/view/gridwin.cxx
+++ b/sc/source/ui/view/gridwin.cxx
@@ -791,25 +791,31 @@ void ScGridWindow::UpdateAutoFilterFromMenu(AutoFilterMode eMode)
        pViewData->GetDispatcher().Execute(SID_FILTER, SfxCallMode::SLOT|SfxCallMode::RECORD);
        return;
    }
    if (eMode != AutoFilterMode::Top10
            && eMode != AutoFilterMode::Empty
            && eMode != AutoFilterMode::NonEmpty)

    ScQueryParam aParam;
    pDBData->GetQueryParam(aParam);

    if (eMode == AutoFilterMode::Normal)
    {
        // do not recreate auto-filter rules if there is no any changes from the user
        // Do not recreate autofilter rules if there are no changes from the user
        ScCheckListMenuWindow::ResultType aResult;
        mpAutoFilterPopup->getResult(aResult);

        if (aResult == aSaveAutoFilterResult)
        {
            SAL_INFO("sc.ui", "nothing to do when autofilter entries are the same");
            SAL_INFO("sc.ui", "Apply autofilter to data when entries are the same");
            // Apply autofilter to data
            ScQueryEntry* pEntry = aParam.FindEntryByField(rPos.Col(), true);
            pEntry->bDoQuery = true;
            pEntry->nField = rPos.Col();
            pEntry->eConnect = SC_AND;
            pEntry->eOp = SC_EQUAL;
            pViewData->GetView()->Query(aParam, nullptr, true);
            return;
        }
    }

    ScQueryParam aParam;
    pDBData->GetQueryParam(aParam);

    // Remove old entries.
    // Remove old entries in auto-filter rules
    aParam.RemoveAllEntriesByField(rPos.Col());

    if( !(eMode == AutoFilterMode::Normal && mpAutoFilterPopup->isAllSelected() ) )
@@ -840,19 +846,6 @@ void ScGridWindow::UpdateAutoFilterFromMenu(AutoFilterMode eMode)
                ScQueryEntry::QueryItemsType& rItems = pEntry->GetQueryItems();
                rItems.clear();
                std::for_each(aResult.begin(), aResult.end(), AddItemToEntry(rItems, rPool));

                if (mpAutoFilterPopup->isAllSelected())
                {
                    // get all strings from the column
                    std::vector<ScTypedStrData> aAllStrings; // case sensitive
                    pDoc->GetDataEntries(rPos.Col(), rPos.Row(), rPos.Tab(), aAllStrings, true);

                    if (rItems.size() == aAllStrings.size() || aAllStrings.empty())
                    {
                        // all selected => Remove filter entries
                        aParam.RemoveAllEntriesByField(rPos.Col());
                    }
                }
            }
            break;
            case AutoFilterMode::Top10: