1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/experiments/test/xpcshell/test_fetch.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,73 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +"use strict"; 1.8 + 1.9 +Cu.import("resource://testing-common/httpd.js"); 1.10 +Cu.import("resource://gre/modules/Services.jsm"); 1.11 +Cu.import("resource://gre/modules/osfile.jsm"); 1.12 +Cu.import("resource:///modules/experiments/Experiments.jsm"); 1.13 + 1.14 +let gProfileDir = null; 1.15 +let gHttpServer = null; 1.16 +let gHttpRoot = null; 1.17 +let gPolicy = new Experiments.Policy(); 1.18 + 1.19 +function run_test() { 1.20 + loadAddonManager(); 1.21 + gProfileDir = do_get_profile(); 1.22 + 1.23 + gHttpServer = new HttpServer(); 1.24 + gHttpServer.start(-1); 1.25 + let port = gHttpServer.identity.primaryPort; 1.26 + gHttpRoot = "http://localhost:" + port + "/"; 1.27 + gHttpServer.registerDirectory("/", do_get_cwd()); 1.28 + do_register_cleanup(() => gHttpServer.stop(() => {})); 1.29 + 1.30 + disableCertificateChecks(); 1.31 + 1.32 + Services.prefs.setBoolPref(PREF_EXPERIMENTS_ENABLED, true); 1.33 + Services.prefs.setIntPref(PREF_LOGGING_LEVEL, 0); 1.34 + Services.prefs.setBoolPref(PREF_LOGGING_DUMP, true); 1.35 + 1.36 + patchPolicy(gPolicy, { 1.37 + updatechannel: () => "nightly", 1.38 + }); 1.39 + 1.40 + run_next_test(); 1.41 +} 1.42 + 1.43 +add_task(function* test_fetchAndCache() { 1.44 + Services.prefs.setCharPref(PREF_MANIFEST_URI, gHttpRoot + "experiments_1.manifest"); 1.45 + let ex = new Experiments.Experiments(gPolicy); 1.46 + 1.47 + Assert.equal(ex._experiments, null, "There should be no cached experiments yet."); 1.48 + yield ex.updateManifest(); 1.49 + Assert.notEqual(ex._experiments.size, 0, "There should be cached experiments now."); 1.50 + 1.51 + yield ex.uninit(); 1.52 +}); 1.53 + 1.54 +add_task(function* test_checkCache() { 1.55 + let ex = new Experiments.Experiments(gPolicy); 1.56 + yield ex.notify(); 1.57 + Assert.notEqual(ex._experiments.size, 0, "There should be cached experiments now."); 1.58 + 1.59 + yield ex.uninit(); 1.60 +}); 1.61 + 1.62 +add_task(function* test_fetchInvalid() { 1.63 + yield removeCacheFile(); 1.64 + 1.65 + Services.prefs.setCharPref(PREF_MANIFEST_URI, gHttpRoot + "experiments_1.manifest"); 1.66 + let ex = new Experiments.Experiments(gPolicy); 1.67 + yield ex.updateManifest(); 1.68 + Assert.notEqual(ex._experiments.size, 0, "There should be experiments"); 1.69 + 1.70 + Services.prefs.setCharPref(PREF_MANIFEST_URI, gHttpRoot + "invalid.manifest"); 1.71 + yield ex.updateManifest() 1.72 + Assert.notEqual(ex._experiments.size, 0, "There should still be experiments: fetch failure shouldn't remove them."); 1.73 + 1.74 + yield ex.uninit(); 1.75 +}); 1.76 +