• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import 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=[],
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',
123                   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
186    def test_open_new_tab(self):
187        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
216if __name__=='__main__':
217    unittest.main()
218