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 rule 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,
rule view context menu 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 = "Rule view context menu test";
michael@0:
michael@0: info("Opening the computed view");
michael@0: let {toolbox, inspector, view} = yield openRuleView();
michael@0:
michael@0: info("Selecting the test node");
michael@0: yield selectNode("div", 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 contentDoc = view.doc;
michael@0: let prop = contentDoc.querySelector(".ruleview-property");
michael@0: let values = contentDoc.querySelectorAll(".ruleview-propertycontainer");
michael@0:
michael@0: let range = contentDoc.createRange();
michael@0: range.setStart(prop, 0);
michael@0: range.setEnd(values[4], 2);
michael@0: let selection = view.doc.defaultView.getSelection().addRange(range);
michael@0:
michael@0: info("Checking that _Copy() returns the correct clipboard value");
michael@0:
michael@0: let expectedPattern = " margin: 10em;[\\r\\n]+" +
michael@0: " font-size: 14pt;[\\r\\n]+" +
michael@0: " font-family: helvetica,sans-serif;[\\r\\n]+" +
michael@0: " color: #AAA;[\\r\\n]+" +
michael@0: "}[\\r\\n]+" +
michael@0: "html {[\\r\\n]+" +
michael@0: " color: #000;[\\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 checkSelectAll(view) {
michael@0: info("Testing select-all copy");
michael@0:
michael@0: let contentDoc = view.doc;
michael@0: let prop = contentDoc.querySelector(".ruleview-property");
michael@0:
michael@0: info("Checking that _SelectAll() then copy returns the correct clipboard value");
michael@0: view._onSelectAll();
michael@0: let expectedPattern = "[\\r\\n]+" +
michael@0: "element {[\\r\\n]+" +
michael@0: " margin: 10em;[\\r\\n]+" +
michael@0: " font-size: 14pt;[\\r\\n]+" +
michael@0: " font-family: helvetica,sans-serif;[\\r\\n]+" +
michael@0: " color: #AAA;[\\r\\n]+" +
michael@0: "}[\\r\\n]+" +
michael@0: "html {[\\r\\n]+" +
michael@0: " color: #000;[\\r\\n]+" +
michael@0: "}[\\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: }