Bug 1146568 - Avoid unsafe CPOW usage in markupview tests, in assertAttributes helper; r=bgrins
authorPatrick Brosset <pbrosset@mozilla.com>
Wed, 25 Mar 2015 14:11:36 +0100
changeset 264555 2d5f393a6f8d6c1a13abc363c821d206e455dcdb
parent 264554 c2ada63128e0ef31b14518de2051daa8c0c9da0d
child 264556 82e0b5eda9218d9d304afb3a15cf6d552c906cf5
push id4718
push userraliiev@mozilla.com
push dateMon, 11 May 2015 18:39:53 +0000
treeherdermozilla-beta@c20c4ef55f08 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbgrins
bugs1146568
milestone39.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 1146568 - Avoid unsafe CPOW usage in markupview tests, in assertAttributes helper; r=bgrins The common assertAttributes helper test function used to access DOM nodes in the content page directly (CPOW). It now becomes an async function that goes through a frame-script to retrieve the list of attributes for a node, given its selector. All tests were passing with this change but one: browser_markupview_tag_edit_06.js This one was checking camelcase attribute names while the actual names on the node were lowercased by the browser. The way we were asserting attributes before didn't care about the case. Now it does.
browser/devtools/markupview/test/browser_markupview_tag_edit_02.js
browser/devtools/markupview/test/browser_markupview_tag_edit_06.js
browser/devtools/markupview/test/browser_markupview_tag_edit_08.js
browser/devtools/markupview/test/browser_markupview_tag_edit_09.js
browser/devtools/markupview/test/head.js
browser/devtools/markupview/test/helper_attributes_test_runner.js
--- a/browser/devtools/markupview/test/browser_markupview_tag_edit_02.js
+++ b/browser/devtools/markupview/test/browser_markupview_tag_edit_02.js
@@ -11,35 +11,35 @@ const TEST_URL = "data:text/html,<div id
 add_task(function*() {
   info("Opening the inspector on the test page");
   let {toolbox, inspector} = yield addTab(TEST_URL).then(openInspector);
 
   info("Selecting the test node");
   yield selectNode("#test-div", inspector);
 
   info("Verify attributes, only ID should be there for now");
-  assertAttributes("#test-div", {
+  yield assertAttributes("#test-div", {
     id: "test-div"
   });
 
   info("Focus the ID attribute and change its content");
   let {editor} = yield getContainerForSelector("#test-div", inspector);
   let attr = editor.attrs["id"].querySelector(".editable");
   let mutated = inspector.once("markupmutation");
   setEditableFieldValue(attr,
     attr.textContent + ' class="newclass" style="color:green"', inspector);
   yield mutated;
 
   info("Verify attributes, should have ID, class and style");
-  assertAttributes("#test-div", {
+  yield assertAttributes("#test-div", {
     id: "test-div",
     class: "newclass",
     style: "color:green"
   });
 
   info("Trying to undo the change");
   yield undoChange(inspector);
-  assertAttributes("#test-div", {
+  yield assertAttributes("#test-div", {
     id: "test-div"
   });
 
   yield inspector.once("inspector-updated");
 });
--- a/browser/devtools/markupview/test/browser_markupview_tag_edit_06.js
+++ b/browser/devtools/markupview/test/browser_markupview_tag_edit_06.js
@@ -61,18 +61,18 @@ let TEST_DATA = [{
   text: "style=\"font-family: 'Lucida Grande', sans-serif; font-size: 75%;\"",
   expectedAttributes: {
     style: "font-family: 'Lucida Grande', sans-serif; font-size: 75%;"
   }
 }, {
   desc: "Object attribute names",
   text: "toString=\"true\" hasOwnProperty=\"false\"",
   expectedAttributes: {
-    toString: "true",
-    hasOwnProperty: "false"
+    tostring: "true",
+    hasownproperty: "false"
   }
 }, {
   desc: "Add event handlers",
   text: "onclick=\"javascript: throw new Error('wont fire');\" onload=\"alert('here');\"",
   expectedAttributes: {
     onclick: "javascript: throw new Error('wont fire');",
     onload: "alert('here');"
   }
--- a/browser/devtools/markupview/test/browser_markupview_tag_edit_08.js
+++ b/browser/devtools/markupview/test/browser_markupview_tag_edit_08.js
@@ -24,17 +24,17 @@ function* testCollapsedLongAttribute(ins
   info("Try to modify the collapsed long attribute, making sure it expands.");
 
   info("Adding test attributes to the node");
   let onMutated = inspector.once("markupmutation");
   yield setNodeAttribute("#node24", "class", "");
   yield setNodeAttribute("#node24", "data-long", LONG_ATTRIBUTE);
   yield onMutated;
 
-  assertAttributes("#node24", {
+  yield assertAttributes("#node24", {
     id: "node24",
     "class": "",
     "data-long": LONG_ATTRIBUTE
   });
 
   let {editor} = yield getContainerForSelector("#node24", inspector);
   let attr = editor.attrs["data-long"].querySelector(".editable");
 
@@ -46,28 +46,28 @@ function* testCollapsedLongAttribute(ins
   EventUtils.sendKey("escape", inspector.panelWin);
 
   setEditableFieldValue(attr, input.value + ' data-short="ABC"', inspector);
   yield inspector.once("markupmutation");
 
   let visibleAttrText = editor.attrs["data-long"].querySelector(".attr-value").textContent;
   is (visibleAttrText, LONG_ATTRIBUTE_COLLAPSED)
 
-  assertAttributes("#node24", {
+  yield assertAttributes("#node24", {
     id: "node24",
     class: "",
     'data-long': LONG_ATTRIBUTE,
     "data-short": "ABC"
   });
 }
 
 function* testModifyInlineStyleWithQuotes(inspector) {
   info("Modify inline style containing \"");
 
-  assertAttributes("#node26", {
+  yield assertAttributes("#node26", {
     id: "node26",
     style: 'background-image: url("moz-page-thumb://thumbnail?url=http%3A%2F%2Fwww.mozilla.org%2F");'
   });
 
   let onMutated = inspector.once("markupmutation");
   let {editor} = yield getContainerForSelector("#node26", inspector);
   let attr = editor.attrs["style"].querySelector(".editable");
 
@@ -84,26 +84,26 @@ function* testModifyInlineStyleWithQuote
 
   value = value.replace(/mozilla\.org/, "mozilla.com");
   input.value = value;
 
   EventUtils.sendKey("return", inspector.panelWin);
 
   yield onMutated;
 
-  assertAttributes("#node26", {
+  yield assertAttributes("#node26", {
     id: "node26",
     style: 'background-image: url("moz-page-thumb://thumbnail?url=http%3A%2F%2Fwww.mozilla.com%2F");'
   });
 }
 
 function* testEditingAttributeWithMixedQuotes(inspector) {
   info("Modify class containing \" and \'");
 
-  assertAttributes("#node27", {
+  yield assertAttributes("#node27", {
     "id": "node27",
     "class": 'Double " and single \''
   });
 
   let onMutated = inspector.once("markupmutation");
   let {editor} = yield getContainerForSelector("#node27", inspector);
   let attr = editor.attrs["class"].querySelector(".editable");
 
@@ -117,13 +117,13 @@ function* testEditingAttributeWithMixedQ
 
   value = value.replace(/Double/, "&quot;").replace(/single/, "'");
   input.value = value;
 
   EventUtils.sendKey("return", inspector.panelWin);
 
   yield onMutated;
 
-  assertAttributes("#node27", {
+  yield assertAttributes("#node27", {
     id: "node27",
     class: '" " and \' \''
   });
 }
--- a/browser/devtools/markupview/test/browser_markupview_tag_edit_09.js
+++ b/browser/devtools/markupview/test/browser_markupview_tag_edit_09.js
@@ -32,17 +32,17 @@ function* testWellformedMixedCase(inspec
   EventUtils.sendKey("return", inspector.panelWin);
 
   info("Editing the attribute value and waiting for the mutation event");
   let input = inplaceEditor(attr).input;
   input.value = "viewBox=\"0 0 1 1\"";
   EventUtils.sendKey("return", inspector.panelWin);
   yield onMutated;
 
-  assertAttributes("svg", {
+  yield assertAttributes("svg", {
     "viewBox": "0 0 1 1",
     "width": "200",
     "height": "200"
   });
 }
 
 function* testMalformedMixedCase(inspector) {
   info("Modifying a malformed, mixed-case attribute, " +
@@ -58,14 +58,14 @@ function* testMalformedMixedCase(inspect
   EventUtils.sendKey("return", inspector.panelWin);
 
   info("Editing the attribute value and waiting for the mutation event");
   let input = inplaceEditor(attr).input;
   input.value = "viewBox=\"<>\"";
   EventUtils.sendKey("return", inspector.panelWin);
   yield onMutated;
 
-  assertAttributes("svg", {
+  yield assertAttributes("svg", {
     "viewBox": "<>",
     "width": "200",
     "height": "200"
   });
 }
--- a/browser/devtools/markupview/test/head.js
+++ b/browser/devtools/markupview/test/head.js
@@ -369,35 +369,38 @@ let addNewAttributes = Task.async(functi
 
   info("Listening for the markupmutation event");
   let nodeMutated = inspector.once("markupmutation");
   setEditableFieldValue(container.editor.newAttr, text, inspector);
   yield nodeMutated;
 });
 
 /**
- * Checks that a node has the given attributes
+ * Checks that a node has the given attributes.
  *
- * @param {String} selector The node or node selector to check.
- * @param {Object} attrs An object containing the attributes to check.
+ * @param {String} selector The selector for the node to check.
+ * @param {Object} expected An object containing the attributes to check.
  *        e.g. {id: "id1", class: "someclass"}
  *
  * Note that node.getAttribute() returns attribute values provided by the HTML
  * parser. The parser only provides unescaped entities so &amp; will return &.
  */
-function assertAttributes(selector, attrs) {
-  let node = getNode(selector);
+let assertAttributes = Task.async(function*(selector, expected) {
+  let {attributes: actual} = yield getNodeInfo(selector);
 
-  is(node.attributes.length, Object.keys(attrs).length,
-    "Node has the correct number of attributes.");
-  for (let attr in attrs) {
-    is(node.getAttribute(attr), attrs[attr],
-      "Node has the correct " + attr + " attribute.");
+  is(actual.length, Object.keys(expected).length,
+    "The node " + selector + " has the expected number of attributes.");
+  for (let attr in expected) {
+    let foundAttr = actual.find(({name, value}) => name === attr);
+    let foundValue = foundAttr ? foundAttr.value : undefined;
+    ok(foundAttr, "The node " + selector + " has the attribute " + attr);
+    is(foundValue, expected[attr],
+      "The node " + selector + " has the correct " + attr + " attribute value");
   }
-}
+});
 
 /**
  * Undo the last markup-view action and wait for the corresponding mutation to
  * occur
  * @param {InspectorPanel} inspector The instance of InspectorPanel currently
  * loaded in the toolbox
  * @return a promise that resolves when the markup-mutation has been treated or
  * rejects if no undo action is possible
--- a/browser/devtools/markupview/test/helper_attributes_test_runner.js
+++ b/browser/devtools/markupview/test/helper_attributes_test_runner.js
@@ -53,28 +53,28 @@ function runAddAttributesTests(tests, no
  */
 function* runAddAttributesTest(test, selector, inspector) {
   let element = getNode(selector);
 
   info("Starting add-attribute test: " + test.desc);
   yield addNewAttributes(selector, test.text, inspector);
 
   info("Assert that the attribute(s) has/have been applied correctly");
-  assertAttributes(element, test.expectedAttributes);
+  yield assertAttributes(selector, test.expectedAttributes);
 
   if (test.validate) {
     let container = yield getContainerForSelector(selector, inspector);
     test.validate(element, container, inspector);
   }
 
   info("Undo the change");
   yield undoChange(inspector);
 
   info("Assert that the attribute(s) has/have been removed correctly");
-  assertAttributes(element, {});
+  yield assertAttributes(selector, {});
 }
 
 /**
  * Run a series of edit-attributes tests.
  * This function will iterate over the provided tests array and run each test.
  * Each test's goal is to locate a given element on the current test page, assert
  * its current attributes, then provide the name of one of them and a value to
  * be set into it, and then check if the new attributes are correct.
@@ -118,33 +118,33 @@ function runEditAttributesTests(tests, i
  */
 function* runEditAttributesTest(test, inspector) {
   info("Starting edit-attribute test: " + test.desc);
 
   info("Selecting the test node " + test.node);
   yield selectNode(test.node, inspector);
 
   info("Asserting that the node has the right attributes to start with");
-  assertAttributes(test.node, test.originalAttributes);
+  yield assertAttributes(test.node, test.originalAttributes);
 
   info("Editing attribute " + test.name + " with value " + test.value);
 
   let container = yield getContainerForSelector(test.node, inspector);
   ok(container && container.editor, "The markup-container for " + test.node +
     " was found");
 
   info("Listening for the markupmutation event");
   let nodeMutated = inspector.once("markupmutation");
   let attr = container.editor.attrs[test.name].querySelector(".editable");
   setEditableFieldValue(attr, test.value, inspector);
   yield nodeMutated;
 
   info("Asserting the new attributes after edition");
-  assertAttributes(test.node, test.expectedAttributes);
+  yield assertAttributes(test.node, test.expectedAttributes);
 
   info("Undo the change and assert that the attributes have been changed back");
   yield undoChange(inspector);
-  assertAttributes(test.node, test.originalAttributes);
+  yield assertAttributes(test.node, test.originalAttributes);
 
   info("Redo the change and assert that the attributes have been changed again");
   yield redoChange(inspector);
-  assertAttributes(test.node, test.expectedAttributes);
+  yield assertAttributes(test.node, test.expectedAttributes);
 }