2 # vi:si:et:sw=4:sts=4:ts=4
4 # gst-python - Python bindings for GStreamer
5 # Copyright (C) 2002 David I. Lehn
6 # Copyright (C) 2004 Johan Dahlin
7 # Copyright (C) 2005 Edward Hervey
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
19 # You should have received a copy of the GNU Lesser General Public
20 # 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
24 from common import gst, unittest, TestCase
26 class CapsTest(TestCase):
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 self.structure = self.caps[0]
32 self.any = gst.Caps("ANY")
33 self.assertEquals(self.any.__refcount__, 1)
34 self.empty = gst.Caps()
35 self.assertEquals(self.empty.__refcount__, 1)
37 def testCapsMime(self):
38 mime = self.structure.get_name()
39 assert mime == 'video/x-raw-yuv'
41 def testCapsList(self):
42 'check if we can access Caps as a list'
43 structure = self.caps[0]
44 mime = structure.get_name()
45 assert mime == 'video/x-raw-yuv'
46 structure = self.caps[1]
47 mime = structure.get_name()
48 assert mime == 'video/x-raw-rgb'
50 def testCapsContainingMiniObjects(self):
51 # buffer contains hex encoding of ascii 'abcd'
52 caps = gst.Caps("video/x-raw-yuv, buf=(buffer)61626364")
54 assert isinstance(buf, gst.Buffer)
55 assert buf.data == "abcd"
57 buf = gst.Buffer("1234")
59 buf2 = caps[0]['buf2']
62 def testCapsConstructEmpty(self):
64 assert isinstance(caps, gst.Caps)
66 def testCapsConstructFromString(self):
67 caps = gst.Caps('video/x-raw-yuv,width=10')
68 assert isinstance(caps, gst.Caps)
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 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)
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)
86 def testCapsConstructFromStructures(self):
87 struct1 = gst.structure_from_string('video/x-raw-yuv,width=10')
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)
93 assert isinstance(struct, gst.Structure), struct
94 assert struct.get_name() == 'video/x-raw-yuv', struct.get_name()
95 assert struct.has_key('width')
96 assert isinstance(struct['width'], int)
97 assert struct['width'] == 10
99 assert isinstance(struct, gst.Structure), struct
100 assert struct.get_name() == 'video/x-raw-rgb', struct.get_name()
101 assert struct.has_key('height')
102 assert isinstance(struct['height'], float)
103 assert struct['height'] == 20.0
105 def testCapsReferenceStructs(self):
106 'test that shows why it\'s not a good idea to use structures by reference'
107 caps = gst.Caps('hi/mom,width=0')
110 assert structure['width'] == 0
113 def testCapsStructureChange(self):
114 'test if changing the structure of the caps works by reference'
115 assert self.structure['width'] == 10
116 self.structure['width'] = 5
117 assert self.structure['width'] == 5.0
118 # check if we changed the caps as well
119 structure = self.caps[0]
120 assert structure['width'] == 5.0
122 def testCapsBadConstructor(self):
123 struct = gst.structure_from_string('video/x-raw-yuv,width=10')
124 self.assertRaises(TypeError, gst.Caps, None)
125 self.assertRaises(TypeError, gst.Caps, 1)
126 self.assertRaises(TypeError, gst.Caps, 2.0)
127 self.assertRaises(TypeError, gst.Caps, object)
128 self.assertRaises(TypeError, gst.Caps, 1, 2, 3)
130 # This causes segfault!
131 #self.assertRaises(TypeError, gst.Caps, struct, 10, None)
133 def testTrueFalse(self):
134 'test that comparisons using caps work the intended way'
135 assert self.any # not empty even though it has no structures
136 assert not self.empty
137 assert not gst.Caps('EMPTY') # also empty
138 assert gst.Caps('your/mom')
140 def testComparisons(self):
141 assert self.empty < self.any
142 assert self.empty < self.structure
143 assert self.empty < self.caps
144 assert self.caps < self.any
145 assert self.empty <= self.empty
146 assert self.caps <= self.caps
147 assert self.caps <= self.any
148 assert self.empty == "EMPTY"
149 assert self.caps != self.any
150 assert self.empty != self.any
151 assert self.any > self.empty
152 assert self.any >= self.empty
154 def testFilters(self):
155 name = 'video/x-raw-yuv'
156 filtercaps = gst.Caps(*[struct for struct in self.caps if struct.get_name() == name])
157 intersection = self.caps & 'video/x-raw-yuv'
158 assert filtercaps == intersection
160 def doSubtract(self, set, subset):
161 '''mimic the test in GStreamer core's testsuite/caps/subtract.c'''
163 assert not subset - subset
164 assert not subset - set
167 test2 = test | subset
170 #our own extensions foolow here
171 assert subset == set & subset
172 assert set == set | subset
173 assert set - subset == set ^ subset
175 def testSubtract(self):
177 gst.Caps ("some/mime, _int = [ 1, 2 ], list = { \"A\", \"B\", \"C\" }"),
178 gst.Caps ("some/mime, _int = 1, list = \"A\""))
180 gst.Caps ("some/mime, _double = (double) 1.0; other/mime, _int = { 1, 2 }"),
181 gst.Caps ("some/mime, _double = (double) 1.0"))
183 def testNoneValue(self):
184 caps = gst.Caps("foo")
186 def invalid_assignment():
187 caps[0]["bar"] = None
188 self.assertRaises(TypeError, invalid_assignment)
190 def invalid_set_value():
191 caps[0].set_value("bar", None)
192 self.assertRaises(TypeError, invalid_set_value)
195 if __name__ == "__main__":