testing/mozbase/mozprofile/tests/test_preferences.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rwxr-xr-x

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 #!/usr/bin/env python
michael@0 2
michael@0 3 # This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 # You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 6
michael@0 7 import mozfile
michael@0 8 import mozhttpd
michael@0 9 import os
michael@0 10 import shutil
michael@0 11 import tempfile
michael@0 12 import unittest
michael@0 13 from mozprofile.cli import MozProfileCLI
michael@0 14 from mozprofile.prefs import Preferences
michael@0 15 from mozprofile.profile import Profile
michael@0 16
michael@0 17 here = os.path.dirname(os.path.abspath(__file__))
michael@0 18
michael@0 19 class PreferencesTest(unittest.TestCase):
michael@0 20 """test mozprofile preference handling"""
michael@0 21
michael@0 22 # preferences from files/prefs_with_comments.js
michael@0 23 _prefs_with_comments = {'browser.startup.homepage': 'http://planet.mozilla.org',
michael@0 24 'zoom.minPercent': 30,
michael@0 25 'zoom.maxPercent': 300,
michael@0 26 'webgl.verbose': 'false'}
michael@0 27
michael@0 28 def run_command(self, *args):
michael@0 29 """
michael@0 30 invokes mozprofile command line via the CLI factory
michael@0 31 - args : command line arguments (equivalent of sys.argv[1:])
michael@0 32 """
michael@0 33
michael@0 34 # instantiate the factory
michael@0 35 cli = MozProfileCLI(list(args))
michael@0 36
michael@0 37 # create the profile
michael@0 38 profile = cli.profile()
michael@0 39
michael@0 40 # return path to profile
michael@0 41 return profile.profile
michael@0 42
michael@0 43 def compare_generated(self, _prefs, commandline):
michael@0 44 """
michael@0 45 writes out to a new profile with mozprofile command line
michael@0 46 reads the generated preferences with prefs.py
michael@0 47 compares the results
michael@0 48 cleans up
michael@0 49 """
michael@0 50 profile = self.run_command(*commandline)
michael@0 51 prefs_file = os.path.join(profile, 'user.js')
michael@0 52 self.assertTrue(os.path.exists(prefs_file))
michael@0 53 read = Preferences.read_prefs(prefs_file)
michael@0 54 if isinstance(_prefs, dict):
michael@0 55 read = dict(read)
michael@0 56 self.assertEqual(_prefs, read)
michael@0 57 shutil.rmtree(profile)
michael@0 58
michael@0 59 def test_basic_prefs(self):
michael@0 60 """test setting a pref from the command line entry point"""
michael@0 61
michael@0 62 _prefs = {"browser.startup.homepage": "http://planet.mozilla.org/"}
michael@0 63 commandline = []
michael@0 64 _prefs = _prefs.items()
michael@0 65 for pref, value in _prefs:
michael@0 66 commandline += ["--pref", "%s:%s" % (pref, value)]
michael@0 67 self.compare_generated(_prefs, commandline)
michael@0 68
michael@0 69 def test_ordered_prefs(self):
michael@0 70 """ensure the prefs stay in the right order"""
michael@0 71 _prefs = [("browser.startup.homepage", "http://planet.mozilla.org/"),
michael@0 72 ("zoom.minPercent", 30),
michael@0 73 ("zoom.maxPercent", 300),
michael@0 74 ("webgl.verbose", 'false')]
michael@0 75 commandline = []
michael@0 76 for pref, value in _prefs:
michael@0 77 commandline += ["--pref", "%s:%s" % (pref, value)]
michael@0 78 _prefs = [(i, Preferences.cast(j)) for i, j in _prefs]
michael@0 79 self.compare_generated(_prefs, commandline)
michael@0 80
michael@0 81 def test_ini(self):
michael@0 82
michael@0 83 # write the .ini file
michael@0 84 _ini = """[DEFAULT]
michael@0 85 browser.startup.homepage = http://planet.mozilla.org/
michael@0 86
michael@0 87 [foo]
michael@0 88 browser.startup.homepage = http://github.com/
michael@0 89 """
michael@0 90 try:
michael@0 91 fd, name = tempfile.mkstemp(suffix='.ini')
michael@0 92 os.write(fd, _ini)
michael@0 93 os.close(fd)
michael@0 94 commandline = ["--preferences", name]
michael@0 95
michael@0 96 # test the [DEFAULT] section
michael@0 97 _prefs = {'browser.startup.homepage': 'http://planet.mozilla.org/'}
michael@0 98 self.compare_generated(_prefs, commandline)
michael@0 99
michael@0 100 # test a specific section
michael@0 101 _prefs = {'browser.startup.homepage': 'http://github.com/'}
michael@0 102 commandline[-1] = commandline[-1] + ':foo'
michael@0 103 self.compare_generated(_prefs, commandline)
michael@0 104
michael@0 105 finally:
michael@0 106 # cleanup
michael@0 107 os.remove(name)
michael@0 108
michael@0 109 def test_reset_should_remove_added_prefs(self):
michael@0 110 """Check that when we call reset the items we expect are updated"""
michael@0 111
michael@0 112 profile = Profile()
michael@0 113 prefs_file = os.path.join(profile.profile, 'user.js')
michael@0 114
michael@0 115 # we shouldn't have any initial preferences
michael@0 116 initial_prefs = Preferences.read_prefs(prefs_file)
michael@0 117 self.assertFalse(initial_prefs)
michael@0 118 initial_prefs = file(prefs_file).read().strip()
michael@0 119 self.assertFalse(initial_prefs)
michael@0 120
michael@0 121 # add some preferences
michael@0 122 prefs1 = [("mr.t.quotes", "i aint getting on no plane!")]
michael@0 123 profile.set_preferences(prefs1)
michael@0 124 self.assertEqual(prefs1, Preferences.read_prefs(prefs_file))
michael@0 125 lines = file(prefs_file).read().strip().splitlines()
michael@0 126 self.assertTrue(bool([line for line in lines
michael@0 127 if line.startswith('#MozRunner Prefs Start')]))
michael@0 128 self.assertTrue(bool([line for line in lines
michael@0 129 if line.startswith('#MozRunner Prefs End')]))
michael@0 130
michael@0 131 profile.reset()
michael@0 132 self.assertNotEqual(prefs1,
michael@0 133 Preferences.read_prefs(os.path.join(profile.profile, 'user.js')),
michael@0 134 "I pity the fool who left my pref")
michael@0 135
michael@0 136 def test_magic_markers(self):
michael@0 137 """ensure our magic markers are working"""
michael@0 138
michael@0 139 profile = Profile()
michael@0 140 prefs_file = os.path.join(profile.profile, 'user.js')
michael@0 141
michael@0 142 # we shouldn't have any initial preferences
michael@0 143 initial_prefs = Preferences.read_prefs(prefs_file)
michael@0 144 self.assertFalse(initial_prefs)
michael@0 145 initial_prefs = file(prefs_file).read().strip()
michael@0 146 self.assertFalse(initial_prefs)
michael@0 147
michael@0 148 # add some preferences
michael@0 149 prefs1 = [("browser.startup.homepage", "http://planet.mozilla.org/"),
michael@0 150 ("zoom.minPercent", 30)]
michael@0 151 profile.set_preferences(prefs1)
michael@0 152 self.assertEqual(prefs1, Preferences.read_prefs(prefs_file))
michael@0 153 lines = file(prefs_file).read().strip().splitlines()
michael@0 154 self.assertTrue(bool([line for line in lines
michael@0 155 if line.startswith('#MozRunner Prefs Start')]))
michael@0 156 self.assertTrue(bool([line for line in lines
michael@0 157 if line.startswith('#MozRunner Prefs End')]))
michael@0 158
michael@0 159 # add some more preferences
michael@0 160 prefs2 = [("zoom.maxPercent", 300),
michael@0 161 ("webgl.verbose", 'false')]
michael@0 162 profile.set_preferences(prefs2)
michael@0 163 self.assertEqual(prefs1 + prefs2, Preferences.read_prefs(prefs_file))
michael@0 164 lines = file(prefs_file).read().strip().splitlines()
michael@0 165 self.assertTrue(len([line for line in lines
michael@0 166 if line.startswith('#MozRunner Prefs Start')]) == 2)
michael@0 167 self.assertTrue(len([line for line in lines
michael@0 168 if line.startswith('#MozRunner Prefs End')]) == 2)
michael@0 169
michael@0 170 # now clean it up
michael@0 171 profile.clean_preferences()
michael@0 172 final_prefs = Preferences.read_prefs(prefs_file)
michael@0 173 self.assertFalse(final_prefs)
michael@0 174 lines = file(prefs_file).read().strip().splitlines()
michael@0 175 self.assertTrue('#MozRunner Prefs Start' not in lines)
michael@0 176 self.assertTrue('#MozRunner Prefs End' not in lines)
michael@0 177
michael@0 178 def test_preexisting_preferences(self):
michael@0 179 """ensure you don't clobber preexisting preferences"""
michael@0 180
michael@0 181 # make a pretend profile
michael@0 182 tempdir = tempfile.mkdtemp()
michael@0 183
michael@0 184 try:
michael@0 185 # make a user.js
michael@0 186 contents = """
michael@0 187 user_pref("webgl.enabled_for_all_sites", true);
michael@0 188 user_pref("webgl.force-enabled", true);
michael@0 189 """
michael@0 190 user_js = os.path.join(tempdir, 'user.js')
michael@0 191 f = file(user_js, 'w')
michael@0 192 f.write(contents)
michael@0 193 f.close()
michael@0 194
michael@0 195 # make sure you can read it
michael@0 196 prefs = Preferences.read_prefs(user_js)
michael@0 197 original_prefs = [('webgl.enabled_for_all_sites', True), ('webgl.force-enabled', True)]
michael@0 198 self.assertTrue(prefs == original_prefs)
michael@0 199
michael@0 200 # now read this as a profile
michael@0 201 profile = Profile(tempdir, preferences={"browser.download.dir": "/home/jhammel"})
michael@0 202
michael@0 203 # make sure the new pref is now there
michael@0 204 new_prefs = original_prefs[:] + [("browser.download.dir", "/home/jhammel")]
michael@0 205 prefs = Preferences.read_prefs(user_js)
michael@0 206 self.assertTrue(prefs == new_prefs)
michael@0 207
michael@0 208 # clean up the added preferences
michael@0 209 profile.cleanup()
michael@0 210 del profile
michael@0 211
michael@0 212 # make sure you have the original preferences
michael@0 213 prefs = Preferences.read_prefs(user_js)
michael@0 214 self.assertTrue(prefs == original_prefs)
michael@0 215 finally:
michael@0 216 shutil.rmtree(tempdir)
michael@0 217
michael@0 218 def test_json(self):
michael@0 219 _prefs = {"browser.startup.homepage": "http://planet.mozilla.org/"}
michael@0 220 json = '{"browser.startup.homepage": "http://planet.mozilla.org/"}'
michael@0 221
michael@0 222 # just repr it...could use the json module but we don't need it here
michael@0 223 fd, name = tempfile.mkstemp(suffix='.json')
michael@0 224 os.write(fd, json)
michael@0 225 os.close(fd)
michael@0 226
michael@0 227 commandline = ["--preferences", name]
michael@0 228 self.compare_generated(_prefs, commandline)
michael@0 229
michael@0 230 def test_prefs_write(self):
michael@0 231 """test that the Preferences.write() method correctly serializes preferences"""
michael@0 232
michael@0 233 _prefs = {'browser.startup.homepage': "http://planet.mozilla.org",
michael@0 234 'zoom.minPercent': 30,
michael@0 235 'zoom.maxPercent': 300}
michael@0 236
michael@0 237 # make a Preferences manager with the testing preferences
michael@0 238 preferences = Preferences(_prefs)
michael@0 239
michael@0 240 # write them to a temporary location
michael@0 241 path = None
michael@0 242 read_prefs = None
michael@0 243 try:
michael@0 244 with mozfile.NamedTemporaryFile(suffix='.js', delete=False) as f:
michael@0 245 path = f.name
michael@0 246 preferences.write(f, _prefs)
michael@0 247
michael@0 248 # read them back and ensure we get what we put in
michael@0 249 read_prefs = dict(Preferences.read_prefs(path))
michael@0 250
michael@0 251 finally:
michael@0 252 # cleanup
michael@0 253 if path and os.path.exists(path):
michael@0 254 os.remove(path)
michael@0 255
michael@0 256 self.assertEqual(read_prefs, _prefs)
michael@0 257
michael@0 258 def test_read_prefs_with_comments(self):
michael@0 259 """test reading preferences from a prefs.js file that contains comments"""
michael@0 260
michael@0 261 path = os.path.join(here, 'files', 'prefs_with_comments.js')
michael@0 262 self.assertEqual(dict(Preferences.read_prefs(path)), self._prefs_with_comments)
michael@0 263
michael@0 264 def test_read_prefs_with_interpolation(self):
michael@0 265 """test reading preferences from a prefs.js file whose values
michael@0 266 require interpolation"""
michael@0 267
michael@0 268 expected_prefs = {
michael@0 269 "browser.foo": "http://server-name",
michael@0 270 "zoom.minPercent": 30,
michael@0 271 "webgl.verbose": "false",
michael@0 272 "browser.bar": "somethingxyz"
michael@0 273 }
michael@0 274 values = {
michael@0 275 "server": "server-name",
michael@0 276 "abc": "something"
michael@0 277 }
michael@0 278 path = os.path.join(here, 'files', 'prefs_with_interpolation.js')
michael@0 279 read_prefs = Preferences.read_prefs(path, interpolation=values)
michael@0 280 self.assertEqual(dict(read_prefs), expected_prefs)
michael@0 281
michael@0 282 def test_read_prefs_ttw(self):
michael@0 283 """test reading preferences through the web via mozhttpd"""
michael@0 284
michael@0 285 # create a MozHttpd instance
michael@0 286 docroot = os.path.join(here, 'files')
michael@0 287 host = '127.0.0.1'
michael@0 288 port = 8888
michael@0 289 httpd = mozhttpd.MozHttpd(host=host, port=port, docroot=docroot)
michael@0 290
michael@0 291 # create a preferences instance
michael@0 292 prefs = Preferences()
michael@0 293
michael@0 294 try:
michael@0 295 # start server
michael@0 296 httpd.start(block=False)
michael@0 297
michael@0 298 # read preferences through the web
michael@0 299 read = prefs.read_prefs('http://%s:%d/prefs_with_comments.js' % (host, port))
michael@0 300 self.assertEqual(dict(read), self._prefs_with_comments)
michael@0 301 finally:
michael@0 302 httpd.stop()
michael@0 303
michael@0 304 if __name__ == '__main__':
michael@0 305 unittest.main()

mercurial