blob: 0820b9123125a30bdafe0fc0578114b98b30490a [file] [log] [blame]
R David Murray35619012012-09-03 12:52:08 -04001import webbrowser
2import unittest
3import subprocess
4from unittest import mock
5from test import support
6
7
8URL = 'http://www.example.com'
9CMD_NAME = 'test'
10
11
12class PopenMock(mock.MagicMock):
13
14 def poll(self):
15 return 0
16
17 def wait(self, seconds=None):
18 return 0
19
20
21class CommandTestMixin:
22
23 def _test(self, meth, *, args=[URL], kw={}, options, arguments):
24 """Given a web browser instance method name along with arguments and
25 keywords for same (which defaults to the single argument URL), creates
26 a browser instance from the class pointed to by self.browser, calls the
27 indicated instance method with the indicated arguments, and compares
28 the resulting options and arguments passed to Popen by the browser
29 instance against the 'options' and 'args' lists. Options are compared
30 in a position independent fashion, and the arguments are compared in
31 sequence order to whatever is left over after removing the options.
32
33 """
34 popen = PopenMock()
35 support.patch(self, subprocess, 'Popen', popen)
36 browser = self.browser_class(name=CMD_NAME)
37 getattr(browser, meth)(*args, **kw)
38 popen_args = subprocess.Popen.call_args[0][0]
39 self.assertEqual(popen_args[0], CMD_NAME)
40 popen_args.pop(0)
41 for option in options:
42 self.assertIn(option, popen_args)
43 popen_args.pop(popen_args.index(option))
44 self.assertEqual(popen_args, arguments)
45
46
47class GenericBrowserCommandTest(CommandTestMixin, unittest.TestCase):
48
49 browser_class = webbrowser.GenericBrowser
50
51 def test_open(self):
52 self._test('open',
53 options=[],
54 arguments=[URL])
55
56
57class BackgroundBrowserCommandTest(CommandTestMixin, unittest.TestCase):
58
59 browser_class = webbrowser.BackgroundBrowser
60
61 def test_open(self):
62 self._test('open',
63 options=[],
64 arguments=[URL])
65
66
67class ChromeCommandTest(CommandTestMixin, unittest.TestCase):
68
69 browser_class = webbrowser.Chrome
70
71 def test_open(self):
72 self._test('open',
73 options=[],
74 arguments=[URL])
75
76 def test_open_with_autoraise_false(self):
77 self._test('open', kw=dict(autoraise=False),
78 options=[],
79 arguments=[URL])
80
81 def test_open_new(self):
82 self._test('open_new',
83 options=['--new-window'],
84 arguments=[URL])
85
86 def test_open_new_tab(self):
87 self._test('open_new_tab',
88 options=[],
89 arguments=[URL])
90
91
92class MozillaCommandTest(CommandTestMixin, unittest.TestCase):
93
94 browser_class = webbrowser.Mozilla
95
96 def test_open(self):
97 self._test('open',
Serhiy Storchakac9b750d2016-10-30 19:16:33 +020098 options=[],
99 arguments=[URL])
100
101 def test_open_with_autoraise_false(self):
102 self._test('open', kw=dict(autoraise=False),
103 options=[],
104 arguments=[URL])
105
106 def test_open_new(self):
107 self._test('open_new',
108 options=[],
109 arguments=['-new-window', URL])
110
111 def test_open_new_tab(self):
112 self._test('open_new_tab',
113 options=[],
114 arguments=['-new-tab', URL])
115
116
117class NetscapeCommandTest(CommandTestMixin, unittest.TestCase):
118
119 browser_class = webbrowser.Netscape
120
121 def test_open(self):
122 self._test('open',
R David Murray35619012012-09-03 12:52:08 -0400123 options=['-raise', '-remote'],
124 arguments=['openURL({})'.format(URL)])
125
126 def test_open_with_autoraise_false(self):
127 self._test('open', kw=dict(autoraise=False),
128 options=['-noraise', '-remote'],
129 arguments=['openURL({})'.format(URL)])
130
131 def test_open_new(self):
132 self._test('open_new',
133 options=['-raise', '-remote'],
134 arguments=['openURL({},new-window)'.format(URL)])
135
136 def test_open_new_tab(self):
137 self._test('open_new_tab',
138 options=['-raise', '-remote'],
139 arguments=['openURL({},new-tab)'.format(URL)])
140
141
142class GaleonCommandTest(CommandTestMixin, unittest.TestCase):
143
144 browser_class = webbrowser.Galeon
145
146 def test_open(self):
147 self._test('open',
148 options=['-n'],
149 arguments=[URL])
150
151 def test_open_with_autoraise_false(self):
152 self._test('open', kw=dict(autoraise=False),
153 options=['-noraise', '-n'],
154 arguments=[URL])
155
156 def test_open_new(self):
157 self._test('open_new',
158 options=['-w'],
159 arguments=[URL])
160
161 def test_open_new_tab(self):
162 self._test('open_new_tab',
163 options=['-w'],
164 arguments=[URL])
165
166
167class OperaCommandTest(CommandTestMixin, unittest.TestCase):
168
169 browser_class = webbrowser.Opera
170
171 def test_open(self):
172 self._test('open',
173 options=['-remote'],
174 arguments=['openURL({})'.format(URL)])
175
176 def test_open_with_autoraise_false(self):
177 self._test('open', kw=dict(autoraise=False),
178 options=['-remote', '-noraise'],
179 arguments=['openURL({})'.format(URL)])
180
181 def test_open_new(self):
182 self._test('open_new',
183 options=['-remote'],
184 arguments=['openURL({},new-window)'.format(URL)])
185
Benjamin Petersone8c8a592013-09-29 10:48:19 -0400186 def test_open_new_tab(self):
R David Murray35619012012-09-03 12:52:08 -0400187 self._test('open_new_tab',
188 options=['-remote'],
189 arguments=['openURL({},new-page)'.format(URL)])
190
191
192class ELinksCommandTest(CommandTestMixin, unittest.TestCase):
193
194 browser_class = webbrowser.Elinks
195
196 def test_open(self):
197 self._test('open', options=['-remote'],
198 arguments=['openURL({})'.format(URL)])
199
200 def test_open_with_autoraise_false(self):
201 self._test('open',
202 options=['-remote'],
203 arguments=['openURL({})'.format(URL)])
204
205 def test_open_new(self):
206 self._test('open_new',
207 options=['-remote'],
208 arguments=['openURL({},new-window)'.format(URL)])
209
210 def test_open_new_tab(self):
211 self._test('open_new_tab',
212 options=['-remote'],
213 arguments=['openURL({},new-tab)'.format(URL)])
214
215
Nick Coghlan56a8ecc2017-02-25 18:14:07 +1000216class BrowserRegistrationTest(unittest.TestCase):
217
218 def setUp(self):
219 # Ensure we don't alter the real registered browser details
220 self._saved_tryorder = webbrowser._tryorder
221 webbrowser._tryorder = []
222 self._saved_browsers = webbrowser._browsers
223 webbrowser._browsers = {}
224
225 def tearDown(self):
226 webbrowser._tryorder = self._saved_tryorder
227 webbrowser._browsers = self._saved_browsers
228
229 def _check_registration(self, preferred):
230 class ExampleBrowser:
231 pass
232
233 expected_tryorder = []
234 expected_browsers = {}
235
236 self.assertEqual(webbrowser._tryorder, expected_tryorder)
237 self.assertEqual(webbrowser._browsers, expected_browsers)
238
239 webbrowser.register('Example1', ExampleBrowser)
240 expected_tryorder = ['Example1']
241 expected_browsers['example1'] = [ExampleBrowser, None]
242 self.assertEqual(webbrowser._tryorder, expected_tryorder)
243 self.assertEqual(webbrowser._browsers, expected_browsers)
244
245 instance = ExampleBrowser()
246 if preferred is not None:
247 webbrowser.register('example2', ExampleBrowser, instance,
248 preferred=preferred)
249 else:
250 webbrowser.register('example2', ExampleBrowser, instance)
251 if preferred:
252 expected_tryorder = ['example2', 'Example1']
253 else:
254 expected_tryorder = ['Example1', 'example2']
255 expected_browsers['example2'] = [ExampleBrowser, instance]
256 self.assertEqual(webbrowser._tryorder, expected_tryorder)
257 self.assertEqual(webbrowser._browsers, expected_browsers)
258
259 def test_register(self):
260 self._check_registration(preferred=False)
261
262 def test_register_default(self):
263 self._check_registration(preferred=None)
264
265 def test_register_preferred(self):
266 self._check_registration(preferred=True)
267
268
Serhiy Storchakaa7cba272017-03-08 17:15:54 +0200269class ImportTest(unittest.TestCase):
270 def test_register(self):
271 webbrowser = support.import_fresh_module('webbrowser')
272 self.assertIsNone(webbrowser._tryorder)
273 self.assertFalse(webbrowser._browsers)
274
275 class ExampleBrowser:
276 pass
277 webbrowser.register('Example1', ExampleBrowser)
278 self.assertTrue(webbrowser._tryorder)
279 self.assertEqual(webbrowser._tryorder[-1], 'Example1')
280 self.assertTrue(webbrowser._browsers)
281 self.assertIn('example1', webbrowser._browsers)
282 self.assertEqual(webbrowser._browsers['example1'], [ExampleBrowser, None])
283
284 def test_get(self):
285 webbrowser = support.import_fresh_module('webbrowser')
286 self.assertIsNone(webbrowser._tryorder)
287 self.assertFalse(webbrowser._browsers)
288
289 with self.assertRaises(webbrowser.Error):
290 webbrowser.get('fakebrowser')
291 self.assertIsNotNone(webbrowser._tryorder)
292
293
R David Murray35619012012-09-03 12:52:08 -0400294if __name__=='__main__':
295 unittest.main()