1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 from uitest
.framework
import UITestCase
11 from uitest
.uihelper
.common
import get_state_as_dict
12 from uitest
.uihelper
.common
import type_text
13 from uitest
.uihelper
.common
import select_pos
16 class AutoRedactDialog(UITestCase
):
18 add_target_counter
= 0
20 def getText(self
, xObj
):
21 return get_state_as_dict(xObj
)["Text"]
23 def parseTargetContent(self
, xObj
):
24 return re
.split(r
'\t+', self
.getText(xObj
))
26 def clearTargetsbox(self
, xDialog
):
27 xTargetsListbox
= xDialog
.getChild("targets")
28 xDeleteBtn
= xDialog
.getChild("delete")
30 child_count
= len(xTargetsListbox
.getChildren())
35 for i
in range(0, child_count
):
36 child
= xTargetsListbox
.getChild(0)
37 child
.executeAction("SELECT", tuple())
38 xDeleteBtn
.executeAction("CLICK", tuple())
41 self
.assertEqual(len(xTargetsListbox
.getChildren()), 0)
43 def test_add_target(self
):
44 with self
.ui_test
.create_doc_in_start_center("writer"):
45 with self
.ui_test
.execute_dialog_through_command(".uno:AutoRedactDoc", close_button
="cancel") as xDialog
:
46 xAddBtn
= xDialog
.getChild("add")
48 # Make sure we are starting with an empty targets list
49 self
.clearTargetsbox(xDialog
)
51 # Names need to be distinct
52 # ["target name", "target content"],
54 ["target1", "content1"],
55 ["target2", "content2"],
56 ["target3", "content3"],
59 for i
in range(0, len(targets_list
)):
60 self
.add_target_counter
= i
61 with self
.ui_test
.execute_blocking_action(xAddBtn
.executeAction
, args
=('CLICK', ()), close_button
="close") as dialog
:
62 xNewNameTxt
=dialog
.getChild("name")
63 xNewContentTxt
=dialog
.getChild("content")
64 xTypeList
= dialog
.getChild("type") #0: Text, 1: Regex, 2: Predefined
66 select_pos(xTypeList
, "0") #Text
67 self
.assertEqual(int(get_state_as_dict(xTypeList
)["SelectEntryPos"]), 0)
69 type_text(xNewNameTxt
, targets_list
[self
.add_target_counter
][0])
70 type_text(xNewContentTxt
, targets_list
[self
.add_target_counter
][1])
72 # Make sure targets are added successfully
73 xTargetsListbox
= xDialog
.getChild("targets")
74 targets_box_state_dict
= get_state_as_dict(xTargetsListbox
)
75 self
.assertEqual(int(targets_box_state_dict
["Children"]), len(targets_list
))
77 # Make sure targets are added with correct names and contents
78 for i
in range(0, len(targets_list
)):
79 child
= xTargetsListbox
.getChild(i
)
80 child_text
= self
.parseTargetContent(child
)
81 self
.assertEqual(child_text
[0], targets_list
[i
][0]) #name
82 self
.assertEqual(child_text
[2], targets_list
[i
][1]) #content
84 # Now let's make sure the dialog remembers last state
85 with self
.ui_test
.execute_dialog_through_command(".uno:AutoRedactDoc", close_button
="cancel") as xDialog
:
86 xTargetsListbox
= xDialog
.getChild("targets")
87 targets_box_state_dict
= get_state_as_dict(xTargetsListbox
)
88 self
.assertEqual(int(targets_box_state_dict
["Children"]), len(targets_list
))
90 # Make sure targets are remembered with correct names and contents
91 for i
in range(0, len(targets_list
)):
92 child
= xTargetsListbox
.getChild(i
)
93 child_text
= self
.parseTargetContent(child
)
94 self
.assertEqual(child_text
[0], targets_list
[i
][0]) #name
95 self
.assertEqual(child_text
[2], targets_list
[i
][1]) #content
99 def test_edit_target(self
):
100 with self
.ui_test
.create_doc_in_start_center("writer"):
101 with self
.ui_test
.execute_dialog_through_command(".uno:AutoRedactDoc", close_button
="cancel") as xDialog
:
102 xAddBtn
= xDialog
.getChild("add")
103 xEditBtn
= xDialog
.getChild("edit")
105 # Make sure we are starting with an empty targets list
106 self
.clearTargetsbox(xDialog
)
108 # We first need to add a target so that we can edit it
109 with self
.ui_test
.execute_blocking_action(xAddBtn
.executeAction
, args
=('CLICK', ()), close_button
="close") as dialog
:
110 xNewNameTxt
=dialog
.getChild("name")
111 xNewContentTxt
=dialog
.getChild("content")
112 xTypeList
= dialog
.getChild("type") #0: Text, 1: Regex, 2: Predefined
114 select_pos(xTypeList
, "0") #Text
115 self
.assertEqual(int(get_state_as_dict(xTypeList
)["SelectEntryPos"]), 0)
117 type_text(xNewNameTxt
, "TestTarget")
118 type_text(xNewContentTxt
, "TestContent")
120 # Make sure target is added successfully
121 xTargetsListbox
= xDialog
.getChild("targets")
122 targets_box_state_dict
= get_state_as_dict(xTargetsListbox
)
123 self
.assertEqual(int(targets_box_state_dict
["Children"]), 1)
125 # Select the added target
126 target_entry
= xTargetsListbox
.getChild(0)
127 target_entry
.executeAction("SELECT", tuple())
129 # Now edit the target
130 with self
.ui_test
.execute_blocking_action(xEditBtn
.executeAction
, args
=('CLICK', ()), close_button
="close") as dialog
:
131 xNameTxt
=dialog
.getChild("name")
132 xContentTxt
=dialog
.getChild("content")
134 xNameTxt
.executeAction("CLEAR", tuple())
135 xContentTxt
.executeAction("CLEAR", tuple())
137 type_text(xNameTxt
, "TestTargetEdited")
138 type_text(xContentTxt
, "TestContentEdited")
140 # Make sure target is still there
141 xTargetsListbox
= xDialog
.getChild("targets")
142 targets_box_state_dict
= get_state_as_dict(xTargetsListbox
)
143 self
.assertEqual(int(targets_box_state_dict
["Children"]), 1)
145 # Make sure target has the new values
146 target_entry
= xTargetsListbox
.getChild(0)
147 target_text
= self
.parseTargetContent(target_entry
)
148 self
.assertEqual(target_text
[0], "TestTargetEdited") #name
149 self
.assertEqual(target_text
[2], "TestContentEdited") #content
153 # vim: set shiftwidth=4 softtabstop=4 expandtab: