michael@0: /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
michael@0: /* Any copyright is dedicated to the Public Domain.
michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0:
michael@0: "use strict";
michael@0:
michael@0: // Tests that properties can be selected and copied from the computed view
michael@0:
michael@0: XPCOMUtils.defineLazyGetter(this, "osString", function() {
michael@0: return Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime).OS;
michael@0: });
michael@0:
michael@0: let test = asyncTest(function*() {
michael@0: yield addTab("data:text/html,computed view copy test");
michael@0:
michael@0: info("Creating the test document");
michael@0: content.document.body.innerHTML = '
\n' +
michael@0: '
Some header text
\n' +
michael@0: '
hi.
\n' +
michael@0: '
I am a test-case. This text exists ' +
michael@0: 'solely to provide some things to ' +
michael@0: 'highlight and count ' +
michael@0: 'style list-items in the box at right. If you are reading this, ' +
michael@0: 'you should go do something else instead. Maybe read a book. Or better ' +
michael@0: 'yet, write some test-cases for another bit of code. ' +
michael@0: 'some text
\n' +
michael@0: '
more text
\n' +
michael@0: '
even more text
' +
michael@0: '
';
michael@0: content.document.title = "Computed view context menu test";
michael@0:
michael@0: info("Opening the computed view");
michael@0: let {toolbox, inspector, view} = yield openComputedView();
michael@0:
michael@0: info("Selecting the test node");
michael@0: yield selectNode("span", inspector);
michael@0:
michael@0: yield checkCopySelection(view);
michael@0: yield checkSelectAll(view);
michael@0: });
michael@0:
michael@0: function checkCopySelection(view) {
michael@0: info("Testing selection copy");
michael@0:
michael@0: let contentDocument = view.styleDocument;
michael@0: let props = contentDocument.querySelectorAll(".property-view");
michael@0: ok(props, "captain, we have the property-view nodes");
michael@0:
michael@0: let range = contentDocument.createRange();
michael@0: range.setStart(props[1], 0);
michael@0: range.setEnd(props[3], 3);
michael@0: contentDocument.defaultView.getSelection().addRange(range);
michael@0:
michael@0: info("Checking that cssHtmlTree.siBoundCopy() returns the correct clipboard value");
michael@0:
michael@0: let expectedPattern = "font-family: helvetica,sans-serif;[\\r\\n]+" +
michael@0: "font-size: 16px;[\\r\\n]+" +
michael@0: "font-variant: small-caps;[\\r\\n]*";
michael@0:
michael@0: return waitForClipboard(() => {
michael@0: fireCopyEvent(props[0]);
michael@0: }, () => {
michael@0: return checkClipboardData(expectedPattern);
michael@0: }).then(() => {}, () => {
michael@0: failedClipboard(expectedPattern);
michael@0: });
michael@0: }
michael@0:
michael@0: function checkSelectAll(view) {
michael@0: info("Testing select-all copy");
michael@0:
michael@0: let contentDoc = view.styleDocument;
michael@0: let prop = contentDoc.querySelector(".property-view");
michael@0:
michael@0: info("Checking that _SelectAll() then copy returns the correct clipboard value");
michael@0: view._onSelectAll();
michael@0: let expectedPattern = "color: #FF0;[\\r\\n]+" +
michael@0: "font-family: helvetica,sans-serif;[\\r\\n]+" +
michael@0: "font-size: 16px;[\\r\\n]+" +
michael@0: "font-variant: small-caps;[\\r\\n]*";
michael@0:
michael@0: return waitForClipboard(() => {
michael@0: fireCopyEvent(prop);
michael@0: }, () => {
michael@0: return checkClipboardData(expectedPattern);
michael@0: }).then(() => {}, () => {
michael@0: failedClipboard(expectedPattern);
michael@0: });
michael@0: }
michael@0:
michael@0: function checkClipboardData(expectedPattern) {
michael@0: let actual = SpecialPowers.getClipboardData("text/unicode");
michael@0: let expectedRegExp = new RegExp(expectedPattern, "g");
michael@0: return expectedRegExp.test(actual);
michael@0: }
michael@0:
michael@0: function failedClipboard(expectedPattern) {
michael@0: // Format expected text for comparison
michael@0: let terminator = osString == "WINNT" ? "\r\n" : "\n";
michael@0: expectedPattern = expectedPattern.replace(/\[\\r\\n\][+*]/g, terminator);
michael@0: expectedPattern = expectedPattern.replace(/\\\(/g, "(");
michael@0: expectedPattern = expectedPattern.replace(/\\\)/g, ")");
michael@0:
michael@0: let actual = SpecialPowers.getClipboardData("text/unicode");
michael@0:
michael@0: // Trim the right hand side of our strings. This is because expectedPattern
michael@0: // accounts for windows sometimes adding a newline to our copied data.
michael@0: expectedPattern = expectedPattern.trimRight();
michael@0: actual = actual.trimRight();
michael@0:
michael@0: dump("TEST-UNEXPECTED-FAIL | Clipboard text does not match expected ... " +
michael@0: "results (escaped for accurate comparison):\n");
michael@0: info("Actual: " + escape(actual));
michael@0: info("Expected: " + escape(expectedPattern));
michael@0: }