Commit a97f0952 authored by Arthur Edelstein's avatar Arthur Edelstein Committed by Georg Koppen
Browse files

Bug 18995: Regression test to ensure CacheStorage is disabled in private browsing

parent e85ecc5a
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
[DEFAULT]
support-files =
  bug18995.html
  worker_bug_18995.js
  worker_bug_18995.html

[browser_tor_bug18995.js]
[browser_tor_bug2950.js]
[browser_tor_omnibox.js]
[browser_tor_TB4.js]
+50 −0
Original line number Diff line number Diff line
// __browser_tor_bug18995.js__.
// In this test, we open a private browsing window, and load pages
// that test whether we can call `caches.open("test")`

// Helper function.
// Returns a promise that is fulfilled when the first event of eventype
// arrives from the target.
let listen = function (target, eventType, useCapture) {
  return new Promise(function (resolve, reject) {
    let listenFunction = function (event) {
      target.removeEventListener(eventType, listenFunction, useCapture);
      resolve(event);
    };
    target.addEventListener(eventType, listenFunction, useCapture);
  });
};

// The main test
add_task(function* () {
  // First open the private browsing window
  let privateWin = yield BrowserTestUtils.openNewBrowserWindow({private: true});
  let privateBrowser = privateWin.gBrowser.selectedBrowser;

  // We have two pages: (1) access CacheStorage in content page
  //                    (2) access CacheStorage in worker
  let testURIs = ["http://mochi.test:8888/browser/tbb-tests/bug18995.html",
                  "http://mochi.test:8888/browser/tbb-tests/worker_bug_18995.html"];
  for (let testURI of testURIs) {
    // Load the test page
    privateBrowser.loadURI(testURI);
    // Wait for it too fully load
    yield BrowserTestUtils.browserLoaded(privateBrowser);
    // Get the <div id="result"/> in the content page
    let resultDiv = privateBrowser.contentDocument.getElementById("result");
    // Send an event to the content page indicating we are ready to receive.
    resultDiv.dispatchEvent(new Event("ready"));
    // Wait for a signal from the content page that a result is ready.
    yield listen(resultDiv, "result", false);
    // Read the result from the result <div>
    let resultValue = resultDiv.innerHTML;
    // Print out the result
    info("received: " + resultValue);
    // If we are in PBM, then the promise returned by caches.open(...)
    // is supposed to arrive at a rejection with a SecurityError.
    ok(resultValue.contains("SecurityError"),
       "CacheStorage should fail in private browsing mode");
  }
  // Close the browser window because we are done testing.
  yield BrowserTestUtils.closeWindow(privateWin);
});
+31 −0
Original line number Diff line number Diff line
<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8" />
  <title>Bug 18995 test</title>
 </head>
 <body>
  <div id="result"></div>
  <script type="application/javascript">
    let resultDiv = document.getElementById("result");
    // Wait for a signal from chrome to start.
    resultDiv.addEventListener("ready", function () {
      let resultEvent = new Event("result");
      // Test caches.open(...)
      caches.open("test1").then(function (value) {
        // We are not supposed to succeed, but if we do,
        // post the result to resultDiv.
        resultDiv.innerHTML = value.toString();
        // Notify chrome that the result is available.
        resultDiv.dispatchEvent(resultEvent);
      }, function (reason) {
        // We should arrive here to fail. Post the result
        // to resultDiv.
        resultDiv.innerHTML = reason.toString();
        // Notify chrome that the result is available.
        resultDiv.dispatchEvent(resultEvent);
      });
    });
  </script>
 </body>
</html>
+26 −0
Original line number Diff line number Diff line
<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8" />
  <title>Bug 18995 test</title>
 </head>
 <body>
  <div id="result"></div>
  <script type="application/javascript">
    let resultDiv = document.getElementById("result");
    // Wait for a signal from chrome to start.
    resultDiv.addEventListener("ready", function() {
      // Run the test worker.
      let worker = new Worker("worker_bug_18995.js");
      // Wait for a message from the worker, which should contain
      // the result or the error from the caches.open(...) call.
      worker.addEventListener("message", function (msg) {
        // Put the result in our resultDiv.
        resultDiv.innerHTML = msg.data;
        // Notify chrome that the result is ready.
        resultDiv.dispatchEvent(new Event("result"));
      });
    });
  </script>
 </body>
</html>
+8 −0
Original line number Diff line number Diff line
// Attempt to open a cache
caches.open("test2").then(function (value) {
  // This is not supposed to happen.
  self.postMessage(value.toString());
}, function (reason) {
  // We are supposed to fail.
  self.postMessage(reason.toString());
});