michael@0: #!/usr/bin/env python michael@0: michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: # You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import mozfile michael@0: import mozhttpd michael@0: import os michael@0: import shutil michael@0: import tempfile michael@0: import unittest michael@0: from mozprofile.cli import MozProfileCLI michael@0: from mozprofile.prefs import Preferences michael@0: from mozprofile.profile import Profile michael@0: michael@0: here = os.path.dirname(os.path.abspath(__file__)) michael@0: michael@0: class PreferencesTest(unittest.TestCase): michael@0: """test mozprofile preference handling""" michael@0: michael@0: # preferences from files/prefs_with_comments.js michael@0: _prefs_with_comments = {'browser.startup.homepage': 'http://planet.mozilla.org', michael@0: 'zoom.minPercent': 30, michael@0: 'zoom.maxPercent': 300, michael@0: 'webgl.verbose': 'false'} michael@0: michael@0: def run_command(self, *args): michael@0: """ michael@0: invokes mozprofile command line via the CLI factory michael@0: - args : command line arguments (equivalent of sys.argv[1:]) michael@0: """ michael@0: michael@0: # instantiate the factory michael@0: cli = MozProfileCLI(list(args)) michael@0: michael@0: # create the profile michael@0: profile = cli.profile() michael@0: michael@0: # return path to profile michael@0: return profile.profile michael@0: michael@0: def compare_generated(self, _prefs, commandline): michael@0: """ michael@0: writes out to a new profile with mozprofile command line michael@0: reads the generated preferences with prefs.py michael@0: compares the results michael@0: cleans up michael@0: """ michael@0: profile = self.run_command(*commandline) michael@0: prefs_file = os.path.join(profile, 'user.js') michael@0: self.assertTrue(os.path.exists(prefs_file)) michael@0: read = Preferences.read_prefs(prefs_file) michael@0: if isinstance(_prefs, dict): michael@0: read = dict(read) michael@0: self.assertEqual(_prefs, read) michael@0: shutil.rmtree(profile) michael@0: michael@0: def test_basic_prefs(self): michael@0: """test setting a pref from the command line entry point""" michael@0: michael@0: _prefs = {"browser.startup.homepage": "http://planet.mozilla.org/"} michael@0: commandline = [] michael@0: _prefs = _prefs.items() michael@0: for pref, value in _prefs: michael@0: commandline += ["--pref", "%s:%s" % (pref, value)] michael@0: self.compare_generated(_prefs, commandline) michael@0: michael@0: def test_ordered_prefs(self): michael@0: """ensure the prefs stay in the right order""" michael@0: _prefs = [("browser.startup.homepage", "http://planet.mozilla.org/"), michael@0: ("zoom.minPercent", 30), michael@0: ("zoom.maxPercent", 300), michael@0: ("webgl.verbose", 'false')] michael@0: commandline = [] michael@0: for pref, value in _prefs: michael@0: commandline += ["--pref", "%s:%s" % (pref, value)] michael@0: _prefs = [(i, Preferences.cast(j)) for i, j in _prefs] michael@0: self.compare_generated(_prefs, commandline) michael@0: michael@0: def test_ini(self): michael@0: michael@0: # write the .ini file michael@0: _ini = """[DEFAULT] michael@0: browser.startup.homepage = http://planet.mozilla.org/ michael@0: michael@0: [foo] michael@0: browser.startup.homepage = http://github.com/ michael@0: """ michael@0: try: michael@0: fd, name = tempfile.mkstemp(suffix='.ini') michael@0: os.write(fd, _ini) michael@0: os.close(fd) michael@0: commandline = ["--preferences", name] michael@0: michael@0: # test the [DEFAULT] section michael@0: _prefs = {'browser.startup.homepage': 'http://planet.mozilla.org/'} michael@0: self.compare_generated(_prefs, commandline) michael@0: michael@0: # test a specific section michael@0: _prefs = {'browser.startup.homepage': 'http://github.com/'} michael@0: commandline[-1] = commandline[-1] + ':foo' michael@0: self.compare_generated(_prefs, commandline) michael@0: michael@0: finally: michael@0: # cleanup michael@0: os.remove(name) michael@0: michael@0: def test_reset_should_remove_added_prefs(self): michael@0: """Check that when we call reset the items we expect are updated""" michael@0: michael@0: profile = Profile() michael@0: prefs_file = os.path.join(profile.profile, 'user.js') michael@0: michael@0: # we shouldn't have any initial preferences michael@0: initial_prefs = Preferences.read_prefs(prefs_file) michael@0: self.assertFalse(initial_prefs) michael@0: initial_prefs = file(prefs_file).read().strip() michael@0: self.assertFalse(initial_prefs) michael@0: michael@0: # add some preferences michael@0: prefs1 = [("mr.t.quotes", "i aint getting on no plane!")] michael@0: profile.set_preferences(prefs1) michael@0: self.assertEqual(prefs1, Preferences.read_prefs(prefs_file)) michael@0: lines = file(prefs_file).read().strip().splitlines() michael@0: self.assertTrue(bool([line for line in lines michael@0: if line.startswith('#MozRunner Prefs Start')])) michael@0: self.assertTrue(bool([line for line in lines michael@0: if line.startswith('#MozRunner Prefs End')])) michael@0: michael@0: profile.reset() michael@0: self.assertNotEqual(prefs1, michael@0: Preferences.read_prefs(os.path.join(profile.profile, 'user.js')), michael@0: "I pity the fool who left my pref") michael@0: michael@0: def test_magic_markers(self): michael@0: """ensure our magic markers are working""" michael@0: michael@0: profile = Profile() michael@0: prefs_file = os.path.join(profile.profile, 'user.js') michael@0: michael@0: # we shouldn't have any initial preferences michael@0: initial_prefs = Preferences.read_prefs(prefs_file) michael@0: self.assertFalse(initial_prefs) michael@0: initial_prefs = file(prefs_file).read().strip() michael@0: self.assertFalse(initial_prefs) michael@0: michael@0: # add some preferences michael@0: prefs1 = [("browser.startup.homepage", "http://planet.mozilla.org/"), michael@0: ("zoom.minPercent", 30)] michael@0: profile.set_preferences(prefs1) michael@0: self.assertEqual(prefs1, Preferences.read_prefs(prefs_file)) michael@0: lines = file(prefs_file).read().strip().splitlines() michael@0: self.assertTrue(bool([line for line in lines michael@0: if line.startswith('#MozRunner Prefs Start')])) michael@0: self.assertTrue(bool([line for line in lines michael@0: if line.startswith('#MozRunner Prefs End')])) michael@0: michael@0: # add some more preferences michael@0: prefs2 = [("zoom.maxPercent", 300), michael@0: ("webgl.verbose", 'false')] michael@0: profile.set_preferences(prefs2) michael@0: self.assertEqual(prefs1 + prefs2, Preferences.read_prefs(prefs_file)) michael@0: lines = file(prefs_file).read().strip().splitlines() michael@0: self.assertTrue(len([line for line in lines michael@0: if line.startswith('#MozRunner Prefs Start')]) == 2) michael@0: self.assertTrue(len([line for line in lines michael@0: if line.startswith('#MozRunner Prefs End')]) == 2) michael@0: michael@0: # now clean it up michael@0: profile.clean_preferences() michael@0: final_prefs = Preferences.read_prefs(prefs_file) michael@0: self.assertFalse(final_prefs) michael@0: lines = file(prefs_file).read().strip().splitlines() michael@0: self.assertTrue('#MozRunner Prefs Start' not in lines) michael@0: self.assertTrue('#MozRunner Prefs End' not in lines) michael@0: michael@0: def test_preexisting_preferences(self): michael@0: """ensure you don't clobber preexisting preferences""" michael@0: michael@0: # make a pretend profile michael@0: tempdir = tempfile.mkdtemp() michael@0: michael@0: try: michael@0: # make a user.js michael@0: contents = """ michael@0: user_pref("webgl.enabled_for_all_sites", true); michael@0: user_pref("webgl.force-enabled", true); michael@0: """ michael@0: user_js = os.path.join(tempdir, 'user.js') michael@0: f = file(user_js, 'w') michael@0: f.write(contents) michael@0: f.close() michael@0: michael@0: # make sure you can read it michael@0: prefs = Preferences.read_prefs(user_js) michael@0: original_prefs = [('webgl.enabled_for_all_sites', True), ('webgl.force-enabled', True)] michael@0: self.assertTrue(prefs == original_prefs) michael@0: michael@0: # now read this as a profile michael@0: profile = Profile(tempdir, preferences={"browser.download.dir": "/home/jhammel"}) michael@0: michael@0: # make sure the new pref is now there michael@0: new_prefs = original_prefs[:] + [("browser.download.dir", "/home/jhammel")] michael@0: prefs = Preferences.read_prefs(user_js) michael@0: self.assertTrue(prefs == new_prefs) michael@0: michael@0: # clean up the added preferences michael@0: profile.cleanup() michael@0: del profile michael@0: michael@0: # make sure you have the original preferences michael@0: prefs = Preferences.read_prefs(user_js) michael@0: self.assertTrue(prefs == original_prefs) michael@0: finally: michael@0: shutil.rmtree(tempdir) michael@0: michael@0: def test_json(self): michael@0: _prefs = {"browser.startup.homepage": "http://planet.mozilla.org/"} michael@0: json = '{"browser.startup.homepage": "http://planet.mozilla.org/"}' michael@0: michael@0: # just repr it...could use the json module but we don't need it here michael@0: fd, name = tempfile.mkstemp(suffix='.json') michael@0: os.write(fd, json) michael@0: os.close(fd) michael@0: michael@0: commandline = ["--preferences", name] michael@0: self.compare_generated(_prefs, commandline) michael@0: michael@0: def test_prefs_write(self): michael@0: """test that the Preferences.write() method correctly serializes preferences""" michael@0: michael@0: _prefs = {'browser.startup.homepage': "http://planet.mozilla.org", michael@0: 'zoom.minPercent': 30, michael@0: 'zoom.maxPercent': 300} michael@0: michael@0: # make a Preferences manager with the testing preferences michael@0: preferences = Preferences(_prefs) michael@0: michael@0: # write them to a temporary location michael@0: path = None michael@0: read_prefs = None michael@0: try: michael@0: with mozfile.NamedTemporaryFile(suffix='.js', delete=False) as f: michael@0: path = f.name michael@0: preferences.write(f, _prefs) michael@0: michael@0: # read them back and ensure we get what we put in michael@0: read_prefs = dict(Preferences.read_prefs(path)) michael@0: michael@0: finally: michael@0: # cleanup michael@0: if path and os.path.exists(path): michael@0: os.remove(path) michael@0: michael@0: self.assertEqual(read_prefs, _prefs) michael@0: michael@0: def test_read_prefs_with_comments(self): michael@0: """test reading preferences from a prefs.js file that contains comments""" michael@0: michael@0: path = os.path.join(here, 'files', 'prefs_with_comments.js') michael@0: self.assertEqual(dict(Preferences.read_prefs(path)), self._prefs_with_comments) michael@0: michael@0: def test_read_prefs_with_interpolation(self): michael@0: """test reading preferences from a prefs.js file whose values michael@0: require interpolation""" michael@0: michael@0: expected_prefs = { michael@0: "browser.foo": "http://server-name", michael@0: "zoom.minPercent": 30, michael@0: "webgl.verbose": "false", michael@0: "browser.bar": "somethingxyz" michael@0: } michael@0: values = { michael@0: "server": "server-name", michael@0: "abc": "something" michael@0: } michael@0: path = os.path.join(here, 'files', 'prefs_with_interpolation.js') michael@0: read_prefs = Preferences.read_prefs(path, interpolation=values) michael@0: self.assertEqual(dict(read_prefs), expected_prefs) michael@0: michael@0: def test_read_prefs_ttw(self): michael@0: """test reading preferences through the web via mozhttpd""" michael@0: michael@0: # create a MozHttpd instance michael@0: docroot = os.path.join(here, 'files') michael@0: host = '127.0.0.1' michael@0: port = 8888 michael@0: httpd = mozhttpd.MozHttpd(host=host, port=port, docroot=docroot) michael@0: michael@0: # create a preferences instance michael@0: prefs = Preferences() michael@0: michael@0: try: michael@0: # start server michael@0: httpd.start(block=False) michael@0: michael@0: # read preferences through the web michael@0: read = prefs.read_prefs('http://%s:%d/prefs_with_comments.js' % (host, port)) michael@0: self.assertEqual(dict(read), self._prefs_with_comments) michael@0: finally: michael@0: httpd.stop() michael@0: michael@0: if __name__ == '__main__': michael@0: unittest.main()