test_utils.py 13.2 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
# Pitivi video editor
3
# Copyright (c) 2009, Alessandro Decina <alessandro.decina@collabora.co.uk>
4
# Copyright (c) 2014, Mathieu Duponchelle <mduponchelle1@gmail.com>
5 6 7 8 9 10 11 12 13 14 15 16 17
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
Hicham HAOUARI's avatar
Hicham HAOUARI committed
18 19
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301, USA.
20
import time
21
from unittest import TestCase
22
from unittest.mock import Mock
23

24
from gi.repository import GLib
25
from gi.repository import Gst
26
from gi.repository import GstPbutils
27

28 29 30 31
from pitivi.check import CairoDependency
from pitivi.check import ClassicDependency
from pitivi.check import GstDependency
from pitivi.check import GtkDependency
32
from pitivi.utils.misc import fixate_caps_with_default_values
33
from pitivi.utils.ui import beautify_last_updated_timestamp
34
from pitivi.utils.ui import beautify_length
35 36 37
from pitivi.utils.ui import format_audiochannels
from pitivi.utils.ui import format_audiorate
from pitivi.utils.ui import format_framerate_value
38

39
second = Gst.SECOND
40 41 42
minute = second * 60
hour = minute * 60

43

44 45
class TestBeautifyTime(TestCase):
    """Tests time beautifying utility methods."""
46

47 48 49 50 51 52 53
    def __check_beautify_last_updated_timestamp(self, seconds, expected):
        time.time = Mock()
        time.time.return_value = seconds
        self.assertEqual(beautify_last_updated_timestamp(0), expected)

    def test_beautify_length(self):
        """Tests beautification of time duration."""
54 55
        self.assertEqual(beautify_length(second), "1 second")
        self.assertEqual(beautify_length(second * 2), "2 seconds")
56

57 58
        self.assertEqual(beautify_length(minute), "1 minute")
        self.assertEqual(beautify_length(minute * 2), "2 minutes")
59

60 61
        self.assertEqual(beautify_length(hour), "1 hour")
        self.assertEqual(beautify_length(hour * 2), "2 hours")
62

63 64 65
        self.assertEqual(beautify_length(minute + second), "1 minute, 1 second")
        self.assertEqual(beautify_length(hour + minute + second), "1 hour, 1 minute")
        self.assertEqual(beautify_length(Gst.CLOCK_TIME_NONE), "")
66

67 68 69 70
    def test_beautify_last_updated_timestamp(self):
        """Tests beautification of project's updation timestamp."""
        self.__check_beautify_last_updated_timestamp(0, "Just now")
        self.__check_beautify_last_updated_timestamp(60 * 45 - 1, "Just now")
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        self.__check_beautify_last_updated_timestamp(60 * 45, "An hour ago")
        self.__check_beautify_last_updated_timestamp(60 * 90 - 1, "An hour ago")

        self.__check_beautify_last_updated_timestamp(60 * 90, "Today")
        self.__check_beautify_last_updated_timestamp(60 * 60 * 24 - 1, "Today")

        self.__check_beautify_last_updated_timestamp(60 * 60 * 24, "Yesterday")
        self.__check_beautify_last_updated_timestamp(60 * 60 * 24 * 2 - 1, "Yesterday")

        self.__check_beautify_last_updated_timestamp(60 * 60 * 24 * 2, "Thursday")
        self.__check_beautify_last_updated_timestamp(60 * 60 * 24 * 7 - 1, "Thursday")

        self.__check_beautify_last_updated_timestamp(60 * 60 * 24 * 7, "January")
        self.__check_beautify_last_updated_timestamp(60 * 60 * 24 * 365 - 1, "January")

        self.__check_beautify_last_updated_timestamp(60 * 60 * 24 * 365, "About a year ago")
        self.__check_beautify_last_updated_timestamp(60 * 60 * 24 * 365 * 1.5 - 1, "About a year ago")

        self.__check_beautify_last_updated_timestamp(60 * 60 * 24 * 365 * 1.5, "About 2 years ago")
        self.__check_beautify_last_updated_timestamp(60 * 60 * 24 * 365 * 3, "About 3 years ago")
92

93

94
class TestFormatFramerateValue(TestCase):
95 96

    def __check(self, num, denom, expected):
97 98 99
        stream = Mock(spec=GstPbutils.DiscovererVideoInfo)
        fraction = Mock(num=num, denom=denom)

100 101
        stream.get_framerate_num = Mock(return_value=num)
        stream.get_framerate_denom = Mock(return_value=denom)
102 103 104

        self.assertEqual(format_framerate_value(stream), expected)
        self.assertEqual(format_framerate_value(fraction), expected)
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

    def test_invalid_fps(self):
        self.__check(0, 1, "0")
        self.__check(0, 0, "0")
        self.__check(1, 0, "0")

    def test_int_fps(self):
        self.__check(1, 1, "1")
        self.__check(24, 1, "24")

    def test_float_fps(self):
        self.__check(24000, 1001, "23.976")
        self.__check(30000, 1001, "29.97")
        self.__check(60000, 1001, "59.94")

    def test_high_fps(self):
        self.__check(2500, 1, "2,500")
        self.__check(120, 1, "120")


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
class TestFormatAudiorate(TestCase):

    def __check(self, rate, expected):
        stream = Mock(spec=GstPbutils.DiscovererAudioInfo)
        stream.get_sample_rate = Mock(return_value=rate)

        self.assertEqual(format_audiorate(stream), expected)
        self.assertEqual(format_audiorate(rate), expected)

    def test_audiorates(self):
        self.__check(8000, "8 kHz")
        self.__check(11025, "11 kHz")
        self.__check(22050, "22 kHz")
        self.__check(44100, "44.1 kHz")
        self.__check(96000, "96 kHz")
        self.__check(960000, "960 kHz")


class TestFormatAudiochannels(TestCase):

    def __check(self, channels, expected):
        stream = Mock(spec=GstPbutils.DiscovererAudioInfo)
        stream.get_channels = Mock(return_value=channels)

        self.assertEqual(format_audiochannels(stream), expected)
        self.assertEqual(format_audiochannels(channels), expected)

    def test_audiochannels(self):
        self.__check(1, "Mono")
        self.__check(2, "Stereo")
        self.__check(6, "6 (5.1)")


158
class TestDependencyChecks(TestCase):
159

160
    def testDependencies(self):
161
        gi_dep = GstDependency("Gst", "1.0", "1.0.0")
162
        gi_dep.check()
163
        self.assertTrue(gi_dep.satisfied)
164

165
        gi_dep = GstDependency("Gst", "1.0", "9.9.9")
166
        gi_dep.check()
167
        self.assertFalse(gi_dep.satisfied)
168 169 170

        gi_dep = GstDependency("ThisShouldNotExist", None)
        gi_dep.check()
171
        self.assertFalse(gi_dep.satisfied)
172

173
        gi_dep = GtkDependency("Gtk", "3.0", "3.0.0")
174
        gi_dep.check()
175
        self.assertTrue(gi_dep.satisfied)
176

177
        gi_dep = GtkDependency("Gtk", "3.0", "9.9.9")
178
        gi_dep.check()
179
        self.assertFalse(gi_dep.satisfied)
180 181 182

        cairo_dep = CairoDependency("1.0.0")
        cairo_dep.check()
183
        self.assertTrue(cairo_dep.satisfied)
184 185 186

        cairo_dep = CairoDependency("9.9.9")
        cairo_dep.check()
187
        self.assertFalse(cairo_dep.satisfied)
188 189 190

        classic_dep = ClassicDependency("numpy", None)
        classic_dep.check()
191
        self.assertTrue(classic_dep.satisfied)
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212


class TestMiscUtils(TestCase):

    def test_fixate_caps_with_defalt_values(self):
        voaacenc_caps = Gst.Caps.from_string(
            "audio/x-raw, format=(string)S16LE, layout=(string)interleaved, rate=(int){ 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000 }, channels=(int)1;"
            "audio/x-raw, format=(string)S16LE, layout=(string)interleaved, rate=(int){ 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000 }, channels=(int)2, channel-mask=(bitmask)0x0000000000000003")
        yt_audiorest = Gst.Caps("audio/x-raw,channels=6,channel-mask=0x3f,rate={48000,96000};"
            "audio/x-raw,channels=2,rate={48000,96000}")

        vorbis_caps = Gst.Caps("audio/x-raw, format=(string)F32LE, layout=(string)interleaved, rate=(int)[ 1, 200000 ], channels=(int)1;"
                               "audio/x-raw, format=(string)F32LE, layout=(string)interleaved, rate=(int)[ 1, 200000 ], channels=(int)2, channel-mask=(bitmask)0x0000000000000003;"
                               "audio/x-raw, format=(string)F32LE, layout=(string)interleaved, rate=(int)[ 1, 200000 ], channels=(int)3, channel-mask=(bitmask)0x0000000000000007;"
                               "audio/x-raw, format=(string)F32LE, layout=(string)interleaved, rate=(int)[ 1, 200000 ], channels=(int)4, channel-mask=(bitmask)0x0000000000000033;"
                               "audio/x-raw, format=(string)F32LE, layout=(string)interleaved, rate=(int)[ 1, 200000 ], channels=(int)5, channel-mask=(bitmask)0x0000000000000037;"
                               "audio/x-raw, format=(string)F32LE, layout=(string)interleaved, rate=(int)[ 1, 200000 ], channels=(int)6, channel-mask=(bitmask)0x000000000000003f;"
                               "audio/x-raw, format=(string)F32LE, layout=(string)interleaved, rate=(int)[ 1, 200000 ], channels=(int)7, channel-mask=(bitmask)0x0000000000000d0f;"
                               "audio/x-raw, format=(string)F32LE, layout=(string)interleaved, rate=(int)[ 1, 200000 ], channels=(int)8, channel-mask=(bitmask)0x0000000000000c3f;"
                               "audio/x-raw, format=(string)F32LE, layout=(string)interleaved, rate=(int)[ 1, 200000 ], channels=(int)[ 9, 255 ], channel-mask=(bitmask)0x0000000000000000")

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        avenc_ac3_caps = Gst.Caps("audio/x-raw, channel-mask=(bitmask)0x0000000000000000, channels=(int)1, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;"
                " audio/x-raw, channel-mask=(bitmask)0x0000000000000003, channels=(int)2, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;"
                " audio/x-raw, channel-mask=(bitmask)0x0000000000000103, channels=(int)3, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;"
                " audio/x-raw, channel-mask=(bitmask)0x0000000000000007, channels=(int)3, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;"
                " audio/x-raw, channel-mask=(bitmask)0x0000000000000c03, channels=(int)4, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;"
                " audio/x-raw, channel-mask=(bitmask)0x0000000000000033, channels=(int)4, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;"
                " audio/x-raw, channel-mask=(bitmask)0x0000000000000107, channels=(int)4, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;"
                " audio/x-raw, channel-mask=(bitmask)0x0000000000000c07, channels=(int)5, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;"
                " audio/x-raw, channel-mask=(bitmask)0x0000000000000037, channels=(int)5, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;"
                " audio/x-raw, channel-mask=(bitmask)0x000000000000000c, channels=(int)2, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;"
                " audio/x-raw, channel-mask=(bitmask)0x000000000000000b, channels=(int)3, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;"
                " audio/x-raw, channel-mask=(bitmask)0x000000000000010b, channels=(int)4, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;"
                " audio/x-raw, channel-mask=(bitmask)0x000000000000000f, channels=(int)4, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;"
                " audio/x-raw, channel-mask=(bitmask)0x0000000000000c0b, channels=(int)5, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;"
                " audio/x-raw, channel-mask=(bitmask)0x000000000000003b, channels=(int)5, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;"
                " audio/x-raw, channel-mask=(bitmask)0x000000000000010f, channels=(int)5, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;"
                " audio/x-raw, channel-mask=(bitmask)0x0000000000000c0f, channels=(int)6, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;"
                " audio/x-raw, channel-mask=(bitmask)0x000000000000003f, channels=(int)6, rate=(int){ 48000, 44100, 32000 }, layout=(string)interleaved, format=(string)F32LE;")

232
        audio_defaults = {"channels": Gst.IntRange(range(1, 2147483647)),
233 234 235 236
                          "rate": Gst.IntRange(range(8000, GLib.MAXINT))}

        dataset = [
            (voaacenc_caps, yt_audiorest, audio_defaults, None, Gst.Caps("audio/x-raw, channels=2,rate=48000,channel-mask=(bitmask)0x03")),
237
            (vorbis_caps, None, audio_defaults, None, Gst.Caps("audio/x-raw,channels=1,rate=8000")),
238
            (avenc_ac3_caps, None, audio_defaults, Gst.Caps("audio/x-raw, channels=(int)6, rate=(int)44100"), Gst.Caps("audio/x-raw, channels=(int)6, rate=(int)44100")),
239 240
        ]

241 242 243
        for template, restrictions, default_values, prev_vals, expected in dataset:
            res = fixate_caps_with_default_values(template, restrictions, default_values, prev_vals)
            self.assertTrue(res.is_equal_fixed(expected), "%s != %s" % (res, expected))