OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 import os |
| 4 import shutil |
| 5 import subprocess |
| 6 import tempfile |
| 7 import unittest |
| 8 from mozprofile.prefs import Preferences |
| 9 from mozprofile.profile import Profile |
| 10 |
| 11 class PreferencesTest(unittest.TestCase): |
| 12 """test mozprofile""" |
| 13 |
| 14 def run_command(self, *args): |
| 15 """ |
| 16 runs mozprofile; |
| 17 returns (stdout, stderr, code) |
| 18 """ |
| 19 process = subprocess.Popen(args, |
| 20 stdout=subprocess.PIPE, |
| 21 stderr=subprocess.PIPE) |
| 22 stdout, stderr = process.communicate() |
| 23 stdout = stdout.strip() |
| 24 stderr = stderr.strip() |
| 25 return stdout, stderr, process.returncode |
| 26 |
| 27 def compare_generated(self, _prefs, commandline): |
| 28 """ |
| 29 writes out to a new profile with mozprofile command line |
| 30 reads the generated preferences with prefs.py |
| 31 compares the results |
| 32 cleans up |
| 33 """ |
| 34 profile, stderr, code = self.run_command(*commandline) |
| 35 prefs_file = os.path.join(profile, 'user.js') |
| 36 self.assertTrue(os.path.exists(prefs_file)) |
| 37 read = Preferences.read_prefs(prefs_file) |
| 38 if isinstance(_prefs, dict): |
| 39 read = dict(read) |
| 40 self.assertEqual(_prefs, read) |
| 41 shutil.rmtree(profile) |
| 42 |
| 43 def test_basic_prefs(self): |
| 44 _prefs = {"browser.startup.homepage": "http://planet.mozilla.org/"} |
| 45 commandline = ["mozprofile"] |
| 46 _prefs = _prefs.items() |
| 47 for pref, value in _prefs: |
| 48 commandline += ["--pref", "%s:%s" % (pref, value)] |
| 49 self.compare_generated(_prefs, commandline) |
| 50 |
| 51 def test_ordered_prefs(self): |
| 52 """ensure the prefs stay in the right order""" |
| 53 _prefs = [("browser.startup.homepage", "http://planet.mozilla.org/"), |
| 54 ("zoom.minPercent", 30), |
| 55 ("zoom.maxPercent", 300), |
| 56 ("webgl.verbose", 'false')] |
| 57 commandline = ["mozprofile"] |
| 58 for pref, value in _prefs: |
| 59 commandline += ["--pref", "%s:%s" % (pref, value)] |
| 60 _prefs = [(i, Preferences.cast(j)) for i, j in _prefs] |
| 61 self.compare_generated(_prefs, commandline) |
| 62 |
| 63 def test_ini(self): |
| 64 |
| 65 # write the .ini file |
| 66 _ini = """[DEFAULT] |
| 67 browser.startup.homepage = http://planet.mozilla.org/ |
| 68 |
| 69 [foo] |
| 70 browser.startup.homepage = http://github.com/ |
| 71 """ |
| 72 fd, name = tempfile.mkstemp(suffix='.ini') |
| 73 os.write(fd, _ini) |
| 74 os.close(fd) |
| 75 commandline = ["mozprofile", "--preferences", name] |
| 76 |
| 77 # test the [DEFAULT] section |
| 78 _prefs = {'browser.startup.homepage': 'http://planet.mozilla.org/'} |
| 79 self.compare_generated(_prefs, commandline) |
| 80 |
| 81 # test a specific section |
| 82 _prefs = {'browser.startup.homepage': 'http://github.com/'} |
| 83 commandline[-1] = commandline[-1] + ':foo' |
| 84 self.compare_generated(_prefs, commandline) |
| 85 |
| 86 # cleanup |
| 87 os.remove(name) |
| 88 |
| 89 def test_reset_should_remove_added_prefs(self): |
| 90 """Check that when we call reset the items we expect are updated""" |
| 91 |
| 92 profile = Profile() |
| 93 prefs_file = os.path.join(profile.profile, 'user.js') |
| 94 |
| 95 # we shouldn't have any initial preferences |
| 96 initial_prefs = Preferences.read_prefs(prefs_file) |
| 97 self.assertFalse(initial_prefs) |
| 98 initial_prefs = file(prefs_file).read().strip() |
| 99 self.assertFalse(initial_prefs) |
| 100 |
| 101 # add some preferences |
| 102 prefs1 = [("mr.t.quotes", "i aint getting on no plane!")] |
| 103 profile.set_preferences(prefs1) |
| 104 self.assertEqual(prefs1, Preferences.read_prefs(prefs_file)) |
| 105 lines = file(prefs_file).read().strip().splitlines() |
| 106 self.assertTrue(bool([line for line in lines |
| 107 if line.startswith('#MozRunner Prefs Start')])) |
| 108 self.assertTrue(bool([line for line in lines |
| 109 if line.startswith('#MozRunner Prefs End')])) |
| 110 |
| 111 profile.reset() |
| 112 self.assertNotEqual(prefs1, \ |
| 113 Preferences.read_prefs(os.path.join(profile.profile, 'user.j
s')),\ |
| 114 "I pity the fool who left my pref") |
| 115 |
| 116 def test_magic_markers(self): |
| 117 """ensure our magic markers are working""" |
| 118 |
| 119 profile = Profile() |
| 120 prefs_file = os.path.join(profile.profile, 'user.js') |
| 121 |
| 122 # we shouldn't have any initial preferences |
| 123 initial_prefs = Preferences.read_prefs(prefs_file) |
| 124 self.assertFalse(initial_prefs) |
| 125 initial_prefs = file(prefs_file).read().strip() |
| 126 self.assertFalse(initial_prefs) |
| 127 |
| 128 # add some preferences |
| 129 prefs1 = [("browser.startup.homepage", "http://planet.mozilla.org/"), |
| 130 ("zoom.minPercent", 30)] |
| 131 profile.set_preferences(prefs1) |
| 132 self.assertEqual(prefs1, Preferences.read_prefs(prefs_file)) |
| 133 lines = file(prefs_file).read().strip().splitlines() |
| 134 self.assertTrue(bool([line for line in lines |
| 135 if line.startswith('#MozRunner Prefs Start')])) |
| 136 self.assertTrue(bool([line for line in lines |
| 137 if line.startswith('#MozRunner Prefs End')])) |
| 138 |
| 139 # add some more preferences |
| 140 prefs2 = [("zoom.maxPercent", 300), |
| 141 ("webgl.verbose", 'false')] |
| 142 profile.set_preferences(prefs2) |
| 143 self.assertEqual(prefs1 + prefs2, Preferences.read_prefs(prefs_file)) |
| 144 lines = file(prefs_file).read().strip().splitlines() |
| 145 self.assertTrue(len([line for line in lines |
| 146 if line.startswith('#MozRunner Prefs Start')]) == 2
) |
| 147 self.assertTrue(len([line for line in lines |
| 148 if line.startswith('#MozRunner Prefs End')]) == 2) |
| 149 |
| 150 # now clean it up |
| 151 profile.clean_preferences() |
| 152 final_prefs = Preferences.read_prefs(prefs_file) |
| 153 self.assertFalse(final_prefs) |
| 154 lines = file(prefs_file).read().strip().splitlines() |
| 155 self.assertTrue('#MozRunner Prefs Start' not in lines) |
| 156 self.assertTrue('#MozRunner Prefs End' not in lines) |
| 157 |
| 158 def test_preexisting_preferences(self): |
| 159 """ensure you don't clobber preexisting preferences""" |
| 160 |
| 161 # make a pretend profile |
| 162 tempdir = tempfile.mkdtemp() |
| 163 |
| 164 try: |
| 165 # make a user.js |
| 166 contents = """ |
| 167 user_pref("webgl.enabled_for_all_sites", true); |
| 168 user_pref("webgl.force-enabled", true); |
| 169 """ |
| 170 user_js = os.path.join(tempdir, 'user.js') |
| 171 f = file(user_js, 'w') |
| 172 f.write(contents) |
| 173 f.close() |
| 174 |
| 175 # make sure you can read it |
| 176 prefs = Preferences.read_prefs(user_js) |
| 177 original_prefs = [('webgl.enabled_for_all_sites', True), ('webgl.for
ce-enabled', True)] |
| 178 self.assertTrue(prefs == original_prefs) |
| 179 |
| 180 # now read this as a profile |
| 181 profile = Profile(tempdir, preferences={"browser.download.dir": "/ho
me/jhammel"}) |
| 182 |
| 183 # make sure the new pref is now there |
| 184 new_prefs = original_prefs[:] + [("browser.download.dir", "/home/jha
mmel")] |
| 185 prefs = Preferences.read_prefs(user_js) |
| 186 self.assertTrue(prefs == new_prefs) |
| 187 |
| 188 # clean up the added preferences |
| 189 profile.cleanup() |
| 190 del profile |
| 191 |
| 192 # make sure you have the original preferences |
| 193 prefs = Preferences.read_prefs(user_js) |
| 194 self.assertTrue(prefs == original_prefs) |
| 195 except: |
| 196 shutil.rmtree(tempdir) |
| 197 raise |
| 198 |
| 199 def test_json(self): |
| 200 _prefs = {"browser.startup.homepage": "http://planet.mozilla.org/"} |
| 201 json = '{"browser.startup.homepage": "http://planet.mozilla.org/"}' |
| 202 |
| 203 # just repr it...could use the json module but we don't need it here |
| 204 fd, name = tempfile.mkstemp(suffix='.json') |
| 205 os.write(fd, json) |
| 206 os.close(fd) |
| 207 |
| 208 commandline = ["mozprofile", "--preferences", name] |
| 209 self.compare_generated(_prefs, commandline) |
| 210 |
| 211 |
| 212 if __name__ == '__main__': |
| 213 unittest.main() |
OLD | NEW |