File: misc.rb

package info (click to toggle)
ruby-gnome2 0.15.0-1.1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 7,704 kB
  • ctags: 8,558
  • sloc: ansic: 69,912; ruby: 19,511; makefile: 97; xml: 35; sql: 13
file content (418 lines) | stat: -rw-r--r-- 13,373 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418

# Copyright (C) 2003 Laurent Sansonetti <lrz@gnome.org>
#
# This file is part of Ruby/GStreamer.
# 
# Ruby/GStreamer 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.
#
# Ruby/GStreamer 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 Ruby/GStreamer; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

require 'test/unit'

module Test 
module Unit 
    class TestCase
        def assert_bool(obj)
            ok = (obj.is_a?(TrueClass) or obj.is_a?(FalseClass))
            _wrap_assertion { assert ok, "not a boolean" }
        end
        def assert_instance_of(klass, obj)
            _wrap_assertion { flunk("#{obj} is a #{obj.class} (expected #{klass})") } unless obj.is_a?(klass)
        end
        def assert_raises(klass)
            _wrap_assertion do
                begin
                    yield
                    flunk "code did not raise any exception"
                rescue => e
                    flunk "code raised exception #{e} (expected #{klass})" unless e.class == klass
                end
            end
        end
    end
end
end        

require 'gst'

Gst.init or raise "Could not initialize GStreamer!"
           
module Gst
class Object 
    def test(tc)
        # Test name
        tc.assert_instance_of(String, n = name)
        unless is_a?(Gst::PadTemplate)
            tc.assert set_name("foo") == self
            tc.assert name == "foo"
            tc.assert (name = "bar") == "bar"
            tc.assert set_name(n) == self 
        end
        # Test flags
        flags = {
            destroyed? => Gst::Object::FLAG_DESTROYED,
            floating?  => Gst::Object::FLAG_FLOATING
        }
        test_flags(tc, flags)
    end
    def test_flags(tc, flags)
        tc.assert_instance_of(Fixnum, f = self.flags)
        flags.each do |ok, flag|
            tc.assert_instance_of(Fixnum,  flag)
            tc.assert_bool ok
            tc.assert flag?(flag) == ok
            if ok
                tc.assert unset_flag(flag) == self
                tc.assert flag?(flag) == false
                tc.assert set_flag(flag) == self
            else
                tc.assert set_flag(flag) == self
                tc.assert flag?(flag) == true 
                tc.assert unset_flag(flag) == self 
            end    
        end
    end
end
class Clock
    def test(tc)
        # Test active
        tc.assert_bool active?
        # Test speed (should be always float)
        tc.assert_instance_of(Float, speed)
        # Test time (should be either Fixnum or Bignum)
        tc.assert_instance_of(Integer, time)
        # Test resolution (should be either Fixnum or Bignum)
        tc.assert_instance_of(Integer, resolution)
        # Test flags
        flags = {
            can_do_single_sync?    => Gst::Clock::FLAG_CAN_DO_SINGLE_SYNC,
            can_do_single_async?   => Gst::Clock::FLAG_CAN_DO_SINGLE_ASYNC,
            can_do_periodic_sync?  => Gst::Clock::FLAG_CAN_DO_PERIODIC_SYNC,
            can_do_periodic_async? => Gst::Clock::FLAG_CAN_DO_PERIODIC_ASYNC,
            can_set_resolution?    => Gst::Clock::FLAG_CAN_SET_RESOLUTION,
            can_set_speed?         => Gst::Clock::FLAG_CAN_SET_SPEED
        }
        test_flags(tc, flags)
    end  
end  
class Element
    def test(tc)
        # Test the element as a Gst::Object
        super(tc)
        # Test clock
        tc.assert_bool requires_clock?
        tc.assert_bool b = provides_clock?
        clock.test(tc) if (clock and b)
        # Test pads
        i = 0
        each_pad { |pad| pad.test(tc); i += 1 }
        tc.assert_instance_of(Array, a = pads)
        tc.assert_equal(a.size, i)
        a.each do |pad| 
            pad.test(tc)
            tc.assert_instance_of(Gst::Pad, pad2 = get_pad(pad.name))
            pad2.test(tc)
            tc.assert_equal(pad, pad2)
        end
        # Test flags
        flags = {
            complex?      => Gst::Element::FLAG_COMPLEX,
            decoupled?    => Gst::Element::FLAG_DECOUPLED,
            thread_suggested?  => Gst::Element::FLAG_THEAD_SUGGESTED,
            has_infinite_loop? => Gst::Element::FLAG_INFINITE_LOOP,
            has_new_loopfunc?  => Gst::Element::FLAG_NEW_LOOPFUNC, 
            event_aware?  => Gst::Element::FLAG_EVENT_AWARE,
            use_threadsafe_properties? => Gst::Element::FLAG_USE_THREADSAFE_PROPERTIES
        }
        test_flags(tc, flags)
        # Test indexable
        tc.assert_bool indexable?
    end
end        
class ElementFactory
    def test(tc) 
        # Test as a PluginFeature
        super(tc)
        # Test the string representation
        tc.assert_instance_of(String, to_s)
        # Test ranks
        ranks = {
            Gst::ElementFactory::RANK_MARGINAL  => rank_marginal?, 
            Gst::ElementFactory::RANK_NONE      => rank_none?,
            Gst::ElementFactory::RANK_PRIMARY   => rank_primary?,
            Gst::ElementFactory::RANK_SECONDARY => rank_secondary?
        }
        tc.assert_instance_of(Fixnum, r = rank) 
        ranks.each do |flag, ok|
            tc.assert_bool ok
            tc.assert_instance_of(Fixnum, flag)
            if ok
                tc.assert_equal(r, flag)
            else
                tc.assert_not_equal(r, flag)
            end
        end
        tc.assert_equal(ranks.values.collect { |v| v if v }.nitems, 1)
        # Test details
        tc.assert_instance_of(Hash, h = details) 
        keys = [ 
            "longname",
            "klass",
            "license",
            "description",
            "version",
            "author",
            "copyright"
        ]
        keys.each do |key|
            tc.assert_instance_of(String, h[key]) if h[key] 
        end
        tc.assert h.keys.size == keys.size
        # Test pad templates
        i = 0
        each_pad_template { |pad| pad.test(tc); i += 1 }
        tc.assert_instance_of(Array, a = pad_templates)
        tc.assert_equal(a.size, i)
        a.each { |pad| pad.test(tc) }
        # Test element creation
        tc.assert_instance_of(Gst::Element, e = create("an_element_#{name}"))
        e.test(tc)
        a = Array.new
        # Test unique auto-generated element names
        10.times { a.push(create) } 
        a.each { |e| tc.assert_instance_of(Gst::Element, e) }
        tc.assert_nil a.collect { |e| e.name }.uniq! 
    end
end
class Bin
    def test(tc) 
        # Test the bin as a Gst::Element
        super(tc)
        # Test clock
        clock.test(tc) if clock
        # Test flags
        flags = {
            manager?     => Gst::Bin::FLAG_MANAGER,
            schedulable? => Gst::Bin::FLAG_SCHEDULABLE,
            prefers_cothreads? => Gst::Bin::FLAG_PREFER_COTHREADS,
            has_fixed_clock?   => Gst::Bin::FLAG_FIXED_CLOCK
        }
        test_flags(tc, flags)
    end
end
class Plugin
    def test(tc)
        # Test loaded
        tc.assert_bool loaded?
        # Test name
        tc.assert_instance_of(String, n = name) 
        # Test longname (may be nil)
        if ln = longname
            tc.assert_instance_of(String, ln)
        end
        # Test filename (may be nil)
        if fn = filename
            tc.assert_instance_of(String, fn)
        end
        # Test features 
        i = 0
        each_feature { |f| f.test(tc); i += 1 }
        tc.assert_instance_of(Array, a = features) 
        tc.assert_equal(a.length, i)
        #a.each { |f| f.test(tc) }
    end
end
class PluginFeature
    def test(tc)
        # Test name
        tc.assert_instance_of(String, name) 
    end
end
class Format
    def test(tc)
        tc.assert_instance_of(Fixnum, id)
        tc.assert_instance_of(String, nick)
        tc.assert_instance_of(String, description)
        tc.assert Gst::Format.find(nick) == self 
    end
end
class QueryType
    def test(tc)
        tc.assert_instance_of(Fixnum, id)
        tc.assert_instance_of(String, nick)
        tc.assert_instance_of(String, description)
        tc.assert Gst::QueryType.find(nick) == self 
    end
end
class Pad
    def test(tc)
        # Test the pad as a Gst::Object
        super(tc)
        # Test name
        tc.assert_instance_of(String, name)
        # Test direction
        valid_directions = [
            Gst::Pad::DIRECTION_SRC,
            Gst::Pad::DIRECTION_SINK
        ]
        tc.assert_instance_of(Fixnum, dir  = direction)
        tc.assert valid_directions.include?(dir) 
        # Test pad template
        if pt = pad_template
            tc.assert_instance_of(Gst::PadTemplate, pt)
            pt.test(tc)
        end
        # Test formats
        i = 0
        each_format { |f| f.test(tc); i += 1 } 
        tc.assert_instance_of(Array, a  = formats)
        tc.assert a.length == i
        tc.assert_bool b = provides_formats?
        tc.assert a.length == 0 unless b
        a.each { |f| f.test(tc) }
        # Test query types
        i = 0
        each_query_type { |f| f.test(tc); i += 1 } 
        tc.assert_instance_of(Array, a  = query_types)
        tc.assert_equal(a.length, i)
        tc.assert_bool b = provides_query_types?
        tc.assert_equal(a.length, 0) unless b
        a.each { |f| f.test(tc) }
        # XXX Test event masks 
        # Test flags
        flags = {
            disabled?    => Gst::Pad::FLAG_DISABLED,
            negotiating? => Gst::Pad::FLAG_NEGOTIATING
        }
        test_flags(tc, flags)
    end
end
class PadTemplate
    def test(tc)
        # Test the padtemplate as a Gst::Object
        super(tc)
        # Test direction
        valid_directions = [
            Gst::Pad::DIRECTION_SRC,
            Gst::Pad::DIRECTION_SINK
        ]
        tc.assert_instance_of(Fixnum, dir = direction)
        tc.assert valid_directions.include?(dir) 
        # Test presence
        valid_presences = [
            Gst::Pad::PRESENCE_ALWAYS,
            Gst::Pad::PRESENCE_SOMETIMES,
            Gst::Pad::PRESENCE_REQUEST
        ]
        tc.assert_instance_of(Fixnum, pres  = presence)
        tc.assert valid_presences.include?(pres) 
        # Test caps
        tc.assert_bool b = has_caps?
        i = 0
        each_caps { |cap| cap.test(tc); i += 1 }
        tc.assert_instance_of(Array, a  = caps)
        tc.assert_equal(a.size, i)
        a.each { |cap| cap.test(tc) }
        tc.assert i > 0 if b
    end
end
class Caps
    def test(tc)
        # Test name 
        tc.assert_instance_of(String, name)
        # Test type id
        tc.assert_instance_of(Fixnum, type_id)
        # Test fixed
        tc.assert_bool fixed?
        # Test properties
        each_property do |name, value, is_fixed|
            tc.assert_instance_of(String, name)
            tc.assert_bool is_fixed
            tc.assert has_property?(name)    
            tc.assert_equal(has_fixed_property?(name), is_fixed)
        end
        # These tests are currently disabled to avoid critical messages.
        # But they work ;-)
        #tc.assert !has_property?("does_not_exist")
        #tc.assert !has_fixed_property?("does_not_exist_too")
    end
end
class Type
    def test(tc)
        # Test id
        tc.assert_instance_of(Fixnum, id)
        # Test mime (may be nil)
        tc.assert_instance_of(String, mime) if mime
        # Test exts (may be nil)
        tc.assert_instance_of(String, exts) if exts
        # Test Gst::Type find methods            
        a = [ Gst::Type.find_by_id(id), Gst::Type.find_by_mime(mime) ] 
        a.each do |t| 
            tc.assert_instance_of(Gst::Type, t)
            tc.assert_equal(self, t)
        end
    end
end
class TypeFactory
    def test(tc)
        # Test as a PluginFeature
        super(tc)
        # Test the string representation
        tc.assert_instance_of(String, to_s)
        # Test mime
        tc.assert_instance_of(String, mime) 
        # Test exts (may be nil)
        tc.assert_instance_of(String, exts) if exts 
    end
end   
class SchedulerFactory
    def test(tc)
        # Test as a PluginFeature
        super(tc)
        # Test the string representation
        tc.assert_instance_of(String, to_s)
    end
end   
class AutoplugFactory
    def test(tc)
        # Test as a PluginFeature
        super(tc)
        # Test the string representation
        tc.assert_instance_of(String, to_s)
    end
end   
class IndexFactory
    def test(tc)
        # Test as a PluginFeature
        super(tc)
        # Test the string representation
        tc.assert_instance_of(String, to_s)
    end
end   
class Registry
    def test(tc)
        tc.assert_instance_of(String, name)
        tc.assert_instance_of(String, details) if details
        tc.assert_bool loaded?
        i = 0
        each_path do |p|
            tc.assert_instance_of(String, p)
            i += 1
        end
        tc.assert_instance_of(Array, a = paths)
        tc.assert_equal(a.size, i)
        a.each { |p| tc.assert_instance_of(String, p) }
    end
end
end