Debian Codegraph @

tests/autopilot/lomiriuitoolkit/_custom_proxy_objects/_mainview.py

raw
1 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2 #
3 # Copyright (C) 2012, 2013, 2014, 2015 Canonical Ltd.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation; version 3.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 """Lomiri UI Toolkit Autopilot custom proxy objects."""
18
19 import logging
20
21 from autopilot import logging as autopilot_logging
22 from autopilot.introspection import dbus
23 from autopilot import introspection
24
25 from lomiriuitoolkit._custom_proxy_objects import (
26 _common,
27 popups,
28 _tabs,
29 _toolbar,
30 )
31
32
33 _NO_TABS_ERROR = 'The MainView has no Tabs.'
34 _NO_TOOLBAR_ERROR = 'The MainView has no Toolbar.'
35
36
37 logger = logging.getLogger(__name__)
38
39
40 class MainView(_common.LomiriUIToolkitCustomProxyObjectBase):
41 """MainView Autopilot custom proxy object."""
42
43 def __init__(self, *args):
44 super().__init__(*args)
45 if not hasattr(self, 'useDeprecatedToolbar'):
46 self.useDeprecatedToolbar = None
47
48 @classmethod
49 def validate_dbus_object(cls, path, state):
50 if super().validate_dbus_object(path, state):
51 # This covers MainView 0.1/1.0/1.1 and possible components
52 # derived from MainView (e.g. "CustomMainView") that have
53 # their own CPO.
54 # Using objectName for selecting a MainView is recommneded.
55 return True
56
57 name = introspection.get_classname_from_path(path)
58 if name == b'MainView12':
59 # MainView 1.2. Must be selected using objectName.
60 return True
61
62 return False
63
64 def get_header(self):
65 """Return the AppHeader custom proxy object of the MainView."""
66 try:
67 return self.select_single(objectName='MainView_Header')
68 except dbus.StateNotFoundError:
69 raise _common.ToolkitException('The main view has no header.')
70
71 def get_toolbar(self):
72 """Return the Toolbar custom proxy object of the MainView.
73
74 :raise ToolkitException: If the main view has no toolbar.
75
76 """
77 try:
78 return self.select_single(_toolbar.Toolbar)
79 except dbus.StateNotFoundError:
80 raise _common.ToolkitException(_NO_TOOLBAR_ERROR)
81
82 @autopilot_logging.log_action(logger.info)
83 def open_toolbar(self):
84 """Open the toolbar if it is not already opened.
85
86 :return: The toolbar.
87 :raise ToolkitException: If the main view has no toolbar.
88
89 """
90 return self.get_toolbar().open()
91
92 @autopilot_logging.log_action(logger.info)
93 def close_toolbar(self):
94 """Close the toolbar if it is opened.
95
96 :raise ToolkitException: If the main view has no toolbar.
97
98 """
99 self.get_toolbar().close()
100
101 def get_tabs(self):
102 """Return the Tabs custom proxy object of the MainView.
103
104 :raise ToolkitException: If the main view has no tabs.
105
106 """
107 try:
108 return self.select_single(_tabs.Tabs)
109 except dbus.StateNotFoundError:
110 raise _common.ToolkitException(_NO_TABS_ERROR)
111
112 @autopilot_logging.log_action(logger.info)
113 def switch_to_next_tab(self):
114 """Open the next tab.
115
116 :return: The newly opened tab.
117
118 """
119 logger.debug('Switch to next tab.')
120 self.get_header().switch_to_next_tab()
121 current_tab = self.get_tabs().get_current_tab()
122 current_tab.visible.wait_for(True)
123 return current_tab
124
125 @autopilot_logging.log_action(logger.info)
126 def switch_to_tab_by_index(self, index):
127 """Open a tab.
128
129 :parameter index: The index of the tab to open.
130 :return: The newly opened tab.
131 :raise ToolkitException: If the tab index
132 is out of range.
133
134 """
135 if self.useDeprecatedToolbar:
136 return self._switch_to_tab_in_deprecated_tabbar_by_index(index)
137 else:
138 return self._switch_to_tab_in_drawer_by_index(index)
139
140 def _switch_to_tab_in_deprecated_tabbar_by_index(self, index):
141 tabs = self.get_tabs()
142 number_of_tabs = tabs.get_number_of_tabs()
143 if index >= number_of_tabs:
144 raise _common.ToolkitException('Tab index out of range.')
145
146 current_tab = tabs.get_current_tab()
147 number_of_switches = 0
148 while not tabs.selectedTabIndex == index:
149 logger.debug(
150 'Current tab index: {0}.'.format(tabs.selectedTabIndex))
151 if number_of_switches >= number_of_tabs - 1:
152 # This prevents a loop. But if this error is ever raised, it's
153 # likely there's a bug on the helper or on the QML Tab.
154 raise _common.ToolkitException(
155 'The tab with index {0} was not selected.'.format(index))
156 current_tab = self.switch_to_next_tab()
157 number_of_switches += 1
158 return current_tab
159
160 def _switch_to_tab_in_drawer_by_index(self, index):
161 tabs = self.get_tabs()
162 number_of_tabs = tabs.get_number_of_tabs()
163 if index >= number_of_tabs:
164 raise _common.ToolkitException('Tab index out of range.')
165
166 if index != tabs.selectedTabIndex:
167 self.get_header().switch_to_tab_by_index(index)
168 current_tab = tabs.get_current_tab()
169 return current_tab
170
171 @autopilot_logging.log_action(logger.info)
172 def switch_to_previous_tab(self):
173 """Open the previous tab.
174
175 :return: The newly opened tab.
176
177 """
178 tabs = self.get_tabs()
179 if tabs.selectedTabIndex == 0:
180 previous_tab_index = tabs.get_number_of_tabs() - 1
181 else:
182 previous_tab_index = tabs.selectedTabIndex - 1
183 return self.switch_to_tab_by_index(previous_tab_index)
184
185 @autopilot_logging.log_action(logger.info)
186 def switch_to_tab(self, object_name):
187 """Open a tab.
188
189 :parameter object_name: The QML objectName property of the tab.
190 :return: The newly opened tab.
191 :raise ToolkitException: If there is no tab with that object
192 name.
193
194 """
195 tabs = self.get_tabs()
196 for index, tab in enumerate(tabs.select_many('Tab')):
197 if tab.objectName == object_name:
198 return self.switch_to_tab_by_index(tab.index)
199 raise _common.ToolkitException(
200 'Tab with objectName "{0}" not found.'.format(object_name))
201
202 def get_action_selection_popover(self, object_name):
203 """Return an ActionSelectionPopover custom proxy object.
204
205 :parameter object_name: The QML objectName property of the popover.
206
207 """
208 return self.wait_select_single(
209 popups.ActionSelectionPopover, objectName=object_name)
210
211 def get_text_input_context_menu(self, object_name):
212 """Return a TextInputContextMenu emulator.
213
214 :parameter object_name: The QML objectName property of the popover.
215
216 """
217 return self.wait_select_single(
218 popups.TextInputPopover, objectName=object_name)
219
220 @autopilot_logging.log_action(logger.info)
221 def go_back(self):
222 """Go to the previous page."""
223 if self.useDeprecatedToolbar:
224 toolbar = self.open_toolbar()
225 toolbar.click_back_button()
226 else:
227 self.get_header().click_back_button()
228
229 @autopilot_logging.log_action(logger.info)
230 def click_action_button(self, action_object_name):
231 """Click the specified button.
232
233 :parameter action_object_name: the objectName of the action to trigger.
234 :raise ToolkitException: The requested button is not available.
235
236 """
237 if self.useDeprecatedToolbar:
238 raise _common.ToolkitException(
239 "App is using deprecated toolbar instead of new header")
240 else:
241 header = self.open_header()
242 header.click_action_button(action_object_name)