netwerk/test/unit/test_cache_jar.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.)

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

mercurial