ยปCore Development>Code coverage>Lib/test/test_webbrowser.py

Python code coverage for Lib/test/test_webbrowser.py

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