| 1 |
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- |
| 2 |
# |
| 3 |
# Copyright (C) 2012-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 |
import logging |
| 18 |
|
| 19 |
from autopilot import logging as autopilot_logging |
| 20 |
from autopilot.introspection import dbus |
| 21 |
|
| 22 |
from lomiriuitoolkit._custom_proxy_objects import ( |
| 23 |
_common, |
| 24 |
_tabbar |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
_NO_TABS_ERROR = 'The MainView has no Tabs.' |
| 29 |
|
| 30 |
|
| 31 |
logger = logging.getLogger(__name__) |
| 32 |
|
| 33 |
|
| 34 |
class AppHeader(_common.LomiriUIToolkitCustomProxyObjectBase): |
| 35 |
"""AppHeader Autopilot custom proxy object.""" |
| 36 |
|
| 37 |
def __init__(self, *args): |
| 38 |
super().__init__(*args) |
| 39 |
if not hasattr(self, 'useDeprecatedToolbar'): |
| 40 |
self.useDeprecatedToolbar = None |
| 41 |
|
| 42 |
def ensure_visible(self): |
| 43 |
if not self._is_visible(): |
| 44 |
self._show() |
| 45 |
|
| 46 |
def _is_visible(self): |
| 47 |
return self.y == 0 |
| 48 |
|
| 49 |
def _show(self): |
| 50 |
# FIXME This will fail if the header is not linked to a flickable that |
| 51 |
# fills the main view. The header has a flickable property but it |
| 52 |
# can't be read by autopilot. See bug http://pad.lv/1318829 |
| 53 |
top_container = self._get_top_container() |
| 54 |
# Make the drag range be a multiple of the drag "rate" value. |
| 55 |
# Workarounds https://bugs.launchpad.net/mir/+bug/1399690 |
| 56 |
rate = 10 |
| 57 |
start_x = stop_x = self.globalRect.x + self.globalRect.width // 2 |
| 58 |
start_y = top_container.globalRect.y + 5 |
| 59 |
stop_y = start_y + self.globalRect.height // rate * rate |
| 60 |
self.pointing_device.drag(start_x, start_y, stop_x, stop_y, rate) |
| 61 |
self.y.wait_for(0) |
| 62 |
|
| 63 |
def wait_for_animation(self): |
| 64 |
try: |
| 65 |
style = self.select_single(objectName='PageHeadStyle') |
| 66 |
style.animating.wait_for(False) |
| 67 |
except dbus.StateNotFoundError: |
| 68 |
# AppHeader is not using the new PageHeadStyle, |
| 69 |
# so no need to wait. |
| 70 |
return |
| 71 |
|
| 72 |
# Wait showing/hiding animation of the header. |
| 73 |
self.moving.wait_for(False) |
| 74 |
|
| 75 |
@autopilot_logging.log_action(logger.info) |
| 76 |
def switch_to_section_by_index(self, index): |
| 77 |
"""Select a section in the header divider |
| 78 |
|
| 79 |
:parameter index: The index of the section to select |
| 80 |
:raise ToolkitEmulatorException: If the selection index is out of |
| 81 |
range or useDeprecatedToolbar is set. |
| 82 |
|
| 83 |
""" |
| 84 |
self.ensure_visible() |
| 85 |
|
| 86 |
if self.useDeprecatedToolbar: |
| 87 |
raise _common.ToolkitException('Old header has no sections') |
| 88 |
|
| 89 |
self.wait_for_animation() |
| 90 |
try: |
| 91 |
# Lomiri.Components >=1.3 |
| 92 |
sections = self.select_single( |
| 93 |
'Sections', objectName='headerSectionsItem') |
| 94 |
sections.click_section_button(index) |
| 95 |
except dbus.StateNotFoundError: |
| 96 |
# Lomiri.Components < 1.3, has no headerSectionsItem. |
| 97 |
try: |
| 98 |
object_name = "section_button_" + str(index) |
| 99 |
button = self.select_single(objectName=object_name) |
| 100 |
except dbus.StateNotFoundError: |
| 101 |
raise _common.ToolkitException( |
| 102 |
'Button for section with given index not found') |
| 103 |
|
| 104 |
self.pointing_device.click_object(button) |
| 105 |
|
| 106 |
def get_selected_section_index(self): |
| 107 |
if self.useDeprecatedToolbar: |
| 108 |
raise _common.ToolkitException('Old header has no sections') |
| 109 |
|
| 110 |
self.wait_for_animation() |
| 111 |
try: |
| 112 |
# Lomiri.Components >=1.3 |
| 113 |
sections = self.select_single( |
| 114 |
'Sections', objectName='headerSectionsItem') |
| 115 |
return sections.selectedIndex |
| 116 |
except dbus.StateNotFoundError: |
| 117 |
# Lomiri.Components < 1.3, has no headerSectionsItem. |
| 118 |
sectionsProperties = self.select_single( |
| 119 |
'QQuickItem', objectName='sectionsProperties') |
| 120 |
return sectionsProperties.selectedIndex |
| 121 |
|
| 122 |
def click_back_button(self): |
| 123 |
self.ensure_visible() |
| 124 |
|
| 125 |
if self.useDeprecatedToolbar: |
| 126 |
raise _common.ToolkitException('Old header has no back button') |
| 127 |
try: |
| 128 |
self.wait_for_animation() |
| 129 |
back_button = self.select_single(objectName='backButton') |
| 130 |
except dbus.StateNotFoundError: |
| 131 |
raise _common.ToolkitException('Missing back button in header') |
| 132 |
if not back_button.visible: |
| 133 |
raise _common.ToolkitException('Back button in header not visible') |
| 134 |
self.pointing_device.click_object(back_button) |
| 135 |
self.wait_for_animation() |
| 136 |
|
| 137 |
def click_custom_back_button(self): |
| 138 |
self.ensure_visible() |
| 139 |
|
| 140 |
if self.useDeprecatedToolbar: |
| 141 |
raise _common.ToolkitException( |
| 142 |
'Old header has no custom back button') |
| 143 |
try: |
| 144 |
self.wait_for_animation() |
| 145 |
custom_back_button = self.select_single( |
| 146 |
objectName='customBackButton') |
| 147 |
except dbus.StateNotFoundError: |
| 148 |
raise _common.ToolkitException( |
| 149 |
'Missing custom back button in header') |
| 150 |
if not custom_back_button.visible: |
| 151 |
raise _common.ToolkitException( |
| 152 |
'Custom back button in header not visible') |
| 153 |
self.pointing_device.click_object(custom_back_button) |
| 154 |
self.wait_for_animation() |
| 155 |
|
| 156 |
def _get_animating(self): |
| 157 |
if self.useDeprecatedToolbar: |
| 158 |
tab_bar_style = self.select_single('TabBarStyle') |
| 159 |
return tab_bar_style.animating |
| 160 |
else: |
| 161 |
return False |
| 162 |
|
| 163 |
@autopilot_logging.log_action(logger.info) |
| 164 |
def switch_to_next_tab(self): |
| 165 |
"""Open the next tab. |
| 166 |
|
| 167 |
:raise ToolkitException: If the main view has no tabs. |
| 168 |
|
| 169 |
""" |
| 170 |
self.ensure_visible() |
| 171 |
|
| 172 |
if self.useDeprecatedToolbar: |
| 173 |
self._switch_to_next_tab_in_deprecated_tabbar() |
| 174 |
else: |
| 175 |
self._switch_to_next_tab_in_drawer() |
| 176 |
|
| 177 |
def _switch_to_next_tab_in_deprecated_tabbar(self): |
| 178 |
try: |
| 179 |
tab_bar = self.select_single(_tabbar.TabBar) |
| 180 |
except dbus.StateNotFoundError: |
| 181 |
raise _common.ToolkitException(_NO_TABS_ERROR) |
| 182 |
tab_bar.switch_to_next_tab() |
| 183 |
self._get_animating().wait_for(False) |
| 184 |
|
| 185 |
def _switch_to_next_tab_in_drawer(self): |
| 186 |
self.wait_for_animation() |
| 187 |
tabs_model_properties = self.select_single( |
| 188 |
'QQuickItem', objectName='tabsModelProperties') |
| 189 |
if tabs_model_properties.count == 0: |
| 190 |
raise _common.ToolkitException(_NO_TABS_ERROR) |
| 191 |
next_tab_index =\ |
| 192 |
(tabs_model_properties.selectedIndex + 1)\ |
| 193 |
% tabs_model_properties.count |
| 194 |
self._switch_to_tab_in_drawer_by_index(next_tab_index) |
| 195 |
|
| 196 |
@autopilot_logging.log_action(logger.info) |
| 197 |
def switch_to_tab_by_index(self, index): |
| 198 |
"""Open a tab. This only supports the new tabs in the header |
| 199 |
|
| 200 |
:parameter index: The index of the tab to open. |
| 201 |
:raise ToolkitException: If the tab index is out |
| 202 |
of range or useDeprecatedToolbar is set. |
| 203 |
|
| 204 |
""" |
| 205 |
self.ensure_visible() |
| 206 |
|
| 207 |
if self.useDeprecatedToolbar: |
| 208 |
raise _common.ToolkitException( |
| 209 |
"AppHeader.switch_to_tab_by_index only works with new header") |
| 210 |
else: |
| 211 |
self._switch_to_tab_in_drawer_by_index(index) |
| 212 |
|
| 213 |
def _switch_to_tab_in_drawer_by_index(self, index): |
| 214 |
self.wait_for_animation() |
| 215 |
try: |
| 216 |
tabs_drawer_button = self.select_single(objectName='tabsButton') |
| 217 |
except dbus.StateNotFoundError: |
| 218 |
raise _common.ToolkitException(_NO_TABS_ERROR) |
| 219 |
self.pointing_device.click_object(tabs_drawer_button) |
| 220 |
|
| 221 |
tabs_model_properties = self.select_single( |
| 222 |
'QQuickItem', objectName='tabsModelProperties') |
| 223 |
|
| 224 |
if tabs_model_properties.selectedIndex == index: |
| 225 |
return |
| 226 |
|
| 227 |
popover = self.get_root_instance().select_single( |
| 228 |
objectName='tabsPopover') |
| 229 |
|
| 230 |
try: |
| 231 |
# 1.3, using ActionSelectionPopover |
| 232 |
action_name = 'select_tab_' + str(index) |
| 233 |
popover.click_action_button(action_name) |
| 234 |
except _common.ToolkitException: |
| 235 |
try: |
| 236 |
# < 1.3 using custom popover |
| 237 |
tab_button = self.get_root_instance().select_single( |
| 238 |
objectName='tabButton' + str(index)) |
| 239 |
self.pointing_device.click_object(tab_button) |
| 240 |
except dbus.StateNotFoundError: |
| 241 |
raise _common.ToolkitException( |
| 242 |
"Tab button {0} not found.".format(index)) |
| 243 |
|
| 244 |
self.wait_for_animation() |
| 245 |
|
| 246 |
def click_action_button(self, action_object_name): |
| 247 |
"""Click an action button of the header. |
| 248 |
|
| 249 |
:parameter object_name: The QML objectName property of the action |
| 250 |
:raise ToolkitException: If there is no action button with that object |
| 251 |
name. |
| 252 |
|
| 253 |
""" |
| 254 |
self.ensure_visible() |
| 255 |
|
| 256 |
try: |
| 257 |
# for Lomiri.Components 1.3 |
| 258 |
actionbar = self.select_single( |
| 259 |
'ActionBar', objectName='headerActionBar') |
| 260 |
actionbar.click_action_button(action_object_name) |
| 261 |
except dbus.StateNotFoundError: |
| 262 |
# for Lomiri.Components < 1.3 |
| 263 |
button = self._get_action_button(action_object_name) |
| 264 |
# In an animating header, the button is disabled until the header |
| 265 |
# animation is done. Wait for that: |
| 266 |
button.enabled.wait_for(True) |
| 267 |
self.pointing_device.click_object(button) |
| 268 |
except _common.ToolkitException: |
| 269 |
# Catch 'Button not found in ActionBar or overflow' exception |
| 270 |
raise _common.ToolkitException( |
| 271 |
'Button not found in header or overflow') |
| 272 |
|
| 273 |
self.wait_for_animation() |
| 274 |
|
| 275 |
def _get_action_button(self, action_object_name): |
| 276 |
self.wait_for_animation() |
| 277 |
try: |
| 278 |
object_name = action_object_name + "_header_button" |
| 279 |
button = self.select_single(objectName=object_name) |
| 280 |
except dbus.StateNotFoundError: |
| 281 |
# the button is not in the header, but it may be in the overflow |
| 282 |
try: |
| 283 |
button = self._get_action_button_in_overflow( |
| 284 |
action_object_name) |
| 285 |
except dbus.StateNotFoundError: |
| 286 |
raise _common.ToolkitException( |
| 287 |
'Button not found in header or overflow') |
| 288 |
|
| 289 |
return button |
| 290 |
|
| 291 |
def _get_action_button_in_overflow(self, action_object_name): |
| 292 |
actions_overflow_button = self.select_single( |
| 293 |
objectName='actions_overflow_button') |
| 294 |
|
| 295 |
if not actions_overflow_button.visible: |
| 296 |
raise _common.ToolkitException('No actions in overflow') |
| 297 |
|
| 298 |
# open the popover |
| 299 |
self.pointing_device.click_object(actions_overflow_button) |
| 300 |
object_name = action_object_name + "_header_overflow_button" |
| 301 |
|
| 302 |
# the popover is not a child of the header, so use the root object |
| 303 |
# to find the requested button |
| 304 |
return self.get_root_instance().select_single(objectName=object_name) |
| 305 |
|
| 306 |
|
| 307 |
class Header(AppHeader): |
| 308 |
"""Autopilot helper for the deprecated Header.""" |
| 309 |
|
| 310 |
def __init__(self, *args): |
| 311 |
logger.warning( |
| 312 |
'Header is an internal QML component of Lomiri.Components and ' |
| 313 |
'its API may change or be removed at any moment. Please use ' |
| 314 |
'MainView and Page instead.') |
| 315 |
super().__init__(*args) |