Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | Cu.import("resource://testing-common/httpd.js"); |
michael@0 | 2 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 3 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 4 | |
michael@0 | 5 | XPCOMUtils.defineLazyGetter(this, "URL", function() { |
michael@0 | 6 | return "http://localhost:" + httpserv.identity.primaryPort + "/cached"; |
michael@0 | 7 | }); |
michael@0 | 8 | |
michael@0 | 9 | var httpserv = null; |
michael@0 | 10 | var handlers_called = 0; |
michael@0 | 11 | |
michael@0 | 12 | function cached_handler(metadata, response) { |
michael@0 | 13 | response.setHeader("Content-Type", "text/plain", false); |
michael@0 | 14 | response.setHeader("Cache-Control", "max-age=10000", false); |
michael@0 | 15 | response.setStatusLine(metadata.httpVersion, 200, "OK"); |
michael@0 | 16 | var body = "0123456789"; |
michael@0 | 17 | response.bodyOutputStream.write(body, body.length); |
michael@0 | 18 | handlers_called++; |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | function makeChan(url, appId, inBrowser) { |
michael@0 | 22 | var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); |
michael@0 | 23 | var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel); |
michael@0 | 24 | chan.notificationCallbacks = { |
michael@0 | 25 | appId: appId, |
michael@0 | 26 | isInBrowserElement: inBrowser, |
michael@0 | 27 | QueryInterface: function(iid) { |
michael@0 | 28 | if (iid.equals(Ci.nsILoadContext)) |
michael@0 | 29 | return this; |
michael@0 | 30 | throw Cr.NS_ERROR_NO_INTERFACE; |
michael@0 | 31 | }, |
michael@0 | 32 | getInterface: function(iid) { return this.QueryInterface(iid); } |
michael@0 | 33 | }; |
michael@0 | 34 | return chan; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | var firstTests = [[0, false, 1], [0, true, 1], [1, false, 1], [1, true, 1]]; |
michael@0 | 38 | var secondTests = [[0, false, 0], [0, true, 0], [1, false, 0], [1, true, 1]]; |
michael@0 | 39 | var thirdTests = [[0, false, 0], [0, true, 0], [1, false, 1], [1, true, 1]]; |
michael@0 | 40 | |
michael@0 | 41 | function run_all_tests() { |
michael@0 | 42 | for (let test of firstTests) { |
michael@0 | 43 | handlers_called = 0; |
michael@0 | 44 | var chan = makeChan(URL, test[0], test[1]); |
michael@0 | 45 | chan.asyncOpen(new ChannelListener(doneFirstLoad, test[2]), null); |
michael@0 | 46 | yield undefined; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | // We can't easily cause webapp data to be cleared from the child process, so skip |
michael@0 | 50 | // the rest of these tests. |
michael@0 | 51 | let procType = Cc["@mozilla.org/xre/runtime;1"].getService(Ci.nsIXULRuntime).processType; |
michael@0 | 52 | if (procType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) |
michael@0 | 53 | return; |
michael@0 | 54 | |
michael@0 | 55 | let subject = { |
michael@0 | 56 | appId: 1, |
michael@0 | 57 | browserOnly: true, |
michael@0 | 58 | QueryInterface: XPCOMUtils.generateQI([Ci.mozIApplicationClearPrivateDataParams]) |
michael@0 | 59 | }; |
michael@0 | 60 | Services.obs.notifyObservers(subject, "webapps-clear-data", null); |
michael@0 | 61 | |
michael@0 | 62 | for (let test of secondTests) { |
michael@0 | 63 | handlers_called = 0; |
michael@0 | 64 | var chan = makeChan(URL, test[0], test[1]); |
michael@0 | 65 | chan.asyncOpen(new ChannelListener(doneFirstLoad, test[2]), null); |
michael@0 | 66 | yield undefined; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | subject = { |
michael@0 | 70 | appId: 1, |
michael@0 | 71 | browserOnly: false, |
michael@0 | 72 | QueryInterface: XPCOMUtils.generateQI([Ci.mozIApplicationClearPrivateDataParams]) |
michael@0 | 73 | }; |
michael@0 | 74 | Services.obs.notifyObservers(subject, "webapps-clear-data", null); |
michael@0 | 75 | |
michael@0 | 76 | for (let test of thirdTests) { |
michael@0 | 77 | handlers_called = 0; |
michael@0 | 78 | var chan = makeChan(URL, test[0], test[1]); |
michael@0 | 79 | chan.asyncOpen(new ChannelListener(doneFirstLoad, test[2]), null); |
michael@0 | 80 | yield undefined; |
michael@0 | 81 | } |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | let gTests; |
michael@0 | 85 | function run_test() { |
michael@0 | 86 | do_get_profile(); |
michael@0 | 87 | do_test_pending(); |
michael@0 | 88 | httpserv = new HttpServer(); |
michael@0 | 89 | httpserv.registerPathHandler("/cached", cached_handler); |
michael@0 | 90 | httpserv.start(-1); |
michael@0 | 91 | gTests = run_all_tests(); |
michael@0 | 92 | gTests.next(); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | function doneFirstLoad(req, buffer, expected) { |
michael@0 | 96 | // Load it again, make sure it hits the cache |
michael@0 | 97 | var chan = makeChan(URL, 0, false); |
michael@0 | 98 | chan.asyncOpen(new ChannelListener(doneSecondLoad, expected), null); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | function doneSecondLoad(req, buffer, expected) { |
michael@0 | 102 | do_check_eq(handlers_called, expected); |
michael@0 | 103 | try { |
michael@0 | 104 | gTests.next(); |
michael@0 | 105 | } catch (x) { |
michael@0 | 106 | do_test_finished(); |
michael@0 | 107 | } |
michael@0 | 108 | } |