1# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
2#
3# This Source Code Form is subject to the terms of the Mozilla Public
4# License, v. 2.0. If a copy of the MPL was not distributed with this
5# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6#
7from uitest.framework import UITestCase
8from uitest.uihelper.calc import enter_text_to_cell
9from libreoffice.uno.propertyvalue import mkPropertyValues
10from libreoffice.calc.document import is_row_hidden
11#Bug 117276 - Autofilter settings being reset in some cases
12
13class tdf117276_autofilter_reset(UITestCase):
14    def get_values_count_in_AutoFilter(self, xGridWindow, columnIndex, buttonName = "cancel"):
15        # open filter pop-up window
16        self.assertIsNotNone(xGridWindow)
17        xGridWindow.executeAction("LAUNCH", mkPropertyValues({"AUTOFILTER": "", "COL": columnIndex, "ROW": "0"}))
18        xFloatWindow = self.xUITest.getFloatWindow()
19        self.assertIsNotNone(xFloatWindow)
20
21        # get check list
22        xCheckListMenu = xFloatWindow.getChild("check_list_menu")
23        self.assertIsNotNone(xCheckListMenu)
24
25        xTreeList = xCheckListMenu.getChild("check_list_box")
26        self.assertIsNotNone(xTreeList)
27
28        valuesCount = len(xTreeList.getChildren())
29
30        # close pop-up window
31        xButton = xFloatWindow.getChild(buttonName)
32        self.assertIsNotNone(xButton)
33        xButton.executeAction("CLICK", tuple())
34
35        return valuesCount
36
37    def openAutoFilterAndCloseIt(self, xGridWindow, columnIndex, buttonName):
38        self.get_values_count_in_AutoFilter(xGridWindow, columnIndex, buttonName)
39
40    def check_state(self, document, xGridWindow):
41        self.assertFalse(is_row_hidden(document, 0))  # column headers
42        self.assertTrue(is_row_hidden(document, 1))
43        self.assertTrue(is_row_hidden(document, 2))
44        self.assertTrue(is_row_hidden(document, 3))
45        self.assertTrue(is_row_hidden(document, 4))
46        self.assertFalse(is_row_hidden(document, 5))
47
48        self.assertEqual(1, self.get_values_count_in_AutoFilter(xGridWindow, "0"))
49        self.assertEqual(2, self.get_values_count_in_AutoFilter(xGridWindow, "1"))
50
51    def test_run(self):
52        self.ui_test.create_doc_in_start_center("calc")
53        document = self.ui_test.get_component()
54        calcDoc = self.xUITest.getTopFocusWindow()
55        xGridWindow = calcDoc.getChild("grid_window")
56
57#        self.ui_test.execute_dialog_through_command(".uno:Insert")  # insert sheet
58#        xDialog = self.xUITest.getTopFocusWindow()
59#        xOKButton = xDialog.getChild("ok")
60#        xOKButton.executeAction("CLICK", tuple())
61
62        # 1. prepare document
63        #    |    A       | B                      |
64        #  -----------------------------------------
65        #  1 | HEADER-A   | HEADER-B               |
66        #  -----------------------------------------
67        #  2 | Unique a2  | common value for B2:B4 |
68        #  3 | Unique a3  | common value for B2:B4 |
69        #  4 | Unique a4  | common value for B2:B4 |
70        #  5 | Unique a5  | Unique b5              |
71        #  6 | Unique a6  | Unique b6              |
72        #
73        # row-1
74        enter_text_to_cell(xGridWindow, "A1", "HEADER-A")
75        enter_text_to_cell(xGridWindow, "B1", "HEADER-B")
76
77        # row-1
78        enter_text_to_cell(xGridWindow, "A2", "Unique a2")
79        enter_text_to_cell(xGridWindow, "B2", "common value for B2:B4")
80
81        # row-2
82        enter_text_to_cell(xGridWindow, "A3", "Unique a3")
83        enter_text_to_cell(xGridWindow, "B3", "common value for B2:B4")
84
85        # row-3
86        enter_text_to_cell(xGridWindow, "A4", "Unique a4")
87        enter_text_to_cell(xGridWindow, "B4", "common value for B2:B4")
88
89        # row-4
90        enter_text_to_cell(xGridWindow, "A5", "Unique a5")
91        enter_text_to_cell(xGridWindow, "B5", "Unique b5")
92
93        # row-5
94        enter_text_to_cell(xGridWindow, "A6", "Unique a6")
95        enter_text_to_cell(xGridWindow, "B6", "Unique b6")
96
97        xGridWindow.executeAction("SELECT", mkPropertyValues({"RANGE": "A1:B6"}))
98
99        # 2. switch on auto-filter
100        self.ui_test.execute_dialog_through_command(".uno:DataFilterAutoFilter")
101        xDialog = self.xUITest.getTopFocusWindow()
102        xYesBtn = xDialog.getChild("yes")
103        self.ui_test.close_dialog_through_button(xYesBtn)
104
105        # autofilter still exist
106        self.assertEqual(document.getPropertyValue("UnnamedDatabaseRanges").getByTable(0).AutoFilter, True)
107
108        # 3. open filter of column A and deselect first 3 entries (Unique a2, Unique a3, Unique a4)
109        xGridWindow.executeAction("LAUNCH", mkPropertyValues({"AUTOFILTER": "", "COL": "0", "ROW": "0"}))
110        xFloatWindow = self.xUITest.getFloatWindow()
111        xCheckListMenu = xFloatWindow.getChild("check_list_menu")
112        xTreeList = xCheckListMenu.getChild("check_list_box")
113
114        xEntry1 = xTreeList.getChild("0")
115        xEntry1.executeAction("CLICK", tuple())   # Unique a2
116        xEntry2 = xTreeList.getChild("1")
117        xEntry2.executeAction("CLICK", tuple())   # Unique a3
118        xEntry3 = xTreeList.getChild("2")
119        xEntry3.executeAction("CLICK", tuple())   # Unique a4
120
121        xOkButton = xFloatWindow.getChild("ok")
122        xOkButton.executeAction("CLICK", tuple())
123
124        # check filtering
125        #    |    A       | B                      |
126        #  -----------------------------------------
127        #  1 | HEADER-A   | HEADER-B               |
128        #  -----------------------------------------
129        #  5 | Unique a5  | Unique b5              |
130        #  6 | Unique a6  | Unique b6              |
131        self.assertFalse(is_row_hidden(document, 0))  # column headers
132        self.assertTrue(is_row_hidden(document, 1))
133        self.assertTrue(is_row_hidden(document, 2))
134        self.assertTrue(is_row_hidden(document, 3))
135        self.assertFalse(is_row_hidden(document, 4))
136        self.assertFalse(is_row_hidden(document, 5))
137
138        self.assertEqual(5, self.get_values_count_in_AutoFilter(xGridWindow, "0"))
139        self.assertEqual(2, self.get_values_count_in_AutoFilter(xGridWindow, "1"))
140
141        # 4. open filter of column B and deselect "Unique b5"
142        xGridWindow.executeAction("LAUNCH", mkPropertyValues({"AUTOFILTER": "", "COL": "1", "ROW": "0"}))
143        xFloatWindow = self.xUITest.getFloatWindow()
144        xCheckListMenu = xFloatWindow.getChild("check_list_menu")
145        xTreeList = xCheckListMenu.getChild("check_list_box")
146
147        xEntry = xTreeList.getChild("0")
148        xEntry.executeAction("CLICK", tuple())   # Unique b5
149
150        xOkButton = xFloatWindow.getChild("ok")
151        xOkButton.executeAction("CLICK", tuple())
152
153        # check filtering
154        #    |    A       | B                      |
155        #  -----------------------------------------
156        #  1 | HEADER-A   | HEADER-B               |
157        #  -----------------------------------------
158        #  6 | Unique a6  | Unique b6              |
159        self.check_state(document, xGridWindow)
160
161        # 4. open filters of column A, B and close it using different buttons
162        self.openAutoFilterAndCloseIt(xGridWindow, "1", "ok")
163        self.check_state(document, xGridWindow)
164
165        self.openAutoFilterAndCloseIt(xGridWindow, "1", "cancel")
166        self.check_state(document, xGridWindow)
167
168        self.openAutoFilterAndCloseIt(xGridWindow, "0", "cancel")
169        self.check_state(document, xGridWindow)
170
171        self.openAutoFilterAndCloseIt(xGridWindow, "0", "ok")
172        self.check_state(document, xGridWindow)
173
174        # finish
175        self.ui_test.close_doc()
176
177
178# vim: set shiftwidth=4 softtabstop=4 expandtab:
179