Debian Codegraph @

tests/autopilot/lomiri_calculator_app/__init__.py

raw symbols SCIP
1 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2 #
3 # Copyright (C) 2017-2019 UBports
4 # Copyright (C) 2013, 2016 Canonical Ltd.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU Lesser General Public License as published by
8 # the Free Software Foundation; version 3.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 """Calculator app autopilot emulators."""
19
20 from time import sleep
21 import lomiriuitoolkit
22
23
24 def click_object(func):
25 """Wrapper which clicks the returned object"""
26 def func_wrapper(self, *args, **kwargs):
27 return self.pointing_device.click_object(func(self, *args, **kwargs))
28
29 return func_wrapper
30
31
32 class CalculatorApp(object):
33 """Autopilot helper object for the calculator application."""
34
35 def __init__(self, app_proxy, test_type):
36 self.app = app_proxy
37 self.test_type = test_type
38 self.main_view = self.app.select_single(MainView)
39
40 @property
41 def pointing_device(self):
42 return self.app.pointing_device
43
44
45 class CalculationHistory(object):
46 """Helper object for the calculation history."""
47
48 def __init__(self, app_proxy):
49 self.app = app_proxy
50
51 def contains(self, entry):
52 found = False
53
54 history = self.get_all_history_entries()
55 for line in history:
56 calculation = self._get_entry(line)
57 if calculation == entry.strip():
58 found = True
59 break
60
61 return found
62
63 def get_all_history_entries(self):
64 return self.app.select_many('QQuickRow', objectName='historyrow')
65
66 def get_history_entry_count(self):
67 return len(self.get_all_history_entries())
68
69 def get_history_entry(self, entry):
70 return self.app.select_single('QQuickText',
71 objectName='result' + str(entry))
72
73 def get_formula_entry(self, entry=0):
74 return self.app.select_single('QQuickText',
75 objectName='formula' + str(entry))
76
77 def get_formula_value(self, entry=0):
78 return self.get_formula_entry(entry).text.strip()
79
80 def get_result_value(self, entry=0):
81 return self.get_history_entry(entry).text.strip()
82
83 def _get_entry(self, line):
84 values = line.select_many('QQuickText')
85 result = ''
86 formula = ''
87 for value in values:
88 if 'result' in value.objectName:
89 result = value.text.strip()
90 elif 'formula' in value.objectName:
91 formula = value.text.replace(" ", "")
92
93 return formula + result
94
95
96 class MainView(lomiriuitoolkit.MainView):
97 """Calculator MainView Autopilot emulator."""
98
99 BUTTONS = {'delete': 'deleteButton', '*': 'multiplyButton',
100 '/': 'divideButton', '.': 'pointButton',
101 '=': 'equalsButton', '-': 'minusButton', '+': 'plusButton',
102 '0': 'zeroButton', '1': 'oneButton', '2': 'twoButton',
103 '3': 'threeButton', '4': 'fourButton', '5': 'fiveButton',
104 '6': 'sixButton', '7': 'sevenButton', '8': 'eightButton',
105 '9': 'nineButton', 'bracket': 'universalBracketButton',
106 'square': 'squareButton', 'cube': 'cubeButton',
107 'power': 'powerButton', 'log': 'logarithmButton',
108 'e': 'eNumberButton', '!': 'factorialNumberButton',
109 'i': 'iButton', 'sin': 'sinusButton', 'cos': 'cosButton'}
110
111 def __init__(self, *args):
112 super(MainView, self).__init__(*args)
113 self.visible.wait_for(True)
114
115 def insert(self, expression):
116 for operand in expression:
117 self.press(operand)
118
119 def press_universal_bracket(self):
120 self.press('bracket')
121
122 def delete(self):
123 self.press('delete')
124
125 def clear(self):
126 self.press_and_hold('delete')
127
128 def press_and_hold(self, button):
129 button = self.wait_select_single('KeyboardButton',
130 objectName=MainView.BUTTONS[button])
131
132 self.pointing_device.move_to_object(button)
133 self.pointing_device.press()
134 button.pressed.wait_for(True)
135 sleep(3)
136 self.pointing_device.release()
137
138 def press(self, button):
139 button = self.wait_select_single('KeyboardButton',
140 objectName=MainView.BUTTONS[button])
141
142 self.pointing_device.move_to_object(button)
143 # we use press and release so we can check the qml property
144 # and ensure the button is pressed long enough to be recieved
145 # and processed correctly
146 # using a larger press_duration for click_object would be inferior
147 # as it would cause longer delays (we are forced to arbitrarily decide
148 # how long to press each time) and potentially fail.
149 # balloons 2015-01-29
150 self.pointing_device.press()
151 # this sleeps represents our minimum press time,
152 # should button.pressed be true without any wait
153 sleep(0.1)
154 button.pressed.wait_for(True)
155 self.pointing_device.release()
156
157 def get_history(self):
158 history = self.wait_select_single('ScrollableView',
159 objectName='scrollableView')
160
161 return CalculationHistory(history)
162
163 def get_result(self):
164 return self.wait_select_single('TextField',
165 objectName='textInputField').displayText
166
167 def get_walkthrough_page(self):
168 return self.wait_select_single('Walkthrough',
169 objectName="walkthroughPage")
170
171 def show_scientific_keyboard(self):
172 self._scientific_keyboard()
173
174 def hide_scientific_keyboard(self):
175 self._scientific_keyboard(enable=False)
176
177 def _scientific_keyboard(self, enable=True):
178 y = (self.globalRect[1] + self.globalRect[3] / 2) + 150
179
180 x_middle = self.globalRect[0] + self.globalRect[2] / 2
181
182 if enable:
183 x_start = x_middle + 150
184 x_stop = x_middle - 150
185 else:
186 x_start = x_middle - 150
187 x_stop = x_middle + 150
188
189 self.pointing_device.drag(x_start, y, x_stop, y)
190
191 # TODO: Find a better implementation to avoid this, if possible.
192 sleep(2)
193
194 def enter_text_via_keyboard(self, textToEnter):
195 """Enter specific formula with keyboard
196
197 :param text: text to enter via keyboard
198
199 """
200 lomiriuitoolkit.get_keyboard().type(textToEnter, delay=0.1)
201
202 def press_and_release_key(self, keyToPress):
203 """Press and release specific key
204
205 :param keyToPress: key to press
206
207 """
208 lomiriuitoolkit.get_keyboard().press_and_release(keyToPress, delay=0.1)
209
210
211 class Page(lomiriuitoolkit.LomiriUIToolkitCustomProxyObjectBase):
212 """Autopilot helper for Pages."""
213 def __init__(self, *args):
214 super(Page, self).__init__(*args)
215
216
217 class Walkthrough(Page):
218 """ Autopilot helper for the walkthrough page """
219 def __init__(self, *args):
220 super(Walkthrough, self).__init__(*args)
221
222 self.visible.wait_for(True)
223
224 @click_object
225 def skip(self):
226 return self.wait_select_single("UCLabel", objectName="skipLabel")