From 56a8eccc43c66ae51c5a6bfc89635b1998fd419e Mon Sep 17 00:00:00 2001
From: Nick Coghlan <ncoghlan@gmail.com>
Date: Sat, 25 Feb 2017 18:14:07 +1000
Subject: [PATCH] bpo-24241: Add dedicated webbrowser.register test case (#288)

---
 Lib/test/test_webbrowser.py | 53 +++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/Lib/test/test_webbrowser.py b/Lib/test/test_webbrowser.py
index e46b6fb089..622c1cbfc9 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()
-- 
2.40.0