1 | n/a | import webbrowser |
---|
2 | n/a | import unittest |
---|
3 | n/a | import subprocess |
---|
4 | n/a | from unittest import mock |
---|
5 | n/a | from test import support |
---|
6 | n/a | |
---|
7 | n/a | |
---|
8 | n/a | URL = 'http://www.example.com' |
---|
9 | n/a | CMD_NAME = 'test' |
---|
10 | n/a | |
---|
11 | n/a | |
---|
12 | n/a | class PopenMock(mock.MagicMock): |
---|
13 | n/a | |
---|
14 | n/a | def poll(self): |
---|
15 | n/a | return 0 |
---|
16 | n/a | |
---|
17 | n/a | def wait(self, seconds=None): |
---|
18 | n/a | return 0 |
---|
19 | n/a | |
---|
20 | n/a | |
---|
21 | n/a | class CommandTestMixin: |
---|
22 | n/a | |
---|
23 | n/a | def _test(self, meth, *, args=[URL], kw={}, options, arguments): |
---|
24 | n/a | """Given a web browser instance method name along with arguments and |
---|
25 | n/a | keywords for same (which defaults to the single argument URL), creates |
---|
26 | n/a | a browser instance from the class pointed to by self.browser, calls the |
---|
27 | n/a | indicated instance method with the indicated arguments, and compares |
---|
28 | n/a | the resulting options and arguments passed to Popen by the browser |
---|
29 | n/a | instance against the 'options' and 'args' lists. Options are compared |
---|
30 | n/a | in a position independent fashion, and the arguments are compared in |
---|
31 | n/a | sequence order to whatever is left over after removing the options. |
---|
32 | n/a | |
---|
33 | n/a | """ |
---|
34 | n/a | popen = PopenMock() |
---|
35 | n/a | support.patch(self, subprocess, 'Popen', popen) |
---|
36 | n/a | browser = self.browser_class(name=CMD_NAME) |
---|
37 | n/a | getattr(browser, meth)(*args, **kw) |
---|
38 | n/a | popen_args = subprocess.Popen.call_args[0][0] |
---|
39 | n/a | self.assertEqual(popen_args[0], CMD_NAME) |
---|
40 | n/a | popen_args.pop(0) |
---|
41 | n/a | for option in options: |
---|
42 | n/a | self.assertIn(option, popen_args) |
---|
43 | n/a | popen_args.pop(popen_args.index(option)) |
---|
44 | n/a | self.assertEqual(popen_args, arguments) |
---|
45 | n/a | |
---|
46 | n/a | |
---|
47 | n/a | class GenericBrowserCommandTest(CommandTestMixin, unittest.TestCase): |
---|
48 | n/a | |
---|
49 | n/a | browser_class = webbrowser.GenericBrowser |
---|
50 | n/a | |
---|
51 | n/a | def test_open(self): |
---|
52 | n/a | self._test('open', |
---|
53 | n/a | options=[], |
---|
54 | n/a | arguments=[URL]) |
---|
55 | n/a | |
---|
56 | n/a | |
---|
57 | n/a | class BackgroundBrowserCommandTest(CommandTestMixin, unittest.TestCase): |
---|
58 | n/a | |
---|
59 | n/a | browser_class = webbrowser.BackgroundBrowser |
---|
60 | n/a | |
---|
61 | n/a | def test_open(self): |
---|
62 | n/a | self._test('open', |
---|
63 | n/a | options=[], |
---|
64 | n/a | arguments=[URL]) |
---|
65 | n/a | |
---|
66 | n/a | |
---|
67 | n/a | class ChromeCommandTest(CommandTestMixin, unittest.TestCase): |
---|
68 | n/a | |
---|
69 | n/a | browser_class = webbrowser.Chrome |
---|
70 | n/a | |
---|
71 | n/a | def test_open(self): |
---|
72 | n/a | self._test('open', |
---|
73 | n/a | options=[], |
---|
74 | n/a | arguments=[URL]) |
---|
75 | n/a | |
---|
76 | n/a | def test_open_with_autoraise_false(self): |
---|
77 | n/a | self._test('open', kw=dict(autoraise=False), |
---|
78 | n/a | options=[], |
---|
79 | n/a | arguments=[URL]) |
---|
80 | n/a | |
---|
81 | n/a | def test_open_new(self): |
---|
82 | n/a | self._test('open_new', |
---|
83 | n/a | options=['--new-window'], |
---|
84 | n/a | arguments=[URL]) |
---|
85 | n/a | |
---|
86 | n/a | def test_open_new_tab(self): |
---|
87 | n/a | self._test('open_new_tab', |
---|
88 | n/a | options=[], |
---|
89 | n/a | arguments=[URL]) |
---|
90 | n/a | |
---|
91 | n/a | |
---|
92 | n/a | class MozillaCommandTest(CommandTestMixin, unittest.TestCase): |
---|
93 | n/a | |
---|
94 | n/a | browser_class = webbrowser.Mozilla |
---|
95 | n/a | |
---|
96 | n/a | def test_open(self): |
---|
97 | n/a | self._test('open', |
---|
98 | n/a | options=[], |
---|
99 | n/a | arguments=[URL]) |
---|
100 | n/a | |
---|
101 | n/a | def test_open_with_autoraise_false(self): |
---|
102 | n/a | self._test('open', kw=dict(autoraise=False), |
---|
103 | n/a | options=[], |
---|
104 | n/a | arguments=[URL]) |
---|
105 | n/a | |
---|
106 | n/a | def test_open_new(self): |
---|
107 | n/a | self._test('open_new', |
---|
108 | n/a | options=[], |
---|
109 | n/a | arguments=['-new-window', URL]) |
---|
110 | n/a | |
---|
111 | n/a | def test_open_new_tab(self): |
---|
112 | n/a | self._test('open_new_tab', |
---|
113 | n/a | options=[], |
---|
114 | n/a | arguments=['-new-tab', URL]) |
---|
115 | n/a | |
---|
116 | n/a | |
---|
117 | n/a | class NetscapeCommandTest(CommandTestMixin, unittest.TestCase): |
---|
118 | n/a | |
---|
119 | n/a | browser_class = webbrowser.Netscape |
---|
120 | n/a | |
---|
121 | n/a | def test_open(self): |
---|
122 | n/a | self._test('open', |
---|
123 | n/a | options=['-raise', '-remote'], |
---|
124 | n/a | arguments=['openURL({})'.format(URL)]) |
---|
125 | n/a | |
---|
126 | n/a | def test_open_with_autoraise_false(self): |
---|
127 | n/a | self._test('open', kw=dict(autoraise=False), |
---|
128 | n/a | options=['-noraise', '-remote'], |
---|
129 | n/a | arguments=['openURL({})'.format(URL)]) |
---|
130 | n/a | |
---|
131 | n/a | def test_open_new(self): |
---|
132 | n/a | self._test('open_new', |
---|
133 | n/a | options=['-raise', '-remote'], |
---|
134 | n/a | arguments=['openURL({},new-window)'.format(URL)]) |
---|
135 | n/a | |
---|
136 | n/a | def test_open_new_tab(self): |
---|
137 | n/a | self._test('open_new_tab', |
---|
138 | n/a | options=['-raise', '-remote'], |
---|
139 | n/a | arguments=['openURL({},new-tab)'.format(URL)]) |
---|
140 | n/a | |
---|
141 | n/a | |
---|
142 | n/a | class GaleonCommandTest(CommandTestMixin, unittest.TestCase): |
---|
143 | n/a | |
---|
144 | n/a | browser_class = webbrowser.Galeon |
---|
145 | n/a | |
---|
146 | n/a | def test_open(self): |
---|
147 | n/a | self._test('open', |
---|
148 | n/a | options=['-n'], |
---|
149 | n/a | arguments=[URL]) |
---|
150 | n/a | |
---|
151 | n/a | def test_open_with_autoraise_false(self): |
---|
152 | n/a | self._test('open', kw=dict(autoraise=False), |
---|
153 | n/a | options=['-noraise', '-n'], |
---|
154 | n/a | arguments=[URL]) |
---|
155 | n/a | |
---|
156 | n/a | def test_open_new(self): |
---|
157 | n/a | self._test('open_new', |
---|
158 | n/a | options=['-w'], |
---|
159 | n/a | arguments=[URL]) |
---|
160 | n/a | |
---|
161 | n/a | def test_open_new_tab(self): |
---|
162 | n/a | self._test('open_new_tab', |
---|
163 | n/a | options=['-w'], |
---|
164 | n/a | arguments=[URL]) |
---|
165 | n/a | |
---|
166 | n/a | |
---|
167 | n/a | class OperaCommandTest(CommandTestMixin, unittest.TestCase): |
---|
168 | n/a | |
---|
169 | n/a | browser_class = webbrowser.Opera |
---|
170 | n/a | |
---|
171 | n/a | def test_open(self): |
---|
172 | n/a | self._test('open', |
---|
173 | n/a | options=['-remote'], |
---|
174 | n/a | arguments=['openURL({})'.format(URL)]) |
---|
175 | n/a | |
---|
176 | n/a | def test_open_with_autoraise_false(self): |
---|
177 | n/a | self._test('open', kw=dict(autoraise=False), |
---|
178 | n/a | options=['-remote', '-noraise'], |
---|
179 | n/a | arguments=['openURL({})'.format(URL)]) |
---|
180 | n/a | |
---|
181 | n/a | def test_open_new(self): |
---|
182 | n/a | self._test('open_new', |
---|
183 | n/a | options=['-remote'], |
---|
184 | n/a | arguments=['openURL({},new-window)'.format(URL)]) |
---|
185 | n/a | |
---|
186 | n/a | def test_open_new_tab(self): |
---|
187 | n/a | self._test('open_new_tab', |
---|
188 | n/a | options=['-remote'], |
---|
189 | n/a | arguments=['openURL({},new-page)'.format(URL)]) |
---|
190 | n/a | |
---|
191 | n/a | |
---|
192 | n/a | class ELinksCommandTest(CommandTestMixin, unittest.TestCase): |
---|
193 | n/a | |
---|
194 | n/a | browser_class = webbrowser.Elinks |
---|
195 | n/a | |
---|
196 | n/a | def test_open(self): |
---|
197 | n/a | self._test('open', options=['-remote'], |
---|
198 | n/a | arguments=['openURL({})'.format(URL)]) |
---|
199 | n/a | |
---|
200 | n/a | def test_open_with_autoraise_false(self): |
---|
201 | n/a | self._test('open', |
---|
202 | n/a | options=['-remote'], |
---|
203 | n/a | arguments=['openURL({})'.format(URL)]) |
---|
204 | n/a | |
---|
205 | n/a | def test_open_new(self): |
---|
206 | n/a | self._test('open_new', |
---|
207 | n/a | options=['-remote'], |
---|
208 | n/a | arguments=['openURL({},new-window)'.format(URL)]) |
---|
209 | n/a | |
---|
210 | n/a | def test_open_new_tab(self): |
---|
211 | n/a | self._test('open_new_tab', |
---|
212 | n/a | options=['-remote'], |
---|
213 | n/a | arguments=['openURL({},new-tab)'.format(URL)]) |
---|
214 | n/a | |
---|
215 | n/a | |
---|
216 | n/a | if __name__=='__main__': |
---|
217 | n/a | unittest.main() |
---|