@@ -17,13 +17,24 @@
1717# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818#
1919
20−from unity8.shell.emulators import UnityEmulatorBase
20+import logging
2121
22+from unity8.shell import emulators
2223
23−class Dash(UnityEmulatorBase):
24+from autopilot import logging as autopilot_logging
2425
26+
27+logger = logging.getLogger(__name__)
28+
29+
30+class Dash(emulators.UnityEmulatorBase):
2531 """An emulator that understands the Dash."""
2632
33+ def __init__(self, *args):
34+ super(Dash, self).__init__(*args)
35+ self.dash_content_list = self.wait_select_single(
36+ 'QQuickListView', objectName='dashContentList')
37+
2738 def get_home_applications_grid(self):
2839 get_grid = self.get_scope('home').wait_select_single(
2940 "GenericFilterGrid",
@@ -43,9 +54,96 @@ class Dash(UnityEmulatorBase):
4354 return resp_grid.select_single('Tile', text=text)
4455
4556 def get_scope(self, scope_name='home'):
46− dash_content = self.wait_select_single(
47− 'QQuickListView',
48− objectName='dashContentList'
49− )
5057 scope_id = "%s.scope" % scope_name
51− return dash_content.select_single('QQuickLoader', scopeId=scope_id)
58+ return self.dash_content_list.select_single(
59+ 'QQuickLoader', scopeId=scope_id)
60+
61+ @autopilot_logging.log_action(logger.info)
62+ def open_scope(self, scope_name):
63+ """Open a dash scope.
64+
65+ :parameter scope_name: The name of the scope.
66+ :return: The scope.
67+
68+ """
69+ scope_id = "%s.scope" % scope_name
70+ scope_index = self._get_scope_index(scope_id)
71+ if scope_index == self.dash_content_list.currentIndex:
72+ logger.info('The scope is already open.')
73+ return self._get_scope_by_index(scope_index)
74+ else:
75+ return self._open_scope_scrolling(scope_index)
76+
77+ def _get_scope_index(self, scope_id):
78+ scope_loaders = self.dash_content_list.select_many('QQuickLoader')
79+ for index, loader in enumerate(scope_loaders):
80+ try:
81+ if loader.scopeId == scope_id:
82+ return index
83+ except AttributeError:
84+ pass
85+ else:
86+ raise emulators.UnityEmulatorException(
87+ 'No scope found with id {0}'.format(scope_id))
88+
89+ def _get_scope_by_index(self, scope_index):
90+ scope_loaders = self.dash_content_list.select_many('QQuickLoader')
91+ return self._get_scope_from_loader(scope_loaders[scope_index])
92+
93+ def _get_scope_from_loader(self, loader):
94+ if loader.scopeId == 'applications.scope':
95+ return loader.select_single(DashApps)
96+ else:
97+ return loader.select_single(GenericScopeView)
98+
99+ def _open_scope_scrolling(self, scope_index):
100+ scroll = self._get_scroll_direction(scope_index)
101+
102+ while scope_index != self.dash_content_list.currentIndex:
103+ scroll()
104+ scope = self._get_scope_by_index(scope_index)
105+ scope.moving.wait_for(False)
106+ scope.isCurrent.wait_for(True)
107+ return scope
108+
109+ def _get_scroll_direction(self, scope_index):
110+ if scope_index < self.dash_content_list.currentIndex:
111+ return self._scroll_to_left_scope
112+ elif scope_index > self.dash_content_list.currentIndex:
113+ return self._scroll_to_right_scope
114+ else:
115+ raise emulators.UnityEmulatorException('The scope is already open')
116+
117+ @autopilot_logging.log_action(logger.info)
118+ def _scroll_to_left_scope(self):
119+ original_index = self.dash_content_list.currentIndex
120+ # Scroll on the dash bar, because some scopes have contents that can be
121+ # scrolled horizontally.
122+ panel = self.select_single('DashBar').select_single('Panel')
123+ start_x = panel.width / 3
124+ stop_x = panel.width / 3 * 2
125+ start_y = stop_y = panel.globalRect.y + panel.height / 2
126+ self.pointing_device.drag(start_x, start_y, stop_x, stop_y)
127+ self.dash_content_list.currentIndex.wait_for(original_index - 1)
128+ panel.opened.wait_for(False)
129+
130+ @autopilot_logging.log_action(logger.info)
131+ def _scroll_to_right_scope(self):
132+ original_index = self.dash_content_list.currentIndex
133+ # Scroll on the dash bar panel, because some scopes have contents that
134+ # can be scrolled horizontally.
135+ panel = self.select_single('DashBar').select_single('Panel')
136+ start_x = panel.width / 3 * 2
137+ stop_x = panel.width / 3
138+ start_y = stop_y = panel.globalRect.y + panel.height / 2
139+ self.pointing_device.drag(start_x, start_y, stop_x, stop_y)
140+ self.dash_content_list.currentIndex.wait_for(original_index + 1)
141+ panel.opened.wait_for(False)
142+
143+
144+class GenericScopeView(emulators.UnityEmulatorBase):
145+ """Autopilot emulators for generic scopes."""
146+
147+
148+class DashApps(emulators.UnityEmulatorBase):
149+ """Autopilot emulators for the applications scope."""