~ubuntu-branches/ubuntu/trusty/miro/trusty

« back to all changes in this revision

Viewing changes to portable/test/utiltest.py

  • Committer: Daniel Hahler
  • Date: 2010-04-13 18:51:35 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: ubuntu-launchpad@thequod.de-20100413185135-xi24v1diqg8w406x
Merging shared upstream rev into target branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import os
2
2
import os.path
3
3
import tempfile
 
4
import shutil
4
5
 
5
6
from miro.test.framework import MiroTestCase
6
7
from miro import download_utils
7
8
from miro import util
8
 
 
 
9
from miro.plat.utils import FilenameType
9
10
 
10
11
# We're going to override this so we can guarantee that if the order
11
12
# changes later that it doesn't really affect us.
14
15
    'video/quicktime', 'video/mpeg']
15
16
 
16
17
class FakeStream:
17
 
    """Fake streams are used for the AutoFlushingStream test.  They don't
18
 
    really do much, except check that write is always called with a string
19
 
    object (unicode won't always work when writing to stdout).
 
18
    """Fake streams are used for the AutoFlushingStream test.  They
 
19
    don't really do much, except check that write is always called
 
20
    with a string object (unicode won't always work when writing to
 
21
    stdout).
20
22
    """
21
23
 
22
24
    def write(self, out):
23
25
        if not isinstance(out, str):
24
26
            raise ValueError("Got non-string object (%s) from "
25
27
            "autoflushing stream" % str.__class__)
 
28
 
26
29
    def flush(self):
27
30
        pass
28
31
 
32
35
        self.stream = FakeStream()
33
36
        self.afs = util.AutoFlushingStream(self.stream)
34
37
 
35
 
    def testBasicWrite(self):
 
38
    def test_basic_write(self):
36
39
        self.afs.write("Hello World\n")
37
40
        self.afs.write("")
38
41
        self.afs.write("LotsofData" * 200)
39
42
 
40
 
    def testUnicodeWrite(self):
 
43
    def test_unicode_write(self):
41
44
        self.afs.write(u'\xf8')
42
45
 
43
46
class LoggingStreamTest(MiroTestCase):
49
52
        self.stderr = util.AutoLoggingStream(self.err_callback, '(from stderr) ')
50
53
 
51
54
    def _check_data(self, data):
52
 
        """Check that write is always called with a string object (unicode
53
 
        won't always work when writing to stdout)
 
55
        """Check that write is always called with a string object
 
56
        (unicode won't always work when writing to stdout)
54
57
        """
55
58
        if not isinstance(data, str):
56
59
            raise ValueError("Got non-string object (%r) from LoggingStream" %
57
 
                    data)
 
60
                             data)
58
61
 
59
62
    def warn_callback(self, data):
60
63
        self._check_data(data)
64
67
        self._check_data(data)
65
68
        self.errors.append(data)
66
69
 
67
 
    def testBasicWrite(self):
 
70
    def test_basic_write(self):
68
71
        self.stdout.write("Hello World\n")
69
72
        self.stdout.write("")
70
73
        self.stderr.write("LotsofData" * 200)
74
77
        self.assertEquals(self.errors[0], '(from stderr) ' + 
75
78
            "LotsofData" * 200)
76
79
 
77
 
    def testUnicodeWrite(self):
 
80
    def test_unicode_write(self):
78
81
        self.stdout.write(u'\xf8')
79
82
        self.assertEquals(len(self.warnings), 1)
80
83
        self.assertEquals(self.warnings[0], '(from stdout) \\xf8')
131
134
             'type': u'video/mpeg',
132
135
             'filesize': u'244700'}]
133
136
 
134
 
    def testStringify(self):
135
 
        # input, handleerror, expected output
136
 
        # if handlerror is None, then it isn't passed in as an argument
137
 
        t = [
138
 
              ( "", None, ""),
139
 
              ( "abc", None, "abc"),
140
 
              ( 5, None, "5"),
141
 
              ( 5.5, None, "5.5"),
142
 
              ( u"abc", None, "abc"),
143
 
              ( u"abc\xe4", None, "abcä"),
144
 
              ( u"abc\xe4", "replace", "abc?")
145
 
            ]
146
 
 
147
 
        for i, h, o in t:
 
137
    def test_is_url_positive(self):
 
138
        for testurl in [u"http://foo.bar.com/",
 
139
                        u"https://foo.bar.com/",
 
140
                        ]:
 
141
            self.assertEqual(util.is_url(testurl), True)
 
142
 
 
143
    def test_is_url_negative(self):
 
144
        for testurl in [u"",
 
145
                        None,
 
146
                        u"feed://foo.bar.com/",
 
147
                        u"http://foo.bar.com",
 
148
                        u"http:foo.bar.com/",
 
149
                        u"https:foo.bar.com/",
 
150
                        u"feed:foo.bar.com/",
 
151
                        u"http:/foo.bar.com/",
 
152
                        u"https:/foo.bar.com/",
 
153
                        u"feed:/foo.bar.com/",
 
154
                        u"http:///foo.bar.com/",
 
155
                        u"https:///foo.bar.com/",
 
156
                        u"feed:///foo.bar.com/",
 
157
                        u"foo.bar.com",
 
158
                        u"crap:foo.bar.com",
 
159
                        u"crap:/foo.bar.com",
 
160
                        u"crap://foo.bar.com",
 
161
                        u"crap:///foo.bar.com",
 
162
                        # Bug #12645
 
163
                        u"No license (All rights reserved)",
 
164
                        ]:
 
165
            self.assertEqual(util.is_url(testurl), False)
 
166
 
 
167
    def test_stringify(self):
 
168
        # input, handleerror, expected output if handlerror is None,
 
169
        # then it isn't passed in as an argument
 
170
 
 
171
        for i, h, o in [
 
172
            ( "", None, ""),
 
173
            ( "abc", None, "abc"),
 
174
            ( 5, None, "5"),
 
175
            ( 5.5, None, "5.5"),
 
176
            ( u"abc", None, "abc"),
 
177
            ( u"abc\xe4", None, "abcä"),
 
178
            ( u"abc\xe4", "replace", "abc?")
 
179
            ]:
 
180
 
148
181
            if h == None:
149
182
                self.assertEquals(util.stringify(i), o)
150
183
            else:
151
184
                self.assertEquals(util.stringify(i, h), o)
152
185
 
153
 
    def testRandomString(self):
 
186
    def test_random_string(self):
154
187
        ret = util.random_string(0)
155
188
        self.assertEquals(len(ret), 0)
156
189
 
159
192
            self.assertEquals(len(ret), length)
160
193
            self.assertEquals(ret.isalpha(), True)
161
194
 
162
 
    def testCmpEnclosures(self):
 
195
    def test_cmp_enclosures(self):
163
196
        """
164
197
        Test for util.cmp_enclosures
165
198
        """
192
225
             u'http://example.org/2.mov',
193
226
             u'http://example.org/5.mpeg'])
194
227
            
195
 
    def testGetFirstVideoEnclosure(self):
 
228
    def test_get_first_video_enclosure(self):
196
229
        """
197
 
        Test for util.getFirstVideoEnclosure
 
230
        Test for util.get_first_video_enclosure
198
231
        """
199
232
        class FakeEntry(object):
200
233
            def __init__(self, enclosures):
206
239
        combinations_entry = FakeEntry(self.combination_elements)
207
240
 
208
241
        # get their "selected" results
209
 
        selected_filesize = util.getFirstVideoEnclosure(filesizes_entry)
210
 
        selected_type = util.getFirstVideoEnclosure(types_entry)
211
 
        selected_combination = util.getFirstVideoEnclosure(combinations_entry)
 
242
        selected_filesize = util.get_first_video_enclosure(filesizes_entry)
 
243
        selected_type = util.get_first_video_enclosure(types_entry)
 
244
        selected_combination = util.get_first_video_enclosure(combinations_entry)
212
245
 
213
246
        # now make sure they returned what we expected..
214
247
        self.assertEqual(selected_filesize['href'], u'http://example.org/4.ogg')
217
250
                         u'http://example.org/1.ogg')
218
251
 
219
252
class DownloadUtilsTest(MiroTestCase):
220
 
    def checkCleanFilename(self, filename, test_against):
221
 
        self.assertEquals(download_utils.cleanFilename(filename),
222
 
                test_against)
 
253
    def check_clean_filename(self, filename, test_against):
 
254
        self.assertEquals(download_utils.clean_filename(filename),
 
255
                          test_against)
223
256
 
224
 
    def testCleanFilename(self):
225
 
        self.checkCleanFilename('normalname', 'normalname')
226
 
        self.checkCleanFilename('a:b?c>d<e|f*/g\\h"\'', 'abcdefgh')
227
 
        self.checkCleanFilename('', '_')
228
 
        longFilename = 'booya' * 100
229
 
        longExtension = '.' + 'foo' * 20
230
 
        self.checkCleanFilename(longFilename, longFilename[:100])
231
 
        # total file length isn't over the limit, so the extension stays the
232
 
        # same
233
 
        self.checkCleanFilename('abc' + longExtension, 
234
 
            'abc' + longExtension)
235
 
        self.checkCleanFilename(longFilename + longExtension,
236
 
            longFilename[:50] + longExtension[:50])
 
257
    def test_clean_filename(self):
 
258
        self.check_clean_filename('normalname', 'normalname')
 
259
        self.check_clean_filename('a:b?c>d<e|f*/g\\h"\'', 'abcdefgh')
 
260
        self.check_clean_filename('', '_')
 
261
        long_filename = 'booya' * 100
 
262
        long_extension = '.' + 'foo' * 20
 
263
        self.check_clean_filename(long_filename, long_filename[:100])
 
264
        # total file length isn't over the limit, so the extension
 
265
        # stays the same
 
266
        self.check_clean_filename('abc' + long_extension, 
 
267
                                  'abc' + long_extension)
 
268
        self.check_clean_filename(long_filename + long_extension,
 
269
                                  long_filename[:50] + long_extension[:50])
237
270
 
238
271
class Test_simple_config_file(MiroTestCase):
239
272
    def test_read_simple_config_file(self):
282
315
            os.remove(fn)
283
316
 
284
317
class MatrixTest(MiroTestCase):
285
 
    def testMatrixInit(self):
 
318
    def test_matrix_init(self):
286
319
        m = util.Matrix(1, 2)
287
320
        self.assertEquals(list(m), [None, None])
288
321
 
307
340
        self.assertEquals(m[3, 0], None)
308
341
        self.assertEquals(m[4, 0], None)
309
342
 
310
 
    def testGetSet(self):
 
343
    def test_get_set(self):
311
344
        m = util.Matrix(3, 2)
312
345
        m[0, 0] = 1
313
346
        m[0, 1] = 2
323
356
        m[0,0] = 17
324
357
        self.assertEquals(m[0,0], 17)
325
358
 
326
 
    def testRowsColumns(self):
 
359
    def test_columns(self):
327
360
        m = util.Matrix(3, 2)
328
361
        m[0, 0] = 1
329
362
        m[0, 1] = 2
332
365
        m[2, 0] = 5
333
366
        m[2, 1] = 6
334
367
 
335
 
        self.assertEquals(list(m.row(0)), [1, 3, 5])
336
 
        self.assertEquals(list(m.row(1)), [2, 4, 6])
337
368
        self.assertEquals(list(m.column(0)), [1, 2])
338
369
        self.assertEquals(list(m.column(1)), [3, 4])
339
370
        self.assertEquals(list(m.column(2)), [5, 6])
340
371
 
341
 
    def testRemove(self):
 
372
 
 
373
    def test_rows(self):
 
374
        m = util.Matrix(3, 2)
 
375
        m[0, 0] = 1
 
376
        m[0, 1] = 2
 
377
        m[1, 0] = 3
 
378
        m[1, 1] = 4
 
379
        m[2, 0] = 5
 
380
        m[2, 1] = 6
 
381
 
 
382
        self.assertEquals(list(m.row(0)), [1, 3, 5])
 
383
        self.assertEquals(list(m.row(1)), [2, 4, 6])
 
384
        
 
385
    def test_remove(self):
342
386
        m = util.Matrix(1, 2)
343
387
        m[0,0] = 1
344
388
        m[0,1] = 2
347
391
 
348
392
        self.assertEquals(m[0,0], 1)
349
393
        self.assertEquals(m[0,1], None)
 
394
 
 
395
class Test_gather_subtitles_files(MiroTestCase):
 
396
    def setUp(self):
 
397
        MiroTestCase.setUp(self)
 
398
        self.tempdir = tempfile.mkdtemp()
 
399
 
 
400
    def tearDown(self):
 
401
        MiroTestCase.tearDown(self)
 
402
        shutil.rmtree(self.tempdir, ignore_errors=True)
 
403
 
 
404
    def create_files(self, movie_file, sub_files=None):
 
405
        if sub_files is None:
 
406
            sub_files = []
 
407
 
 
408
        movie_file = os.path.join(self.tempdir, movie_file)
 
409
        sub_files = [os.path.join(self.tempdir, mem) for mem in sub_files]
 
410
        sub_files.sort()
 
411
 
 
412
        all_files = [movie_file] + list(sub_files)
 
413
        for mem in all_files:
 
414
            dirname = os.path.dirname(mem)
 
415
            if not os.path.exists(dirname):
 
416
                os.makedirs(dirname)
 
417
 
 
418
            filep = open(mem, "w")
 
419
            filep.write("lalala")
 
420
            filep.close()
 
421
 
 
422
        return movie_file, sub_files
 
423
 
 
424
    def test_no_directory(self):
 
425
        # tests the case where the foofeed directory doesn't exist
 
426
        movie_file = os.path.join(self.tempdir, "foofeed", "foo.mov")
 
427
        self.assertEquals([],
 
428
                          util.gather_subtitle_files(FilenameType(movie_file)))
 
429
 
 
430
    def test_no_subtitle_files(self):
 
431
        movie_file, sub_files = self.create_files("foo.mov")
 
432
        self.assertEquals(sub_files,
 
433
                          util.gather_subtitle_files(FilenameType(movie_file)))
 
434
 
 
435
    def test_single_file(self):
 
436
        movie_file, sub_files = self.create_files(
 
437
            "foo.mov", ["foo.en.srt"])
 
438
        self.assertEquals(sub_files,
 
439
                          util.gather_subtitle_files(FilenameType(movie_file)))
 
440
 
 
441
    def test_multiple_files(self):
 
442
        movie_file, sub_files = self.create_files(
 
443
            "foo.mov", ["foo.en.srt", "foo.fr.srt", "foo.es.srt"])
 
444
        self.assertEquals(sub_files,
 
445
                          util.gather_subtitle_files(FilenameType(movie_file)))
 
446
 
 
447
    def test_lots_of_files(self):
 
448
        movie_file, sub_files = self.create_files(
 
449
            "foo.mov", ["foo.en.srt", "blah.ogv", "foo.ogv"])
 
450
 
 
451
        # weed out the non-srt files so we can test correctly
 
452
        sub_files = [mem for mem in sub_files if mem.endswith(".srt")]
 
453
        self.assertEquals(sub_files,
 
454
                          util.gather_subtitle_files(FilenameType(movie_file)))
 
455
 
 
456
    def test_subtitles_dir(self):
 
457
        movie_file, sub_files = self.create_files(
 
458
            "foo.mov", ["subtitles/foo.en.srt", "subtitles/foo.fr.srt"])
 
459
        self.assertEquals(sub_files,
 
460
                          util.gather_subtitle_files(FilenameType(movie_file)))
 
461
 
 
462
    def test_filename_possibilities(self):
 
463
        movie_file, sub_files = self.create_files(
 
464
            "foo.mov", ["foo.en.srt", "foo.en.sub", "foo.srt", "foo.sub"])
 
465
 
 
466
        self.assertEquals(sub_files,
 
467
                          util.gather_subtitle_files(FilenameType(movie_file)))
 
468
 
 
469
class Test_copy_subtitle_file(MiroTestCase):
 
470
    def setUp(self):
 
471
        MiroTestCase.setUp(self)
 
472
        self.tempdir = tempfile.mkdtemp()
 
473
 
 
474
    def tearDown(self):
 
475
        MiroTestCase.tearDown(self)
 
476
        shutil.rmtree(self.tempdir, ignore_errors=True)
 
477
 
 
478
    def create_files(self, files):
 
479
        for mem in files:
 
480
            dirname = os.path.dirname(mem)
 
481
            if not os.path.exists(dirname):
 
482
                os.makedirs(dirname)
 
483
 
 
484
            filep = open(mem, "w")
 
485
            filep.write("lalala")
 
486
            filep.close()
 
487
 
 
488
    def test_simple(self):
 
489
        sub_path = os.path.join(self.tempdir, "otherdir/subtitle.srt")
 
490
        video_path = os.path.join(self.tempdir, "foo.mov")
 
491
        self.create_files([sub_path, video_path])
 
492
 
 
493
        ret = util.copy_subtitle_file(sub_path, video_path)
 
494
        expected = os.path.join(self.tempdir, "foo.srt")
 
495
        self.assert_(os.path.exists(expected))
 
496
        self.assertEqual(expected, ret)
 
497
 
 
498
    def test_simple_with_language(self):
 
499
        sub_path = os.path.join(self.tempdir, "otherdir/subtitle.en.srt")
 
500
        video_path = os.path.join(self.tempdir, "foo.mov")
 
501
        self.create_files([sub_path, video_path])
 
502
 
 
503
        ret = util.copy_subtitle_file(sub_path, video_path)
 
504
        expected = os.path.join(self.tempdir, "foo.en.srt")
 
505
        self.assert_(os.path.exists(expected))
 
506
        self.assertEqual(expected, ret)
 
507
 
 
508
    def test_nonlanguage(self):
 
509
        # "ex" is not a valid language code, so this should ignore
 
510
        # that part
 
511
        sub_path = os.path.join(self.tempdir, "otherdir/subtitle.ex.srt")
 
512
        video_path = os.path.join(self.tempdir, "foo.mov")
 
513
        self.create_files([sub_path, video_path])
 
514
 
 
515
        ret = util.copy_subtitle_file(sub_path, video_path)
 
516
        expected = os.path.join(self.tempdir, "foo.srt")
 
517
        self.assert_(os.path.exists(expected))
 
518
        self.assertEqual(expected, ret)