Merging gst-python
[platform/upstream/gstreamer.git] / testsuite / old / test_iterator.py
1 # -*- Mode: Python -*-
2 # vi:si:et:sw=4:sts=4:ts=4
3 #
4 # Copyright (C) 2005 Johan Dahlin
5 #
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
10 #
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19
20 import unittest
21 from common import gst, TestCase
22
23 class IteratorTest(TestCase):
24     # XXX: Elements
25     def testBinIterateElements(self):
26         pipeline = gst.parse_launch("fakesrc name=src ! fakesink name=sink")
27         elements = list(pipeline.elements())
28         fakesrc = pipeline.get_by_name("src")
29         fakesink = pipeline.get_by_name("sink")
30         
31         self.assertEqual(len(elements), 2)
32         self.failUnless(fakesrc in elements)
33         self.failUnless(fakesink in elements)
34
35         pipeline.remove(fakesrc)
36         elements = list(pipeline.elements())
37
38         self.assertEqual(len(elements), 1)
39         self.failUnless(not fakesrc in pipeline)
40
41         # XXX : There seems to be a problem about the GType
42         #       set in gst_bin_iterated_sorted
43
44     def testBinIterateSorted(self):
45         pipeline =  gst.parse_launch("fakesrc name=src ! fakesink name=sink")
46         elements = list(pipeline.sorted())
47         fakesrc = pipeline.get_by_name("src")
48         fakesink = pipeline.get_by_name("sink")
49
50         self.assertEqual(elements[0], fakesink)
51         self.assertEqual(elements[1], fakesrc)
52
53     def testBinIterateRecurse(self):
54         pipeline = gst.parse_launch("fakesrc name=src ! fakesink name=sink")
55         elements = list(pipeline.recurse())
56         fakesrc = pipeline.get_by_name("src")
57         fakesink = pipeline.get_by_name("sink")
58
59         self.assertEqual(elements[0], fakesink)
60         self.assertEqual(elements[1], fakesrc)
61
62     def testBinIterateSinks(self):
63         pipeline = gst.parse_launch("fakesrc name=src ! fakesink name=sink")
64         elements = list(pipeline.sinks())
65         fakesrc = pipeline.get_by_name("src")
66         fakesink = pipeline.get_by_name("sink")
67
68         self.assertEqual(len(elements), 1)
69         self.failUnless(fakesink in elements)
70         self.failUnless(not fakesrc in elements)
71
72     
73     def testIteratePadsFakeSrc(self):
74         fakesrc = gst.element_factory_make('fakesrc')
75         pads = list(fakesrc.pads())
76         srcpad = fakesrc.get_pad('src')
77         self.assertEqual(len(pads), 1)
78         self.assertEqual(pads[0], srcpad)
79         srcpads = list(fakesrc.src_pads())
80         self.assertEqual(len(srcpads), 1)
81         self.assertEqual(srcpads[0], srcpad)
82         sinkpads = list(fakesrc.sink_pads())
83         self.assertEqual(sinkpads, [])
84
85         self.assertEqual(len(list(fakesrc)), 1)
86         for pad in fakesrc:
87             self.assertEqual(pad, srcpad)
88             break
89         else:
90             raise AssertionError
91         
92     def testIteratePadsFakeSink(self):
93         fakesink = gst.element_factory_make('fakesink')
94         pads = list(fakesink.pads())
95         sinkpad = fakesink.get_pad('sink')
96         self.assertEqual(len(pads), 1)
97         self.assertEqual(pads[0], sinkpad)
98         srcpads = list(fakesink.src_pads())
99         self.assertEqual(srcpads, [])
100         sinkpads = list(fakesink.sink_pads())
101         self.assertEqual(len(sinkpads), 1)
102         self.assertEqual(sinkpads[0], sinkpad)
103
104         self.assertEqual(len(list(fakesink)), 1)
105         for pad in fakesink:
106             self.assertEqual(pad, sinkpad)
107             break
108         else:
109             raise AssertionError
110
111     def testInvalidIterator(self):
112         p = gst.Pad("p", gst.PAD_SRC)
113         # The C function will return NULL, we should
114         # therefore have an exception raised
115         self.assertRaises(TypeError, p.iterate_internal_links)
116         del p
117