1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/styleinspector/test/browser_ruleview_select-and-copy-styles.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,129 @@ 1.4 +/* vim: set ft=javascript ts=2 et sw=2 tw=80: */ 1.5 +/* Any copyright is dedicated to the Public Domain. 1.6 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +// Tests that properties can be selected and copied from the rule view 1.11 + 1.12 +XPCOMUtils.defineLazyGetter(this, "osString", function() { 1.13 + return Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime).OS; 1.14 +}); 1.15 + 1.16 +let test = asyncTest(function*() { 1.17 + yield addTab("data:text/html,<p>rule view context menu test</p>"); 1.18 + 1.19 + info("Creating the test document"); 1.20 + content.document.body.innerHTML = '<style type="text/css"> ' + 1.21 + 'html { color: #000000; } ' + 1.22 + 'span { font-variant: small-caps; color: #000000; } ' + 1.23 + '.nomatches {color: #ff0000;}</style> <div id="first" style="margin: 10em; ' + 1.24 + 'font-size: 14pt; font-family: helvetica, sans-serif; color: #AAA">\n' + 1.25 + '<h1>Some header text</h1>\n' + 1.26 + '<p id="salutation" style="font-size: 12pt">hi.</p>\n' + 1.27 + '<p id="body" style="font-size: 12pt">I am a test-case. This text exists ' + 1.28 + 'solely to provide some things to <span style="color: yellow">' + 1.29 + 'highlight</span> and <span style="font-weight: bold">count</span> ' + 1.30 + 'style list-items in the box at right. If you are reading this, ' + 1.31 + 'you should go do something else instead. Maybe read a book. Or better ' + 1.32 + 'yet, write some test-cases for another bit of code. ' + 1.33 + '<span style="font-style: italic">some text</span></p>\n' + 1.34 + '<p id="closing">more text</p>\n' + 1.35 + '<p>even more text</p>' + 1.36 + '</div>'; 1.37 + content.document.title = "Rule view context menu test"; 1.38 + 1.39 + info("Opening the computed view"); 1.40 + let {toolbox, inspector, view} = yield openRuleView(); 1.41 + 1.42 + info("Selecting the test node"); 1.43 + yield selectNode("div", inspector); 1.44 + 1.45 + yield checkCopySelection(view); 1.46 + yield checkSelectAll(view); 1.47 +}); 1.48 + 1.49 +function checkCopySelection(view) { 1.50 + info("Testing selection copy"); 1.51 + 1.52 + let contentDoc = view.doc; 1.53 + let prop = contentDoc.querySelector(".ruleview-property"); 1.54 + let values = contentDoc.querySelectorAll(".ruleview-propertycontainer"); 1.55 + 1.56 + let range = contentDoc.createRange(); 1.57 + range.setStart(prop, 0); 1.58 + range.setEnd(values[4], 2); 1.59 + let selection = view.doc.defaultView.getSelection().addRange(range); 1.60 + 1.61 + info("Checking that _Copy() returns the correct clipboard value"); 1.62 + 1.63 + let expectedPattern = " margin: 10em;[\\r\\n]+" + 1.64 + " font-size: 14pt;[\\r\\n]+" + 1.65 + " font-family: helvetica,sans-serif;[\\r\\n]+" + 1.66 + " color: #AAA;[\\r\\n]+" + 1.67 + "}[\\r\\n]+" + 1.68 + "html {[\\r\\n]+" + 1.69 + " color: #000;[\\r\\n]*"; 1.70 + 1.71 + return waitForClipboard(() => { 1.72 + fireCopyEvent(prop); 1.73 + }, () => { 1.74 + return checkClipboardData(expectedPattern); 1.75 + }).then(() => {}, () => { 1.76 + failedClipboard(expectedPattern); 1.77 + }); 1.78 +} 1.79 + 1.80 +function checkSelectAll(view) { 1.81 + info("Testing select-all copy"); 1.82 + 1.83 + let contentDoc = view.doc; 1.84 + let prop = contentDoc.querySelector(".ruleview-property"); 1.85 + 1.86 + info("Checking that _SelectAll() then copy returns the correct clipboard value"); 1.87 + view._onSelectAll(); 1.88 + let expectedPattern = "[\\r\\n]+" + 1.89 + "element {[\\r\\n]+" + 1.90 + " margin: 10em;[\\r\\n]+" + 1.91 + " font-size: 14pt;[\\r\\n]+" + 1.92 + " font-family: helvetica,sans-serif;[\\r\\n]+" + 1.93 + " color: #AAA;[\\r\\n]+" + 1.94 + "}[\\r\\n]+" + 1.95 + "html {[\\r\\n]+" + 1.96 + " color: #000;[\\r\\n]+" + 1.97 + "}[\\r\\n]*"; 1.98 + 1.99 + return waitForClipboard(() => { 1.100 + fireCopyEvent(prop); 1.101 + }, () => { 1.102 + return checkClipboardData(expectedPattern); 1.103 + }).then(() => {}, () => { 1.104 + failedClipboard(expectedPattern); 1.105 + }); 1.106 +} 1.107 + 1.108 +function checkClipboardData(expectedPattern) { 1.109 + let actual = SpecialPowers.getClipboardData("text/unicode"); 1.110 + let expectedRegExp = new RegExp(expectedPattern, "g"); 1.111 + return expectedRegExp.test(actual); 1.112 +} 1.113 + 1.114 +function failedClipboard(expectedPattern) { 1.115 + // Format expected text for comparison 1.116 + let terminator = osString == "WINNT" ? "\r\n" : "\n"; 1.117 + expectedPattern = expectedPattern.replace(/\[\\r\\n\][+*]/g, terminator); 1.118 + expectedPattern = expectedPattern.replace(/\\\(/g, "("); 1.119 + expectedPattern = expectedPattern.replace(/\\\)/g, ")"); 1.120 + 1.121 + let actual = SpecialPowers.getClipboardData("text/unicode"); 1.122 + 1.123 + // Trim the right hand side of our strings. This is because expectedPattern 1.124 + // accounts for windows sometimes adding a newline to our copied data. 1.125 + expectedPattern = expectedPattern.trimRight(); 1.126 + actual = actual.trimRight(); 1.127 + 1.128 + dump("TEST-UNEXPECTED-FAIL | Clipboard text does not match expected ... " + 1.129 + "results (escaped for accurate comparison):\n"); 1.130 + info("Actual: " + escape(actual)); 1.131 + info("Expected: " + escape(expectedPattern)); 1.132 +}