Bug 902156 - Mochitest for Persist 'disable protection' option for Mixed Content Blocker. r=smaug, r=tanvi, a=bajaj
authorChristoph Kerschbaumer <mozilla@christophkerschbaumer.com>
Fri, 16 Aug 2013 22:15:02 -0700
changeset 153857 8dbe776a3ebcacc59a67028347ba62ed7f761317
parent 153856 688e49041611991e439709952c17516ce88a456b
child 153858 acf2b6d71b4eac07eb22cd8850a43e6ea22a9f7e
push id2859
push userakeybl@mozilla.com
push dateMon, 16 Sep 2013 19:14:59 +0000
treeherdermozilla-beta@87d3c51cd2bf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, tanvi, bajaj
bugs902156
milestone25.0a2
Bug 902156 - Mochitest for Persist 'disable protection' option for Mixed Content Blocker. r=smaug, r=tanvi, a=bajaj
browser/base/content/test/Makefile.in
browser/base/content/test/browser_bug902156.js
browser/base/content/test/file_bug902156.js
browser/base/content/test/file_bug902156_1.html
browser/base/content/test/file_bug902156_2.html
browser/base/content/test/file_bug902156_3.html
--- a/browser/base/content/test/Makefile.in
+++ b/browser/base/content/test/Makefile.in
@@ -177,16 +177,17 @@ MOCHITEST_BROWSER_FILES = \
                  browser_bug787619.js \
                  browser_bug797677.js \
                  browser_bug812562.js \
                  browser_bug816527.js \
                  browser_bug817947.js \
                  browser_bug818118.js \
                  browser_bug820497.js \
                  browser_bug822367.js \
+                 browser_bug902156.js \
                  browser_bug832435.js \
                  browser_bug839103.js \
                  browser_bug880101.js \
                  browser_bug882977.js \
                  browser_bug887515.js \
                  browser_canonizeURL.js \
                  browser_clearplugindata_noage.html \
                  browser_clearplugindata.html \
@@ -291,16 +292,20 @@ MOCHITEST_BROWSER_FILES = \
                  file_bug822367_1.js \
                  file_bug822367_2.html \
                  file_bug822367_3.html \
                  file_bug822367_4.html \
                  file_bug822367_4.js \
                  file_bug822367_4B.html \
                  file_bug822367_5.html \
                  file_bug822367_6.html \
+                 file_bug902156_1.html \
+                 file_bug902156_2.html \
+                 file_bug902156_3.html \
+                 file_bug902156.js \
                  file_fullscreen-window-open.html \
                  healthreport_testRemoteCommands.html \
                  moz.png \
                  offlineQuotaNotification.cacheManifest \
                  offlineQuotaNotification.html \
                  page_style_sample.html \
                  plugin_add_dynamically.html \
                  plugin_alternate_content.html \
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/browser_bug902156.js
@@ -0,0 +1,213 @@
+/*
+ * Description of the Tests for
+ *  - Bug 902156: Persist "disable protection" option for Mixed Content Blocker
+ *
+ * 1. Navigate to the same domain via document.location
+ *    - Load a html page which has mixed content
+ *    - Doorhanger to disable protection appears - we disable it
+ *    - Load a new page from the same origin using document.location
+ *    - Doorhanger should not appear anymore!
+ * 
+ * 2. Navigate to the same domain via simulateclick for a link on the page
+ *    - Load a html page which has mixed content
+ *    - Doorhanger to disable protection appears - we disable it
+ *    - Load a new page from the same origin simulating a click
+ *    - Doorhanger should not appear anymore!
+ *
+ * 3. Navigate to a differnet domain and show the content is still blocked
+ *    - Load a different html page which has mixed content
+ *    - Doorhanger to disable protection should appear again because
+ *      we navigated away from html page where we disabled the protection.
+ *
+ * Note, for all tests we set gHttpTestRoot to use 'https'.
+ */
+
+const PREF_ACTIVE = "security.mixed_content.block_active_content";
+
+// We alternate for even and odd test cases to simulate different hosts
+const gHttpTestRoot1 = "https://test1.example.com/browser/browser/base/content/test/";
+const gHttpTestRoot2 = "https://test2.example.com/browser/browser/base/content/test/";
+
+var origBlockActive;
+var gTestBrowser = null;
+
+registerCleanupFunction(function() {
+  // Set preferences back to their original values
+  Services.prefs.setBoolPref(PREF_ACTIVE, origBlockActive);
+});
+
+function cleanUpAfterTests() {
+  gBrowser.removeCurrentTab();
+  window.focus();
+  finish();
+}
+/*
+ * Whenever we disable the Mixed Content Blocker of the page
+ * we have to make sure that our condition is properly loaded.
+ */
+function waitForCondition(condition, nextTest, errorMsg) {
+  var tries = 0;
+  var interval = setInterval(function() {
+    if (tries >= 30) {
+      ok(false, errorMsg);
+      moveOn();
+    }
+    if (condition()) {
+      moveOn();
+    }
+    tries++;
+  }, 100);
+  var moveOn = function() {
+    clearInterval(interval); nextTest();
+  };
+}
+
+//------------------------ Test 1 ------------------------------
+
+function test1A() {
+  // Removing EventListener because we have to register a new
+  // one once the page is loaded with mixed content blocker disabled
+  gTestBrowser.removeEventListener("load", test1A, true);
+  gTestBrowser.addEventListener("load", test1B, true);
+  
+  var notification = PopupNotifications.getNotification("mixed-content-blocked", gTestBrowser);
+  ok(notification, "OK: Mixed Content Doorhanger appeared in Test1A!");
+
+  // Disable Mixed Content Protection for the page 
+  notification.secondaryActions[0].callback();
+}
+
+function test1B() {
+  var expected = "Mixed Content Blocker disabled";
+  waitForCondition(
+    function() content.document.getElementById('mctestdiv').innerHTML == expected,
+    test1C, "Error: Waited too long for mixed script to run in Test 1B");
+}
+
+function test1C() {
+  gTestBrowser.removeEventListener("load", test1B, true);
+  var actual = content.document.getElementById('mctestdiv').innerHTML;
+  is(actual, "Mixed Content Blocker disabled", "OK: Executed mixed script in Test 1C");
+
+  // The Script loaded after we disabled the page, now we are going to reload the
+  // page and see if our decision is persistent
+  gTestBrowser.addEventListener("load", test1D, true);
+
+  var url = gHttpTestRoot1 + "file_bug902156_2.html";
+  gTestBrowser.contentWindow.location = url;
+}
+
+function test1D() {
+  gTestBrowser.removeEventListener("load", test1D, true);
+
+  // The Doorhanger should not appear, because our decision of disabling the
+  // mixed content blocker is persistent.
+  var notification = PopupNotifications.getNotification("mixed-content-blocked", gTestBrowser);
+  ok(!notification, "OK: Mixed Content Doorhanger did not appear again in Test1D!");
+
+  var actual = content.document.getElementById('mctestdiv').innerHTML;
+  is(actual, "Mixed Content Blocker disabled", "OK: Executed mixed script in Test 1D");
+
+  // move on to Test 2
+  test2();
+}
+
+//------------------------ Test 2 ------------------------------
+
+function test2() {
+  gTestBrowser.addEventListener("load", test2A, true);
+  var url = gHttpTestRoot2 + "file_bug902156_2.html";
+  gTestBrowser.contentWindow.location = url;
+}
+
+function test2A() {
+  // Removing EventListener because we have to register a new
+  // one once the page is loaded with mixed content blocker disabled
+  gTestBrowser.removeEventListener("load", test2A, true);
+  gTestBrowser.addEventListener("load", test2B, true);
+  
+  var notification = PopupNotifications.getNotification("mixed-content-blocked", gTestBrowser);
+  ok(notification, "OK: Mixed Content Doorhanger appeared in Test 2A!");
+
+  // Disable Mixed Content Protection for the page 
+  notification.secondaryActions[0].callback();
+}
+
+function test2B() {
+  var expected = "Mixed Content Blocker disabled";
+  waitForCondition(
+    function() content.document.getElementById('mctestdiv').innerHTML == expected,
+    test2C, "Error: Waited too long for mixed script to run in Test 2B");
+}
+
+function test2C() {
+  gTestBrowser.removeEventListener("load", test2B, true);
+  var actual = content.document.getElementById('mctestdiv').innerHTML;
+  is(actual, "Mixed Content Blocker disabled", "OK: Executed mixed script in Test 2C");
+
+  // The Script loaded after we disabled the page, now we are going to reload the
+  // page and see if our decision is persistent
+  gTestBrowser.addEventListener("load", test2D, true);
+
+  // reload the page using the provided link in the html file
+  var mctestlink = content.document.getElementById("mctestlink");
+  mctestlink.click();
+}
+
+function test2D() {
+  gTestBrowser.removeEventListener("load", test2D, true);
+
+  // The Doorhanger should not appear, because our decision of disabling the
+  // mixed content blocker is persistent.
+  var notification = PopupNotifications.getNotification("mixed-content-blocked", gTestBrowser);
+  ok(!notification, "OK: Mixed Content Doorhanger did not appear again in Test2D!");
+
+  var actual = content.document.getElementById('mctestdiv').innerHTML;
+  is(actual, "Mixed Content Blocker disabled", "OK: Executed mixed script in Test 2D");
+
+  // move on to Test 3
+  test3();
+}
+
+//------------------------ Test 3 ------------------------------
+
+function test3() {
+  gTestBrowser.addEventListener("load", test3A, true);
+  var url = gHttpTestRoot1 + "file_bug902156_3.html";
+  gTestBrowser.contentWindow.location = url;
+}
+
+function test3A() {
+  // Removing EventListener because we have to register a new
+  // one once the page is loaded with mixed content blocker disabled
+  gTestBrowser.removeEventListener("load", test3A, true);
+  
+  var notification = PopupNotifications.getNotification("mixed-content-blocked", gTestBrowser);
+  ok(notification, "OK: Mixed Content Doorhanger appeared in Test 3A!");
+
+  // We are done with tests, clean up
+  cleanUpAfterTests();
+}
+
+//------------------------------------------------------
+
+function test() {
+  // Performing async calls, e.g. 'onload', we have to wait till all of them finished
+  waitForExplicitFinish();
+
+  // Store original preferences so we can restore settings after testing
+  origBlockActive = Services.prefs.getBoolPref(PREF_ACTIVE);
+
+  Services.prefs.setBoolPref(PREF_ACTIVE, true);
+
+  // Not really sure what this is doing
+  var newTab = gBrowser.addTab();
+  gBrowser.selectedTab = newTab;
+  gTestBrowser = gBrowser.selectedBrowser;
+  newTab.linkedBrowser.stop()
+
+  // Starting Test Number 1:
+  gTestBrowser.addEventListener("load", test1A, true);
+  var url = gHttpTestRoot1 + "file_bug902156_1.html";
+  gTestBrowser.contentWindow.location = url;
+}
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/file_bug902156.js
@@ -0,0 +1,5 @@
+/* 
+ * Once the mixed content blocker is disabled for the page, this scripts loads
+ * and updates the text inside the div container.
+ */
+document.getElementById("mctestdiv").innerHTML = "Mixed Content Blocker disabled";
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/file_bug902156_1.html
@@ -0,0 +1,15 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+  Test 1 for Bug 902156 - See file browser_bug902156.js for description.
+  https://bugzilla.mozilla.org/show_bug.cgi?id=902156
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Test 1 for Bug 902156</title>
+</head>
+<body>
+  <div id="mctestdiv">Mixed Content Blocker enabled</div>
+  <script src="http://test1.example.com/browser/browser/base/content/test/file_bug902156.js" ></script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/file_bug902156_2.html
@@ -0,0 +1,17 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+  Test 2 for Bug 902156 - See file browser_bug902156.js for description.
+  https://bugzilla.mozilla.org/show_bug.cgi?id=902156
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Test 2 for Bug 902156</title>
+</head>
+<body>
+  <div id="mctestdiv">Mixed Content Blocker enabled</div>
+  <a href="https://test2.example.com/browser/browser/base/content/test/file_bug902156_1.html"
+     id="mctestlink" target="_top">Go to http site</a>
+  <script src="http://test2.example.com/browser/browser/base/content/test/file_bug902156.js" ></script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/file_bug902156_3.html
@@ -0,0 +1,15 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+  Test 3 for Bug 902156 - See file browser_bug902156.js for description.
+  https://bugzilla.mozilla.org/show_bug.cgi?id=902156
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Test 3 for Bug 902156</title>
+</head>
+<body>
+  <div id="mctestdiv">Mixed Content Blocker enabled</div>
+  <script src="http://test1.example.com/browser/browser/base/content/test/file_bug902156.js" ></script>
+</body>
+</html>