summaryrefslogtreecommitdiffstats
path: root/devtools/shared/tests/chrome
diff options
context:
space:
mode:
Diffstat (limited to 'devtools/shared/tests/chrome')
-rw-r--r--devtools/shared/tests/chrome/chrome.ini8
-rw-r--r--devtools/shared/tests/chrome/test_css-logic-findCssSelector.html115
-rw-r--r--devtools/shared/tests/chrome/test_css-logic-getCssPath.html106
-rw-r--r--devtools/shared/tests/chrome/test_css-logic-getXPath.html95
4 files changed, 324 insertions, 0 deletions
diff --git a/devtools/shared/tests/chrome/chrome.ini b/devtools/shared/tests/chrome/chrome.ini
new file mode 100644
index 0000000000..026b575d33
--- /dev/null
+++ b/devtools/shared/tests/chrome/chrome.ini
@@ -0,0 +1,8 @@
+[DEFAULT]
+tags = devtools
+skip-if = os == 'android'
+
+[test_css-logic-findCssSelector.html]
+[test_css-logic-getCssPath.html]
+[test_css-logic-getXPath.html]
+skip-if = os == 'linux' && debug # Bug 1205739
diff --git a/devtools/shared/tests/chrome/test_css-logic-findCssSelector.html b/devtools/shared/tests/chrome/test_css-logic-findCssSelector.html
new file mode 100644
index 0000000000..b7d364664b
--- /dev/null
+++ b/devtools/shared/tests/chrome/test_css-logic-findCssSelector.html
@@ -0,0 +1,115 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <meta charset="utf-8">
+ <title>Test for CSS logic helper </title>
+
+ <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+ <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
+ <script type="application/javascript">
+"use strict";
+
+const { require } = ChromeUtils.importESModule("resource://devtools/shared/loader/Loader.sys.mjs");
+const { findCssSelector } = require("devtools/shared/inspector/css-logic");
+
+var _tests = [];
+function addTest(test) {
+ _tests.push(test);
+}
+
+function runNextTest() {
+ if (!_tests.length) {
+ SimpleTest.finish();
+ return;
+ }
+ _tests.shift()();
+}
+
+window.onload = function() {
+ SimpleTest.waitForExplicitFinish();
+ runNextTest();
+};
+
+addTest(function findAllCssSelectors() {
+ const nodes = document.querySelectorAll("*");
+ for (let i = 0; i < nodes.length; i++) {
+ const selector = findCssSelector(nodes[i]);
+ const matches = document.querySelectorAll(selector);
+
+ is(matches.length, 1, "There is a single match: " + selector);
+ is(matches[0], nodes[i], "The selector matches the correct node: " + selector);
+ }
+
+ runNextTest();
+});
+
+addTest(function findCssSelectorNotContainedInDocument() {
+ const unattached = document.createElement("div");
+ unattached.id = "unattached";
+ is(findCssSelector(unattached), "", "Unattached node returns empty string");
+
+ const unattachedChild = document.createElement("div");
+ unattached.appendChild(unattachedChild);
+ is(findCssSelector(unattachedChild), "", "Unattached child returns empty string");
+
+ const unattachedBody = document.createElement("body");
+ is(findCssSelector(unattachedBody), "", "Unattached body returns empty string");
+
+ runNextTest();
+});
+
+addTest(function findCssSelectorBasic() {
+ const data = [
+ "#one",
+ "#" + CSS.escape("2"),
+ ".three",
+ "." + CSS.escape("4"),
+ "#find-css-selector > div:nth-child(5)",
+ "#find-css-selector > p:nth-child(6)",
+ ".seven",
+ ".eight",
+ ".nine",
+ ".ten",
+ "div.sameclass:nth-child(11)",
+ "div.sameclass:nth-child(12)",
+ "div.sameclass:nth-child(13)",
+ "#" + CSS.escape("!, \", #, $, %, &, ', (, ), *, +, ,, -, ., /, :, ;, <, =, >, ?, @, [, \\, ], ^, `, {, |, }, ~"),
+ ];
+
+ const container = document.querySelector("#find-css-selector");
+ is(container.children.length, data.length, "Container has correct number of children.");
+
+ for (let i = 0; i < data.length; i++) {
+ const node = container.children[i];
+ is(findCssSelector(node), data[i], "matched id for index " + (i - 1));
+ }
+
+ runNextTest();
+});
+ </script>
+</head>
+<body>
+ <div id="find-css-selector">
+ <div id="one"></div> <!-- Basic ID -->
+ <div id="2"></div> <!-- Escaped ID -->
+ <div class="three"></div> <!-- Basic Class -->
+ <div class="4"></div> <!-- Escaped Class -->
+ <div attr="5"></div> <!-- Only an attribute -->
+ <p></p> <!-- Nothing unique -->
+ <div class="seven seven"></div> <!-- Two classes with same name -->
+ <div class="eight eight2"></div> <!-- Two classes with different names -->
+
+ <!-- Two elements with the same id - should not use ID -->
+ <div class="nine" id="nine-and-ten"></div>
+ <div class="ten" id="nine-and-ten"></div>
+
+ <!-- Three elements with the same id - should use class and nth-child instead -->
+ <div class="sameclass" id="11-12-13"></div>
+ <div class="sameclass" id="11-12-13"></div>
+ <div class="sameclass" id="11-12-13"></div>
+
+ <!-- Special characters -->
+ <div id="!, &quot;, #, $, %, &amp;, ', (, ), *, +, ,, -, ., /, :, ;, <, =, >, ?, @, [, \, ], ^, `, {, |, }, ~"></div>
+ </div>
+</body>
+</html>
diff --git a/devtools/shared/tests/chrome/test_css-logic-getCssPath.html b/devtools/shared/tests/chrome/test_css-logic-getCssPath.html
new file mode 100644
index 0000000000..333c9e0fdf
--- /dev/null
+++ b/devtools/shared/tests/chrome/test_css-logic-getCssPath.html
@@ -0,0 +1,106 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1323700
+-->
+<head>
+ <meta charset="utf-8">
+ <title>Test for Bug 1323700</title>
+
+ <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+ <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
+ <script type="application/javascript">
+"use strict";
+
+const { require } = ChromeUtils.importESModule("resource://devtools/shared/loader/Loader.sys.mjs");
+const CssLogic = require("devtools/shared/inspector/css-logic");
+
+var _tests = [];
+function addTest(test) {
+ _tests.push(test);
+}
+
+function runNextTest() {
+ if (!_tests.length) {
+ SimpleTest.finish()
+ return;
+ }
+ _tests.shift()();
+}
+
+window.onload = function() {
+ SimpleTest.waitForExplicitFinish();
+ runNextTest();
+}
+
+addTest(function getCssPathForUnattachedElement() {
+ const unattached = document.createElement("div");
+ unattached.id = "unattached";
+ is(CssLogic.getCssPath(unattached), "", "Unattached node returns empty string");
+
+ const unattachedChild = document.createElement("div");
+ unattached.appendChild(unattachedChild);
+ is(CssLogic.getCssPath(unattachedChild), "", "Unattached child returns empty string");
+
+ const unattachedBody = document.createElement("body");
+ is(CssLogic.getCssPath(unattachedBody), "", "Unattached body returns empty string");
+
+ runNextTest();
+});
+
+addTest(function cssPathHasOneStepForEachAncestor() {
+ for (const el of [...document.querySelectorAll('*')]) {
+ const splitPath = CssLogic.getCssPath(el).split(" ");
+
+ let expectedNbOfParts = 0;
+ let parent = el.parentNode;
+ while (parent) {
+ expectedNbOfParts ++;
+ parent = parent.parentNode;
+ }
+
+ is(splitPath.length, expectedNbOfParts, "There are enough parts in the full path");
+ }
+
+ runNextTest();
+});
+
+addTest(function getCssPath() {
+ const data = [{
+ selector: "#id",
+ path: "html body div div div.class div#id"
+ }, {
+ selector: "html",
+ path: "html"
+ }, {
+ selector: "body",
+ path: "html body"
+ }, {
+ selector: ".c1.c2.c3",
+ path: "html body span.c1.c2.c3"
+ }, {
+ selector: "#i",
+ path: "html body span#i.c1.c2"
+ }];
+
+ for (const {selector, path} of data) {
+ const node = document.querySelector(selector);
+ is (CssLogic.getCssPath(node), path, `Full css path is correct for ${selector}`);
+ }
+
+ runNextTest();
+});
+ </script>
+</head>
+<body>
+ <div>
+ <div>
+ <div class="class">
+ <div id="id"></div>
+ </div>
+ </div>
+ </div>
+ <span class="c1 c2 c3"></span>
+ <span id="i" class="c1 c2"></span>
+</body>
+</html>
diff --git a/devtools/shared/tests/chrome/test_css-logic-getXPath.html b/devtools/shared/tests/chrome/test_css-logic-getXPath.html
new file mode 100644
index 0000000000..469e188cf0
--- /dev/null
+++ b/devtools/shared/tests/chrome/test_css-logic-getXPath.html
@@ -0,0 +1,95 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=987877
+-->
+<head>
+ <meta charset="utf-8">
+ <title>Test for Bug 987877</title>
+
+ <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+ <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
+ <script type="application/javascript">
+"use strict";
+
+const { require } = ChromeUtils.importESModule("resource://devtools/shared/loader/Loader.sys.mjs");
+const CssLogic = require("devtools/shared/inspector/css-logic");
+
+const _tests = [];
+function addTest(test) {
+ _tests.push(test);
+}
+
+function runNextTest() {
+ if (!_tests.length) {
+ SimpleTest.finish();
+ return;
+ }
+ _tests.shift()();
+}
+
+window.onload = function () {
+ SimpleTest.waitForExplicitFinish();
+ runNextTest();
+};
+
+addTest(function getXPathForUnattachedElement() {
+ const unattached = document.createElement("div");
+ unattached.id = "unattached";
+ is(CssLogic.getXPath(unattached), "", "Unattached node returns empty string");
+
+ const unattachedChild = document.createElement("div");
+ unattached.appendChild(unattachedChild);
+ is(CssLogic.getXPath(unattachedChild), "", "Unattached child returns empty string");
+
+ const unattachedBody = document.createElement("body");
+ is(CssLogic.getXPath(unattachedBody), "", "Unattached body returns empty string");
+
+ runNextTest();
+});
+
+addTest(function getXPath() {
+ const data = [{
+ // Target elements that have an ID get a short XPath.
+ selector: "#i-have-an-id",
+ path: "//*[@id=\"i-have-an-id\"]"
+ }, {
+ selector: "html",
+ path: "/html"
+ }, {
+ selector: "body",
+ path: "/html/body"
+ }, {
+ selector: "body > div:nth-child(2) > div > div:nth-child(4)",
+ path: "/html/body/div[2]/div/div[4]"
+ }, {
+ // XPath should support namespace.
+ selector: "namespace\\:body",
+ path: "/html/body/namespace:test/namespace:body"
+ }];
+
+ for (const {selector, path} of data) {
+ const node = document.querySelector(selector);
+ is(CssLogic.getXPath(node), path, `Full css path is correct for ${selector}`);
+ }
+
+ runNextTest();
+});
+ </script>
+</head>
+<body>
+ <div id="i-have-an-id">find me</div>
+ <div>
+ <div>
+ <div></div>
+ <div></div>
+ <div></div>
+ <div>me too!</div>
+ </div>
+ </div>
+ <namespace:test>
+ <namespace:header></namespace:header>
+ <namespace:body>and me</namespace:body>
+ </namespace:test>
+</body>
+</html>