netwerk/test/unit/test_offlinecache_custom-directory.js

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /**
michael@0 6 * This test executes nsIOfflineCacheUpdateService.scheduleAppUpdate API
michael@0 7 * 1. preloads an app with a manifest to a custom sudir in the profile (for simplicity)
michael@0 8 * 2. observes progress and completion of the update
michael@0 9 * 3. checks presence of index.sql and files in the expected location
michael@0 10 */
michael@0 11
michael@0 12 Cu.import("resource://testing-common/httpd.js");
michael@0 13
michael@0 14 var httpServer = null;
michael@0 15 var cacheUpdateObserver = null;
michael@0 16
michael@0 17 function make_channel(url, callback, ctx) {
michael@0 18 var ios = Cc["@mozilla.org/network/io-service;1"].
michael@0 19 getService(Ci.nsIIOService);
michael@0 20 return ios.newChannel(url, "", null);
michael@0 21 }
michael@0 22
michael@0 23 function make_uri(url) {
michael@0 24 var ios = Cc["@mozilla.org/network/io-service;1"].
michael@0 25 getService(Ci.nsIIOService);
michael@0 26 return ios.newURI(url, null, null);
michael@0 27 }
michael@0 28
michael@0 29 // start the test with loading this master entry referencing the manifest
michael@0 30 function masterEntryHandler(metadata, response)
michael@0 31 {
michael@0 32 var masterEntryContent = "<html manifest='/manifest'></html>";
michael@0 33 response.setHeader("Content-Type", "text/html");
michael@0 34 response.bodyOutputStream.write(masterEntryContent, masterEntryContent.length);
michael@0 35 }
michael@0 36
michael@0 37 // manifest defines fallback namespace from any /redirect path to /content
michael@0 38 function manifestHandler(metadata, response)
michael@0 39 {
michael@0 40 var manifestContent = "CACHE MANIFEST\n";
michael@0 41 response.setHeader("Content-Type", "text/cache-manifest");
michael@0 42 response.bodyOutputStream.write(manifestContent, manifestContent.length);
michael@0 43 }
michael@0 44
michael@0 45 // finally check we got fallback content
michael@0 46 function finish_test(customDir)
michael@0 47 {
michael@0 48 var offlineCacheDir = customDir.clone();
michael@0 49 offlineCacheDir.append("OfflineCache");
michael@0 50
michael@0 51 var indexSqlFile = offlineCacheDir.clone();
michael@0 52 indexSqlFile.append('index.sqlite');
michael@0 53 do_check_eq(indexSqlFile.exists(), true);
michael@0 54
michael@0 55 var file1 = offlineCacheDir.clone();
michael@0 56 file1.append("2");
michael@0 57 file1.append("E");
michael@0 58 file1.append("2C99DE6E7289A5-0");
michael@0 59 do_check_eq(file1.exists(), true);
michael@0 60
michael@0 61 var file2 = offlineCacheDir.clone();
michael@0 62 file2.append("8");
michael@0 63 file2.append("6");
michael@0 64 file2.append("0B457F75198B29-0");
michael@0 65 do_check_eq(file2.exists(), true);
michael@0 66
michael@0 67 // This must not throw an exception. After the update has finished
michael@0 68 // the index file can be freely removed. This way we check this process
michael@0 69 // is no longer keeping the file open. Check like this will probably
michael@0 70 // work only Windows systems.
michael@0 71
michael@0 72 // This test could potentially randomaly fail when we start closing
michael@0 73 // the offline cache database off the main thread. Tries in a loop
michael@0 74 // may be a solution then.
michael@0 75 try {
michael@0 76 indexSqlFile.remove(false);
michael@0 77 do_check_true(true);
michael@0 78 }
michael@0 79 catch (ex) {
michael@0 80 do_throw("Could not remove the sqlite.index file, we still keep it open \n" + ex + "\n");
michael@0 81 }
michael@0 82
michael@0 83 httpServer.stop(do_test_finished);
michael@0 84 }
michael@0 85
michael@0 86 function run_test()
michael@0 87 {
michael@0 88 httpServer = new HttpServer();
michael@0 89 httpServer.registerPathHandler("/masterEntry", masterEntryHandler);
michael@0 90 httpServer.registerPathHandler("/manifest", manifestHandler);
michael@0 91 httpServer.start(4444);
michael@0 92
michael@0 93 var profileDir = do_get_profile();
michael@0 94 var customDir = profileDir.clone();
michael@0 95 customDir.append("customOfflineCacheDir" + Math.random());
michael@0 96
michael@0 97 var pm = Cc["@mozilla.org/permissionmanager;1"]
michael@0 98 .getService(Ci.nsIPermissionManager);
michael@0 99 var uri = make_uri("http://localhost:4444");
michael@0 100 var principal = Cc["@mozilla.org/scriptsecuritymanager;1"]
michael@0 101 .getService(Ci.nsIScriptSecurityManager)
michael@0 102 .getNoAppCodebasePrincipal(uri);
michael@0 103
michael@0 104 if (pm.testPermissionFromPrincipal(principal, "offline-app") != 0) {
michael@0 105 dump("Previous test failed to clear offline-app permission! Expect failures.\n");
michael@0 106 }
michael@0 107 pm.addFromPrincipal(principal, "offline-app", Ci.nsIPermissionManager.ALLOW_ACTION);
michael@0 108
michael@0 109 var ps = Cc["@mozilla.org/preferences-service;1"]
michael@0 110 .getService(Ci.nsIPrefBranch);
michael@0 111 ps.setBoolPref("browser.cache.offline.enable", true);
michael@0 112 // Set this pref to mimic the default browser behavior.
michael@0 113 ps.setComplexValue("browser.cache.offline.parent_directory", Ci.nsILocalFile, profileDir);
michael@0 114
michael@0 115 var us = Cc["@mozilla.org/offlinecacheupdate-service;1"].
michael@0 116 getService(Ci.nsIOfflineCacheUpdateService);
michael@0 117 var update = us.scheduleAppUpdate(
michael@0 118 make_uri("http://localhost:4444/manifest"),
michael@0 119 make_uri("http://localhost:4444/masterEntry"),
michael@0 120 0 /* no AppID */, false /* not in browser*/,
michael@0 121 customDir);
michael@0 122
michael@0 123 var expectedStates = [
michael@0 124 Ci.nsIOfflineCacheUpdateObserver.STATE_DOWNLOADING,
michael@0 125 Ci.nsIOfflineCacheUpdateObserver.STATE_ITEMSTARTED,
michael@0 126 Ci.nsIOfflineCacheUpdateObserver.STATE_ITEMPROGRESS,
michael@0 127 Ci.nsIOfflineCacheUpdateObserver.STATE_ITEMCOMPLETED,
michael@0 128 Ci.nsIOfflineCacheUpdateObserver.STATE_FINISHED
michael@0 129 ];
michael@0 130
michael@0 131 update.addObserver({
michael@0 132 updateStateChanged: function(update, state)
michael@0 133 {
michael@0 134 do_check_eq(state, expectedStates.shift());
michael@0 135
michael@0 136 if (state == Ci.nsIOfflineCacheUpdateObserver.STATE_FINISHED)
michael@0 137 finish_test(customDir);
michael@0 138 },
michael@0 139
michael@0 140 applicationCacheAvailable: function(appCache)
michael@0 141 {
michael@0 142 }
michael@0 143 }, false);
michael@0 144
michael@0 145 do_test_pending();
michael@0 146 }

mercurial