~didrocks/ubuntuone-client/use_result_var

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
# tests.platform.linux.test_event_logging
#
# Author: Alejandro J. Cura <alecu@canonical.com>
#
# Copyright 2010 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/>.
"""Test the event logging on linux."""

import sys

from twisted.trial.unittest import TestCase

from ubuntuone.platform.linux import event_logging


class ZeitgeistDetectionTestCase(TestCase):
    """Test the is_zeitgeist_installed function."""

    def patch_module(self, module_name, fake_module):
        """Monkey patch a module for the duration of the test."""
        UNDEFINED = object()
        real_module = sys.modules.get(module_name, UNDEFINED)

        def restore():
            """Restore the real_module."""
            if real_module is UNDEFINED:
                del(sys.modules[module_name])
            else:
                sys.modules[module_name] = real_module

        self.addCleanup(restore)
        sys.modules[module_name] = fake_module

    def test_zg_installed_returns_true(self):
        """When zg is installed, it returns true."""
        self.patch_module("zeitgeist", object())
        self.patch_module("zeitgeist.mimetypes", object())
        result = event_logging.is_zeitgeist_installed()
        self.assertEqual(result, True)

    def test_zg_not_installed_returns_false(self):
        """When zg is not installed, it returns false."""
        self.patch_module("zeitgeist", None)
        result = event_logging.is_zeitgeist_installed()
        self.assertEqual(result, False)

    def test_old_zg_installed_returns_false(self):
        """When an old zg is installed, it returns false."""
        self.patch_module("zeitgeist", object())
        self.patch_module("zeitgeist.mimetypes", None)
        result = event_logging.is_zeitgeist_installed()
        self.assertEqual(result, False)


class GetListenerTestCase(TestCase):
    """The zg listener is created."""

    def test_zeitgeist_installed_returns_listener(self):
        """get_listener returns a listener if ZG installed."""
        self.patch(event_logging, "is_zeitgeist_installed", lambda: True)
        listener = event_logging.get_listener(None, None)
        self.assertNotEqual(listener, None)

    def test_zeitgeist_not_installed_returns_none(self):
        """get_listener returns None if ZG not installed."""
        self.patch(event_logging, "is_zeitgeist_installed", lambda: False)
        listener = event_logging.get_listener(None, None)
        self.assertEqual(listener, None)