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