diff options
Diffstat (limited to 'testing/web-platform/tests/shadow-dom/resources')
4 files changed, 351 insertions, 0 deletions
diff --git a/testing/web-platform/tests/shadow-dom/resources/Document-prototype-currentScript-helper.js b/testing/web-platform/tests/shadow-dom/resources/Document-prototype-currentScript-helper.js new file mode 100644 index 0000000000..c25693cdb9 --- /dev/null +++ b/testing/web-platform/tests/shadow-dom/resources/Document-prototype-currentScript-helper.js @@ -0,0 +1 @@ +executeExternalScript(); diff --git a/testing/web-platform/tests/shadow-dom/resources/event-path-test-helpers.js b/testing/web-platform/tests/shadow-dom/resources/event-path-test-helpers.js new file mode 100644 index 0000000000..f0e8ec33ff --- /dev/null +++ b/testing/web-platform/tests/shadow-dom/resources/event-path-test-helpers.js @@ -0,0 +1,92 @@ + +function dispatchEventWithEventLog(shadow, target, event) { + var eventPath = []; + var targets = []; + var relatedTargets = []; + var pathAtTargets = []; + + var attachedNodes = []; + for (var nodeKey in shadow) { + var startingNode = shadow[nodeKey]; + for (var node = startingNode; node; node = node.parentNode) { + if (attachedNodes.indexOf(node) >= 0) + continue; + attachedNodes.push(node); + node.addEventListener(event.type, (function (event) { + eventPath.push(this.label); + relatedTargets.push(event.relatedTarget ? event.relatedTarget.label : null); + + pathAtTargets.push(event.composedPath().map(function (node) { return node.label; })); + targets.push(event.target); + }).bind(node)); + } + } + + target.dispatchEvent(event); + + return {event: event, targets: targets, eventPath: eventPath, relatedTargets: relatedTargets, pathAtTargets: pathAtTargets}; +} + +/* +-SR: ShadowRoot -S: Slot +A ------------------------------- A-SR ++ B ------------ B-SR + A1 --- A1-SR + + C + B1 --- B1-SR + A2-S + A1a + + D --- D-SR + B1a + B1b --- B1b-SR + + D1 + B1c-S + B1b1 + + B1b2 +*/ +function createFixedTestTree(mode) { + var namedNodes = {}; + + function element(name) { + var element = document.createElement(name.indexOf('-S') > 0 ? 'slot' : 'div'); + element.label = name; + namedNodes[name] = element; + for (var i = 1; i < arguments.length; i++) { + var item = arguments[i]; + if (typeof(item) == 'function') + item(element); + else + element.appendChild(item); + } + return element; + } + + function shadow(name) { + var children = []; + for (var i = 1; i < arguments.length; i++) + children.push(arguments[i]); + return function (element) { + var shadowRoot = element.attachShadow({mode: mode}); + shadowRoot.label = name; + namedNodes[name] = shadowRoot; + for (var child of children) + shadowRoot.appendChild(child); + } + } + + var host = element('A', + shadow('A-SR', + element('A1', + shadow('A1-SR', + element('A1a'))), + element('A2-S') + ), + element('B', + shadow('B-SR', + element('B1', + shadow('B1-SR', + element('B1b', + shadow('B1b-SR', + element('B1b1'), + element('B1b2'))), + element('B1c-S')), + element('B1a'))), + element('C'), + element('D', + shadow('D-SR', + element('D1'))))); + + return namedNodes; +} diff --git a/testing/web-platform/tests/shadow-dom/resources/shadow-dom-utils.js b/testing/web-platform/tests/shadow-dom/resources/shadow-dom-utils.js new file mode 100644 index 0000000000..303d1a573d --- /dev/null +++ b/testing/web-platform/tests/shadow-dom/resources/shadow-dom-utils.js @@ -0,0 +1,95 @@ +"use strict"; + +function unit(f) { + return function () { + var ctx = newContext(); + try { + f(ctx); + } finally { + cleanContext(ctx); + } + } +} + +function step_unit(f, ctx, t) { + return function () { + var done = false; + try { + f(); + done = true; + } finally { + if (done) { + t.done(); + } + cleanContext(ctx); + } + } +} + +function assert_nodelist_contents_equal_noorder(actual, expected, message) { + assert_equals(actual.length, expected.length, message); + var used = []; + for (var i = 0; i < expected.length; i++) { + used.push(false); + } + for (i = 0; i < expected.length; i++) { + var found = false; + for (var j = 0; j < actual.length; j++) { + if (used[j] == false && expected[i] == actual[j]) { + used[j] = true; + found = true; + break; + } + } + if (!found) { + assert_unreached(message + ". Fail reason: element not found: " + expected[i]); + } + } +} + +//Example taken from http://www.w3.org/TR/shadow-dom/#event-retargeting-example +function createTestMediaPlayer(d) { + d.body.innerHTML = '' + + '<div id="player">' + + '<input type="checkbox" id="outside-control">' + + '<div id="player-shadow-host">' + + '</div>' + + '</div>'; + + var playerShadowRoot = d.querySelector('#player-shadow-host').attachShadow({mode: 'open'}); + playerShadowRoot.innerHTML = '' + + '<div id="controls">' + + '<button class="play-button">PLAY</button>' + + '<div tabindex="0" id="timeline">' + + '<div id="timeline-shadow-host">' + + '</div>' + + '</div>' + + '<div class="volume-slider-container" id="volume-slider-container">' + + '<div tabindex="0" class="volume-slider" id="volume-slider">' + + '<div id="volume-shadow-host">' + + '</div>' + + '</div>' + + '</div>' + + '</div>'; + + var timeLineShadowRoot = playerShadowRoot.querySelector('#timeline-shadow-host').attachShadow({mode: 'open'}); + timeLineShadowRoot.innerHTML = '<div class="slider-thumb" id="timeline-slider-thumb"></div>'; + + var volumeShadowRoot = playerShadowRoot.querySelector('#volume-shadow-host').attachShadow({mode: 'open'}); + volumeShadowRoot.innerHTML = '<div class="slider-thumb" id="volume-slider-thumb"></div>'; + + return { + 'playerShadowRoot': playerShadowRoot, + 'timeLineShadowRoot': timeLineShadowRoot, + 'volumeShadowRoot': volumeShadowRoot + }; +} + +//FIXME This call of initKeyboardEvent works for WebKit-only. +//See https://bugs.webkit.org/show_bug.cgi?id=16735 +// and https://bugs.webkit.org/show_bug.cgi?id=13368. Add check for browser here +function fireKeyboardEvent(doc, element, key) { + var event = doc.createEvent('KeyboardEvent'); + event.initKeyboardEvent("keydown", true, true, doc.defaultView, key, 0, false, false, false, false); + element.dispatchEvent(event); +} diff --git a/testing/web-platform/tests/shadow-dom/resources/shadow-dom.js b/testing/web-platform/tests/shadow-dom/resources/shadow-dom.js new file mode 100644 index 0000000000..3a4ad39d42 --- /dev/null +++ b/testing/web-platform/tests/shadow-dom/resources/shadow-dom.js @@ -0,0 +1,163 @@ +function removeWhiteSpaceOnlyTextNodes(node) +{ + for (var i = 0; i < node.childNodes.length; i++) { + var child = node.childNodes[i]; + if (child.nodeType === Node.TEXT_NODE && child.nodeValue.trim().length == 0) { + node.removeChild(child); + i--; + } else if (child.nodeType === Node.ELEMENT_NODE || child.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { + removeWhiteSpaceOnlyTextNodes(child); + } + } + if (node.shadowRoot) { + removeWhiteSpaceOnlyTextNodes(node.shadowRoot); + } +} + +function createTestTree(node) { + + let ids = {}; + + function attachShadowFromTemplate(template) { + let parent = template.parentNode; + parent.removeChild(template); + let shadowRoot; + if (template.getAttribute('data-slot-assignment') === 'manual') { + shadowRoot = + parent.attachShadow({mode: template.getAttribute('data-mode'), + slotAssignment: 'manual'}); + } else { + shadowRoot = parent.attachShadow( + {mode: template.getAttribute('data-mode')}); + } + let id = template.id; + if (id) { + shadowRoot.id = id; + ids[id] = shadowRoot; + } + shadowRoot.appendChild(document.importNode(template.content, true)); + return shadowRoot; + } + + function walk(root) { + if (root.id) { + ids[root.id] = root; + } + for (let e of Array.from(root.querySelectorAll('[id]'))) { + ids[e.id] = e; + } + for (let e of Array.from(root.querySelectorAll('template'))) { + walk(attachShadowFromTemplate(e)); + } + } + + walk(node.cloneNode(true)); + return ids; +} + +// TODO: Refactor this so that only interested results are recorded. +// Callers of this function would not be interested in every results. +function dispatchEventWithLog(nodes, target, event, options) { + + function labelFor(e) { + return e.id || e.tagName; + } + + let log = []; + let attachedNodes = []; + for (let label in nodes) { + let startingNode = nodes[label]; + for (let node = startingNode; node; node = node.parentNode) { + if (attachedNodes.indexOf(node) >= 0) + continue; + let id = node.id; + if (!id) + continue; + attachedNodes.push(node); + if (options && options.capture) { + // Record [currentTarget, target, relatedTarget, composedPath(), 'capture' | 'non-capture'] + // TODO: Support registering listeners in different orders. + // e.g. Register a non-capture listener at first, then register a capture listener. + node.addEventListener(event.type, (e) => { + log.push([id, + labelFor(e.target), + e.relatedTarget ? labelFor(e.relatedTarget) : null, + e.composedPath().map((n) => { + return labelFor(n); + }), + 'capture']); + }, true); + node.addEventListener(event.type, (e) => { + log.push([id, + labelFor(e.target), + e.relatedTarget ? labelFor(e.relatedTarget) : null, + e.composedPath().map((n) => { + return labelFor(n); + }), + 'non-capture']); + }); + } else { + // Record [currentTarget, target, relatedTarget, composedPath()] + node.addEventListener(event.type, (e) => { + log.push([id, + labelFor(e.target), + e.relatedTarget ? labelFor(e.relatedTarget) : null, + e.composedPath().map((n) => { + return labelFor(n); + })] + ); + }); + } + } + } + target.dispatchEvent(event); + return log; +} + +// TODO(hayato): Merge this into dispatchEventWithLog +function dispatchUAEventWithLog(nodes, target, eventType, callback) { + + function labelFor(e) { + return e.id || e.tagName; + } + + let log = []; + let attachedNodes = []; + for (let label in nodes) { + let startingNode = nodes[label]; + for (let node = startingNode; node; node = node.parentNode) { + if (attachedNodes.indexOf(node) >= 0) + continue; + let id = node.id; + if (!id) + continue; + attachedNodes.push(node); + node.addEventListener(eventType, (e) => { + // Record [currentTarget, target, relatedTarget, composedPath()] + log.push([id, + labelFor(e.target), + e.relatedTarget ? labelFor(e.relatedTarget) : null, + e.composedPath().map((n) => { + return labelFor(n); + })]); + }); + } + } + callback(target); + return log; +} + +// This function assumes that testharness.js is available. +function assert_event_path_equals(actual, expected) { + assert_equals(actual.length, expected.length); + for (let i = 0; i < actual.length; ++i) { + assert_equals(actual[i].length, expected[i].length); + assert_equals(actual[i][0], expected[i][0], 'currentTarget at ' + i + ' should be same'); + assert_equals(actual[i][1], expected[i][1], 'target at ' + i + ' should be same'); + assert_equals(actual[i][2], expected[i][2], 'relatedTarget at ' + i + ' should be same'); + assert_array_equals(actual[i][3], expected[i][3], 'composedPath at ' + i + ' should be same'); + if (actual[i][4]) { + assert_equals(actual[i][4], expected[i][4], 'listener type should be same at ' + i); + } + } +} |