blob: c3292c493d16095c348307187464e2163e98cc98 [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',
98 options=['-raise', '-remote'],
99 arguments=['openURL({})'.format(URL)])
100
101 def test_open_with_autoraise_false(self):
102 self._test('open', kw=dict(autoraise=False),
103 options=['-noraise', '-remote'],
104 arguments=['openURL({})'.format(URL)])
105
106 def test_open_new(self):
107 self._test('open_new',
108 options=['-raise', '-remote'],
109 arguments=['openURL({},new-window)'.format(URL)])
110
111 def test_open_new_tab(self):
112 self._test('open_new_tab',
113 options=['-raise', '-remote'],
114 arguments=['openURL({},new-tab)'.format(URL)])
115
116
117class GaleonCommandTest(CommandTestMixin, unittest.TestCase):
118
119 browser_class = webbrowser.Galeon
120
121 def test_open(self):
122 self._test('open',
123 options=['-n'],
124 arguments=[URL])
125
126 def test_open_with_autoraise_false(self):
127 self._test('open', kw=dict(autoraise=False),
128 options=['-noraise', '-n'],
129 arguments=[URL])
130
131 def test_open_new(self):
132 self._test('open_new',
133 options=['-w'],
134 arguments=[URL])
135
136 def test_open_new_tab(self):
137 self._test('open_new_tab',
138 options=['-w'],
139 arguments=[URL])
140
141
142class OperaCommandTest(CommandTestMixin, unittest.TestCase):
143
144 browser_class = webbrowser.Opera
145
146 def test_open(self):
147 self._test('open',
148 options=['-remote'],
149 arguments=['openURL({})'.format(URL)])
150
151 def test_open_with_autoraise_false(self):
152 self._test('open', kw=dict(autoraise=False),
153 options=['-remote', '-noraise'],
154 arguments=['openURL({})'.format(URL)])
155
156 def test_open_new(self):
157 self._test('open_new',
158 options=['-remote'],
159 arguments=['openURL({},new-window)'.format(URL)])
160
Benjamin Petersone8c8a592013-09-29 10:48:19 -0400161 def test_open_new_tab(self):
R David Murray35619012012-09-03 12:52:08 -0400162 self._test('open_new_tab',
163 options=['-remote'],
164 arguments=['openURL({},new-page)'.format(URL)])
165
166
167class ELinksCommandTest(CommandTestMixin, unittest.TestCase):
168
169 browser_class = webbrowser.Elinks
170
171 def test_open(self):
172 self._test('open', options=['-remote'],
173 arguments=['openURL({})'.format(URL)])
174
175 def test_open_with_autoraise_false(self):
176 self._test('open',
177 options=['-remote'],
178 arguments=['openURL({})'.format(URL)])
179
180 def test_open_new(self):
181 self._test('open_new',
182 options=['-remote'],
183 arguments=['openURL({},new-window)'.format(URL)])
184
185 def test_open_new_tab(self):
186 self._test('open_new_tab',
187 options=['-remote'],
188 arguments=['openURL({},new-tab)'.format(URL)])
189
190
191if __name__=='__main__':
192 unittest.main()