~ubuntu-branches/ubuntu/trusty/pitivi/trusty

« back to all changes in this revision

Viewing changes to tests/test_pipeline_action.py

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2011-07-07 13:43:47 UTC
  • mto: (6.1.9 sid) (1.2.12)
  • mto: This revision was merged to the branch mainline in revision 32.
  • Revision ID: james.westby@ubuntu.com-20110707134347-cari9kxjiakzej9z
Tags: upstream-0.14.1
ImportĀ upstreamĀ versionĀ 0.14.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# PiTiVi , Non-linear video editor
 
2
#
 
3
#       tests/test_pipeline_action.py
 
4
#
 
5
# Copyright (c) 2009, Edward Hervey <bilboed@bilboed.com>
 
6
#
 
7
# This program is free software; you can redistribute it and/or
 
8
# modify it under the terms of the GNU Lesser General Public
 
9
# License as published by the Free Software Foundation; either
 
10
# version 2.1 of the License, or (at your option) any later version.
 
11
#
 
12
# This program is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
# Lesser General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU Lesser General Public
 
18
# License along with this program; if not, write to the
 
19
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 
20
# Boston, MA 02110-1301, USA.
 
21
 
 
22
"""
 
23
Tests for interaction between action and pipeline
 
24
"""
 
25
 
 
26
import time
 
27
from unittest import TestCase, main
 
28
from pitivi.pipeline import Pipeline, STATE_READY, STATE_PLAYING, STATE_NULL
 
29
from pitivi.action import Action, STATE_ACTIVE, STATE_NOT_ACTIVE, ActionError
 
30
from pitivi.stream import MultimediaStream, VideoStream
 
31
from pitivi.factories.test import VideoTestSourceFactory
 
32
from common import TestCase
 
33
import common
 
34
import gst
 
35
 
 
36
 
 
37
class DynamicAction(Action):
 
38
    def getDynamicLinks(self, producer, stream):
 
39
        links = Action.getDynamicLinks(self, producer, stream)
 
40
        consumer = common.FakeSinkFactory()
 
41
 
 
42
        links.append((producer, consumer, stream, None))
 
43
        gst.debug("Returning link")
 
44
        return links
 
45
 
 
46
 
 
47
class TestPipelineAction(TestCase):
 
48
 
 
49
    def setUp(self):
 
50
        gst.debug("Test starting")
 
51
        TestCase.setUp(self)
 
52
 
 
53
    def testPipelineAction(self):
 
54
        """Testing pipeline state interaction"""
 
55
        p = Pipeline()
 
56
        a = Action()
 
57
        src = VideoTestSourceFactory()
 
58
        sink = common.FakeSinkFactory()
 
59
        sink.addInputStream(MultimediaStream(gst.Caps("any"), pad_name="sink"))
 
60
 
 
61
        # set the Action on the Pipeline
 
62
        p.setAction(a)
 
63
        self.assertEquals(p.actions, [a])
 
64
 
 
65
        # set the Producer and Consumer
 
66
        a.addProducers(src)
 
67
        a.addConsumers(sink)
 
68
 
 
69
        a.setLink(src, sink)
 
70
 
 
71
        # activate the Action
 
72
        a.activate()
 
73
 
 
74
        self.failUnlessEqual(src.current_bins, 1)
 
75
        self.failUnlessEqual(sink.current_bins, 1)
 
76
 
 
77
        # call get*ForFactoryStream(..., automake=False). They will raise
 
78
        # exceptions if the action didn't create the elements.
 
79
        bin = p.getBinForFactoryStream(src, automake=False)
 
80
        p.releaseBinForFactoryStream(src)
 
81
 
 
82
        tee = p.getTeeForFactoryStream(src, automake=False)
 
83
        p.releaseTeeForFactoryStream(src)
 
84
 
 
85
        bin = p.getBinForFactoryStream(sink, automake=False)
 
86
 
 
87
        queue = p.getQueueForFactoryStream(sink, automake=False)
 
88
 
 
89
        self.failUnlessEqual(queue.get_pad('src').get_peer().get_parent(), bin)
 
90
 
 
91
        p.releaseBinForFactoryStream(sink)
 
92
        p.releaseQueueForFactoryStream(sink)
 
93
 
 
94
        # switch to PLAYING
 
95
        p.setState(STATE_PLAYING)
 
96
 
 
97
        # wait half a second
 
98
 
 
99
        # switch to READY
 
100
        p.setState(STATE_READY)
 
101
 
 
102
        # deactivate action
 
103
        a.deactivate()
 
104
 
 
105
        # since we're the last Action to be release, the tees
 
106
        # and queues should have gone
 
107
        self.failUnlessEqual(src.current_bins, 0)
 
108
        self.failUnlessEqual(sink.current_bins, 0)
 
109
 
 
110
        # remove the action from the pipeline
 
111
        p.removeAction(a)
 
112
 
 
113
        # the gst.Pipeline should be empty !
 
114
        self.assertEquals(list(p._pipeline.elements()), [])
 
115
 
 
116
        p.release()
 
117
 
 
118
    def testPendingLink(self):
 
119
        a = Action()
 
120
        p = Pipeline()
 
121
        src = common.FakeGnlFactory()
 
122
        src.addOutputStream(VideoStream(gst.Caps("video/x-raw-yuv"),
 
123
                                        pad_name="src"))
 
124
        sink = common.FakeSinkFactory()
 
125
        sink.addInputStream(MultimediaStream(gst.Caps("any"),
 
126
                                             pad_name="sink"))
 
127
 
 
128
        # set the link, it will be activated once the pad is added
 
129
        a.setLink(src, sink)
 
130
        # Let's see if the link is present
 
131
        self.assertEquals(a._links, [(src, sink, None, None)])
 
132
 
 
133
        p.setAction(a)
 
134
 
 
135
        gst.debug("about to activate action")
 
136
        a.activate()
 
137
        # only the producer and the consumer are created, the other elements are
 
138
        # created dinamically
 
139
        self.assertEquals(len(list(p._pipeline.elements())), 2)
 
140
 
 
141
        p.setState(STATE_PLAYING)
 
142
        time.sleep(1)
 
143
        # and make sure that all other elements were created (4)
 
144
        # FIXME  if it's failing here, run the test a few times trying to raise
 
145
        # the time.sleep() above, it may just be racy...
 
146
        self.assertEquals(len(list(p._pipeline.elements())), 4)
 
147
 
 
148
        a.deactivate()
 
149
        p.setState(STATE_NULL)
 
150
        self.assertEquals(len(list(p._pipeline.elements())), 0)
 
151
        p.release()
 
152
 
 
153
    def testDynamicLink(self):
 
154
        a = DynamicAction()
 
155
        p = Pipeline()
 
156
        src = common.FakeGnlFactory()
 
157
        src.addOutputStream(VideoStream(gst.Caps("video/x-raw-yuv"),
 
158
                                        pad_name="src"))
 
159
 
 
160
        # the link will be added dynamically
 
161
        self.assertEquals(a._links, [])
 
162
 
 
163
        p.setAction(a)
 
164
        a.addProducers(src)
 
165
 
 
166
        self.assertEquals(len(list(p._pipeline.elements())), 0)
 
167
 
 
168
        a.activate()
 
169
        # theoretically... there shouldn't only be the source, since
 
170
        # the pad for the source hasn't been created yet (and therefore not
 
171
        # requiring a consumer
 
172
        self.assertEquals(len(list(p._pipeline.elements())), 1)
 
173
 
 
174
        p.setState(STATE_PLAYING)
 
175
        time.sleep(1)
 
176
        p.getState()
 
177
 
 
178
        # and make sure that all other elements were created (4)
 
179
        self.assertEquals(len(list(p._pipeline.elements())), 4)
 
180
 
 
181
        p.setState(STATE_READY)
 
182
        time.sleep(1)
 
183
        a.deactivate()
 
184
 
 
185
        self.assertEquals(len(list(p._pipeline.elements())), 0)
 
186
        p.setState(STATE_NULL)
 
187
 
 
188
        p.release()
 
189
 
 
190
if __name__ == "__main__":
 
191
    main()