image/test/unit/test_private_channel.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/image/test/unit/test_private_channel.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,133 @@
     1.4 +const Cc = Components.classes;
     1.5 +const Ci = Components.interfaces;
     1.6 +const Cr = Components.results;
     1.7 +const Cu = Components.utils;
     1.8 +
     1.9 +Cu.import("resource://gre/modules/Services.jsm");
    1.10 +Cu.import("resource://testing-common/httpd.js");
    1.11 +
    1.12 +var server = new HttpServer();
    1.13 +server.registerPathHandler('/image.png', imageHandler);
    1.14 +server.start(-1);
    1.15 +
    1.16 +load('image_load_helpers.js');
    1.17 +
    1.18 +var gHits = 0;
    1.19 +
    1.20 +var gIoService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    1.21 +var gPublicLoader = Cc["@mozilla.org/image/loader;1"].createInstance(Ci.imgILoader);
    1.22 +var gPrivateLoader = Cc["@mozilla.org/image/loader;1"].createInstance(Ci.imgILoader);
    1.23 +gPrivateLoader.QueryInterface(Ci.imgICache).respectPrivacyNotifications();
    1.24 +
    1.25 +function imageHandler(metadata, response) {
    1.26 +  gHits++;
    1.27 +  response.setHeader("Cache-Control", "max-age=10000", false);
    1.28 +  response.setStatusLine(metadata.httpVersion, 200, "OK");
    1.29 +  response.setHeader("Content-Type", "image/png", false);
    1.30 +  var body = "iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAIAAADZSiLoAAAAEUlEQVQImWP4z8AAQTAamQkAhpcI+DeMzFcAAAAASUVORK5CYII=";
    1.31 +  response.bodyOutputStream.write(body, body.length);
    1.32 +}
    1.33 +
    1.34 +var requests = [];
    1.35 +var listeners = [];
    1.36 +
    1.37 +function NotificationCallbacks(isPrivate) {
    1.38 +  this.usePrivateBrowsing = isPrivate;
    1.39 +}
    1.40 +
    1.41 +NotificationCallbacks.prototype = {
    1.42 +  QueryInterface: function (iid) {
    1.43 +    if (iid.equals(Ci.nsISupports) ||
    1.44 +        iid.equals(Ci.nsILoadContext))
    1.45 +      return this;
    1.46 +    throw Cr.NS_ERROR_NO_INTERFACE;
    1.47 +  },
    1.48 +  getInterface: function(iid) {
    1.49 +    if (iid.equals(Ci.nsILoadContext))
    1.50 +      return this;
    1.51 +    throw Cr.NS_ERROR_NO_INTERFACE;
    1.52 +  }
    1.53 +};
    1.54 +
    1.55 +var gImgPath = 'http://localhost:' + server.identity.primaryPort + '/image.png';
    1.56 +
    1.57 +function setup_chan(path, isPrivate, callback) {
    1.58 +  var uri = gIoService.newURI(gImgPath, null, null);
    1.59 +  var chan = gIoService.newChannelFromURI(uri);
    1.60 +  chan.notificationCallbacks = new NotificationCallbacks(isPrivate);
    1.61 +  var channelListener = new ChannelListener();
    1.62 +  chan.asyncOpen(channelListener, null);
    1.63 +
    1.64 +  var listener = new ImageListener(null, callback);
    1.65 +  var outlistener = {};
    1.66 +  var loader = isPrivate ? gPrivateLoader : gPublicLoader;
    1.67 +  var outer = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
    1.68 +                .createScriptedObserver(listener);
    1.69 +  listeners.push(outer);
    1.70 +  requests.push(loader.loadImageWithChannelXPCOM(chan, outer, null, outlistener));
    1.71 +  channelListener.outputListener = outlistener.value;
    1.72 +  listener.synchronous = false;
    1.73 +}
    1.74 +
    1.75 +function loadImage(isPrivate, callback) {
    1.76 +  var listener = new ImageListener(null, callback);
    1.77 +  var outer = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
    1.78 +                .createScriptedObserver(listener);
    1.79 +  var uri = gIoService.newURI(gImgPath, null, null);
    1.80 +  var loadGroup = Cc["@mozilla.org/network/load-group;1"].createInstance(Ci.nsILoadGroup);
    1.81 +  loadGroup.notificationCallbacks = new NotificationCallbacks(isPrivate);
    1.82 +  var loader = isPrivate ? gPrivateLoader : gPublicLoader;
    1.83 +  requests.push(loader.loadImageXPCOM(uri, null, null, null, loadGroup, outer, null, 0, null, null));
    1.84 +  listener.synchronous = false;
    1.85 +}
    1.86 +
    1.87 +function run_loadImage_tests() {
    1.88 +  function observer() {
    1.89 +    Services.obs.removeObserver(observer, "cacheservice:empty-cache");
    1.90 +    gHits = 0;
    1.91 +    loadImage(false, function() {
    1.92 +      loadImage(false, function() {
    1.93 +        loadImage(true, function() {
    1.94 +          loadImage(true, function() {
    1.95 +            do_check_eq(gHits, 2);
    1.96 +            server.stop(do_test_finished);
    1.97 +          });
    1.98 +        });
    1.99 +      });
   1.100 +    });
   1.101 +  }
   1.102 +
   1.103 +  Services.obs.addObserver(observer, "cacheservice:empty-cache", false);
   1.104 +  let cs = Cc["@mozilla.org/netwerk/cache-storage-service;1"]
   1.105 +             .getService(Ci.nsICacheStorageService);
   1.106 +  cs.clear();
   1.107 +}
   1.108 +
   1.109 +function cleanup()
   1.110 +{
   1.111 +  for (var i = 0; i < requests.length; ++i) {
   1.112 +    requests[i].cancelAndForgetObserver(0);
   1.113 +  }
   1.114 +}
   1.115 +
   1.116 +function run_test() {
   1.117 +  do_register_cleanup(cleanup);
   1.118 +
   1.119 +  do_test_pending();
   1.120 +
   1.121 +  // We create a public channel that loads an image, then an identical
   1.122 +  // one that should cause a cache read. We then create a private channel
   1.123 +  // and load the same image, and do that a second time to ensure a cache
   1.124 +  // read. In total, we should cause two separate http responses to occur,
   1.125 +  // since the private channels shouldn't be able to use the public cache.
   1.126 +  setup_chan('/image.png', false, function() {
   1.127 +    setup_chan('/image.png', false, function() {
   1.128 +      setup_chan('/image.png', true, function() {
   1.129 +        setup_chan('/image.png', true, function() {
   1.130 +          do_check_eq(gHits, 2);
   1.131 +          run_loadImage_tests();
   1.132 +        });
   1.133 +      });
   1.134 +    });
   1.135 +  });
   1.136 +}

mercurial