test_caps.py 7.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
#
# gst-python - Python bindings for GStreamer
# Copyright (C) 2002 David I. Lehn
# Copyright (C) 2004 Johan Dahlin
# Copyright (C) 2005 Edward Hervey
#
# This library 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 library 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 library; if not, write to the Free Software
21
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
22

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
23
import sys
24
from common import gst, unittest, TestCase
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
25

26
class CapsTest(TestCase):
27
    def setUp(self):
28
        TestCase.setUp(self)
29
        self.caps = gst.caps_from_string('video/x-raw-yuv,width=10,framerate=5/1;video/x-raw-rgb,width=15,framerate=10/1')
30
        self.assertEquals(self.caps.__refcount__, 1)
31 32
        self.structure = self.caps[0]
        self.any = gst.Caps("ANY")
33
        self.assertEquals(self.any.__refcount__, 1)
34
        self.empty = gst.Caps()
35
        self.assertEquals(self.empty.__refcount__, 1)
Johan Dahlin's avatar
Johan Dahlin committed
36

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
37
    def testCapsMime(self):
38 39
        mime = self.structure.get_name()
        assert mime == 'video/x-raw-yuv'
40 41

    def testCapsList(self):
42 43 44 45 46 47 48
        'check if we can access Caps as a list'
        structure = self.caps[0]
        mime = structure.get_name()
        assert mime == 'video/x-raw-yuv'
        structure = self.caps[1]
        mime = structure.get_name()
        assert mime == 'video/x-raw-rgb'
49

50 51 52
    def testCapsContainingMiniObjects(self):
        # buffer contains hex encoding of ascii 'abcd'
        caps = gst.Caps("video/x-raw-yuv, buf=(buffer)61626364")
53 54 55
        buf = caps[0]['buf']
        assert isinstance(buf, gst.Buffer)
        assert buf.data == "abcd"
56 57 58 59 60 61

        buf = gst.Buffer("1234")
        caps[0]['buf2'] = buf
        buf2 = caps[0]['buf2']
        assert buf2 == buf

62 63
    def testCapsConstructEmpty(self):
        caps = gst.Caps()
64
        assert isinstance(caps, gst.Caps)
65 66 67

    def testCapsConstructFromString(self):
        caps = gst.Caps('video/x-raw-yuv,width=10')
68
        assert isinstance(caps, gst.Caps)
69
        assert len(caps) == 1
70
        assert isinstance(caps[0], gst.Structure)
71
        assert caps[0].get_name() == 'video/x-raw-yuv'
72
        assert isinstance(caps[0]['width'], int)
73 74
        assert caps[0]['width'] == 10

75
    def testCapsConstructFromStructure(self):
76
        struct = gst.structure_from_string('video/x-raw-yuv,width=10,framerate=[0/1, 25/3]')
77
        caps = gst.Caps(struct)
78
        assert isinstance(caps, gst.Caps)
79
        assert len(caps) == 1
80
        assert isinstance(caps[0], gst.Structure)
81
        assert caps[0].get_name() == 'video/x-raw-yuv'
82
        assert isinstance(caps[0]['width'], int)
83
        assert caps[0]['width'] == 10
84
        assert isinstance(caps[0]['framerate'], gst.FractionRange)
85

86 87
    def testCapsConstructFromStructures(self):
        struct1 = gst.structure_from_string('video/x-raw-yuv,width=10')
Johan Dahlin's avatar
Johan Dahlin committed
88
        struct2 = gst.structure_from_string('video/x-raw-rgb,height=20.0')
89
        caps = gst.Caps(struct1, struct2)
90
        assert isinstance(caps, gst.Caps)
91
        assert len(caps) == 2
Johan Dahlin's avatar
Johan Dahlin committed
92
        struct = caps[0]
93
        assert isinstance(struct, gst.Structure), struct
Johan Dahlin's avatar
Johan Dahlin committed
94 95
        assert struct.get_name() == 'video/x-raw-yuv', struct.get_name()
        assert struct.has_key('width')
96
        assert isinstance(struct['width'], int)
Johan Dahlin's avatar
Johan Dahlin committed
97 98
        assert struct['width'] == 10
        struct = caps[1]
99
        assert isinstance(struct, gst.Structure), struct
Johan Dahlin's avatar
Johan Dahlin committed
100 101
        assert struct.get_name() == 'video/x-raw-rgb', struct.get_name()
        assert struct.has_key('height')
102
        assert isinstance(struct['height'], float)
Johan Dahlin's avatar
Johan Dahlin committed
103
        assert struct['height'] == 20.0
104

105
    def testCapsReferenceStructs(self):
106
        'test that shows why it\'s not a good idea to use structures by reference'
107 108 109 110 111
        caps = gst.Caps('hi/mom,width=0')
        structure = caps[0]
        del caps
        assert structure['width'] == 0
        
112

Johan Dahlin's avatar
Johan Dahlin committed
113
    def testCapsStructureChange(self):
114 115
        'test if changing the structure of the caps works by reference'
        assert self.structure['width'] == 10
116
        self.structure['width'] = 5
117 118 119 120
        assert self.structure['width'] == 5.0
        # check if we changed the caps as well
        structure = self.caps[0]
        assert structure['width'] == 5.0
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
121

Johan Dahlin's avatar
Johan Dahlin committed
122 123 124 125 126 127 128 129 130 131
    def testCapsBadConstructor(self):
        struct = gst.structure_from_string('video/x-raw-yuv,width=10')
        self.assertRaises(TypeError, gst.Caps, None)
        self.assertRaises(TypeError, gst.Caps, 1)
        self.assertRaises(TypeError, gst.Caps, 2.0)
        self.assertRaises(TypeError, gst.Caps, object)
        self.assertRaises(TypeError, gst.Caps, 1, 2, 3)
        
        # This causes segfault!
        #self.assertRaises(TypeError, gst.Caps, struct, 10, None)
132 133 134

    def testTrueFalse(self):
        'test that comparisons using caps work the intended way'
135 136 137 138
        assert self.any # not empty even though it has no structures
        assert not self.empty
        assert not gst.Caps('EMPTY') # also empty
        assert gst.Caps('your/mom')
139 140

    def testComparisons(self):
141 142 143 144 145 146 147 148 149 150 151 152
        assert self.empty < self.any
        assert self.empty < self.structure
        assert self.empty < self.caps
        assert self.caps < self.any
        assert self.empty <= self.empty
        assert self.caps <= self.caps
        assert self.caps <= self.any
        assert self.empty == "EMPTY"
        assert self.caps != self.any
        assert self.empty != self.any
        assert self.any > self.empty
        assert self.any >= self.empty
153 154

    def testFilters(self):
155 156 157 158
        name = 'video/x-raw-yuv'
        filtercaps = gst.Caps(*[struct for struct in self.caps if struct.get_name() == name])
        intersection = self.caps & 'video/x-raw-yuv'
        assert filtercaps == intersection
159 160

    def doSubtract(self, set, subset):
161 162 163 164 165 166 167 168 169 170 171 172 173
        '''mimic the test in GStreamer core's testsuite/caps/subtract.c'''
        assert not set - set
        assert not subset - subset
        assert not subset - set
        test = set - subset
        assert test
        test2 = test | subset
        test = test2 - set
        assert not test
        #our own extensions foolow here
        assert subset == set & subset
        assert set == set | subset
        assert set - subset == set ^ subset
174 175

    def testSubtract(self):
176 177 178 179 180 181
        self.doSubtract(
            gst.Caps ("some/mime, _int = [ 1, 2 ], list = { \"A\", \"B\", \"C\" }"),
            gst.Caps ("some/mime, _int = 1, list = \"A\""))
        self.doSubtract(
            gst.Caps ("some/mime, _double = (double) 1.0; other/mime, _int = { 1, 2 }"),
            gst.Caps ("some/mime, _double = (double) 1.0"))
182

183 184
    def testNoneValue(self):
        caps = gst.Caps("foo")
Johan Dahlin's avatar
Johan Dahlin committed
185
        
186 187 188 189 190 191 192 193 194
        def invalid_assignment():
            caps[0]["bar"] = None
        self.assertRaises(TypeError, invalid_assignment)

        def invalid_set_value():
            caps[0].set_value("bar", None)
        self.assertRaises(TypeError, invalid_set_value)


Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
195 196
if __name__ == "__main__":
    unittest.main()