1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
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/.
7 from uitest
.framework
import UITestCase
8 from libreoffice
.uno
.propertyvalue
import mkPropertyValues
9 from uitest
.uihelper
.common
import get_state_as_dict
10 from uitest
.uihelper
.common
import type_text
11 from uitest
.uihelper
.common
import select_pos
12 from uitest
.uihelper
.common
import select_text
15 from uitest
.debug
import sleep
17 class AutoRedactDialog(UITestCase
):
19 add_target_counter
= 0
21 # Open the Auto Redact Dialog
22 def launch_and_get_autoredact_dialog(self
):
23 self
.ui_test
.execute_dialog_through_command(".uno:AutoRedactDoc")
24 xDialog
= self
.xUITest
.getTopFocusWindow()
25 self
.assertTrue(xDialog
is not None)
28 def getText(self
, xObj
):
29 return get_state_as_dict(xObj
)["Text"]
31 def parseTargetContent(self
, xObj
):
32 return re
.split(r
'\t+', self
.getText(xObj
))
34 def clearTargetsbox(self
, xDialog
):
35 xTargetsListbox
= xDialog
.getChild("targets")
36 xDeleteBtn
= xDialog
.getChild("delete")
38 child_count
= len(xTargetsListbox
.getChildren())
43 for i
in range(0, child_count
):
44 child
= xTargetsListbox
.getChild(0)
45 child
.executeAction("SELECT", tuple())
46 xDeleteBtn
.executeAction("CLICK", tuple())
49 self
.assertEqual(len(xTargetsListbox
.getChildren()), 0)
52 def test_open_AutoRedactDialog_writer(self
):
53 self
.ui_test
.create_doc_in_start_center("writer")
54 xDialog
= self
.launch_and_get_autoredact_dialog()
55 xcancBtn
= xDialog
.getChild("cancel")
56 self
.ui_test
.close_dialog_through_button(xcancBtn
)
57 self
.ui_test
.close_doc()
59 def test_add_target(self
):
60 self
.ui_test
.create_doc_in_start_center("writer")
61 xDialog
= self
.launch_and_get_autoredact_dialog()
62 xAddBtn
= xDialog
.getChild("add")
64 # Make sure we are starting with an empty targets list
65 self
.clearTargetsbox(xDialog
)
67 # Names need to be distinct
68 # ["target name", "target content"],
70 ["target1", "content1"],
71 ["target2", "content2"],
72 ["target3", "content3"],
75 def handle_add_dlg(dialog
): #handle add target dialog - need special handling
76 xNewNameTxt
=dialog
.getChild("name")
77 xNewContentTxt
=dialog
.getChild("content")
78 xOKBtn
= dialog
.getChild("close")
79 xTypeList
= dialog
.getChild("type") #0: Text, 1: Regex, 2: Predefined
81 select_pos(xTypeList
, 0) #Text
82 self
.assertEqual(int(get_state_as_dict(xTypeList
)["SelectEntryPos"]), 0)
84 type_text(xNewNameTxt
, targets_list
[self
.add_target_counter
][0])
85 type_text(xNewContentTxt
, targets_list
[self
.add_target_counter
][1])
87 self
.ui_test
.close_dialog_through_button(xOKBtn
)
89 for i
in range(0, len(targets_list
)):
90 self
.add_target_counter
= i
91 self
.ui_test
.execute_blocking_action(xAddBtn
.executeAction
, args
=('CLICK', ()),
92 dialog_handler
=handle_add_dlg
) #close add target dialog with OK button
94 # Make sure targets are added successfully
95 xTargetsListbox
= xDialog
.getChild("targets")
96 targets_box_state_dict
= get_state_as_dict(xTargetsListbox
)
97 self
.assertEqual(int(targets_box_state_dict
["Children"]), len(targets_list
))
99 # Make sure targets are added with correct names and contents
100 for i
in range(0, len(targets_list
)):
101 child
= xTargetsListbox
.getChild(i
)
102 child_text
= self
.parseTargetContent(child
)
103 self
.assertEqual(child_text
[0], targets_list
[i
][0]) #name
104 self
.assertEqual(child_text
[2], targets_list
[i
][1]) #content
106 xcancBtn
= xDialog
.getChild("cancel")
107 self
.ui_test
.close_dialog_through_button(xcancBtn
)
109 # Now let's make sure the dialog remembers last state
110 xDialog
= self
.launch_and_get_autoredact_dialog()
111 xTargetsListbox
= xDialog
.getChild("targets")
112 targets_box_state_dict
= get_state_as_dict(xTargetsListbox
)
113 self
.assertEqual(int(targets_box_state_dict
["Children"]), len(targets_list
))
115 # Make sure targets are remembered with correct names and contents
116 for i
in range(0, len(targets_list
)):
117 child
= xTargetsListbox
.getChild(i
)
118 child_text
= self
.parseTargetContent(child
)
119 self
.assertEqual(child_text
[0], targets_list
[i
][0]) #name
120 self
.assertEqual(child_text
[2], targets_list
[i
][1]) #content
122 xcancBtn
= xDialog
.getChild("cancel")
123 self
.ui_test
.close_dialog_through_button(xcancBtn
)
125 self
.ui_test
.close_doc()
128 def test_edit_target(self
):
129 self
.ui_test
.create_doc_in_start_center("writer")
130 xDialog
= self
.launch_and_get_autoredact_dialog()
131 xAddBtn
= xDialog
.getChild("add")
132 xEditBtn
= xDialog
.getChild("edit")
134 # Make sure we are starting with an empty targets list
135 self
.clearTargetsbox(xDialog
)
137 # We first need to add a target so that we can edit it
138 def handle_add_dlg(dialog
): #handle add target dialog - need special handling
139 xNewNameTxt
=dialog
.getChild("name")
140 xNewContentTxt
=dialog
.getChild("content")
141 xOKBtn
= dialog
.getChild("close")
142 xTypeList
= dialog
.getChild("type") #0: Text, 1: Regex, 2: Predefined
144 select_pos(xTypeList
, 0) #Text
145 self
.assertEqual(int(get_state_as_dict(xTypeList
)["SelectEntryPos"]), 0)
147 type_text(xNewNameTxt
, "TestTarget")
148 type_text(xNewContentTxt
, "TestContent")
150 self
.ui_test
.close_dialog_through_button(xOKBtn
)
152 self
.ui_test
.execute_blocking_action(xAddBtn
.executeAction
, args
=('CLICK', ()),
153 dialog_handler
=handle_add_dlg
) #close add target dialog with OK button
155 # Make sure target is added successfully
156 xTargetsListbox
= xDialog
.getChild("targets")
157 targets_box_state_dict
= get_state_as_dict(xTargetsListbox
)
158 self
.assertEqual(int(targets_box_state_dict
["Children"]), 1)
160 # Select the added target
161 target_entry
= xTargetsListbox
.getChild(0)
162 target_entry
.executeAction("SELECT", tuple())
164 # Now edit the target
165 def handle_edit_dlg(dialog
): #handle add target dialog - need special handling
166 xNameTxt
=dialog
.getChild("name")
167 xContentTxt
=dialog
.getChild("content")
168 xOKBtn
= dialog
.getChild("close")
170 xNameTxt
.executeAction("CLEAR", tuple())
171 xContentTxt
.executeAction("CLEAR", tuple())
173 type_text(xNameTxt
, "TestTargetEdited")
174 type_text(xContentTxt
, "TestContentEdited")
176 self
.ui_test
.close_dialog_through_button(xOKBtn
)
178 self
.ui_test
.execute_blocking_action(xEditBtn
.executeAction
, args
=('CLICK', ()),
179 dialog_handler
=handle_edit_dlg
) #close add target dialog with OK button
181 # Make sure target is still there
182 xTargetsListbox
= xDialog
.getChild("targets")
183 targets_box_state_dict
= get_state_as_dict(xTargetsListbox
)
184 self
.assertEqual(int(targets_box_state_dict
["Children"]), 1)
186 # Make sure target has the new values
187 target_entry
= xTargetsListbox
.getChild(0)
188 target_text
= self
.parseTargetContent(target_entry
)
189 self
.assertEqual(target_text
[0], "TestTargetEdited") #name
190 self
.assertEqual(target_text
[2], "TestContentEdited") #content
192 xcancBtn
= xDialog
.getChild("cancel")
193 self
.ui_test
.close_dialog_through_button(xcancBtn
)
195 self
.ui_test
.close_doc()
198 # vim: set shiftwidth=4 softtabstop=4 expandtab: