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.

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

mercurial