tdf#154818 - Find bar: remember and reuse last search string

Changed remembered search string in the find bar from user
configuration to session and added UI test.

Change-Id: Idf211f1fadd87ba935fe3948c20c4e511aa8afdc
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151335
Tested-by: Jenkins
Reviewed-by: Andreas Heinisch <andreas.heinisch@yahoo.de>
diff --git a/svx/source/inc/findtextfield.hxx b/svx/source/inc/findtextfield.hxx
index 8cfa98f..2c79a529 100644
--- a/svx/source/inc/findtextfield.hxx
+++ b/svx/source/inc/findtextfield.hxx
@@ -67,6 +67,8 @@ private:

    // tdf#154269 - respect FindReplaceRememberedSearches expert option
    sal_uInt16 m_nRememberSize;
    // tdf#154818 - remember last search string
    static OUString m_sRememberedSearchString;
};

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/svx/source/tbxctrls/tbunosearchcontrollers.cxx b/svx/source/tbxctrls/tbunosearchcontrollers.cxx
index 1ae8a0b..e2c76a9 100644
--- a/svx/source/tbxctrls/tbunosearchcontrollers.cxx
+++ b/svx/source/tbxctrls/tbunosearchcontrollers.cxx
@@ -60,7 +60,6 @@
#include <svx/labelitemwindow.hxx>
#include <svx/srchdlg.hxx>
#include <vcl/event.hxx>
#include <unotools/viewoptions.hxx>

#include <findtextfield.hxx>

@@ -192,6 +191,9 @@ void impl_executeSearch( const css::uno::Reference< css::uno::XComponentContext 

}

// tdf#154818 - remember last search string
OUString FindTextFieldControl::m_sRememberedSearchString;

FindTextFieldControl::FindTextFieldControl( vcl::Window* pParent,
    css::uno::Reference< css::frame::XFrame > xFrame,
    css::uno::Reference< css::uno::XComponentContext > xContext) :
@@ -260,20 +262,12 @@ void FindTextFieldControl::SetTextToSelected_Impl()
        m_xWidget->set_entry_text(aString);
        m_aChangeHdl.Call(*m_xWidget);
    }
    else
    // tdf#154818 - reuse last search string
    else if (!m_sRememberedSearchString.isEmpty() || get_count() > 0)
    {
        // tdf#154818 - reuse last search string
        SvtViewOptions aDlgOpt(EViewType::Dialog, m_xWidget->get_help_id());
        if (aDlgOpt.Exists())
        {
            css::uno::Any aUserItem = aDlgOpt.GetUserItem("UserItem");
            aUserItem >>= aString;
        }
        else if (get_count() > 0)
            aString = m_xWidget->get_text(0);

        // prepopulate with last search word (fdo#84256)
        m_xWidget->set_entry_text(aString);
        m_xWidget->set_entry_text(m_sRememberedSearchString.isEmpty() ? m_xWidget->get_text(0)
                                                                      : m_sRememberedSearchString);
    }
}

@@ -340,10 +334,8 @@ IMPL_LINK(FindTextFieldControl, KeyInputHdl, const KeyEvent&, rKeyEvent, bool)
void FindTextFieldControl::ActivateFind(bool bShift)
{
    // tdf#154818 - remember last search string
    const OUString aLastSearchString = m_xWidget->get_active_text();
    SvtViewOptions aDlgOpt(EViewType::Dialog, m_xWidget->get_help_id());
    aDlgOpt.SetUserItem("UserItem", css::uno::Any(aLastSearchString));
    Remember_Impl(aLastSearchString);
    m_sRememberedSearchString = m_xWidget->get_active_text();
    Remember_Impl(m_sRememberedSearchString);

    vcl::Window* pWindow = GetParent();
    ToolBox* pToolBox = static_cast<ToolBox*>(pWindow);
diff --git a/sw/qa/uitest/findBar/tdf154818.py b/sw/qa/uitest/findBar/tdf154818.py
new file mode 100644
index 0000000..e0470206f
--- /dev/null
+++ b/sw/qa/uitest/findBar/tdf154818.py
@@ -0,0 +1,65 @@
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-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/.
#
from uitest.framework import UITestCase
from uitest.uihelper.common import get_state_as_dict, select_pos
from libreoffice.uno.propertyvalue import mkPropertyValues

class tdf154818(UITestCase):

    def test_tdf154818_remember_search_item(self):
        with self.ui_test.create_doc_in_start_center("writer"):
            xWriterDoc = self.xUITest.getTopFocusWindow()
            xWriterEdit = xWriterDoc.getChild("writer_edit")

            # Search for an entry and check again if it is preselected (A -> B -> A)
            searchTerms = ["A", "B", "A"]
            for searchTerm in searchTerms:
                xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+f"}))
                xFind = xWriterDoc.getChild("find")
                xFind.executeAction("TYPE", mkPropertyValues({"KEYCODE":"BACKSPACE"}))
                xFind.executeAction("TYPE", mkPropertyValues({"TEXT":searchTerm}))
                xFind.executeAction("TYPE", mkPropertyValues({"KEYCODE":"RETURN"}))
                xFindBar = xWriterDoc.getChild("FindBar")
                xFindBar.executeAction("CLICK", mkPropertyValues({"POS":"0"}))

            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+f"}))
            xFind = xWriterDoc.getChild("find")
            # Without the fix in place, this test would have failed with
            # AssertionError: 'A' != 'B'
            # i.e., the last search item was not remembered
            self.assertEqual("A", get_state_as_dict(xFind)["Text"])

    def test_tdf154818_search_history(self):
        with self.ui_test.create_doc_in_start_center("writer"):
                xWriterDoc = self.xUITest.getTopFocusWindow()
                xWriterEdit = xWriterDoc.getChild("writer_edit")

                # Search for an entry and check for the search history (A -> B -> C -> B)
                searchTerms = ["A", "B", "C", "B"]
                for searchTerm in searchTerms:
                    xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+f"}))
                    xFind = xWriterDoc.getChild("find")
                    xFind.executeAction("TYPE", mkPropertyValues({"KEYCODE":"BACKSPACE"}))
                    xFind.executeAction("TYPE", mkPropertyValues({"TEXT":searchTerm}))
                    xFind.executeAction("TYPE", mkPropertyValues({"KEYCODE":"RETURN"}))
                    xFindBar = xWriterDoc.getChild("FindBar")
                    xFindBar.executeAction("CLICK", mkPropertyValues({"POS":"0"}))

                # Check if the search history was respected
                searchTerms = ["B", "C", "A"]
                xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+f"}))
                xFind = xWriterDoc.getChild("find")
                for searchTerm in searchTerms:
                    select_pos(xFind, str(searchTerms.index(searchTerm)))
                    # Without the fix in place, this test would have failed with
                    # AssertionError: 'B' != 'C'
                    # i.e., the search history was not respected
                    self.assertEqual(searchTerm, get_state_as_dict(xFind)["Text"])

# vim: set shiftwidth=4 softtabstop=4 expandtab:
diff --git a/sw/qa/uitest/findReplace/findReplace.py b/sw/qa/uitest/findReplace/findReplace.py
index b4a3f99..7c9f531 100644
--- a/sw/qa/uitest/findReplace/findReplace.py
+++ b/sw/qa/uitest/findReplace/findReplace.py
@@ -211,4 +211,26 @@ class findReplace(UITestCase):

                self.assertEqual(document.Text.String, "ß")

    def test_tdf154818_search_history(self):
        with self.ui_test.create_doc_in_start_center("writer"):
            with self.ui_test.execute_modeless_dialog_through_command(".uno:SearchDialog", close_button="close") as xDialog:
                xSearchTerm = xDialog.getChild("searchterm")
                # Search for an entry and check for the search history (A -> B -> C -> B)
                searchTerms = ["A", "B", "C", "B"]
                for searchTerm in searchTerms:
                    xSearchTerm.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+A"}))
                    xSearchTerm.executeAction("TYPE", mkPropertyValues({"KEYCODE":"BACKSPACE"}))
                    xSearchTerm.executeAction("TYPE", mkPropertyValues({"TEXT":searchTerm}))
                    xSearchNext = xDialog.getChild("search")
                    xSearchNext.executeAction("CLICK", tuple())

                # Check if the search history was respected
                searchTerms = ["B", "C", "A"]
                for searchTerm in searchTerms:
                    select_pos(xSearchTerm, str(searchTerms.index(searchTerm)))
                    # Without the fix in place, this test would have failed with
                    # AssertionError: 'B' != 'C'
                    # i.e., the search history was not respected
                    self.assertEqual(searchTerm, get_state_as_dict(xSearchTerm)["Text"])

# vim: set shiftwidth=4 softtabstop=4 expandtab: