browser/devtools/styleinspector/test/browser_computedview_select-and-copy-styles.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
michael@0 2 /* Any copyright is dedicated to the Public Domain.
michael@0 3 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 // Tests that properties can be selected and copied from the computed view
michael@0 8
michael@0 9 XPCOMUtils.defineLazyGetter(this, "osString", function() {
michael@0 10 return Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime).OS;
michael@0 11 });
michael@0 12
michael@0 13 let test = asyncTest(function*() {
michael@0 14 yield addTab("data:text/html,computed view copy test");
michael@0 15
michael@0 16 info("Creating the test document");
michael@0 17 content.document.body.innerHTML = '<style type="text/css"> ' +
michael@0 18 'span { font-variant: small-caps; color: #000000; } ' +
michael@0 19 '.nomatches {color: #ff0000;}</style> <div id="first" style="margin: 10em; ' +
michael@0 20 'font-size: 14pt; font-family: helvetica, sans-serif; color: #AAA">\n' +
michael@0 21 '<h1>Some header text</h1>\n' +
michael@0 22 '<p id="salutation" style="font-size: 12pt">hi.</p>\n' +
michael@0 23 '<p id="body" style="font-size: 12pt">I am a test-case. This text exists ' +
michael@0 24 'solely to provide some things to <span style="color: yellow">' +
michael@0 25 'highlight</span> and <span style="font-weight: bold">count</span> ' +
michael@0 26 'style list-items in the box at right. If you are reading this, ' +
michael@0 27 'you should go do something else instead. Maybe read a book. Or better ' +
michael@0 28 'yet, write some test-cases for another bit of code. ' +
michael@0 29 '<span style="font-style: italic">some text</span></p>\n' +
michael@0 30 '<p id="closing">more text</p>\n' +
michael@0 31 '<p>even more text</p>' +
michael@0 32 '</div>';
michael@0 33 content.document.title = "Computed view context menu test";
michael@0 34
michael@0 35 info("Opening the computed view");
michael@0 36 let {toolbox, inspector, view} = yield openComputedView();
michael@0 37
michael@0 38 info("Selecting the test node");
michael@0 39 yield selectNode("span", inspector);
michael@0 40
michael@0 41 yield checkCopySelection(view);
michael@0 42 yield checkSelectAll(view);
michael@0 43 });
michael@0 44
michael@0 45 function checkCopySelection(view) {
michael@0 46 info("Testing selection copy");
michael@0 47
michael@0 48 let contentDocument = view.styleDocument;
michael@0 49 let props = contentDocument.querySelectorAll(".property-view");
michael@0 50 ok(props, "captain, we have the property-view nodes");
michael@0 51
michael@0 52 let range = contentDocument.createRange();
michael@0 53 range.setStart(props[1], 0);
michael@0 54 range.setEnd(props[3], 3);
michael@0 55 contentDocument.defaultView.getSelection().addRange(range);
michael@0 56
michael@0 57 info("Checking that cssHtmlTree.siBoundCopy() returns the correct clipboard value");
michael@0 58
michael@0 59 let expectedPattern = "font-family: helvetica,sans-serif;[\\r\\n]+" +
michael@0 60 "font-size: 16px;[\\r\\n]+" +
michael@0 61 "font-variant: small-caps;[\\r\\n]*";
michael@0 62
michael@0 63 return waitForClipboard(() => {
michael@0 64 fireCopyEvent(props[0]);
michael@0 65 }, () => {
michael@0 66 return checkClipboardData(expectedPattern);
michael@0 67 }).then(() => {}, () => {
michael@0 68 failedClipboard(expectedPattern);
michael@0 69 });
michael@0 70 }
michael@0 71
michael@0 72 function checkSelectAll(view) {
michael@0 73 info("Testing select-all copy");
michael@0 74
michael@0 75 let contentDoc = view.styleDocument;
michael@0 76 let prop = contentDoc.querySelector(".property-view");
michael@0 77
michael@0 78 info("Checking that _SelectAll() then copy returns the correct clipboard value");
michael@0 79 view._onSelectAll();
michael@0 80 let expectedPattern = "color: #FF0;[\\r\\n]+" +
michael@0 81 "font-family: helvetica,sans-serif;[\\r\\n]+" +
michael@0 82 "font-size: 16px;[\\r\\n]+" +
michael@0 83 "font-variant: small-caps;[\\r\\n]*";
michael@0 84
michael@0 85 return waitForClipboard(() => {
michael@0 86 fireCopyEvent(prop);
michael@0 87 }, () => {
michael@0 88 return checkClipboardData(expectedPattern);
michael@0 89 }).then(() => {}, () => {
michael@0 90 failedClipboard(expectedPattern);
michael@0 91 });
michael@0 92 }
michael@0 93
michael@0 94 function checkClipboardData(expectedPattern) {
michael@0 95 let actual = SpecialPowers.getClipboardData("text/unicode");
michael@0 96 let expectedRegExp = new RegExp(expectedPattern, "g");
michael@0 97 return expectedRegExp.test(actual);
michael@0 98 }
michael@0 99
michael@0 100 function failedClipboard(expectedPattern) {
michael@0 101 // Format expected text for comparison
michael@0 102 let terminator = osString == "WINNT" ? "\r\n" : "\n";
michael@0 103 expectedPattern = expectedPattern.replace(/\[\\r\\n\][+*]/g, terminator);
michael@0 104 expectedPattern = expectedPattern.replace(/\\\(/g, "(");
michael@0 105 expectedPattern = expectedPattern.replace(/\\\)/g, ")");
michael@0 106
michael@0 107 let actual = SpecialPowers.getClipboardData("text/unicode");
michael@0 108
michael@0 109 // Trim the right hand side of our strings. This is because expectedPattern
michael@0 110 // accounts for windows sometimes adding a newline to our copied data.
michael@0 111 expectedPattern = expectedPattern.trimRight();
michael@0 112 actual = actual.trimRight();
michael@0 113
michael@0 114 dump("TEST-UNEXPECTED-FAIL | Clipboard text does not match expected ... " +
michael@0 115 "results (escaped for accurate comparison):\n");
michael@0 116 info("Actual: " + escape(actual));
michael@0 117 info("Expected: " + escape(expectedPattern));
michael@0 118 }

mercurial