browser/base/content/test/general/browser_save_private_link_perwindowpb.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/base/content/test/general/browser_save_private_link_perwindowpb.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,139 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +let {LoadContextInfo} = Cu.import("resource://gre/modules/LoadContextInfo.jsm", null);
     1.9 +
    1.10 +function test() {
    1.11 +  // initialization
    1.12 +  waitForExplicitFinish();
    1.13 +  let windowsToClose = [];
    1.14 +  let testURI = "http://mochi.test:8888/browser/browser/base/content/test/general/bug792517.html";
    1.15 +  let fileName;
    1.16 +  let MockFilePicker = SpecialPowers.MockFilePicker;
    1.17 +  let cache = Cc["@mozilla.org/netwerk/cache-storage-service;1"]
    1.18 +              .getService(Ci.nsICacheStorageService);
    1.19 +
    1.20 +  function checkDiskCacheFor(filename, goon) {
    1.21 +    Visitor.prototype = {
    1.22 +      onCacheStorageInfo: function(num, consumption)
    1.23 +      {
    1.24 +        info("disk storage contains " + num + " entries");
    1.25 +      },
    1.26 +      onCacheEntryInfo: function(entry)
    1.27 +      {
    1.28 +        info(entry.key);
    1.29 +        is(entry.key.contains(filename), false, "web content present in disk cache");
    1.30 +      },
    1.31 +      onCacheEntryVisitCompleted: function()
    1.32 +      {
    1.33 +        goon();
    1.34 +      }
    1.35 +    };
    1.36 +    function Visitor() {}
    1.37 +
    1.38 +    var storage = cache.diskCacheStorage(LoadContextInfo.default, false);
    1.39 +    storage.asyncVisitStorage(new Visitor(), true /* Do walk entries */);
    1.40 +  }
    1.41 +
    1.42 +  function onTransferComplete(downloadSuccess) {
    1.43 +    ok(downloadSuccess, "Image file should have been downloaded successfully");
    1.44 +
    1.45 +    // Give the request a chance to finish and create a cache entry
    1.46 +    executeSoon(function() {
    1.47 +      checkDiskCacheFor(fileName, finish);
    1.48 +      mockTransferCallback = null;
    1.49 +    });
    1.50 +  }
    1.51 +
    1.52 +  function createTemporarySaveDirectory() {
    1.53 +    var saveDir = Cc["@mozilla.org/file/directory_service;1"]
    1.54 +                    .getService(Ci.nsIProperties)
    1.55 +                    .get("TmpD", Ci.nsIFile);
    1.56 +    saveDir.append("testsavedir");
    1.57 +    if (!saveDir.exists())
    1.58 +      saveDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
    1.59 +    return saveDir;
    1.60 +  }
    1.61 +
    1.62 +  function doTest(aIsPrivateMode, aWindow, aCallback) {
    1.63 +    function contextMenuOpened(event) {
    1.64 +      cache.clear();
    1.65 +
    1.66 +      aWindow.document.removeEventListener("popupshown", contextMenuOpened);
    1.67 +
    1.68 +      // Create the folder the image will be saved into.
    1.69 +      var destDir = createTemporarySaveDirectory();
    1.70 +      var destFile = destDir.clone();
    1.71 +
    1.72 +      MockFilePicker.displayDirectory = destDir;
    1.73 +      MockFilePicker.showCallback = function(fp) {
    1.74 +        fileName = fp.defaultString;
    1.75 +        destFile.append (fileName);
    1.76 +        MockFilePicker.returnFiles = [destFile];
    1.77 +        MockFilePicker.filterIndex = 1; // kSaveAsType_URL
    1.78 +      };
    1.79 +
    1.80 +      mockTransferCallback = onTransferComplete;
    1.81 +      mockTransferRegisterer.register();
    1.82 +
    1.83 +      registerCleanupFunction(function () {
    1.84 +        mockTransferRegisterer.unregister();
    1.85 +        MockFilePicker.cleanup();
    1.86 +        destDir.remove(true);
    1.87 +      });
    1.88 +
    1.89 +      // Select "Save Image As" option from context menu
    1.90 +      var saveVideoCommand = aWindow.document.getElementById("context-saveimage");
    1.91 +      saveVideoCommand.doCommand();
    1.92 +
    1.93 +      event.target.hidePopup();
    1.94 +    }
    1.95 +
    1.96 +    aWindow.gBrowser.addEventListener("pageshow", function pageShown(event) {
    1.97 +      // If data: -url PAC file isn't loaded soon enough, we may get about:privatebrowsing loaded
    1.98 +      if (event.target.location == "about:blank" ||
    1.99 +          event.target.location == "about:privatebrowsing") {
   1.100 +        aWindow.gBrowser.selectedBrowser.loadURI(testURI);
   1.101 +        return;
   1.102 +      }
   1.103 +      aWindow.gBrowser.removeEventListener("pageshow", pageShown);
   1.104 +
   1.105 +      waitForFocus(function () {
   1.106 +        aWindow.document.addEventListener("popupshown", contextMenuOpened, false);
   1.107 +        var img = aWindow.gBrowser.selectedBrowser.contentDocument.getElementById("img");
   1.108 +        EventUtils.synthesizeMouseAtCenter(img,
   1.109 +                                           { type: "contextmenu", button: 2 },
   1.110 +                                           aWindow.gBrowser.contentWindow);
   1.111 +      }, aWindow.gBrowser.selectedBrowser.contentWindow);
   1.112 +    });
   1.113 +  }
   1.114 +
   1.115 +  function testOnWindow(aOptions, aCallback) {
   1.116 +    whenNewWindowLoaded(aOptions, function(aWin) {
   1.117 +      windowsToClose.push(aWin);
   1.118 +      // execute should only be called when need, like when you are opening
   1.119 +      // web pages on the test. If calling executeSoon() is not necesary, then
   1.120 +      // call whenNewWindowLoaded() instead of testOnWindow() on your test.
   1.121 +      executeSoon(function() aCallback(aWin));
   1.122 +    });
   1.123 +  };
   1.124 +
   1.125 +   // this function is called after calling finish() on the test.
   1.126 +  registerCleanupFunction(function() {
   1.127 +    windowsToClose.forEach(function(aWin) {
   1.128 +      aWin.close();
   1.129 +    });
   1.130 +  });
   1.131 +
   1.132 +  MockFilePicker.init(window);
   1.133 +  // then test when on private mode
   1.134 +  testOnWindow({private: true}, function(aWin) {
   1.135 +    doTest(true, aWin, finish);
   1.136 +  });
   1.137 +}
   1.138 +
   1.139 +Cc["@mozilla.org/moz/jssubscript-loader;1"]
   1.140 +  .getService(Ci.mozIJSSubScriptLoader)
   1.141 +  .loadSubScript("chrome://mochitests/content/browser/toolkit/content/tests/browser/common/mockTransfer.js",
   1.142 +                 this);

mercurial