michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: michael@0: var httpServer = null; michael@0: // Need to randomize, because apparently no one clears our cache michael@0: var randomPath = "/error/" + Math.random(); michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "randomURI", function() { michael@0: return "http://localhost:" + httpServer.identity.primaryPort + randomPath; michael@0: }); michael@0: michael@0: var cacheUpdateObserver = null; michael@0: michael@0: function make_channel(url, callback, ctx) { michael@0: var ios = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService); michael@0: return ios.newChannel(url, "", null); michael@0: } michael@0: michael@0: function make_uri(url) { michael@0: var ios = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService); michael@0: return ios.newURI(url, null, null); michael@0: } michael@0: michael@0: var responseBody = "Content body"; michael@0: michael@0: // start the test with loading this master entry referencing the manifest michael@0: function masterEntryHandler(metadata, response) michael@0: { michael@0: var masterEntryContent = ""; michael@0: response.setHeader("Content-Type", "text/html"); michael@0: response.bodyOutputStream.write(masterEntryContent, masterEntryContent.length); michael@0: } michael@0: michael@0: // manifest defines fallback namespace from any /error path to /content michael@0: function manifestHandler(metadata, response) michael@0: { michael@0: var manifestContent = "CACHE MANIFEST\nFALLBACK:\nerror /content\n"; michael@0: response.setHeader("Content-Type", "text/cache-manifest"); michael@0: response.bodyOutputStream.write(manifestContent, manifestContent.length); michael@0: } michael@0: michael@0: // content handler correctly returns some plain text data michael@0: function contentHandler(metadata, response) michael@0: { michael@0: response.setHeader("Content-Type", "text/plain"); michael@0: response.bodyOutputStream.write(responseBody, responseBody.length); michael@0: } michael@0: michael@0: // error handler returns error michael@0: function errorHandler(metadata, response) michael@0: { michael@0: response.setStatusLine(metadata.httpVersion, 404, "Bad request"); michael@0: } michael@0: michael@0: // finally check we got fallback content michael@0: function finish_test(request, buffer) michael@0: { michael@0: do_check_eq(buffer, responseBody); michael@0: httpServer.stop(do_test_finished); michael@0: } michael@0: michael@0: function run_test() michael@0: { michael@0: httpServer = new HttpServer(); michael@0: httpServer.registerPathHandler("/masterEntry", masterEntryHandler); michael@0: httpServer.registerPathHandler("/manifest", manifestHandler); michael@0: httpServer.registerPathHandler("/content", contentHandler); michael@0: httpServer.registerPathHandler(randomPath, errorHandler); michael@0: httpServer.start(-1); michael@0: michael@0: var pm = Cc["@mozilla.org/permissionmanager;1"] michael@0: .getService(Ci.nsIPermissionManager); michael@0: var uri = make_uri("http://localhost:" + httpServer.identity.primaryPort); michael@0: var principal = Cc["@mozilla.org/scriptsecuritymanager;1"] michael@0: .getService(Ci.nsIScriptSecurityManager) michael@0: .getNoAppCodebasePrincipal(uri); michael@0: michael@0: if (pm.testPermissionFromPrincipal(principal, "offline-app") != 0) { michael@0: dump("Previous test failed to clear offline-app permission! Expect failures.\n"); michael@0: } michael@0: pm.addFromPrincipal(principal, "offline-app", Ci.nsIPermissionManager.ALLOW_ACTION); michael@0: michael@0: var ps = Cc["@mozilla.org/preferences-service;1"] michael@0: .getService(Ci.nsIPrefBranch); michael@0: dump(ps.getBoolPref("browser.cache.offline.enable")); michael@0: ps.setBoolPref("browser.cache.offline.enable", true); michael@0: ps.setComplexValue("browser.cache.offline.parent_directory", Ci.nsILocalFile, do_get_profile()); michael@0: michael@0: cacheUpdateObserver = {observe: function() { michael@0: dump("got offline-cache-update-completed\n"); michael@0: // offline cache update completed. michael@0: var chan = make_channel(randomURI); michael@0: var chanac = chan.QueryInterface(Ci.nsIApplicationCacheChannel); michael@0: chanac.chooseApplicationCache = true; michael@0: chan.asyncOpen(new ChannelListener(finish_test), null); michael@0: }} michael@0: michael@0: var os = Cc["@mozilla.org/observer-service;1"]. michael@0: getService(Ci.nsIObserverService); michael@0: os.addObserver(cacheUpdateObserver, "offline-cache-update-completed", false); michael@0: michael@0: var us = Cc["@mozilla.org/offlinecacheupdate-service;1"]. michael@0: getService(Ci.nsIOfflineCacheUpdateService); michael@0: us.scheduleUpdate(make_uri("http://localhost:" + michael@0: httpServer.identity.primaryPort + "/manifest"), michael@0: make_uri("http://localhost:" + michael@0: httpServer.identity.primaryPort + "/masterEntry"), michael@0: null); michael@0: michael@0: do_test_pending(); michael@0: }