test_element.py 9.68 KB
Newer Older
1 2
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
3
#
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# 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
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
22

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

25 26
import sys

27 28 29 30 31 32
# since I can't subclass gst.Element for some reason, I use a bin here
# it don't matter to Jesus
class TestElement(gst.Bin):
    def break_it_down(self):
        self.debug('Hammer Time')

33
class ElementTest(TestCase):
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
34 35
    name = 'fakesink'
    alias = 'sink'
36

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
37 38 39 40 41 42
    def testGoodConstructor(self):
        element = gst.element_factory_make(self.name, self.alias)
        assert element is not None, 'element is None'
        assert isinstance(element, gst.Element)
        assert element.get_name() == self.alias

Edward Hervey's avatar
Edward Hervey committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
## FIXME : Make a new test for state changes, using bus signals
        
## class FakeSinkTest(ElementTest):
##     FAKESINK_STATE_ERROR_NONE           = "0"
##     FAKESINK_STATE_ERROR_NULL_READY,    = "1"
##     FAKESINK_STATE_ERROR_READY_PAUSED,  = "2"
##     FAKESINK_STATE_ERROR_PAUSED_PLAYING = "3"
##     FAKESINK_STATE_ERROR_PLAYING_PAUSED = "4"
##     FAKESINK_STATE_ERROR_PAUSED_READY   = "5"
##     FAKESINK_STATE_ERROR_READY_NULL     = "6"

##     name = 'fakesink'
##     alias = 'sink'
##     def setUp(self):
##         ElementTest.setUp(self)
##         self.element = gst.element_factory_make('fakesink', 'sink')

##     def tearDown(self):
##         self.element.set_state(gst.STATE_NULL)
##         del self.element
##         ElementTest.tearDown(self)

##     def checkError(self, old_state, state, name):
##         assert self.element.get_state() == gst.STATE_NULL
##         assert self.element.set_state(old_state)
##         assert self.element.get_state() == old_state
##         self.element.set_property('state-error', name)
##         self.error = False
##         def error_cb(element, source, gerror, debug):
##             assert isinstance(element, gst.Element)
##             assert element == self.element
##             assert isinstance(source, gst.Element)
##             assert source == self.element
##             assert isinstance(gerror, gst.GError)
##             self.error = True
Johan Dahlin's avatar
Johan Dahlin committed
78
            
Edward Hervey's avatar
Edward Hervey committed
79 80 81 82
##         self.element.connect('error', error_cb)
##         self.element.set_state (state)
##         assert self.error, 'error not set'
##         #assert error_message.find('ERROR') != -1
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
83
        
Edward Hervey's avatar
Edward Hervey committed
84
##         self.element.get_state() == old_state, 'state changed'
Johan Dahlin's avatar
Johan Dahlin committed
85
        
Edward Hervey's avatar
Edward Hervey committed
86 87 88
##     def testStateErrorNullReady(self):
##         self.checkError(gst.STATE_NULL, gst.STATE_READY,
##                         self.FAKESINK_STATE_ERROR_NULL_READY)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
89
        
Edward Hervey's avatar
Edward Hervey committed
90 91 92
##     def testStateErrorReadyPaused(self):
##         self.checkError(gst.STATE_READY, gst.STATE_PAUSED,
##                         self.FAKESINK_STATE_ERROR_READY_PAUSED)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
93
        
Edward Hervey's avatar
Edward Hervey committed
94 95 96
##     def testStateErrorPausedPlaying(self):
##         self.checkError(gst.STATE_PAUSED, gst.STATE_PLAYING,
##                         self.FAKESINK_STATE_ERROR_PAUSED_PLAYING)        
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
97

Edward Hervey's avatar
Edward Hervey committed
98 99 100
##     def testStateErrorPlayingPaused(self):
##         self.checkError(gst.STATE_PLAYING, gst.STATE_PAUSED,
##                         self.FAKESINK_STATE_ERROR_PLAYING_PAUSED)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
101
        
Edward Hervey's avatar
Edward Hervey committed
102 103 104
##     def testStateErrorPausedReady(self):
##         self.checkError(gst.STATE_PAUSED, gst.STATE_READY,
##                         self.FAKESINK_STATE_ERROR_PAUSED_READY)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
105

Edward Hervey's avatar
Edward Hervey committed
106 107 108
##     def testStateErrorReadyNull(self):
##         self.checkError(gst.STATE_READY, gst.STATE_NULL,
##                         self.FAKESINK_STATE_ERROR_READY_NULL)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
109

Edward Hervey's avatar
Edward Hervey committed
110 111 112 113 114 115
##     def checkStateChange(self, old, new):
##         def state_change_cb(element, old_s, new_s):
##             assert isinstance(element, gst.Element)
##             assert element == self.element
##             assert old_s == old
##             assert new_s == new
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
116
            
Edward Hervey's avatar
Edward Hervey committed
117 118
##         assert self.element.set_state(old)
##         assert self.element.get_state(0.0)[1] == old
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
119

Edward Hervey's avatar
Edward Hervey committed
120 121
## # FIXME: replace with messages
## #        self.element.connect('state-change', state_change_cb)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
122

Edward Hervey's avatar
Edward Hervey committed
123 124
##         assert self.element.set_state(new)
##         assert self.element.get_state(0.0)[1] == new
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
125
        
Edward Hervey's avatar
Edward Hervey committed
126 127
##     def testStateChangeNullReady(self):
##         self.checkStateChange(gst.STATE_NULL, gst.STATE_READY)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
128
        
Edward Hervey's avatar
Edward Hervey committed
129 130
##     def testStateChangeReadyPaused(self):
##         self.checkStateChange(gst.STATE_READY, gst.STATE_PAUSED)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
131

Edward Hervey's avatar
Edward Hervey committed
132 133
##     def testStateChangePausedPlaying(self):
##         self.checkStateChange(gst.STATE_PAUSED, gst.STATE_PLAYING)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
134
        
Edward Hervey's avatar
Edward Hervey committed
135 136
##     def testStateChangePlayingPaused(self):
##         self.checkStateChange(gst.STATE_PLAYING, gst.STATE_PAUSED)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
137
        
Edward Hervey's avatar
Edward Hervey committed
138 139
##     def testStateChangePausedReady(self):
##         self.checkStateChange(gst.STATE_PAUSED, gst.STATE_READY)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
140

Edward Hervey's avatar
Edward Hervey committed
141 142
##     def testStateChangeReadyNull(self):
##         self.checkStateChange(gst.STATE_READY, gst.STATE_NULL)
143

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
class NonExistentTest(ElementTest):
    name = 'this-element-does-not-exist'
    alias = 'no-alias'
    
    testGoodConstructor = lambda s: None
    testGoodConstructor2 = lambda s: None

class FileSrcTest(ElementTest):
    name = 'filesrc'
    alias = 'source'
    
class FileSinkTest(ElementTest):
    name = 'filesink'
    alias = 'sink'

Edward Hervey's avatar
Edward Hervey committed
159
class ElementName(TestCase):
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
160 161 162 163 164 165 166 167 168 169 170
    def testElementStateGetName(self):
        get_name = gst.element_state_get_name
        for state in ('NULL',
                      'READY',
                      'PLAYING',
                      'PAUSED'):
            name = 'STATE_' + state
            assert hasattr(gst, name)
            attr = getattr(gst, name)
            assert get_name(attr) == state
            
171
        assert get_name(gst.STATE_VOID_PENDING) == 'VOID_PENDING'
Edward Hervey's avatar
Edward Hervey committed
172
        assert get_name(-1) == 'UNKNOWN!(-1)'
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
173 174
        self.assertRaises(TypeError, get_name, '')
        
175
class QueryTest(TestCase):
176
    def setUp(self):
177
        TestCase.setUp(self)
178
        self.pipeline = gst.parse_launch('fakesrc name=source ! fakesink')
179 180
        self.assertEquals(self.pipeline.__gstrefcount__, 1)

181
        self.element = self.pipeline.get_by_name('source')
182 183
        self.assertEquals(self.pipeline.__gstrefcount__, 1)
        self.assertEquals(self.element.__gstrefcount__, 2)
184
        self.assertEquals(sys.getrefcount(self.element), pygobject_2_13 and 2 or 3)
185 186 187 188

    def tearDown(self):
        del self.pipeline
        del self.element
189
        TestCase.tearDown(self)
190 191
        
    def testQuery(self):
192
        gst.debug('querying fakesrc in FORMAT_BYTES')
Edward Hervey's avatar
Edward Hervey committed
193
        res = self.element.query_position(gst.FORMAT_BYTES)
194
        self.assertEquals(self.pipeline.__gstrefcount__, 1)
195
        self.assertEquals(sys.getrefcount(self.pipeline), pygobject_2_13 and 2 or 3)
196
        self.assertEquals(self.element.__gstrefcount__, 2)
197
        self.assertEquals(sys.getrefcount(self.element), pygobject_2_13 and 2 or 3)
Edward Hervey's avatar
Edward Hervey committed
198 199
        assert res
        assert res[0] == 0
200 201
        self.assertRaises(gst.QueryError, self.element.query_position,
            gst.FORMAT_TIME)
202
        self.gccollect()
203

204
class QueueTest(TestCase):
205 206 207
    def testConstruct(self):
        queue = gst.element_factory_make('queue')
        assert queue.get_name() == 'queue0'
208 209
        self.assertEquals(queue.__gstrefcount__, 1)

210
class DebugTest(TestCase):
211 212 213 214 215 216 217 218
    def testDebug(self):
        e = gst.element_factory_make('fakesrc')
        e.error('I am an error string')
        e.warning('I am a warning string')
        e.info('I am an info string')
        e.debug('I am a debug string')
        e.log('I am a log string')
        e.debug('I am a formatted %s %s' % ('log', 'string'))
219 220 221 222 223

    def testElementDebug(self):
        e = TestElement("testelement")
        e.set_property("name", "testelement")
        e.break_it_down()
224
        
225
class LinkTest(TestCase):
226 227 228 229 230
    def testLinkNoPads(self):
        src = gst.Bin()
        sink = gst.Bin()
        self.assertRaises(gst.LinkError, src.link, sink)

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    def testLink(self):
        src = gst.element_factory_make('fakesrc')
        sink = gst.element_factory_make('fakesink')
        self.failUnless(src.link(sink))
        # FIXME: this unlink leaks, no idea why
        # src.unlink(sink)
        # print src.__gstrefcount__

    def testLinkPads(self):
        src = gst.element_factory_make('fakesrc')
        sink = gst.element_factory_make('fakesink')
        # print src.__gstrefcount__
        self.failUnless(src.link_pads("src", sink, "sink"))
        src.unlink_pads("src", sink, "sink")

    def testLinkFiltered(self):
        # a filtered link uses capsfilter and thus needs a bin
        bin = gst.Bin()
        src = gst.element_factory_make('fakesrc')
        sink = gst.element_factory_make('fakesink')
        bin.add(src, sink)
        caps = gst.caps_from_string("audio/x-raw-int")

        self.failUnless(src.link(sink, caps))

        # DANGER WILL.  src is not actually connected to sink, since
        # there's a capsfilter in the way.  What a leaky abstraction.
        # FIXME
        # src.unlink(sink)

        # instead, mess with pads directly
        pad = src.get_pad('src')
        pad.unlink(pad.get_peer())
        pad = sink.get_pad('sink')
        pad.get_peer().unlink(pad)

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