Initial import to Tizen
[profile/ivi/gstreamer-python.git] / testsuite / test_caps.py
1 # -*- Mode: Python -*-
2 # vi:si:et:sw=4:sts=4:ts=4
3 #
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
8 #
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.
13 #
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.
18 #
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
22
23 import sys
24 from common import gst, unittest, TestCase
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         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)
36
37     def testCapsMime(self):
38         mime = self.structure.get_name()
39         assert mime == 'video/x-raw-yuv'
40
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'
49
50     def testCapsContainingMiniObjects(self):
51         # buffer contains hex encoding of ascii 'abcd'
52         caps = gst.Caps("video/x-raw-yuv, buf=(buffer)61626364")
53         buf = caps[0]['buf']
54         assert isinstance(buf, gst.Buffer)
55         assert buf.data == "abcd"
56
57         buf = gst.Buffer("1234")
58         caps[0]['buf2'] = buf
59         buf2 = caps[0]['buf2']
60         assert buf2 == buf
61
62     def testCapsConstructEmpty(self):
63         caps = gst.Caps()
64         assert isinstance(caps, gst.Caps)
65
66     def testCapsConstructFromString(self):
67         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         assert caps[0]['width'] == 10
74
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     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)
91         assert len(caps) == 2
92         struct = caps[0]
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
98         struct = caps[1]
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
104
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')
108         structure = caps[0]
109         del caps
110         assert structure['width'] == 0
111         
112
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
121
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)
129         
130         # This causes segfault!
131         #self.assertRaises(TypeError, gst.Caps, struct, 10, None)
132
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')
139
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
153
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
159
160     def doSubtract(self, set, subset):
161         '''mimic the test in GStreamer core's testsuite/caps/subtract.c'''
162         assert not set - set
163         assert not subset - subset
164         assert not subset - set
165         test = set - subset
166         assert test
167         test2 = test | subset
168         test = test2 - set
169         assert not test
170         #our own extensions foolow here
171         assert subset == set & subset
172         assert set == set | subset
173         assert set - subset == set ^ subset
174
175     def testSubtract(self):
176         self.doSubtract(
177             gst.Caps ("some/mime, _int = [ 1, 2 ], list = { \"A\", \"B\", \"C\" }"),
178             gst.Caps ("some/mime, _int = 1, list = \"A\""))
179         self.doSubtract(
180             gst.Caps ("some/mime, _double = (double) 1.0; other/mime, _int = { 1, 2 }"),
181             gst.Caps ("some/mime, _double = (double) 1.0"))
182
183     def testNoneValue(self):
184         caps = gst.Caps("foo")
185         
186         def invalid_assignment():
187             caps[0]["bar"] = None
188         self.assertRaises(TypeError, invalid_assignment)
189
190         def invalid_set_value():
191             caps[0].set_value("bar", None)
192         self.assertRaises(TypeError, invalid_set_value)
193
194
195 if __name__ == "__main__":
196     unittest.main()