~diegosarmentero/ubuntuone-client/refactor-messaging

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
# tests.platform.linux.test_messaging
#
# Author: Eric Casteleijn <eric.casteleijn@canonical.com>
#
# Copyright 2010-2012 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the
# OpenSSL library under certain conditions as described in each
# individual source file, and distribute linked combinations
# including the two.
# You must obey the GNU General Public License in all respects
# for all of the code used other than OpenSSL.  If you modify
# file(s) with this exception, you may extend this exception to your
# version of the file(s), but you are not obligated to do so.  If you
# do not wish to do so, delete this exception statement from your
# version.  If you delete this exception statement from all source
# files in the program, then also delete it here.
"""
Test the messaging on linux. These tests are kind of stupid, but at
least they ensure 100% coverage and hence no silly/syntax errors.
"""

import sys

from mocker import Mocker, ANY
from twisted.internet import defer
from twisted.trial.unittest import TestCase
from ubuntuone.devtools.testcases import skipIf

from ubuntuone.platform.messaging.linux import (
    Messaging,
    _server_callback,
    indicate,
)

FAKE_TIME = 123
FAKE_TIME2 = 456
FAKE_COUNT = 11
FAKE_SENDER = "Mom"
FAKE_ICON = object()


def callback(indicator, message_time=None):
    """Dummy callback."""
    pass


@skipIf(indicate is None, "No valid Indicate found.")
class MessagingTestCase(TestCase):
    """Test the Messaging API."""

    @defer.inlineCallbacks
    def setUp(self):
        yield super(MessagingTestCase, self).setUp()
        self.mocker = Mocker()

    @defer.inlineCallbacks
    def tearDown(self):
        yield super(MessagingTestCase, self).tearDown()
        self.mocker.restore()
        self.mocker.verify()

    # pylint: disable=R0913
    def _show_message_setup(self, message_time=None, message_count=None,
                            icon=None, update_count=None, real_callback=False):
        """Set up the mocker expectations for show_method."""
        mock_server = self.mocker.mock()
        if 'gobject' in sys.modules and sys.modules['gobject'] is not None:
            indicate = self.mocker.replace("indicate")
            indicate.indicate_server_ref_default()
        else:
            indicate = self.mocker.replace('gi.repository.Indicate')
            indicate.Server.ref_default()
        self.mocker.result(mock_server)
        mock_server.connect('server-display', _server_callback)
        mock_server.set_type("message.u1")
        mock_server.set_desktop_file(
            "/usr/share/applications/ubuntuone-installer.desktop")
        mock_server.show()
        mock_indicator = self.mocker.mock()
        indicate.Indicator()
        self.mocker.result(mock_indicator)
        mock_indicator.set_property("subtype", "u1")
        mock_indicator.set_property("name", FAKE_SENDER)
        mock_indicator.set_property("sender", FAKE_SENDER)
        mock_indicator.connect("user-display", ANY)
        if icon is not None:
            mock_indicator.set_property_icon("icon", icon)
        if message_count is not None:
            mock_indicator.set_property("count", str(message_count))
        else:
            if message_time is not None:
                mock_indicator.set_property_time("time", message_time)
        mock_indicator.set_property("draw-attention", "true")
        if update_count:
            mock_indicator.get_property("count")
            self.mocker.result("1200")
            mock_indicator.set_property("count", '2500')
        mock_indicator.show()
        if real_callback:
            open_volumes = self.mocker.replace(
                "ubuntuone.platform.linux.messaging.open_volumes")
            open_volumes()
            mock_indicator.set_property('draw-attention', 'false')
            mock_indicator.hide()
        self.mocker.replay()
    # pylint: enable=R0913

    def test_show_message(self):
        """On message, libnotify receives the proper calls."""
        self._show_message_setup()
        messaging = Messaging()
        messaging.show_message(FAKE_SENDER, callback)

    def test_show_message_with_time(self):
        """On message with time, libnotify receives the proper calls."""
        self._show_message_setup(FAKE_TIME)
        messaging = Messaging()
        messaging.show_message(FAKE_SENDER, callback, message_time=FAKE_TIME)

    def test_show_message_with_icon(self):
        """On message with icon, libnotify receives the proper calls."""
        self._show_message_setup(icon=FAKE_ICON)
        messaging = Messaging()
        messaging.show_message(FAKE_SENDER, callback, icon=FAKE_ICON)

    def test_show_message_with_count(self):
        """On message with count, libnotify receives the proper calls."""
        self._show_message_setup(message_count='1200')
        messaging = Messaging()
        messaging.show_message(FAKE_SENDER, callback, message_count=1200)

    def test_update_count(self):
        """On message count update, libnotify receives the proper calls."""
        self._show_message_setup(message_count='1200', update_count='1300')
        messaging = Messaging()
        messaging.show_message(
            FAKE_SENDER, callback, message_count=1200)
        messaging.update_count(messaging.indicators[-1], 1300)

    def test_callback(self):
        """When the callback fires, libnotify receives the proper calls."""
        self._show_message_setup(real_callback=True)
        messaging = Messaging()
        messaging.show_message(FAKE_SENDER)
        self.assertEquals(1, len(messaging.indicators))
        actual_callback = messaging.create_callback()
        actual_callback(messaging.indicators[-1])
        self.assertEquals(0, len(messaging.indicators))