4
from mock import patch, Mock
6
from tests.utils import (
14
from softwarecenter.db.database import StoreDatabase
15
from softwarecenter.ui.gtk3.views import lobbyview
16
from softwarecenter.ui.gtk3.widgets.exhibits import (
21
class ExhibitsTestCase(unittest.TestCase):
22
"""The test suite for the exhibits carousel."""
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)
31
def _get_banner_from_lobby(self):
32
return self.lobby.vbox.get_children()[-1].get_child()
34
def test_featured_exhibit_by_default(self):
35
"""Show the featured exhibit before querying the remote service."""
36
self.lobby._append_banner_ads()
38
banner = self._get_banner_from_lobby()
39
self.assertEqual(1, len(banner.exhibits))
40
self.assertIsInstance(banner.exhibits[0], lobbyview.FeaturedExhibit)
42
def test_no_exhibit_if_not_available(self):
43
"""The exhibit should not be shown if the package is not available."""
45
exhibit.package_names = u'foobarbaz'
47
sca = ObjectWithSignals()
48
sca.query_exhibits = lambda: sca.emit('exhibits', sca, [exhibit])
50
with patch.object(lobbyview, 'SoftwareCenterAgent', lambda: sca):
51
self.lobby._append_banner_ads()
53
banner = self._get_banner_from_lobby()
54
self.assertEqual(1, len(banner.exhibits))
55
self.assertIsInstance(banner.exhibits[0], lobbyview.FeaturedExhibit)
57
def test_exhibit_if_available(self):
58
"""The exhibit should be shown if the package is available."""
60
exhibit.package_names = u'foobarbaz'
61
exhibit.banner_urls = ['banner']
62
exhibit.title_translated = ''
64
self.cache[u'foobarbaz'] = Mock()
66
sca = ObjectWithSignals()
67
sca.query_exhibits = lambda: sca.emit('exhibits', sca, [exhibit])
69
with patch.object(lobbyview, 'SoftwareCenterAgent', lambda: sca):
70
self.lobby._append_banner_ads()
72
banner = self._get_banner_from_lobby()
73
self.assertEqual(1, len(banner.exhibits))
74
self.assertIs(banner.exhibits[0], exhibit)
76
def test_exhibit_if_mixed_availability(self):
77
"""The exhibit should be shown even if some are not available."""
80
exhibit.package_names = u'foobarbaz'
81
exhibit.banner_urls = ['banner']
82
exhibit.title_translated = ''
84
self.cache[u'foobarbaz'] = Mock()
86
# not available exhibit
88
other.package_names = u'not-there'
90
sca = ObjectWithSignals()
91
sca.query_exhibits = lambda: sca.emit('exhibits', sca,
94
with patch.object(lobbyview, 'SoftwareCenterAgent', lambda: sca):
95
self.lobby._append_banner_ads()
97
banner = self._get_banner_from_lobby()
98
self.assertEqual(1, len(banner.exhibits))
99
self.assertIs(banner.exhibits[0], exhibit)
101
def test_exhibit_with_url(self):
104
exhibit.package_names = ''
105
exhibit.click_url = 'http://example.com'
106
exhibit.banner_urls = ['banner']
107
exhibit.title_translated = ''
109
sca = ObjectWithSignals()
110
sca.query_exhibits = lambda: sca.emit('exhibits', sca,
113
with patch.object(lobbyview, 'SoftwareCenterAgent', lambda: sca):
115
self.lobby._append_banner_ads()
117
alloc = self.lobby.exhibit_banner.get_allocation()
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")
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()])
136
with patch.object(lobbyview, 'SoftwareCenterAgent', lambda: sca):
138
self.lobby._append_banner_ads()
140
alloc = self.lobby.exhibit_banner.get_allocation()
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")
154
class HtmlRendererTestCase(unittest.TestCase):
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]))
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",
169
mock_exhibit.html = "url('/path1/banner1.png')#"\
170
"url('/path2/banner2.png')"
172
renderer.set_exhibit(mock_exhibit)
173
# assert the stuff we expected to get downloaded got downloaded
175
renderer._downloaded_banner_images,
176
["banner1.png", "banner2.png"])
177
# test that the path mangling worked
179
mock_exhibit.html, "url('banner1.png')#url('banner2.png')")
181
if __name__ == "__main__":