~didrocks/ubuntuone-client/use_result_var

« back to all changes in this revision

Viewing changes to tests/platform/linux/test_messaging.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2011-02-11 16:18:11 UTC
  • mto: This revision was merged to the branch mainline in revision 67.
  • Revision ID: james.westby@ubuntu.com-20110211161811-n18dj9lde7dxqjzr
Tags: upstream-1.5.4
ImportĀ upstreamĀ versionĀ 1.5.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# tests.platform.linux.test_messaging
 
2
#
 
3
# Author: Eric Casteleijn <eric.casteleijn@canonical.com>
 
4
#
 
5
# Copyright 2010 Canonical Ltd.
 
6
#
 
7
# This program is free software: you can redistribute it and/or modify it
 
8
# under the terms of the GNU General Public License version 3, as published
 
9
# by the Free Software Foundation.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but
 
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
14
# PURPOSE.  See the GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along
 
17
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
"""Test the messaging on linux. These tests are kind of stupid, but at
 
20
least they ensure 100% coverage and hence no silly/syntax errors.
 
21
 
 
22
"""
 
23
 
 
24
 
 
25
from mocker import Mocker
 
26
from twisted.trial.unittest import TestCase
 
27
 
 
28
from ubuntuone.platform.linux.messaging import Messaging, hide_message
 
29
 
 
30
FAKE_TIME = 123
 
31
FAKE_TIME2 = 456
 
32
FAKE_COUNT = 11
 
33
FAKE_SENDER = "Mom"
 
34
FAKE_ICON = object()
 
35
 
 
36
 
 
37
def callback(indicator, message_time=None):
 
38
    """Dummy callback."""
 
39
    pass
 
40
 
 
41
 
 
42
class MessagingTestCase(TestCase):
 
43
    """Test the Messaging API."""
 
44
 
 
45
    def setUp(self):
 
46
        self.mocker = Mocker()
 
47
 
 
48
    def tearDown(self):
 
49
        self.mocker.restore()
 
50
        self.mocker.verify()
 
51
 
 
52
    # pylint: disable=R0913
 
53
    def _show_message_setup(self, message_time=None, message_count=None,
 
54
                            icon=None, update_count=None, hide=False):
 
55
        """Set up the mocker expectations for show_method."""
 
56
        indicate = self.mocker.replace("indicate")
 
57
        mock_server = self.mocker.mock()
 
58
        indicate.indicate_server_ref_default()
 
59
        self.mocker.result(mock_server)
 
60
        mock_server.set_type("message.u1")
 
61
        mock_server.set_desktop_file(
 
62
            "/usr/share/applications/ubuntuone-control-panel-gtk.desktop")
 
63
        mock_indicator = self.mocker.mock()
 
64
        indicate.Indicator()
 
65
        self.mocker.result(mock_indicator)
 
66
        mock_indicator.set_property("subtype", "u1")
 
67
        mock_indicator.set_property("name", FAKE_SENDER)
 
68
        mock_indicator.set_property("sender", FAKE_SENDER)
 
69
        mock_indicator.connect("user-display", callback)
 
70
        if icon is not None:
 
71
            mock_indicator.set_property_icon("icon", icon)
 
72
        if message_count is not None:
 
73
            mock_indicator.set_property("count", str(message_count))
 
74
        else:
 
75
            if message_time is None:
 
76
                mock_time = self.mocker.replace("time.time")
 
77
                mock_time()
 
78
                self.mocker.result(FAKE_TIME2)
 
79
                mock_indicator.set_property_time("time", FAKE_TIME2)
 
80
            else:
 
81
                mock_indicator.set_property_time("time", message_time)
 
82
        mock_indicator.set_property("draw-attention", "true")
 
83
        mock_server.show()
 
84
        if update_count:
 
85
            mock_indicator.get_property("count")
 
86
            self.mocker.result("1200")
 
87
            mock_indicator.set_property("count", '2500')
 
88
        if hide:
 
89
            mock_indicator.set_property('draw-attention', 'false')
 
90
            mock_indicator.hide()
 
91
        mock_indicator.show()
 
92
        self.mocker.replay()
 
93
    # pylint: enable=R0913
 
94
 
 
95
    def test_show_message(self):
 
96
        """On message, libnotify receives the proper calls."""
 
97
        self._show_message_setup()
 
98
        messaging = Messaging()
 
99
        messaging.show_message(FAKE_SENDER, callback)
 
100
 
 
101
    def test_show_message_with_time(self):
 
102
        """On message with time, libnotify receives the proper calls."""
 
103
        self._show_message_setup(FAKE_TIME)
 
104
        messaging = Messaging()
 
105
        messaging.show_message(FAKE_SENDER, callback, message_time=FAKE_TIME)
 
106
 
 
107
    def test_show_message_with_icon(self):
 
108
        """On message with icon, libnotify receives the proper calls."""
 
109
        self._show_message_setup(icon=FAKE_ICON)
 
110
        messaging = Messaging()
 
111
        messaging.show_message(FAKE_SENDER, callback, icon=FAKE_ICON)
 
112
 
 
113
    def test_show_message_with_count(self):
 
114
        """On message with count, libnotify receives the proper calls."""
 
115
        self._show_message_setup(message_count='1200')
 
116
        messaging = Messaging()
 
117
        messaging.show_message(FAKE_SENDER, callback, message_count=1200)
 
118
 
 
119
    def test_update_count(self):
 
120
        """On message count update, libnotify receives the proper calls."""
 
121
        self._show_message_setup(message_count='1200', update_count='1300')
 
122
        messaging = Messaging()
 
123
        indicator = messaging.show_message(
 
124
            FAKE_SENDER, callback, message_count=1200)
 
125
        messaging.update_count(indicator, 1300)
 
126
 
 
127
    def test_hide_message(self):
 
128
        """On message hide, libnotify receives the proper calls."""
 
129
        self._show_message_setup(hide=True)
 
130
        messaging = Messaging()
 
131
        mock_indicator = messaging.show_message(FAKE_SENDER, callback)
 
132
        hide_message(mock_indicator)