~ahayzen/music-app/convergence-tabs-with-sidebar-01

« back to all changes in this revision

Viewing changes to tests/autopilot/music_app/tests/__init__.py

  • Committer: Tarmac
  • Author(s): vamrocks602 at gmail, nskaggs
  • Date: 2016-01-05 01:38:49 UTC
  • mfrom: (954.1.34 music-app)
  • Revision ID: tarmac-20160105013849-uzx3ar6aobcg3gtx
* Add test to make sure that the LibraryEmptyState page is visible when no music is detected on the device. Fixes: https://bugs.launchpad.net/bugs/1261587.

Approved by Jenkins Bot, Victor Thompson, Andrew Hayzen.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
logger = logging.getLogger(__name__)
36
36
 
37
37
 
38
 
class BaseTestCaseWithPatchedHome(AutopilotTestCase):
39
 
 
 
38
class BaseTestClassWithPatchedHome(AutopilotTestCase):
40
39
    """A common test case class that provides several useful methods for
41
 
    music-app tests.
42
 
 
43
 
    """
 
40
    music-app tests."""
44
41
 
45
42
    working_dir = os.getcwd()
46
43
    local_location_dir = os.path.dirname(os.path.dirname(working_dir))
59
56
            test_type = 'click'
60
57
        return launch, test_type
61
58
 
62
 
    def setUp(self):
63
 
        super(BaseTestCaseWithPatchedHome, self).setUp()
64
 
        self.launcher, self.test_type = self.get_launcher_method_and_type()
65
 
        self.home_dir = self._patch_home()
66
 
        self._create_music_library()
67
 
 
68
59
    @autopilot_logging.log_action(logger.info)
69
60
    def launch_test_local(self):
70
61
        return self.launch_test_application(
90
81
            emulator_base=ubuntuuitoolkit.UbuntuUIToolkitCustomProxyObjectBase)
91
82
 
92
83
    def _copy_xauthority_file(self, directory):
93
 
        """ Copy .Xauthority file to directory, if it exists in /home
94
 
        """
 
84
        """ Copy .Xauthority file to directory, if it exists in /home"""
 
85
 
95
86
        # If running under xvfb, as jenkins does,
96
87
        # xsession will fail to start without xauthority file
97
88
        # Thus if the Xauthority file is in the home directory
107
98
                os.path.join(directory, '.Xauthority'))
108
99
 
109
100
    def _patch_home(self):
110
 
        """ mock /home for testing purposes to preserve user data
111
 
        """
 
101
        """ mock /home for testing purposes to preserve user data"""
112
102
 
113
103
        # if running on non-phablet device,
114
104
        # run in temp folder to avoid mucking up home
141
131
            logger.debug("Patched home to fake home directory %s" % temp_dir)
142
132
        return temp_dir
143
133
 
144
 
    def _create_music_library(self):
 
134
    def _create_music_library(self, db_dir):
145
135
        logger.debug("Creating music library for %s test" % self.test_type)
146
136
        logger.debug("Home set to %s" % self.home_dir)
147
137
        musicpath = os.path.join(self.home_dir, 'Music')
148
138
        logger.debug("Music path set to %s" % musicpath)
149
139
        mediascannerpath = os.path.join(self.home_dir,
150
140
                                        '.cache/mediascanner-2.0')
 
141
        logger.debug("Mediascanner path set to %s" % mediascannerpath)
 
142
 
151
143
        if not os.path.exists(musicpath):
152
144
            os.makedirs(musicpath)
153
 
        logger.debug("Mediascanner path set to %s" % mediascannerpath)
154
145
 
155
146
        # set content path
156
147
        content_dir = os.path.join(os.path.dirname(music_app.__file__),
157
 
                                   'content')
158
 
 
 
148
                                   'content', db_dir)
 
149
        songs_dir = os.path.join(content_dir, 'songs')
159
150
        logger.debug("Content dir set to %s" % content_dir)
160
151
 
161
152
        # copy content
162
 
        shutil.copy(os.path.join(content_dir, '1.ogg'), musicpath)
163
 
        shutil.copy(os.path.join(content_dir, '2.ogg'), musicpath)
164
 
        shutil.copy(os.path.join(content_dir, '3.mp3'), musicpath)
165
 
 
166
 
        logger.debug("Music copied, files " + str(os.listdir(musicpath)))
 
153
        if os.path.isdir(songs_dir) and db_dir == 'mediascanner-2.0':
 
154
            shutil.copy(os.path.join(songs_dir, '1.ogg'), musicpath)
 
155
            shutil.copy(os.path.join(songs_dir, '2.ogg'), musicpath)
 
156
            shutil.copy(os.path.join(songs_dir, '3.mp3'), musicpath)
 
157
 
 
158
            logger.debug("Music copied, files " + str(os.listdir(musicpath)))
 
159
        # delete content if previously copied
 
160
        elif not os.path.isdir(songs_dir):
 
161
            try:
 
162
                os.remove(os.path.join(musicpath, '1.ogg'))
 
163
            except OSError as e:
 
164
                logger.debug("Error removing" + str(e))
 
165
            try:
 
166
                os.remove(os.path.join(musicpath, '2.ogg'))
 
167
            except OSError as e:
 
168
                logger.debug("Error removing" + str(e))
 
169
            try:
 
170
                os.remove(os.path.join(musicpath, '3.mp3'))
 
171
            except OSError as e:
 
172
                logger.debug("Error removing" + str(e))
 
173
 
 
174
            logger.debug("Music deleted, files " + str(os.listdir(musicpath)))
167
175
 
168
176
        if self.test_type is not 'click':
169
177
            self._patch_mediascanner_home(content_dir, mediascannerpath)
172
180
        # do some inline db patching
173
181
        # patch mediaindex to proper home
174
182
        # these values are dependent upon our sampled db
175
 
        shutil.copytree(
176
 
            os.path.join(content_dir, 'mediascanner-2.0'), mediascannerpath)
 
183
        shutil.copytree(content_dir, mediascannerpath)
 
184
 
177
185
        logger.debug("Patching fake mediascanner database in %s" %
178
186
                     mediascannerpath)
179
187
        logger.debug(
182
190
 
183
191
        relhome = self.home_dir[1:]
184
192
        dblocation = "home/phablet"
 
193
 
185
194
        # patch mediaindex
186
195
        self._file_find_replace(mediascannerpath +
187
196
                                "/mediastore.sql", dblocation, relhome)
190
199
        f = open(mediascannerpath + "/mediastore.sql", 'rb')
191
200
        sql = f.read().decode("utf-8")
192
201
        cur = con.cursor()
193
 
        cur.executescript(sql)
 
202
        try:
 
203
            cur.executescript(sql)
 
204
            con.commit()
 
205
        except Exception as e:
 
206
            logger.debug("Mediscanner patching failed %s" % e)
 
207
            raise
194
208
        con.close()
195
209
 
196
210
        logger.debug(
213
227
        os.rename(out_filename, in_filename)
214
228
 
215
229
 
 
230
class BaseTestCaseWithPatchedHome(BaseTestClassWithPatchedHome):
 
231
 
 
232
    """ Base test case class for music-app, with viable audio files loaded."""
 
233
 
 
234
    def setUp(self):
 
235
        super(BaseTestClassWithPatchedHome, self).setUp()
 
236
        self.launcher, self.test_type = self.get_launcher_method_and_type()
 
237
        self.home_dir = self._patch_home()
 
238
        self._create_music_library('mediascanner-2.0')
 
239
 
 
240
 
 
241
class EmptyLibraryWithPatchedHome(BaseTestClassWithPatchedHome):
 
242
 
 
243
    """ Base test case class for music-app with empty library. """
 
244
 
 
245
    def setUp(self):
 
246
        super(BaseTestClassWithPatchedHome, self).setUp()
 
247
        self.launcher, self.test_type = self.get_launcher_method_and_type()
 
248
        self.home_dir = self._patch_home()
 
249
        self._create_music_library('blank-mediascanner-2.0')
 
250
 
 
251
 
216
252
class MusicAppTestCase(BaseTestCaseWithPatchedHome):
217
253
 
218
254
    """Base test case that launches the music-app."""
220
256
    def setUp(self):
221
257
        super(MusicAppTestCase, self).setUp()
222
258
        self.app = MusicApp(self.launcher())
 
259
 
 
260
 
 
261
class MusicAppTestCaseEmptyLibrary(EmptyLibraryWithPatchedHome):
 
262
 
 
263
    """Test case that launches the music-app with no music:
 
264
    an empty library."""
 
265
 
 
266
    def setUp(self):
 
267
        super(MusicAppTestCaseEmptyLibrary, self).setUp()
 
268
        self.app = MusicApp(self.launcher())