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.
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 rule 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,<p>rule view context menu test</p>"); |
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 | 'html { color: #000000; } ' + |
michael@0 | 19 | 'span { font-variant: small-caps; color: #000000; } ' + |
michael@0 | 20 | '.nomatches {color: #ff0000;}</style> <div id="first" style="margin: 10em; ' + |
michael@0 | 21 | 'font-size: 14pt; font-family: helvetica, sans-serif; color: #AAA">\n' + |
michael@0 | 22 | '<h1>Some header text</h1>\n' + |
michael@0 | 23 | '<p id="salutation" style="font-size: 12pt">hi.</p>\n' + |
michael@0 | 24 | '<p id="body" style="font-size: 12pt">I am a test-case. This text exists ' + |
michael@0 | 25 | 'solely to provide some things to <span style="color: yellow">' + |
michael@0 | 26 | 'highlight</span> and <span style="font-weight: bold">count</span> ' + |
michael@0 | 27 | 'style list-items in the box at right. If you are reading this, ' + |
michael@0 | 28 | 'you should go do something else instead. Maybe read a book. Or better ' + |
michael@0 | 29 | 'yet, write some test-cases for another bit of code. ' + |
michael@0 | 30 | '<span style="font-style: italic">some text</span></p>\n' + |
michael@0 | 31 | '<p id="closing">more text</p>\n' + |
michael@0 | 32 | '<p>even more text</p>' + |
michael@0 | 33 | '</div>'; |
michael@0 | 34 | content.document.title = "Rule view context menu test"; |
michael@0 | 35 | |
michael@0 | 36 | info("Opening the computed view"); |
michael@0 | 37 | let {toolbox, inspector, view} = yield openRuleView(); |
michael@0 | 38 | |
michael@0 | 39 | info("Selecting the test node"); |
michael@0 | 40 | yield selectNode("div", inspector); |
michael@0 | 41 | |
michael@0 | 42 | yield checkCopySelection(view); |
michael@0 | 43 | yield checkSelectAll(view); |
michael@0 | 44 | }); |
michael@0 | 45 | |
michael@0 | 46 | function checkCopySelection(view) { |
michael@0 | 47 | info("Testing selection copy"); |
michael@0 | 48 | |
michael@0 | 49 | let contentDoc = view.doc; |
michael@0 | 50 | let prop = contentDoc.querySelector(".ruleview-property"); |
michael@0 | 51 | let values = contentDoc.querySelectorAll(".ruleview-propertycontainer"); |
michael@0 | 52 | |
michael@0 | 53 | let range = contentDoc.createRange(); |
michael@0 | 54 | range.setStart(prop, 0); |
michael@0 | 55 | range.setEnd(values[4], 2); |
michael@0 | 56 | let selection = view.doc.defaultView.getSelection().addRange(range); |
michael@0 | 57 | |
michael@0 | 58 | info("Checking that _Copy() returns the correct clipboard value"); |
michael@0 | 59 | |
michael@0 | 60 | let expectedPattern = " margin: 10em;[\\r\\n]+" + |
michael@0 | 61 | " font-size: 14pt;[\\r\\n]+" + |
michael@0 | 62 | " font-family: helvetica,sans-serif;[\\r\\n]+" + |
michael@0 | 63 | " color: #AAA;[\\r\\n]+" + |
michael@0 | 64 | "}[\\r\\n]+" + |
michael@0 | 65 | "html {[\\r\\n]+" + |
michael@0 | 66 | " color: #000;[\\r\\n]*"; |
michael@0 | 67 | |
michael@0 | 68 | return waitForClipboard(() => { |
michael@0 | 69 | fireCopyEvent(prop); |
michael@0 | 70 | }, () => { |
michael@0 | 71 | return checkClipboardData(expectedPattern); |
michael@0 | 72 | }).then(() => {}, () => { |
michael@0 | 73 | failedClipboard(expectedPattern); |
michael@0 | 74 | }); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | function checkSelectAll(view) { |
michael@0 | 78 | info("Testing select-all copy"); |
michael@0 | 79 | |
michael@0 | 80 | let contentDoc = view.doc; |
michael@0 | 81 | let prop = contentDoc.querySelector(".ruleview-property"); |
michael@0 | 82 | |
michael@0 | 83 | info("Checking that _SelectAll() then copy returns the correct clipboard value"); |
michael@0 | 84 | view._onSelectAll(); |
michael@0 | 85 | let expectedPattern = "[\\r\\n]+" + |
michael@0 | 86 | "element {[\\r\\n]+" + |
michael@0 | 87 | " margin: 10em;[\\r\\n]+" + |
michael@0 | 88 | " font-size: 14pt;[\\r\\n]+" + |
michael@0 | 89 | " font-family: helvetica,sans-serif;[\\r\\n]+" + |
michael@0 | 90 | " color: #AAA;[\\r\\n]+" + |
michael@0 | 91 | "}[\\r\\n]+" + |
michael@0 | 92 | "html {[\\r\\n]+" + |
michael@0 | 93 | " color: #000;[\\r\\n]+" + |
michael@0 | 94 | "}[\\r\\n]*"; |
michael@0 | 95 | |
michael@0 | 96 | return waitForClipboard(() => { |
michael@0 | 97 | fireCopyEvent(prop); |
michael@0 | 98 | }, () => { |
michael@0 | 99 | return checkClipboardData(expectedPattern); |
michael@0 | 100 | }).then(() => {}, () => { |
michael@0 | 101 | failedClipboard(expectedPattern); |
michael@0 | 102 | }); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | function checkClipboardData(expectedPattern) { |
michael@0 | 106 | let actual = SpecialPowers.getClipboardData("text/unicode"); |
michael@0 | 107 | let expectedRegExp = new RegExp(expectedPattern, "g"); |
michael@0 | 108 | return expectedRegExp.test(actual); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | function failedClipboard(expectedPattern) { |
michael@0 | 112 | // Format expected text for comparison |
michael@0 | 113 | let terminator = osString == "WINNT" ? "\r\n" : "\n"; |
michael@0 | 114 | expectedPattern = expectedPattern.replace(/\[\\r\\n\][+*]/g, terminator); |
michael@0 | 115 | expectedPattern = expectedPattern.replace(/\\\(/g, "("); |
michael@0 | 116 | expectedPattern = expectedPattern.replace(/\\\)/g, ")"); |
michael@0 | 117 | |
michael@0 | 118 | let actual = SpecialPowers.getClipboardData("text/unicode"); |
michael@0 | 119 | |
michael@0 | 120 | // Trim the right hand side of our strings. This is because expectedPattern |
michael@0 | 121 | // accounts for windows sometimes adding a newline to our copied data. |
michael@0 | 122 | expectedPattern = expectedPattern.trimRight(); |
michael@0 | 123 | actual = actual.trimRight(); |
michael@0 | 124 | |
michael@0 | 125 | dump("TEST-UNEXPECTED-FAIL | Clipboard text does not match expected ... " + |
michael@0 | 126 | "results (escaped for accurate comparison):\n"); |
michael@0 | 127 | info("Actual: " + escape(actual)); |
michael@0 | 128 | info("Expected: " + escape(expectedPattern)); |
michael@0 | 129 | } |