tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_autoconfig.py (3690B)


      1 # This Source Code Form is subject to the terms of the Mozilla Public
      2 # License, v. 2.0. If a copy of the MPL was not distributed with this
      3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      4 
      5 import os
      6 import shutil
      7 
      8 from marionette_harness import MarionetteTestCase
      9 
     10 
     11 class TestAutoConfig(MarionetteTestCase):
     12    def tearDown(self):
     13        self.marionette.quit(in_app=False, clean=True)
     14 
     15        if hasattr(self, "pref_file"):
     16            os.remove(self.pref_file)
     17        if hasattr(self, "autoconfig_file"):
     18            os.remove(self.autoconfig_file)
     19        if hasattr(self, "pref_file_dir_created"):
     20            os.rmdir(self.pref_file_dir)
     21 
     22        super().tearDown()
     23 
     24    def pref_has_user_value(self, pref):
     25        with self.marionette.using_context("chrome"):
     26            return self.marionette.execute_script(
     27                """
     28                return Services.prefs.prefHasUserValue(arguments[0]);
     29                """,
     30                script_args=(pref,),
     31            )
     32 
     33    def pref_is_locked(self, pref):
     34        with self.marionette.using_context("chrome"):
     35            return self.marionette.execute_script(
     36                """
     37                return Services.prefs.prefIsLocked(arguments[0]);
     38                """,
     39                script_args=(pref,),
     40            )
     41 
     42    def test_autoconfig(self):
     43        with self.marionette.using_context("chrome"):
     44            self.exe_dir = self.marionette.execute_script(
     45                """
     46              return Services.dirsvc.get("GreD", Ci.nsIFile).path;
     47            """
     48            )
     49 
     50        self.marionette.quit()
     51 
     52        test_dir = os.path.dirname(__file__)
     53        self.pref_file_dir = os.path.join(self.exe_dir, "defaults", "pref")
     54        if not os.path.exists(self.pref_file_dir):
     55            os.makedirs(self.pref_file_dir, exist_ok=True)
     56            self.pref_file_dir_created = True
     57        self.pref_file = os.path.join(self.pref_file_dir, "autoconfig.js")
     58        shutil.copyfile(os.path.join(test_dir, "autoconfig.js"), self.pref_file)
     59        self.autoconfig_file = os.path.join(self.exe_dir, "autoconfig.cfg")
     60        shutil.copyfile(os.path.join(test_dir, "autoconfig.cfg"), self.autoconfig_file)
     61 
     62        self.marionette.start_session()
     63 
     64        with self.marionette.using_context("chrome"):
     65            self.assertTrue(
     66                self.pref_has_user_value("_autoconfig_.test.userpref"),
     67                "Pref should have user value",
     68            )
     69 
     70            self.assertEqual(
     71                self.marionette.get_pref("_autoconfig_.test.userpref"),
     72                "userpref",
     73                "User pref should be set",
     74            )
     75 
     76            self.assertEqual(
     77                self.marionette.get_pref("_autoconfig_.test.defaultpref", True),
     78                "defaultpref",
     79                "Default pref should be set",
     80            )
     81 
     82            self.assertTrue(
     83                self.pref_is_locked("_autoconfig_.test.lockpref"),
     84                "Pref should be locked",
     85            )
     86 
     87            self.assertEqual(
     88                self.marionette.get_pref("_autoconfig_.test.lockpref"),
     89                "lockpref",
     90                "Locked pref should be set",
     91            )
     92 
     93            self.assertFalse(
     94                self.pref_is_locked("_autoconfig_.test.unlockpref"),
     95                "Pref should be unlocked",
     96            )
     97 
     98            self.assertEqual(
     99                self.marionette.get_pref("_autoconfig_.test.unlockpref"),
    100                "unlockpref",
    101                "Unlocked pref should be set",
    102            )
    103 
    104            self.assertFalse(
    105                self.pref_has_user_value("_autoconfig_.test.clearpref"),
    106                "Pref should be cleared",
    107            )