~osomon/moovida/account_admin

« back to all changes in this revision

Viewing changes to elisa-plugins/elisa/plugins/pigment/tests/graph/test_image.py

  • Committer: Olivier Tilloy
  • Date: 2009-09-09 10:17:40 UTC
  • mfrom: (1498.3.43 resource_provider)
  • Revision ID: olivier@fluendo.com-20090909101740-wpdqtxgs6v0v3f1t
Merged the latest resource provider branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Moovida - Home multimedia server
 
3
# Copyright (C) 2006-2009 Fluendo Embedded S.L. (www.fluendo.com).
 
4
# All rights reserved.
 
5
#
 
6
# This file is available under one of two license agreements.
 
7
#
 
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.
 
11
#
 
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.
 
16
 
 
17
"""
 
18
Testing of L{elisa.plugins.pigment.graph.image.Image} class.
 
19
"""
 
20
 
 
21
import pgm
 
22
import glib
 
23
from pkg_resources import resource_filename
 
24
 
 
25
from twisted.trial.unittest import TestCase
 
26
 
 
27
from elisa.core.utils import defer
 
28
from elisa.plugins.pigment.graph.image import Image
 
29
 
 
30
 
 
31
class LoadingImageTestCase(TestCase):
 
32
    """
 
33
    Facilities for test cases involving image loading.
 
34
    """
 
35
 
 
36
    def setUp(self):
 
37
        self._flush_glib_main_context_sources()
 
38
        self.timeout = 2
 
39
 
 
40
        # necessary for in texture memory image caching (cloning)
 
41
        canvas = pgm.Canvas()
 
42
        Image.canvas = canvas
 
43
 
 
44
    def _flush_glib_main_context_sources(self):
 
45
        context = glib.main_context_default()
 
46
        while context.pending():
 
47
            context.iteration(True)
 
48
 
 
49
    def run_pending_image_loading(self):
 
50
        """
 
51
        Iterate the main loop in order to allow pending Pigment image loading
 
52
        to be completed.
 
53
 
 
54
        Warning: if no image loading was initiated that function will block
 
55
                 indefinitely.
 
56
        """
 
57
        context = glib.main_context_default()
 
58
        context.iteration(True)
 
59
 
 
60
    def signal_connect_after(self, emitter, signal, callback, *args, **kwargs):
 
61
        """
 
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
 
68
        emitted once.
 
69
 
 
70
        # FIXME: this generic code is copied/pasted from
 
71
                 pigment.tests.widgets.test_widget
 
72
        """
 
73
        dfr = defer.Deferred()
 
74
        id = None
 
75
        def callback_wrapper(*args, **kwargs):
 
76
            emitter.disconnect(id)
 
77
            try:
 
78
                callback(*args, **kwargs)
 
79
            except Exception, e:
 
80
                dfr.errback(e)
 
81
            else:
 
82
                dfr.callback(None)
 
83
 
 
84
        id = emitter.connect_after(signal, callback_wrapper, *args, **kwargs)
 
85
        return dfr
 
86
 
 
87
    def assertSignalEmitted(self, emitter, signal, expected_parameters=()):
 
88
        """
 
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}.
 
91
 
 
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.
 
94
 
 
95
        @rtype L{elisa.core.utils.defer.CancellableDeferred}
 
96
        """
 
97
        def callback(e, *parameters):
 
98
            self.assertEquals(parameters, expected_parameters)
 
99
 
 
100
        dfr = self.signal_connect_after(emitter, signal, callback)
 
101
        return dfr
 
102
 
 
103
 
 
104
class TestSetFromFileDeferred(LoadingImageTestCase):
 
105
    """
 
106
    Tests for method 'set_from_file_deferred'. 
 
107
    """
 
108
 
 
109
    def test_non_existant_image_file(self):
 
110
        raise NotImplementedError()
 
111
 
 
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 "\
 
119
                                        "deferred."
 
120
    def test_invalid_image_file(self):
 
121
        raise NotImplementedError()
 
122
 
 
123
    test_invalid_image_file.todo = test_non_existant_image_file.todo
 
124
 
 
125
    def test_valid_image_file(self):
 
126
        i = Image()
 
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()
 
132
 
 
133
        return defer.DeferredList([dfr, dfr_loading])
 
134
 
 
135
 
 
136
class TestSetFromFileWithCaching(LoadingImageTestCase):
 
137
    """
 
138
    Tests for method 'set_from_file_with_caching'. 
 
139
    """
 
140
 
 
141
    def test_valid_image_file(self):
 
142
        i = Image()
 
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()
 
148
 
 
149
        return defer.DeferredList([dfr, dfr_loading])