michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "URL", function() { michael@0: return "http://localhost:" + httpserv.identity.primaryPort + "/cached"; michael@0: }); michael@0: michael@0: var httpserv = null; michael@0: var handlers_called = 0; michael@0: michael@0: function cached_handler(metadata, response) { michael@0: response.setHeader("Content-Type", "text/plain", false); michael@0: response.setHeader("Cache-Control", "max-age=10000", false); michael@0: response.setStatusLine(metadata.httpVersion, 200, "OK"); michael@0: var body = "0123456789"; michael@0: response.bodyOutputStream.write(body, body.length); michael@0: handlers_called++; michael@0: } michael@0: michael@0: function makeChan(url, appId, inBrowser) { michael@0: var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); michael@0: var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel); michael@0: chan.notificationCallbacks = { michael@0: appId: appId, michael@0: isInBrowserElement: inBrowser, michael@0: QueryInterface: function(iid) { michael@0: if (iid.equals(Ci.nsILoadContext)) michael@0: return this; michael@0: throw Cr.NS_ERROR_NO_INTERFACE; michael@0: }, michael@0: getInterface: function(iid) { return this.QueryInterface(iid); } michael@0: }; michael@0: return chan; michael@0: } michael@0: michael@0: var firstTests = [[0, false, 1], [0, true, 1], [1, false, 1], [1, true, 1]]; michael@0: var secondTests = [[0, false, 0], [0, true, 0], [1, false, 0], [1, true, 1]]; michael@0: var thirdTests = [[0, false, 0], [0, true, 0], [1, false, 1], [1, true, 1]]; michael@0: michael@0: function run_all_tests() { michael@0: for (let test of firstTests) { michael@0: handlers_called = 0; michael@0: var chan = makeChan(URL, test[0], test[1]); michael@0: chan.asyncOpen(new ChannelListener(doneFirstLoad, test[2]), null); michael@0: yield undefined; michael@0: } michael@0: michael@0: // We can't easily cause webapp data to be cleared from the child process, so skip michael@0: // the rest of these tests. michael@0: let procType = Cc["@mozilla.org/xre/runtime;1"].getService(Ci.nsIXULRuntime).processType; michael@0: if (procType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) michael@0: return; michael@0: michael@0: let subject = { michael@0: appId: 1, michael@0: browserOnly: true, michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.mozIApplicationClearPrivateDataParams]) michael@0: }; michael@0: Services.obs.notifyObservers(subject, "webapps-clear-data", null); michael@0: michael@0: for (let test of secondTests) { michael@0: handlers_called = 0; michael@0: var chan = makeChan(URL, test[0], test[1]); michael@0: chan.asyncOpen(new ChannelListener(doneFirstLoad, test[2]), null); michael@0: yield undefined; michael@0: } michael@0: michael@0: subject = { michael@0: appId: 1, michael@0: browserOnly: false, michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.mozIApplicationClearPrivateDataParams]) michael@0: }; michael@0: Services.obs.notifyObservers(subject, "webapps-clear-data", null); michael@0: michael@0: for (let test of thirdTests) { michael@0: handlers_called = 0; michael@0: var chan = makeChan(URL, test[0], test[1]); michael@0: chan.asyncOpen(new ChannelListener(doneFirstLoad, test[2]), null); michael@0: yield undefined; michael@0: } michael@0: } michael@0: michael@0: let gTests; michael@0: function run_test() { michael@0: do_get_profile(); michael@0: do_test_pending(); michael@0: httpserv = new HttpServer(); michael@0: httpserv.registerPathHandler("/cached", cached_handler); michael@0: httpserv.start(-1); michael@0: gTests = run_all_tests(); michael@0: gTests.next(); michael@0: } michael@0: michael@0: function doneFirstLoad(req, buffer, expected) { michael@0: // Load it again, make sure it hits the cache michael@0: var chan = makeChan(URL, 0, false); michael@0: chan.asyncOpen(new ChannelListener(doneSecondLoad, expected), null); michael@0: } michael@0: michael@0: function doneSecondLoad(req, buffer, expected) { michael@0: do_check_eq(handlers_called, expected); michael@0: try { michael@0: gTests.next(); michael@0: } catch (x) { michael@0: do_test_finished(); michael@0: } michael@0: }