test_chrome_profile.py (1836B)
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 file, 3 # You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 import json 6 import os 7 8 import mozunit 9 from mozprofile import ChromeProfile 10 11 12 def test_chrome_profile_pre_existing(tmpdir): 13 path = tmpdir.strpath 14 profile = ChromeProfile(profile=path) 15 assert not profile.create_new 16 assert os.path.isdir(profile.profile) 17 assert profile.profile == path 18 19 20 def test_chrome_profile_create_new(): 21 profile = ChromeProfile() 22 assert profile.create_new 23 assert os.path.isdir(profile.profile) 24 assert profile.profile.endswith("Default") 25 26 27 def test_chrome_preferences(tmpdir): 28 prefs = {"foo": "bar"} 29 profile = ChromeProfile(preferences=prefs) 30 prefs_file = os.path.join(profile.profile, "Preferences") 31 32 assert os.path.isfile(prefs_file) 33 34 with open(prefs_file) as fh: 35 assert json.load(fh) == prefs 36 37 # test with existing prefs 38 prefs_file = tmpdir.join("Preferences").strpath 39 with open(prefs_file, "w") as fh: 40 json.dump({"num": "1"}, fh) 41 42 profile = ChromeProfile(profile=tmpdir.strpath, preferences=prefs) 43 44 def assert_prefs(): 45 with open(prefs_file) as fh: 46 data = json.load(fh) 47 48 assert len(data) == 2 49 assert data.get("foo") == "bar" 50 assert data.get("num") == "1" 51 52 assert_prefs() 53 profile.reset() 54 assert_prefs() 55 56 57 def test_chrome_addons(): 58 addons = ["foo", "bar"] 59 profile = ChromeProfile(addons=addons) 60 61 assert isinstance(profile.addons, list) 62 assert profile.addons == addons 63 64 profile.addons.install("baz") 65 assert profile.addons == addons + ["baz"] 66 67 profile.reset() 68 assert profile.addons == addons 69 70 71 if __name__ == "__main__": 72 mozunit.main()