1
# -*- coding: utf-8 -*-
2
# Moovida - Home multimedia server
3
# Copyright (C) 2006-2009 Fluendo Embedded S.L. (www.fluendo.com).
6
# This file is available under one of two license agreements.
8
# This file is licensed under the GPL version 3.
9
# See "LICENSE.GPL" in the root of this distribution including a special
10
# exception to use Moovida with Fluendo's plugins.
12
# The GPL part of Moovida is also available under a commercial licensing
13
# agreement from Fluendo.
14
# See "LICENSE.Moovida" in the root directory of this distribution package
15
# for details on that license.
18
Testing of L{elisa.plugins.pigment.graph.image.Image} class.
23
from pkg_resources import resource_filename
25
from twisted.trial.unittest import TestCase
27
from elisa.core.utils import defer
28
from elisa.plugins.pigment.graph.image import Image
31
class LoadingImageTestCase(TestCase):
33
Facilities for test cases involving image loading.
37
self._flush_glib_main_context_sources()
40
# necessary for in texture memory image caching (cloning)
44
def _flush_glib_main_context_sources(self):
45
context = glib.main_context_default()
46
while context.pending():
47
context.iteration(True)
49
def run_pending_image_loading(self):
51
Iterate the main loop in order to allow pending Pigment image loading
54
Warning: if no image loading was initiated that function will block
57
context = glib.main_context_default()
58
context.iteration(True)
60
def signal_connect_after(self, emitter, signal, callback, *args, **kwargs):
62
Connect L{callback} to L{signal} emitted by L{emitter} object and
63
return a deferred that will fail if the callback raises an exception
64
and succeed otherwise.
65
L{callback} is added to the signal handler list after the default class
66
signal handler; see gobject.GObject.connect_object_after.
67
Disconnection is performed automatically when the signal has been
70
# FIXME: this generic code is copied/pasted from
71
pigment.tests.widgets.test_widget
73
dfr = defer.Deferred()
75
def callback_wrapper(*args, **kwargs):
76
emitter.disconnect(id)
78
callback(*args, **kwargs)
84
id = emitter.connect_after(signal, callback_wrapper, *args, **kwargs)
87
def assertSignalEmitted(self, emitter, signal, expected_parameters=()):
89
Return a deferred that will succeed if L{signal} is emitted by
90
L{emitted} and the parameters sent are equal to L{expected_parameters}.
92
If the signal is not emitted the test will hang. To prevent that make
93
sure that the test case has a timeout set.
95
@rtype L{elisa.core.utils.defer.CancellableDeferred}
97
def callback(e, *parameters):
98
self.assertEquals(parameters, expected_parameters)
100
dfr = self.signal_connect_after(emitter, signal, callback)
104
class TestSetFromFileDeferred(LoadingImageTestCase):
106
Tests for method 'set_from_file_deferred'.
109
def test_non_existant_image_file(self):
110
raise NotImplementedError()
112
test_non_existant_image_file.todo = "Since Pigment does not report missing "\
113
"files, the way to test it is to wait "\
114
"enough time and make sure that the "\
115
"file-loaded signal was not emitted. "\
116
"That should be done in "\
117
"Image.set_from_file_deferred that "\
118
"should call errback on the returned "\
120
def test_invalid_image_file(self):
121
raise NotImplementedError()
123
test_invalid_image_file.todo = test_non_existant_image_file.todo
125
def test_valid_image_file(self):
127
image_path = resource_filename("elisa.plugins.pigment",
128
"tests/data/little_star.png")
129
dfr = self.assertSignalEmitted(i, "file-loaded")
130
dfr_loading = i.set_from_file_deferred(image_path)
131
self.run_pending_image_loading()
133
return defer.DeferredList([dfr, dfr_loading])
136
class TestSetFromFileWithCaching(LoadingImageTestCase):
138
Tests for method 'set_from_file_with_caching'.
141
def test_valid_image_file(self):
143
image_path = resource_filename("elisa.plugins.pigment",
144
"tests/data/little_star.png")
145
dfr = self.assertSignalEmitted(i, "file-loaded")
146
dfr_loading = i.set_from_file_with_caching(image_path)
147
self.run_pending_image_loading()
149
return defer.DeferredList([dfr, dfr_loading])