1# Copyright 2014 Christoph Reiter
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7
8import os
9
10from mutagen.aac import AAC
11
12from quodlibet.formats.aac import AACFile
13
14from . import TestCase, get_data_path, skipUnless
15from .helper import get_temp_copy
16
17
18class _TAACFile(TestCase):
19
20    NAME = None
21
22    def setUp(self):
23        self.f = get_temp_copy(get_data_path(self.NAME))
24        self.song = AACFile(self.f)
25
26    def tearDown(self):
27        os.unlink(self.f)
28
29
30class _TAACFileMixin(object):
31
32    def test_basic(self):
33        self.song["title"] = u"SomeTestValue"
34        self.song.write()
35        self.song.reload()
36        self.assertEqual(self.song("title"), u"SomeTestValue")
37
38    def test_write(self):
39        self.song.write()
40
41    def test_can_change(self):
42        self.assertTrue(self.song.can_change("title"))
43        self.assertFalse(self.song.can_change("foobar"))
44        self.assertTrue("title" in self.song.can_change())
45
46    def test_can_multiple_values(self):
47        self.assertEqual(self.song.can_multiple_values(), True)
48        self.assertTrue(self.song.can_multiple_values("title"))
49
50    def test_invalid(self):
51        path = get_data_path('empty.xm')
52        self.assertTrue(os.path.exists(path))
53        self.assertRaises(Exception, AACFile, path)
54
55    def test_format_codec(self):
56        self.assertEqual(self.song("~format"), "AAC")
57        self.assertEqual(self.song("~codec"), "AAC")
58        self.assertEqual(self.song("~encoding"), "")
59
60    def test_channels(self):
61        assert self.song("~#channels") == 2
62
63
64@skipUnless(AAC, "too old mutagen")
65class TADTSFile(_TAACFile, _TAACFileMixin):
66
67    NAME = "empty.aac"
68
69    def test_length(self):
70        self.assertAlmostEqual(self.song("~#length"), 3.7, 2)
71
72    def test_bitrate(self):
73        self.assertEqual(self.song("~#bitrate"), 3)
74
75    def test_samplerate(self):
76        assert self.song("~#samplerate") == 44100
77
78
79@skipUnless(AAC, "too old mutagen")
80class TADIFFile(_TAACFile, _TAACFileMixin):
81
82    NAME = "adif.aac"
83
84    def test_length(self):
85        self.assertAlmostEqual(self.song("~#length"), 0.25, 2)
86
87    def test_bitrate(self):
88        self.assertEqual(self.song("~#bitrate"), 128)
89
90    def test_samplerate(self):
91        assert self.song("~#samplerate") == 48000
92