tdf#153721 SwNavigator: fix chapter move

Fixes move chapter up/down regressions introduced by commit
e27b936fe864cdd2753470e0fef1e3cb1f891555. For regression details please
see comments 14 and 16 in the bug report.

Change-Id: I0a57483fae51656c89e8b4f3711d8057174517e4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/152347
Tested-by: Jenkins
Reviewed-by: Jim Raykowski <raykowj@gmail.com>
diff --git a/sw/qa/uitest/data/MoveChapterUpDown.odt b/sw/qa/uitest/data/MoveChapterUpDown.odt
new file mode 100644
index 0000000..4500ce9
--- /dev/null
+++ b/sw/qa/uitest/data/MoveChapterUpDown.odt
Binary files differ
diff --git a/sw/qa/uitest/navigator/movechapterupdown.py b/sw/qa/uitest/navigator/movechapterupdown.py
new file mode 100644
index 0000000..ce73391
--- /dev/null
+++ b/sw/qa/uitest/navigator/movechapterupdown.py
@@ -0,0 +1,507 @@
# -*- 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 libreoffice.uno.propertyvalue import mkPropertyValues
from uitest.uihelper.common import get_state_as_dict, get_url_for_data_file
import time

class movechapterupdown(UITestCase):

    def test_movechapterupdown(self):

        with self.ui_test.load_file(get_url_for_data_file('MoveChapterUpDown.odt')):
            xWriterDoc = self.xUITest.getTopFocusWindow()
            xWriterEdit = xWriterDoc.getChild('writer_edit')

            self.xUITest.executeCommand('.uno:Sidebar')
            xWriterEdit.executeAction('SIDEBAR', mkPropertyValues({'PANEL': 'SwNavigatorPanel'}))

            # wait until the navigator panel is available
            xNavigatorPanel = self.ui_test.wait_until_child_is_available('NavigatorPanel')

            # HACK, see the `m_aUpdTimer.SetTimeout(1000)` in the SwContentTree ctor in
            # sw/source/uibase/utlui/content.cxx, where that m_aUpdTimer is started by
            # SwContentTree::ShowTree triggered from the SIDEBAR action above, and which can
            # invalidate the TreeListEntryUIObjects used by the below code (see
            # 2798430c8a711861fdcdfbf9ac00a0527abd3bfc "Mark the uses of TreeListEntryUIObject as
            # dubious"); lets double that 1000 ms timeout value here to hopefully be on the safe
            # side:
            time.sleep(2)

            # Given the document chapter structure:
                # 1. One H1
                #   1.1. one_A (H2)
                #   1.2. one_B (H2)
                # 2. Two (H1)
                #     A heading of level 3
                #   2.1. Two_A (H2)
                #   2.1. Two_B (H2)
                # 3. Three (H1)
                #   3.1. Three_A (H2)
                #   3.2. Three_B (H2)

            xNavigatorPanelContentTree = xNavigatorPanel.getChild("contenttree")

            xNavigatorPanelContentTreeHeadings = xNavigatorPanelContentTree.getChild('0')
            xNavigatorPanelContentTreeHeadings.executeAction("EXPAND", tuple())

            #
            # test a level 1 chapter move up then move down
            #

            # Double click on the "2. Two (H1)" entry to select and set focus
            xHeadingsChild1 = xNavigatorPanelContentTreeHeadings.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1)["Text"], "2. Two (H1)")
            xHeadingsChild1.executeAction("DOUBLECLICK", tuple())

            self.ui_test.wait_until_property_is_updated(xNavigatorPanelContentTree, "SelectEntryText", "2. Two (H1)")

            # Click on the 'Move chapter up' button in the Navigator tool box
            xNavigatorPanel = xWriterEdit.getChild("NavigatorPanel")
            xToolBarContent6 = xNavigatorPanel.getChild("content6")
            xToolBarContent6.executeAction("CLICK", mkPropertyValues({"POS": "4"}))

            # Expected chapter order:
                # 2. Two (H1)
                #     A heading of level 3
                #   2.1. Two_A (H2)
                #   2.1. Two_B (H2)
                # 1. One (H1)
                #   1.1. One_A (H2)
                #   1.2. One_B (H2)
                # 3. Three (H1)
                #   3.1. Three_A (H2)
                #   3.2. Three_B (H2)

            xNavigatorPanelContentTreeHeadings = xNavigatorPanelContentTree.getChild('0')

            xHeadingsChild0 = xNavigatorPanelContentTreeHeadings.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0)["Text"], "2. Two (H1)")
            xHeadingsChild0Child0 = xHeadingsChild0.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child0)["Text"], "A heading of level 3")
            xHeadingsChild0Child1 = xHeadingsChild0.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child1)["Text"], "2.1. Two_A (H2)")
            xHeadingsChild0Child2 = xHeadingsChild0.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child2)["Text"], "2.1. Two_B (H2)")

            xHeadingsChild1 = xNavigatorPanelContentTreeHeadings.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1)["Text"], "1. One (H1)")
            xHeadingsChild1Child0 = xHeadingsChild1.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child0)["Text"], "1.1. One_A (H2)")
            xHeadingsChild1Child1 = xHeadingsChild1.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child1)["Text"], "1.2. One_B (H2)")

            xHeadingsChild2 = xNavigatorPanelContentTreeHeadings.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild2)["Text"], "3. Three (H1)")
            xHeadingsChild2Child0 = xHeadingsChild2.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child0)["Text"], "3.1. Three_A (H2)")
            xHeadingsChild2Child1 = xHeadingsChild2.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child1)["Text"], "3.2. Three_B (H2)")

            # Click on the 'Move chapter down' button in the Navigator tool box
            xNavigatorPanel = xWriterEdit.getChild("NavigatorPanel")
            xToolBarContent6 = xNavigatorPanel.getChild("content6")
            xToolBarContent6.executeAction("CLICK", mkPropertyValues({"POS": "5"}))

            # Expected chapter order is the original order
            xNavigatorPanelContentTreeHeadings = xNavigatorPanelContentTree.getChild('0')

            xHeadingsChild0 = xNavigatorPanelContentTreeHeadings.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0)["Text"], "1. One (H1)")
            xHeadingsChild0Child0 = xHeadingsChild0.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child0)["Text"], "1.1. One_A (H2)")
            xHeadingsChild0Child1 = xHeadingsChild0.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child1)["Text"], "1.2. One_B (H2)")

            xHeadingsChild1 = xNavigatorPanelContentTreeHeadings.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1)["Text"], "2. Two (H1)")
            xHeadingsChild1Child0 = xHeadingsChild1.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child0)["Text"], "A heading of level 3")
            xHeadingsChild1Child1 = xHeadingsChild1.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child1)["Text"], "2.1. Two_A (H2)")
            xHeadingsChild1Child2 = xHeadingsChild1.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child2)["Text"], "2.1. Two_B (H2)")

            xHeadingsChild2 = xNavigatorPanelContentTreeHeadings.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild2)["Text"], "3. Three (H1)")
            xHeadingsChild2Child0 = xHeadingsChild2.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child0)["Text"], "3.1. Three_A (H2)")
            xHeadingsChild2Child1 = xHeadingsChild2.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child1)["Text"], "3.2. Three_B (H2)")

            #
            # test a level 1 chapter move down then move up
            #

            # Double click on the "2. Two (H1)" entry to select and set focus
            xHeadingsChild1 = xNavigatorPanelContentTreeHeadings.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1)["Text"], "2. Two (H1)")
            xHeadingsChild1.executeAction("DOUBLECLICK", tuple())

            self.ui_test.wait_until_property_is_updated(xNavigatorPanelContentTree, "SelectEntryText", "2. Two (H1)")

            # Click on the 'Move chapter down' button in the Navigator tool box
            xNavigatorPanel = xWriterEdit.getChild("NavigatorPanel")
            xToolBarContent6 = xNavigatorPanel.getChild("content6")
            xToolBarContent6.executeAction("CLICK", mkPropertyValues({"POS": "5"}))

            # Expected chapter order:
                # 1. One (H1)
                #   1.1. One_A (H2)
                #   1.2. One_B (H2)
                # 3. Three (H1)
                #   3.1. Three_A (H2)
                #   3.2. Three_B (H2)
                # 2. Two (H1)
                #     A heading of level 3
                #   2.1. Two_A (H2)
                #   2.1. Two_B (H2)

            xNavigatorPanelContentTreeHeadings = xNavigatorPanelContentTree.getChild('0')

            xHeadingsChild0 = xNavigatorPanelContentTreeHeadings.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0)["Text"], "1. One (H1)")
            xHeadingsChild0Child0 = xHeadingsChild0.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child0)["Text"], "1.1. One_A (H2)")
            xHeadingsChild0Child1 = xHeadingsChild0.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child1)["Text"], "1.2. One_B (H2)")

            xHeadingsChild1 = xNavigatorPanelContentTreeHeadings.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1)["Text"], "3. Three (H1)")
            xHeadingsChild1Child0 = xHeadingsChild1.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child0)["Text"], "3.1. Three_A (H2)")
            xHeadingsChild1Child1 = xHeadingsChild1.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child1)["Text"], "3.2. Three_B (H2)")

            xHeadingsChild2 = xNavigatorPanelContentTreeHeadings.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild2)["Text"], "2. Two (H1)")
            xHeadingsChild2Child0 = xHeadingsChild2.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child0)["Text"], "A heading of level 3")
            xHeadingsChild2Child1 = xHeadingsChild2.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child1)["Text"], "2.1. Two_A (H2)")
            xHeadingsChild2Child2 = xHeadingsChild2.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child2)["Text"], "2.1. Two_B (H2)")

            # Click on the 'Move chapter up' button in the Navigator tool box
            xNavigatorPanel = xWriterEdit.getChild("NavigatorPanel")
            xToolBarContent6 = xNavigatorPanel.getChild("content6")
            xToolBarContent6.executeAction("CLICK", mkPropertyValues({"POS": "4"}))

            # Expected chapter order is the original order
            xNavigatorPanelContentTreeHeadings = xNavigatorPanelContentTree.getChild('0')

            xHeadingsChild0 = xNavigatorPanelContentTreeHeadings.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0)["Text"], "1. One (H1)")
            xHeadingsChild0Child0 = xHeadingsChild0.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child0)["Text"], "1.1. One_A (H2)")
            xHeadingsChild0Child1 = xHeadingsChild0.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child1)["Text"], "1.2. One_B (H2)")

            xHeadingsChild1 = xNavigatorPanelContentTreeHeadings.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1)["Text"], "2. Two (H1)")
            xHeadingsChild1Child0 = xHeadingsChild1.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child0)["Text"], "A heading of level 3")
            xHeadingsChild1Child1 = xHeadingsChild1.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child1)["Text"], "2.1. Two_A (H2)")
            xHeadingsChild1Child2 = xHeadingsChild1.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child2)["Text"], "2.1. Two_B (H2)")

            xHeadingsChild2 = xNavigatorPanelContentTreeHeadings.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild2)["Text"], "3. Three (H1)")
            xHeadingsChild2Child0 = xHeadingsChild2.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child0)["Text"], "3.1. Three_A (H2)")
            xHeadingsChild2Child1 = xHeadingsChild2.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child1)["Text"], "3.2. Three_B (H2)")

            #
            # test a sub chapter move chapter up then move down
            #

            # Double click on the "2.1. Two_A (H2)" entry to select and set focus
            xHeadingsChild1 = xNavigatorPanelContentTreeHeadings.getChild('1')
            xHeadingsChild1Child1 = xHeadingsChild1.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child1)["Text"], "2.1. Two_A (H2)")
            xHeadingsChild1Child1.executeAction("DOUBLECLICK", tuple())

            self.ui_test.wait_until_property_is_updated(xNavigatorPanelContentTree, "SelectEntryText", "2.1. Two_A (H2)")

            # Click on the 'Move chapter up' button in the Navigator tool box
            xNavigatorPanel = xWriterEdit.getChild("NavigatorPanel")
            xToolBarContent6 = xNavigatorPanel.getChild("content6")
            xToolBarContent6.executeAction("CLICK", mkPropertyValues({"POS": "4"}))

            # Expected chapter order:
                # 1. One H1
                #   1.1. one_A (H2)
                #   1.2. one_B (H2)
                # 2. Two (H1)
                #   2.1. Two_A (H2)
                #     A heading of level 3
                #   2.1. Two_B (H2)
                # 3. Three (H1)
                #   3.1. Three_A (H2)
                #   3.2. Three_B (H2)

            xNavigatorPanelContentTreeHeadings = xNavigatorPanelContentTree.getChild('0')

            xHeadingsChild0 = xNavigatorPanelContentTreeHeadings.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0)["Text"], "1. One (H1)")
            xHeadingsChild0Child0 = xHeadingsChild0.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child0)["Text"], "1.1. One_A (H2)")
            xHeadingsChild0Child1 = xHeadingsChild0.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child1)["Text"], "1.2. One_B (H2)")

            xHeadingsChild1 = xNavigatorPanelContentTreeHeadings.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1)["Text"], "2. Two (H1)")
            xHeadingsChild1Child0 = xHeadingsChild1.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child0)["Text"], "2.1. Two_A (H2)")
            xHeadingsChild1Child0Child0 = xHeadingsChild1Child0.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child0Child0)["Text"], "A heading of level 3")
            xHeadingsChild1Child1 = xHeadingsChild1.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child1)["Text"], "2.1. Two_B (H2)")

            xHeadingsChild2 = xNavigatorPanelContentTreeHeadings.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild2)["Text"], "3. Three (H1)")
            xHeadingsChild2Child0 = xHeadingsChild2.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child0)["Text"], "3.1. Three_A (H2)")
            xHeadingsChild2Child1 = xHeadingsChild2.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child1)["Text"], "3.2. Three_B (H2)")

            self.ui_test.wait_until_property_is_updated(xNavigatorPanelContentTree, "SelectEntryText", "2.1. Two_A (H2)")

            # Click on the 'Move chapter down' button in the Navigator tool box
            xNavigatorPanel = xWriterEdit.getChild("NavigatorPanel")
            xToolBarContent6 = xNavigatorPanel.getChild("content6")
            xToolBarContent6.executeAction("CLICK", mkPropertyValues({"POS": "5"}))

            # Expected chapter order:
                # 1. One H1
                #   1.1. one_A (H2)
                #   1.2. one_B (H2)
                # 2. Two (H1)
                #   2.1. Two_B (H2)
                #   2.1. Two_A (H2)
                #     A heading of level 3
                # 3. Three (H1)
                #   3.1. Three_A (H2)
                #   3.2. Three_B (H2)

            xNavigatorPanelContentTreeHeadings = xNavigatorPanelContentTree.getChild('0')

            xHeadingsChild0 = xNavigatorPanelContentTreeHeadings.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0)["Text"], "1. One (H1)")
            xHeadingsChild0Child0 = xHeadingsChild0.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child0)["Text"], "1.1. One_A (H2)")
            xHeadingsChild0Child1 = xHeadingsChild0.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child1)["Text"], "1.2. One_B (H2)")

            xHeadingsChild1 = xNavigatorPanelContentTreeHeadings.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1)["Text"], "2. Two (H1)")
            xHeadingsChild1Child0 = xHeadingsChild1.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child0)["Text"], "2.1. Two_B (H2)")
            xHeadingsChild1Child1 = xHeadingsChild1.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child1)["Text"], "2.1. Two_A (H2)")
            xHeadingsChild1Child1Child0 = xHeadingsChild1Child1.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child1Child0)["Text"], "A heading of level 3")

            xHeadingsChild2 = xNavigatorPanelContentTreeHeadings.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild2)["Text"], "3. Three (H1)")
            xHeadingsChild2Child0 = xHeadingsChild2.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child0)["Text"], "3.1. Three_A (H2)")
            xHeadingsChild2Child1 = xHeadingsChild2.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child1)["Text"], "3.2. Three_B (H2)")

            # Move "A heading of level 3" to its orignal position

            # Double click on the "A heading of level 3" entry to select and set focus
            xHeadingsChild1 = xNavigatorPanelContentTreeHeadings.getChild('1')
            xHeadingsChild1Child1Child0 = xHeadingsChild1Child1.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child1Child0)["Text"], "A heading of level 3")
            xHeadingsChild1Child1Child0.executeAction("DOUBLECLICK", tuple())

            self.ui_test.wait_until_property_is_updated(xNavigatorPanelContentTree, "SelectEntryText", "A heading of level 3")

            # Click on the 'Move chapter up' button in the Navigator tool box
            xNavigatorPanel = xWriterEdit.getChild("NavigatorPanel")
            xToolBarContent6 = xNavigatorPanel.getChild("content6")
            xToolBarContent6.executeAction("CLICK", mkPropertyValues({"POS": "4"}))

            # Click on the 'Move chapter up' button in the Navigator tool box
            xNavigatorPanel = xWriterEdit.getChild("NavigatorPanel")
            xToolBarContent6 = xNavigatorPanel.getChild("content6")
            xToolBarContent6.executeAction("CLICK", mkPropertyValues({"POS": "4"}))

            # Expected chapter order:
                # 1. One H1
                #   1.1. one_A (H2)
                #   1.2. one_B (H2)
                # 2. Two (H1)
                #     A heading of level 3
                #   2.1. Two_B (H2)
                #   2.1. Two_A (H2)
                # 3. Three (H1)
                #   3.1. Three_A (H2)
                #   3.2. Three_B (H2)

            xNavigatorPanelContentTreeHeadings = xNavigatorPanelContentTree.getChild('0')

            xHeadingsChild0 = xNavigatorPanelContentTreeHeadings.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0)["Text"], "1. One (H1)")
            xHeadingsChild0Child0 = xHeadingsChild0.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child0)["Text"], "1.1. One_A (H2)")
            xHeadingsChild0Child1 = xHeadingsChild0.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child1)["Text"], "1.2. One_B (H2)")

            xHeadingsChild1 = xNavigatorPanelContentTreeHeadings.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1)["Text"], "2. Two (H1)")
            xHeadingsChild1Child0 = xHeadingsChild1.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child0)["Text"], "A heading of level 3")
            xHeadingsChild1Child1 = xHeadingsChild1.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child1)["Text"], "2.1. Two_B (H2)")
            xHeadingsChild1Child2 = xHeadingsChild1.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child2)["Text"], "2.1. Two_A (H2)")

            xHeadingsChild2 = xNavigatorPanelContentTreeHeadings.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild2)["Text"], "3. Three (H1)")
            xHeadingsChild2Child0 = xHeadingsChild2.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child0)["Text"], "3.1. Three_A (H2)")
            xHeadingsChild2Child1 = xHeadingsChild2.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child1)["Text"], "3.2. Three_B (H2)")

            # Move "2.1. Two_B (H2)" to its orignal position

            # Double click on the "2.1. Two_B (H2)" entry to select and set focus
            xHeadingsChild1 = xNavigatorPanelContentTreeHeadings.getChild('1')
            xHeadingsChild1Child1 = xHeadingsChild1.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child1)["Text"], "2.1. Two_B (H2)")
            xHeadingsChild1Child1.executeAction("DOUBLECLICK", tuple())

            self.ui_test.wait_until_property_is_updated(xNavigatorPanelContentTree, "SelectEntryText", "2.1. Two_B (H2)")

            # Click on the 'Move chapter down' button in the Navigator tool box
            xNavigatorPanel = xWriterEdit.getChild("NavigatorPanel")
            xToolBarContent6 = xNavigatorPanel.getChild("content6")
            xToolBarContent6.executeAction("CLICK", mkPropertyValues({"POS": "5"}))

            # Expected chapter order is the original order
            xNavigatorPanelContentTreeHeadings = xNavigatorPanelContentTree.getChild('0')

            xHeadingsChild0 = xNavigatorPanelContentTreeHeadings.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0)["Text"], "1. One (H1)")
            xHeadingsChild0Child0 = xHeadingsChild0.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child0)["Text"], "1.1. One_A (H2)")
            xHeadingsChild0Child1 = xHeadingsChild0.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child1)["Text"], "1.2. One_B (H2)")

            xHeadingsChild1 = xNavigatorPanelContentTreeHeadings.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1)["Text"], "2. Two (H1)")
            xHeadingsChild1Child0 = xHeadingsChild1.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child0)["Text"], "A heading of level 3")
            xHeadingsChild1Child1 = xHeadingsChild1.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child1)["Text"], "2.1. Two_A (H2)")
            xHeadingsChild1Child2 = xHeadingsChild1.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child2)["Text"], "2.1. Two_B (H2)")

            xHeadingsChild2 = xNavigatorPanelContentTreeHeadings.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild2)["Text"], "3. Three (H1)")
            xHeadingsChild2Child0 = xHeadingsChild2.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child0)["Text"], "3.1. Three_A (H2)")
            xHeadingsChild2Child1 = xHeadingsChild2.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child1)["Text"], "3.2. Three_B (H2)")

            #
            # test moving a sub chapter out of and then back into its parent
            #

            # Double click on the "1.1. One_A (H2)" entry to select and set focus
            xHeadingsChild0 = xNavigatorPanelContentTreeHeadings.getChild('0')
            xHeadingsChild0Child0 = xHeadingsChild0.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child0)["Text"], "1.1. One_A (H2)")
            xHeadingsChild0Child0.executeAction("DOUBLECLICK", tuple())

            self.ui_test.wait_until_property_is_updated(xNavigatorPanelContentTree, "SelectEntryText", "1.1. One_A (H2)")

            # Click on the 'Move chapter up' button in the Navigator tool box
            xNavigatorPanel = xWriterEdit.getChild("NavigatorPanel")
            xToolBarContent6 = xNavigatorPanel.getChild("content6")
            xToolBarContent6.executeAction("CLICK", mkPropertyValues({"POS": "4"}))

            # Expected chapter order:
                #   1.1. One_A (H2)
                # 1. One H1
                #   1.2. One_B (H2)
                # 2. Two (H1)
                #     A heading of level 3
                #   2.1. Two_A (H2)
                #   2.1. Two_B (H2)
                # 3. Three (H1)
                #   3.1. Three_A (H2)
                #   3.2. Three_B (H2)

            xNavigatorPanelContentTreeHeadings = xNavigatorPanelContentTree.getChild('0')

            xHeadingsChild0 = xNavigatorPanelContentTreeHeadings.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0)["Text"], "1.1. One_A (H2)")

            xHeadingsChild1 = xNavigatorPanelContentTreeHeadings.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1)["Text"], "1. One (H1)")
            xHeadingsChild1Child0 = xHeadingsChild1.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child0)["Text"], "1.2. One_B (H2)")

            xHeadingsChild2 = xNavigatorPanelContentTreeHeadings.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild2)["Text"], "2. Two (H1)")
            xHeadingsChild2Child0 = xHeadingsChild2.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child0)["Text"], "A heading of level 3")
            xHeadingsChild2Child1 = xHeadingsChild2.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child1)["Text"], "2.1. Two_A (H2)")
            xHeadingsChild2Child2 = xHeadingsChild2.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child2)["Text"], "2.1. Two_B (H2)")

            xHeadingsChild3 = xNavigatorPanelContentTreeHeadings.getChild('3')
            self.assertEqual(get_state_as_dict(xHeadingsChild3)["Text"], "3. Three (H1)")
            xHeadingsChild3Child0 = xHeadingsChild3.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild3Child0)["Text"], "3.1. Three_A (H2)")
            xHeadingsChild3Child1 = xHeadingsChild3.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild3Child1)["Text"], "3.2. Three_B (H2)")

            # Click on the 'Move chapter down' button in the Navigator tool box
            xNavigatorPanel = xWriterEdit.getChild("NavigatorPanel")
            xToolBarContent6 = xNavigatorPanel.getChild("content6")
            xToolBarContent6.executeAction("CLICK", mkPropertyValues({"POS": "5"}))

            # Expected chapter order is the original order
            xNavigatorPanelContentTreeHeadings = xNavigatorPanelContentTree.getChild('0')

            xHeadingsChild0 = xNavigatorPanelContentTreeHeadings.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0)["Text"], "1. One (H1)")
            xHeadingsChild0Child0 = xHeadingsChild0.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child0)["Text"], "1.1. One_A (H2)")
            xHeadingsChild0Child1 = xHeadingsChild0.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild0Child1)["Text"], "1.2. One_B (H2)")

            xHeadingsChild1 = xNavigatorPanelContentTreeHeadings.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1)["Text"], "2. Two (H1)")
            xHeadingsChild1Child0 = xHeadingsChild1.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child0)["Text"], "A heading of level 3")
            xHeadingsChild1Child1 = xHeadingsChild1.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child1)["Text"], "2.1. Two_A (H2)")
            xHeadingsChild1Child2 = xHeadingsChild1.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild1Child2)["Text"], "2.1. Two_B (H2)")

            xHeadingsChild2 = xNavigatorPanelContentTreeHeadings.getChild('2')
            self.assertEqual(get_state_as_dict(xHeadingsChild2)["Text"], "3. Three (H1)")
            xHeadingsChild2Child0 = xHeadingsChild2.getChild('0')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child0)["Text"], "3.1. Three_A (H2)")
            xHeadingsChild2Child1 = xHeadingsChild2.getChild('1')
            self.assertEqual(get_state_as_dict(xHeadingsChild2Child1)["Text"], "3.2. Three_B (H2)")

            self.xUITest.executeCommand('.uno:Sidebar')

# vim: set shiftwidth=4 softtabstop=4 expandtab:
diff --git a/sw/source/uibase/utlui/content.cxx b/sw/source/uibase/utlui/content.cxx
index 05721ad..6d22bc5 100644
--- a/sw/source/uibase/utlui/content.cxx
+++ b/sw/source/uibase/utlui/content.cxx
@@ -3267,7 +3267,8 @@ void SwContentTree::ExecCommand(std::u16string_view rCmd, bool bOutlineWithChild
    SwWrtShell *const pShell = GetWrtShell();

    const SwNodes& rNodes = pShell->GetNodes();
    const SwOutlineNodes::size_type nOutlineNdsSize = rNodes.GetOutLineNds().size();
    const SwOutlineNodes& rOutlineNodes = rNodes.GetOutLineNds();
    const SwOutlineNodes::size_type nOutlineNdsSize = rOutlineNodes.size();

    std::vector<SwTextNode*> selectedOutlineNodes;
    std::vector<std::unique_ptr<weld::TreeIter>> selected;
@@ -3323,13 +3324,27 @@ void SwContentTree::ExecCommand(std::u16string_view rCmd, bool bOutlineWithChild

    MakeAllOutlineContentTemporarilyVisible a(GetWrtShell()->GetDoc());

    // get first regular document content node outline node position in outline nodes array
    SwOutlineNodes::size_type nFirstRegularDocContentOutlineNodePos = SwOutlineNodes::npos;
    SwNodeOffset nEndOfExtrasIndex = rNodes.GetEndOfExtras().GetIndex();
    for (SwOutlineNodes::size_type nPos = 0; nPos < nOutlineNdsSize; nPos++)
    {
        if (rOutlineNodes[nPos]->GetIndex() > nEndOfExtrasIndex)
        {
            nFirstRegularDocContentOutlineNodePos = nPos;
            break;
        }
    }

    for (auto const& pCurrentEntry : selected)
    {
        nActPos = weld::fromId<SwOutlineContent*>(
                    m_xTreeView->get_id(*pCurrentEntry))->GetOutlinePos();

        // outline nodes in frames and tables are not up/down moveable
        if (nActPos == SwOutlineNodes::npos || (bUpDown && !pShell->IsOutlineMovable(nActPos)))
        if (nActPos == SwOutlineNodes::npos ||
                (bUpDown && (!pShell->IsOutlineMovable(nActPos) ||
                 nFirstRegularDocContentOutlineNodePos == SwOutlineNodes::npos)))
        {
            continue;
        }
@@ -3352,13 +3367,66 @@ void SwContentTree::ExecCommand(std::u16string_view rCmd, bool bOutlineWithChild
            {
                // make outline selection for use by MoveOutlinePara
                pShell->MakeOutlineSel(nActPos, nActPos, bOutlineWithChildren);
                if (pShell->MoveOutlinePara(nDir))
                {
                    // Set cursor back to the current position
                    pShell->GotoOutline(nActPos + nDir);
                }
            }

                int nActPosOutlineLevel =
                        rOutlineNodes[nActPos]->GetTextNode()->GetAttrOutlineLevel();
                SwOutlineNodes::size_type nPos = nActPos;
                if (!bUp)
                {
                    // move down
                    int nPosOutlineLevel = -1;
                    while (++nPos < nOutlineNdsSize)
                    {
                        nPosOutlineLevel =
                                rOutlineNodes[nPos]->GetTextNode()->GetAttrOutlineLevel();
                        // discontinue if moving out of parent or equal level is found
                        if (nPosOutlineLevel <= nActPosOutlineLevel)
                            break;
                        // count the children of the node when they are not included in the move
                        if (!bOutlineWithChildren)
                            nDir++;
                    }
                    if (nPosOutlineLevel >= nActPosOutlineLevel)
                    {
                        // move past children
                        while (++nPos < nOutlineNdsSize)
                        {
                            nPosOutlineLevel =
                                    rOutlineNodes[nPos]->GetTextNode()->GetAttrOutlineLevel();
                            // discontinue if moving out of parent or equal level is found
                            if (nPosOutlineLevel <= nActPosOutlineLevel)
                                break;
                            nDir++;
                        }
                    }
                }
                else
                {
                    // move up
                    while (nPos && --nPos >= nFirstRegularDocContentOutlineNodePos)
                    {
                        int nPosOutlineLevel =
                                rOutlineNodes[nPos]->GetTextNode()->GetAttrOutlineLevel();
                        // discontinue if equal level is found
                        if (nPosOutlineLevel == nActPosOutlineLevel)
                            break;
                        // discontinue if moving out of parent
                        if (nPosOutlineLevel < nActPosOutlineLevel)
                        {
                            // Required for expected chapter placement when the chapter being moved
                            // up has an outline level less than the outline level of chapters it
                            // is being moved above and then encounters a chapter with an outline
                            // level that is greater before reaching a chapter with the same
                            // outline level as itself.
                            if (nDir < -1)
                                nDir++;
                            break;
                        }
                        nDir--;
                    }
                }
                pShell->MoveOutlinePara(nDir);
            }
            pShell->ClearMark();
        }
        else