~music-app-dev/music-app/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
# Copyright 2013, 2014, 2015, 2016 Canonical
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.

"""music-app tests and emulators - top level package."""
from ubuntuuitoolkit import (
    MainView, UbuntuUIToolkitCustomProxyObjectBase, UCListItem
)
from autopilot.introspection import dbus


class MusicAppException(Exception):
    """Exception raised when there's an error in the Music App."""


def click_object(func):
    """Wrapper which clicks the returned object"""
    def func_wrapper(self, *args, **kwargs):
        return self.pointing_device.click_object(func(self, *args, **kwargs))

    return func_wrapper


def ensure_now_playing_full(func):
    """Wrapper which ensures the now playing is full before clicking"""
    def func_wrapper(self, *args, **kwargs):
        if self.isListView:
            self.click_full_view()

        return func(self, *args, **kwargs)

    return func_wrapper


def ensure_now_playing_list(func):
    """Wrapper which ensures the now playing is list before clicking"""
    def func_wrapper(self, *args, **kwargs):
        if not self.isListView:
            self.click_queue_view()

        return func(self, *args, **kwargs)

    return func_wrapper


class MusicApp(object):
    """Autopilot helper object for the Music application."""

    def __init__(self, app_proxy):
        self.app = app_proxy

        # Use only objectName due to bug 1350532 as it is MainView12
        self.main_view = self.app.wait_select_single(
            objectName="musicMainView")

    def get_add_to_playlist_page(self):
        return self.app.wait_select_single(AddToPlaylist,
                                           objectName="addToPlaylistPage")

    def get_albums_page(self):
        self.main_view.switch_to_tab_wrapper('albumsTabAction')

        return self.main_view.wait_select_single(
            Albums, objectName='albumsPage')

    def get_artist_view_page(self):
        return self.main_view.wait_select_single(
            ArtistView, objectName='artistViewPage')

    def get_artists_page(self):
        self.main_view.switch_to_tab_wrapper('artistsTabAction')

        return self.main_view.wait_select_single(
            Artists, objectName='artistsPage')

    def get_new_playlist_dialog(self):
        return self.main_view.wait_select_single(
            Dialog, objectName="dialogNewPlaylist")

    def get_delete_playlist_dialog(self):
        return self.main_view.wait_select_single(
            RemovePlaylistDialog, objectName="dialogRemovePlaylist")

    def get_now_playing_page(self):
        return self.app.wait_select_single(NowPlaying,
                                           objectName="nowPlayingPage")

    def get_playlists_page(self):
        self.main_view.switch_to_tab_wrapper('playlistsTabAction')

        return self.main_view.wait_select_single(
            Playlists, objectName='playlistsPage')

    def get_queue_count(self):
        return self.player.count

    def get_songs_view(self):
        return self.app.wait_select_single(SongsView, objectName="songsPage")

    def get_toolbar(self):
        return self.app.wait_select_single(
            MusicToolbar, objectName="musicToolbarObject", visible=True,
        )

    def get_songs_page(self):
        self.main_view.switch_to_tab_wrapper('songsTabAction')

        return self.main_view.wait_select_single(
            Songs, objectName='songsPage')

    def get_walkthrough_page(self):
        return self.main_view.wait_select_single(Walkthrough,
                                                 objectName="walkthroughPage")

    @property
    def loaded(self):
        return (not self.main_view.select_single("ActivityIndicator",
                objectName="LoadingSpinner").running and
                self.main_view.select_single("*", "allSongsModel").populated)

    @property
    def player(self):
        # Get new player each time as data changes (eg currentMeta)
        return self.app.select_single(Player, objectName='player')

    def populate_queue(self):
        tracksPage = self.get_songs_page()  # switch to track tab

        # get and click to play first track
        track = tracksPage.get_track(0)
        self.app.pointing_device.click_object(track)

        tracksPage.visible.wait_for(False)  # wait until page has hidden

        # TODO: when using bottom edge wait for .isReady on tracksPage

        # wait for now playing page to be visible
        self.get_now_playing_page().visible.wait_for(True)

    def get_library_empty_state_page(self):
        return self.app.wait_select_single(LibraryEmptyState,
                                           objectName="emptyLibrary")


class LibraryEmptyState(UbuntuUIToolkitCustomProxyObjectBase):
    """Autopilot helper for LibraryEmptyState"""
    def __init__(self, *args):
        super(LibraryEmptyState, self).__init__(*args)


class Page(UbuntuUIToolkitCustomProxyObjectBase):
    """Autopilot helper for Pages."""
    def __init__(self, *args):
        super(Page, self).__init__(*args)
        # XXX we need a better way to keep reference to the main view.
        # --elopio - 2014-01-31

        # Use only objectName due to bug 1350532 as it is MainView12
        self.main_view = self.get_root_instance().select_single(
            objectName="musicMainView")


class MusicPage(Page):
    def __init__(self, *args):
        super(MusicPage, self).__init__(*args)


class Walkthrough(Page):
    """ Autopilot helper for the walkthrough page """
    def __init__(self, *args):
        super(Walkthrough, self).__init__(*args)

        self.visible.wait_for(True)

    @click_object
    def skip(self):
        return self.wait_select_single("UCLabel", objectName="skipLabel")


class Albums(MusicPage):
    """ Autopilot helper for the albums page """
    def __init__(self, *args):
        super(Albums, self).__init__(*args)

        self.visible.wait_for(True)

    @click_object
    def click_album(self, i):
        return (self.wait_select_single("*",
                objectName="albumsPageGridItem" + str(i)))


class Artists(MusicPage):
    """ Autopilot helper for the artists page """
    def __init__(self, *args):
        super(Artists, self).__init__(*args)

        self.visible.wait_for(True)

    @click_object
    def click_artist(self, i):
        return (self.wait_select_single("Card",
                objectName="artistsPageGridItem" + str(i)))


class Songs(MusicPage):
    """ Autopilot helper for the tracks page """
    def __init__(self, *args):
        super(Songs, self).__init__(*args)

        self.visible.wait_for(True)

    def get_track(self, i):
        return (self.wait_select_single(MusicListItem,
                objectName="tracksPageListItem" + str(i)))


class Playlists(MusicPage):
    """ Autopilot helper for the playlists page """
    def __init__(self, *args):
        super(Playlists, self).__init__(*args)

        self.visible.wait_for(True)

    def get_count(self):
        return self.wait_select_single(
            "MusicGridView", objectName="playlistsGridView").count

    def click_new_playlist_action(self):
        self.wait_select_single(
            "ActionBar", objectName="playlistTrailingActionBar", visible=True,
        ).click_action_button("newPlaylistButton")

    @click_object
    def click_playlist(self, i):
        return self.get_playlist(i)

    def get_playlist(self, i):
        return (self.wait_select_single("Card",
                objectName="playlistCardItem" + str(i)))


class AddToPlaylist(MusicPage):
    """ Autopilot helper for add to playlist page """
    def __init__(self, *args):
        super(AddToPlaylist, self).__init__(*args)

        self.visible.wait_for(True)

    def click_new_playlist_action(self):
        self.wait_select_single(
            "ActionBar", objectName="playlistTrailingActionBar", visible=True,
        ).click_action_button("newPlaylistButton")

    @click_object
    def click_playlist(self, i):
        return self.get_playlist(i)

    def get_count(self):  # careful not to conflict until Page11 is fixed
        return self.wait_select_single(
            "MusicGridView", objectName="addToPlaylistGridView").count

    def get_playlist(self, i):
        return (self.wait_select_single("Card",
                objectName="addToPlaylistCardItem" + str(i)))


class Player(UbuntuUIToolkitCustomProxyObjectBase):
    """Autopilot helper for Player"""

    @property
    def currentMeta(self):
        return self.select_single("*", objectName="currentMeta")


class NowPlaying(MusicPage):
    """ Autopilot helper for now playing page """
    def __init__(self, *args):
        super(NowPlaying, self).__init__(*args)

        self.visible.wait_for(True)

    @ensure_now_playing_full
    @click_object
    def click_forward_button(self):
        return self.wait_select_single("*", objectName="forwardShape")

    def click_full_view(self):
        self.get_sections().click_section_button(0)

    @ensure_now_playing_full
    @click_object
    def click_play_button(self):
        return self.wait_select_single("*", objectName="playShape")

    @ensure_now_playing_full
    @click_object
    def click_previous_button(self):
        return self.wait_select_single("*", objectName="previousShape")

    def click_queue_view(self):
        self.get_sections().click_section_button(1)

    @ensure_now_playing_full
    @click_object
    def click_repeat_button(self):
        return self.wait_select_single("*", objectName="repeatShape")

    @ensure_now_playing_full
    @click_object
    def click_shuffle_button(self):
        return self.wait_select_single("*", objectName="shuffleShape")

    @click_object
    def click_track(self, i):
        return self.get_track(i)

    def get_sections(self):
        return self.wait_select_single(
            "Sections", objectName="nowPlayingSections", visible=True,
        )

    @ensure_now_playing_list
    def get_track(self, i):
        return (self.wait_select_single(MusicListItem,
                objectName="nowPlayingListItem" + str(i)))

    @property
    def player(self):
        # Get new player each time as data changes (eg currentMeta)
        root = self.get_root_instance()
        return root.select_single(Player, objectName="player")

    @ensure_now_playing_full
    def seek_to(self, percentage):
        progress_bar = self.wait_select_single(
            "*", objectName="progressSliderShape")

        x1, y1, width, height = progress_bar.globalRect
        y1 += height // 2

        x2 = x1 + int(width * percentage / 100)

        self.pointing_device.drag(x1, y1, x2, y1)

    def set_repeat(self, state):
        if self.player.repeat != state:
            self.click_repeat_button()

        self.player.repeat.wait_for(state)

    def set_shuffle(self, state):
        if self.player.shuffle != state:
            self.click_shuffle_button()

        self.player.shuffle.wait_for(state)


class ArtistView(MusicPage):
    """ Autopilot helper for the albums page """
    def __init__(self, *args):
        super(ArtistView, self).__init__(*args)

        self.visible.wait_for(True)

    @click_object
    def click_artist(self, i):
        return self.wait_select_single("Card",
                                       objectName="albumsPageGridItem" +
                                       str(i))

    def get_artist(self):
        return self.wait_select_single("UCLabel",
                                       objectName="artistLabel").text


class SongsView(MusicPage):
    """ Autopilot helper for the songs page """
    def __init__(self, *args):
        super(SongsView, self).__init__(*args)

        self.visible.wait_for(True)

    def click_delete_playlist_action(self):
        self.wait_select_single(
            "ActionBar", objectName="playlistTrailingActionBar", visible=True,
        ).click_action_button("deletePlaylist")

    @click_object
    def click_track(self, i):
        return self.get_track(i)

    def get_header_artist_label(self):
        return self.wait_select_single("UCLabel",
                                       objectName="songsPageHeaderAlbumArtist")

    def get_track(self, i):
        return (self.wait_select_single(MusicListItem,
                objectName="songsPageListItem" + str(i)))


class MusicToolbar(UbuntuUIToolkitCustomProxyObjectBase):
    """Autopilot helper for the toolbar"""
    def __init__(self, *args):
        super(MusicToolbar, self).__init__(*args)

    @click_object
    def click_play_button(self):
        return self.wait_select_single("*", objectName="playShape")

    @click_object
    def click_jump_to_now_playing(self):
        return self.wait_select_single("*", objectName="jumpNowPlaying")

    def switch_to_now_playing(self):
        self.click_jump_to_now_playing()

        root = self.get_root_instance()
        now_playing_page = root.wait_select_single(NowPlaying,
                                                   objectName="nowPlayingPage")

        now_playing_page.visible.wait_for(True)


class MusicListItem(UCListItem):
    def click_add_to_playlist_action(self):
        return self.trigger_trailing_action("addToPlaylistAction")

    def click_add_to_queue_action(self):
        return self.trigger_trailing_action("addToQueueAction")

    def click_remove_action(self):
        return self.trigger_leading_action("swipeDeleteAction",
                                           self.wait_until_destroyed)

    def get_label_text(self, name):
        return self.wait_select_single(objectName=name).text


class Dialog(UbuntuUIToolkitCustomProxyObjectBase):
    def __init__(self, *args):
        super(Dialog, self).__init__(*args)

        self.visible.wait_for(True)

    @click_object
    def click_new_playlist_dialog_create_button(self):
        return self.wait_select_single(
            "Button", objectName="newPlaylistDialogCreateButton")

    def type_new_playlist_dialog_name(self, text):
        self.wait_select_single(
            "TextField", objectName="playlistNameTextField").write(text)


class RemovePlaylistDialog(UbuntuUIToolkitCustomProxyObjectBase):
    @click_object
    def click_remove_playlist_dialog_remove_button(self):
        return self.wait_select_single(
            "Button", objectName="removePlaylistDialogRemoveButton")


class MainView(MainView):
    """Autopilot custom proxy object for the MainView."""
    retry_delay = 0.2

    def __init__(self, *args):
        super(MainView, self).__init__(*args)
        self.visible.wait_for(True)

        # wait for activity indicator to stop spinning
        spinner = self.wait_select_single("ActivityIndicator",
                                          objectName="LoadingSpinner")
        spinner.running.wait_for(False)

    def go_back_wrapper(self):
        self.wait_select_single(
            "ActionBar", objectName="tabsLeadingActionBar", visible=True,
        ).click_action_button("backAction")

    def switch_to_tab_wrapper(self, objectName):
        # We use leadingActionBar instead of Tabs so create wrapper
        action_bar = self.wait_select_single(
            "ActionBar", objectName="tabsLeadingActionBar", visible=True,
        )
        try:
            action_bar.click_action_button(objectName)
        except dbus.StateNotFoundError:
            # the popover was deleted when clicking the button
            pass