diff --git a/Lib/test/test_webbrowser.py b/Lib/test/test_webbrowser.py index e46b6fb0893..622c1cbfc93 100644 --- a/Lib/test/test_webbrowser.py +++ b/Lib/test/test_webbrowser.py @@ -213,5 +213,58 @@ class ELinksCommandTest(CommandTestMixin, unittest.TestCase): arguments=['openURL({},new-tab)'.format(URL)]) +class BrowserRegistrationTest(unittest.TestCase): + + def setUp(self): + # Ensure we don't alter the real registered browser details + self._saved_tryorder = webbrowser._tryorder + webbrowser._tryorder = [] + self._saved_browsers = webbrowser._browsers + webbrowser._browsers = {} + + def tearDown(self): + webbrowser._tryorder = self._saved_tryorder + webbrowser._browsers = self._saved_browsers + + def _check_registration(self, preferred): + class ExampleBrowser: + pass + + expected_tryorder = [] + expected_browsers = {} + + self.assertEqual(webbrowser._tryorder, expected_tryorder) + self.assertEqual(webbrowser._browsers, expected_browsers) + + webbrowser.register('Example1', ExampleBrowser) + expected_tryorder = ['Example1'] + expected_browsers['example1'] = [ExampleBrowser, None] + self.assertEqual(webbrowser._tryorder, expected_tryorder) + self.assertEqual(webbrowser._browsers, expected_browsers) + + instance = ExampleBrowser() + if preferred is not None: + webbrowser.register('example2', ExampleBrowser, instance, + preferred=preferred) + else: + webbrowser.register('example2', ExampleBrowser, instance) + if preferred: + expected_tryorder = ['example2', 'Example1'] + else: + expected_tryorder = ['Example1', 'example2'] + expected_browsers['example2'] = [ExampleBrowser, instance] + self.assertEqual(webbrowser._tryorder, expected_tryorder) + self.assertEqual(webbrowser._browsers, expected_browsers) + + def test_register(self): + self._check_registration(preferred=False) + + def test_register_default(self): + self._check_registration(preferred=None) + + def test_register_preferred(self): + self._check_registration(preferred=True) + + if __name__=='__main__': unittest.main()