~ubuntu-branches/ubuntu/raring/software-center/raring-proposed

« back to all changes in this revision

Viewing changes to tests/gtk3/test_exhibits.py

  • Committer: Package Import Robot
  • Author(s): Michael Vogt
  • Date: 2012-10-11 15:33:05 UTC
  • mfrom: (195.1.18 quantal)
  • Revision ID: package-import@ubuntu.com-20121011153305-fm5ln7if3rpzts4n
Tags: 5.4.1.1
* lp:~mvo/software-center/reinstall-previous-purchase-token-fix:
  - fix reinstall previous purchases that have a system-wide
    license key LP: #1065481
* lp:~mvo/software-center/lp1060106:
  - Add missing gettext init for utils/update-software-center-agent
    (LP: #1060106)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
import unittest
 
3
 
 
4
from mock import patch, Mock
 
5
 
 
6
from tests.utils import (
 
7
    FakedCache,
 
8
    ObjectWithSignals,
 
9
    setup_test_env,
 
10
)
 
11
setup_test_env()
 
12
 
 
13
 
 
14
from softwarecenter.db.database import StoreDatabase
 
15
from softwarecenter.ui.gtk3.views import lobbyview
 
16
from softwarecenter.ui.gtk3.widgets.exhibits import (
 
17
    _HtmlRenderer,
 
18
    )
 
19
 
 
20
 
 
21
class ExhibitsTestCase(unittest.TestCase):
 
22
    """The test suite for the exhibits carousel."""
 
23
 
 
24
    def setUp(self):
 
25
        self.cache = FakedCache()
 
26
        self.db = StoreDatabase(cache=self.cache)
 
27
        self.lobby = lobbyview.LobbyView(cache=self.cache, db=self.db,
 
28
                                         icons=None, apps_filter=None)
 
29
        self.addCleanup(self.lobby.destroy)
 
30
 
 
31
    def _get_banner_from_lobby(self):
 
32
        return self.lobby.vbox.get_children()[-1].get_child()
 
33
 
 
34
    def test_featured_exhibit_by_default(self):
 
35
        """Show the featured exhibit before querying the remote service."""
 
36
        self.lobby._append_banner_ads()
 
37
 
 
38
        banner = self._get_banner_from_lobby()
 
39
        self.assertEqual(1, len(banner.exhibits))
 
40
        self.assertIsInstance(banner.exhibits[0], lobbyview.FeaturedExhibit)
 
41
 
 
42
    def test_no_exhibit_if_not_available(self):
 
43
        """The exhibit should not be shown if the package is not available."""
 
44
        exhibit = Mock()
 
45
        exhibit.package_names = u'foobarbaz'
 
46
 
 
47
        sca = ObjectWithSignals()
 
48
        sca.query_exhibits = lambda: sca.emit('exhibits', sca, [exhibit])
 
49
 
 
50
        with patch.object(lobbyview, 'SoftwareCenterAgent', lambda: sca):
 
51
            self.lobby._append_banner_ads()
 
52
 
 
53
        banner = self._get_banner_from_lobby()
 
54
        self.assertEqual(1, len(banner.exhibits))
 
55
        self.assertIsInstance(banner.exhibits[0], lobbyview.FeaturedExhibit)
 
56
 
 
57
    def test_exhibit_if_available(self):
 
58
        """The exhibit should be shown if the package is available."""
 
59
        exhibit = Mock()
 
60
        exhibit.package_names = u'foobarbaz'
 
61
        exhibit.banner_urls = ['banner']
 
62
        exhibit.title_translated = ''
 
63
 
 
64
        self.cache[u'foobarbaz'] = Mock()
 
65
 
 
66
        sca = ObjectWithSignals()
 
67
        sca.query_exhibits = lambda: sca.emit('exhibits', sca, [exhibit])
 
68
 
 
69
        with patch.object(lobbyview, 'SoftwareCenterAgent', lambda: sca):
 
70
            self.lobby._append_banner_ads()
 
71
 
 
72
        banner = self._get_banner_from_lobby()
 
73
        self.assertEqual(1, len(banner.exhibits))
 
74
        self.assertIs(banner.exhibits[0], exhibit)
 
75
 
 
76
    def test_exhibit_if_mixed_availability(self):
 
77
        """The exhibit should be shown even if some are not available."""
 
78
        # available exhibit
 
79
        exhibit = Mock()
 
80
        exhibit.package_names = u'foobarbaz'
 
81
        exhibit.banner_urls = ['banner']
 
82
        exhibit.title_translated = ''
 
83
 
 
84
        self.cache[u'foobarbaz'] = Mock()
 
85
 
 
86
        # not available exhibit
 
87
        other = Mock()
 
88
        other.package_names = u'not-there'
 
89
 
 
90
        sca = ObjectWithSignals()
 
91
        sca.query_exhibits = lambda: sca.emit('exhibits', sca,
 
92
                                              [exhibit, other])
 
93
 
 
94
        with patch.object(lobbyview, 'SoftwareCenterAgent', lambda: sca):
 
95
            self.lobby._append_banner_ads()
 
96
 
 
97
        banner = self._get_banner_from_lobby()
 
98
        self.assertEqual(1, len(banner.exhibits))
 
99
        self.assertIs(banner.exhibits[0], exhibit)
 
100
 
 
101
    def test_exhibit_with_url(self):
 
102
        # available exhibit
 
103
        exhibit = Mock()
 
104
        exhibit.package_names = ''
 
105
        exhibit.click_url = 'http://example.com'
 
106
        exhibit.banner_urls = ['banner']
 
107
        exhibit.title_translated = ''
 
108
 
 
109
        sca = ObjectWithSignals()
 
110
        sca.query_exhibits = lambda: sca.emit('exhibits', sca,
 
111
                                              [exhibit])
 
112
 
 
113
        with patch.object(lobbyview, 'SoftwareCenterAgent', lambda: sca):
 
114
            # add the banners
 
115
            self.lobby._append_banner_ads()
 
116
            # fake click
 
117
            alloc = self.lobby.exhibit_banner.get_allocation()
 
118
            mock_event = Mock()
 
119
            mock_event.x = alloc.x
 
120
            mock_event.y = alloc.y
 
121
            with patch.object(self.lobby.exhibit_banner, 'emit') as mock_emit:
 
122
                self.lobby.exhibit_banner.on_button_press(None, mock_event)
 
123
                self.lobby.exhibit_banner.on_button_release(None, mock_event)
 
124
                mock_emit.assert_called()
 
125
                signal_name = mock_emit.call_args[0][0]
 
126
                call_exhibit = mock_emit.call_args[0][1]
 
127
                self.assertEqual(signal_name, "show-exhibits-clicked")
 
128
                self.assertEqual(call_exhibit.click_url, "http://example.com")
 
129
 
 
130
    def test_exhibit_with_featured_exhibit(self):
 
131
        """ regression test for bug #1023777 """
 
132
        sca = ObjectWithSignals()
 
133
        sca.query_exhibits = lambda: sca.emit('exhibits', sca,
 
134
                                              [lobbyview.FeaturedExhibit()])
 
135
 
 
136
        with patch.object(lobbyview, 'SoftwareCenterAgent', lambda: sca):
 
137
            # add the banners
 
138
            self.lobby._append_banner_ads()
 
139
            # fake click
 
140
            alloc = self.lobby.exhibit_banner.get_allocation()
 
141
            mock_event = Mock()
 
142
            mock_event.x = alloc.x
 
143
            mock_event.y = alloc.y
 
144
            with patch.object(self.lobby, 'emit') as mock_emit:
 
145
                self.lobby.exhibit_banner.on_button_press(None, mock_event)
 
146
                self.lobby.exhibit_banner.on_button_release(None, mock_event)
 
147
                mock_emit.assert_called()
 
148
                signal_name = mock_emit.call_args[0][0]
 
149
                call_category = mock_emit.call_args[0][1]
 
150
                self.assertEqual(signal_name, "category-selected")
 
151
                self.assertEqual(call_category.name, "Our star apps")
 
152
 
 
153
 
 
154
class HtmlRendererTestCase(unittest.TestCase):
 
155
 
 
156
    def test_multiple_images(self):
 
157
        downloader = ObjectWithSignals()
 
158
        downloader.download_file = lambda *args, **kwargs: downloader.emit(
 
159
            "file-download-complete", downloader, os.path.basename(args[0]))
 
160
 
 
161
        with patch("softwarecenter.ui.gtk3.widgets.exhibits."
 
162
                   "SimpleFileDownloader", lambda: downloader):
 
163
            renderer = _HtmlRenderer()
 
164
            mock_exhibit = Mock()
 
165
            mock_exhibit.banner_urls = [
 
166
                "http://example.com/path1/banner1.png",
 
167
                "http://example.com/path2/banner2.png",
 
168
                ]
 
169
            mock_exhibit.html = "url('/path1/banner1.png')#"\
 
170
                                "url('/path2/banner2.png')"
 
171
 
 
172
            renderer.set_exhibit(mock_exhibit)
 
173
            # assert the stuff we expected to get downloaded got downloaded
 
174
            self.assertEqual(
 
175
                renderer._downloaded_banner_images,
 
176
                ["banner1.png", "banner2.png"])
 
177
            # test that the path mangling worked
 
178
            self.assertEqual(
 
179
                mock_exhibit.html, "url('banner1.png')#url('banner2.png')")
 
180
 
 
181
if __name__ == "__main__":
 
182
    unittest.main()