Bug 1020244 - Tests for the AnonymousContent document insertion API; r=smaug
authorPatrick Brosset <pbrosset@mozilla.com>
Tue, 28 Oct 2014 11:15:28 +0100
changeset 212700 cf419403a2eb60b5444908c2633e35318e21ddab
parent 212699 8a8090c2051edf8d3578a68fc3e5582774b67efe
child 212701 f26373e62bcb66229526c07d5d2cc644c7abe4db
push id51042
push userryanvm@gmail.com
push dateTue, 28 Oct 2014 20:25:03 +0000
treeherdermozilla-inbound@53d84829b2b8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1020244
milestone36.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1020244 - Tests for the AnonymousContent document insertion API; r=smaug
dom/base/test/chrome.ini
dom/base/test/mochitest.ini
dom/base/test/test_anonymousContent_api.html
dom/base/test/test_anonymousContent_append_after_reflow.html
dom/base/test/test_anonymousContent_insert.html
dom/base/test/test_anonymousContent_manipulate_content.html
dom/base/test/test_anonymousContent_xul_window.xul
--- a/dom/base/test/chrome.ini
+++ b/dom/base/test/chrome.ini
@@ -1,13 +1,14 @@
 [DEFAULT]
 support-files =
   file_url.jsm
   file_empty.html
 
+[test_anonymousContent_xul_window.xul]
 [test_bug715041.xul]
 [test_bug715041_removal.xul]
 [test_domrequesthelper.xul]
 [test_url.xul]
 [test_console.xul]
 [test_messageChannel.xul]
 [test_navigator_resolve_identity_xrays.xul]
 [test_sendQueryContentAndSelectionSetEvent.html]
--- a/dom/base/test/mochitest.ini
+++ b/dom/base/test/mochitest.ini
@@ -234,16 +234,20 @@ support-files =
   send_gzip_content.sjs
   somedatas.resource
   somedatas.resource^headers^
   variable_style_sheet.sjs
   viewport_helpers.js
   w3element_traversal.svg
   wholeTexty-helper.xml
 
+[test_anonymousContent_api.html]
+[test_anonymousContent_append_after_reflow.html]
+[test_anonymousContent_insert.html]
+[test_anonymousContent_manipulate_content.html]
 [test_appname_override.html]
 [test_audioWindowUtils.html]
 [test_audioNotification.html]
 skip-if = buildapp == 'mulet'
 [test_bug793311.html]
 [test_bug913761.html]
 [test_bug976673.html]
 [test_bug978522.html]
new file mode 100644
--- /dev/null
+++ b/dom/base/test/test_anonymousContent_api.html
@@ -0,0 +1,55 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1020244
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Test for Bug 1020244 - Test the chrome-only AnonymousContent API</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1020244">Mozilla Bug 1020244</a>
+  <script type="application/javascript;version=1.8">
+
+  // Testing the presence of the chrome-only API
+  ok(!document.insertAnonymousContent,
+    "Content document shouldn't have access to insertAnonymousContent");
+  ok(!document.removeAnonymousContent,
+    "Content document shouldn't have access to removeAnonymousContent");
+
+  let chromeDocument = SpecialPowers.wrap(document);
+  ok(chromeDocument.insertAnonymousContent,
+    "Chrome document should have access to insertAnonymousContent");
+  ok(chromeDocument.removeAnonymousContent,
+    "Chrome document should have access to removeAnonymousContent");
+
+  // Testing invalid inputs
+  let invalidNodes = [null, undefined, false, 1, "string"];
+  for (let node of invalidNodes) {
+    let didThrow = false;
+    try {
+      chromeDocument.insertAnonymousContent(node);
+    } catch (e) {
+      didThrow = true;
+    }
+    ok(didThrow, "Passing an invalid node to insertAnonymousContent should throw");
+  }
+
+  // Testing the API of the returned object
+  let div = document.createElement("div");
+  div.textContent = "this is a test element";
+  let anonymousContent = chromeDocument.insertAnonymousContent(div);
+  ok(anonymousContent, "AnonymousContent object returned");
+
+  let members = ["getTextContentForElement", "setTextContentForElement",
+                 "getAttributeForElement", "setAttributeForElement",
+                 "removeAttributeForElement"];
+  for (let member of members) {
+    ok(member in anonymousContent, "AnonymousContent object defines " + member);
+  }
+  chromeDocument.removeAnonymousContent(anonymousContent);
+  </script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/base/test/test_anonymousContent_append_after_reflow.html
@@ -0,0 +1,40 @@
+<!DOCTYPE HTML>
+<html>
+<!-- https://bugzilla.mozilla.org/show_bug.cgi?id=1020244 -->
+<head>
+  <meta charset="utf-8">
+  <title>Test for Bug 1020244 - Make sure anonymous content still works after a reflow (after the canvasframe has been reconstructed)</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<div>
+  <div id="test-element" style="color:red;">text content</div>
+</div>
+<script type="application/javascript;version=1.8">
+  info("Inserting anonymous content into the document frame");
+  let chromeDocument = SpecialPowers.wrap(document);
+  let testElement = document.querySelector("div");
+  let anonymousContent = chromeDocument.insertAnonymousContent(testElement);
+
+  info("Modifying the style of an element in the anonymous content");
+  let style = anonymousContent.setAttributeForElement("test-element",
+                                                      "style", "color:green;");
+
+  info("Toggling the display style on the document element to force reframing");
+  // Note that we force sync reflows to make sure the canvasframe is recreated
+  // synchronously.
+  document.documentElement.style.display = "none";
+  let forceFlush = document.documentElement.offsetHeight;
+  document.documentElement.style.display = "block";
+  forceFlush = document.documentElement.offsetHeight;
+
+  info("Checking that the anonymous content can be retrieved still");
+  let style = anonymousContent.getAttributeForElement("test-element", "style");
+  is(style, "color:green;", "The anonymous content still exists after reflow");
+
+  info("Removing the anonymous content");
+  chromeDocument.removeAnonymousContent(anonymousContent);
+</script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/base/test/test_anonymousContent_insert.html
@@ -0,0 +1,45 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1020244
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Test for Bug 1020244 - Insert content using the AnonymousContent API, several times, and don't remove it</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1020244">Mozilla Bug 1020244</a>
+<div>
+  <div id="id" class="test">text content</div>
+</div>
+  <script type="application/javascript;version=1.8">
+  const INSERTED_NB = 5;
+
+  // Insert the same content several times
+  let chromeDocument = SpecialPowers.wrap(document);
+  let testElement = document.querySelector("div");
+
+  let anonymousContents = [];
+  for (let i = 0; i < INSERTED_NB; i ++) {
+    let content = chromeDocument.insertAnonymousContent(testElement);
+    // Adding an expando pointing to the document to make sure this doesn't
+    // cause leaks.
+    content.dummyExpando = testElement.ownerDocument;
+    anonymousContents.push(content);
+  }
+
+  // Make sure that modifying one of the inserted elements does not modify the
+  // other ones.
+  anonymousContents[0].setAttributeForElement("id", "class", "updated");
+  for (let i = 1; i < INSERTED_NB; i ++) {
+    is(anonymousContents[i].getAttributeForElement("id", "class"),
+       "test",
+       "Element " + i + " didn't change when element 0 was changed");
+  }
+
+  // Do not remove inserted elements on purpose to test for potential leaks too.
+  </script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/base/test/test_anonymousContent_manipulate_content.html
@@ -0,0 +1,66 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1020244
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Test for Bug 1020244 - Manipulate content created with the AnonymousContent API</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1020244">Mozilla Bug 1020244</a>
+<div>
+  <div id="test-element" class="test-class" test="test">text content</div>
+</div>
+  <script type="application/javascript;version=1.8">
+
+  // Insert content
+  let chromeDocument = SpecialPowers.wrap(document);
+  let testElement = document.querySelector("div");
+  let anonymousContent = chromeDocument.insertAnonymousContent(testElement);
+
+  // Test getting/setting text content.
+  is(anonymousContent.getTextContentForElement("test-element"),
+    "text content", "Textcontent for the test element is correct");
+
+  anonymousContent.setTextContentForElement("test-element",
+    "updated text content");
+  is(anonymousContent.getTextContentForElement("test-element"),
+    "updated text content",
+    "Textcontent for the test element is correct after update");
+
+  // Test that modifying the original DOM element doesn't change the inserted
+  // element.
+  testElement.removeAttribute("test");
+  is(anonymousContent.getAttributeForElement("test-element", "test"),
+    "test",
+    "Removing attributes on the original DOM node does not change the inserted node");
+
+  testElement.setAttribute("test", "test-updated");
+  is(anonymousContent.getAttributeForElement("test-element", "test"),
+    "test",
+    "Setting attributes on the original DOM node does not change the inserted node");
+
+  // Test getting/setting/removing attributes on the inserted element and test
+  // that this doesn't change the original DOM element.
+  is(anonymousContent.getAttributeForElement("test-element", "class"),
+    "test-class", "Class attribute for the test element is correct");
+
+  anonymousContent.setAttributeForElement("test-element", "class",
+    "updated-test-class");
+  is(anonymousContent.getAttributeForElement("test-element", "class"),
+    "updated-test-class",
+    "Class attribute for the test element is correct after update");
+  ok(testElement.getAttribute("class") !== "updated-test-class",
+    "Class attribute change on the inserted node does not change the original DOM node");
+
+  anonymousContent.removeAttributeForElement("test-element", "class");
+  is(anonymousContent.getAttributeForElement("test-element", "class"), null,
+    "Class attribute for the test element was removed");
+
+  chromeDocument.removeAnonymousContent(anonymousContent);
+  </script>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/base/test/test_anonymousContent_xul_window.xul
@@ -0,0 +1,30 @@
+<?xml version="1.0"?>
+<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
+<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1020244
+Check that XUL windows aren't supported, that the API throws, but doesn't crash.
+-->
+<window title="Anonymous content in a XUL window"
+xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
+
+  <!-- test results are displayed in the html:body -->
+  <body xmlns="http://www.w3.org/1999/xhtml"></body>
+  <box>This is a test box</box>
+
+  <script type="application/javascript;version=1.8">
+    // Insert content
+    let testElement = document.querySelector("box");
+
+    let didThrow = false;
+    try {
+      document.insertAnonymousContent(testElement);
+    } catch (e) {
+      didThrow = true;
+    }
+
+    ok(didThrow,
+      "Inserting anonymous content in a XUL document did throw an exception")
+  </script>
+</window>