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