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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/styleinspector/test/browser_computedview_select-and-copy-styles.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,118 @@
     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 computed 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,computed view copy test");
    1.18 +
    1.19 +  info("Creating the test document");
    1.20 +  content.document.body.innerHTML = '<style type="text/css"> ' +
    1.21 +    'span { font-variant: small-caps; color: #000000; } ' +
    1.22 +    '.nomatches {color: #ff0000;}</style> <div id="first" style="margin: 10em; ' +
    1.23 +    'font-size: 14pt; font-family: helvetica, sans-serif; color: #AAA">\n' +
    1.24 +    '<h1>Some header text</h1>\n' +
    1.25 +    '<p id="salutation" style="font-size: 12pt">hi.</p>\n' +
    1.26 +    '<p id="body" style="font-size: 12pt">I am a test-case. This text exists ' +
    1.27 +    'solely to provide some things to <span style="color: yellow">' +
    1.28 +    'highlight</span> and <span style="font-weight: bold">count</span> ' +
    1.29 +    'style list-items in the box at right. If you are reading this, ' +
    1.30 +    'you should go do something else instead. Maybe read a book. Or better ' +
    1.31 +    'yet, write some test-cases for another bit of code. ' +
    1.32 +    '<span style="font-style: italic">some text</span></p>\n' +
    1.33 +    '<p id="closing">more text</p>\n' +
    1.34 +    '<p>even more text</p>' +
    1.35 +    '</div>';
    1.36 +  content.document.title = "Computed view context menu test";
    1.37 +
    1.38 +  info("Opening the computed view");
    1.39 +  let {toolbox, inspector, view} = yield openComputedView();
    1.40 +
    1.41 +  info("Selecting the test node");
    1.42 +  yield selectNode("span", inspector);
    1.43 +
    1.44 +  yield checkCopySelection(view);
    1.45 +  yield checkSelectAll(view);
    1.46 +});
    1.47 +
    1.48 +function checkCopySelection(view) {
    1.49 +  info("Testing selection copy");
    1.50 +
    1.51 +  let contentDocument = view.styleDocument;
    1.52 +  let props = contentDocument.querySelectorAll(".property-view");
    1.53 +  ok(props, "captain, we have the property-view nodes");
    1.54 +
    1.55 +  let range = contentDocument.createRange();
    1.56 +  range.setStart(props[1], 0);
    1.57 +  range.setEnd(props[3], 3);
    1.58 +  contentDocument.defaultView.getSelection().addRange(range);
    1.59 +
    1.60 +  info("Checking that cssHtmlTree.siBoundCopy() returns the correct clipboard value");
    1.61 +
    1.62 +  let expectedPattern = "font-family: helvetica,sans-serif;[\\r\\n]+" +
    1.63 +                        "font-size: 16px;[\\r\\n]+" +
    1.64 +                        "font-variant: small-caps;[\\r\\n]*";
    1.65 +
    1.66 +  return waitForClipboard(() => {
    1.67 +    fireCopyEvent(props[0]);
    1.68 +  }, () => {
    1.69 +    return checkClipboardData(expectedPattern);
    1.70 +  }).then(() => {}, () => {
    1.71 +    failedClipboard(expectedPattern);
    1.72 +  });
    1.73 +}
    1.74 +
    1.75 +function checkSelectAll(view) {
    1.76 +  info("Testing select-all copy");
    1.77 +
    1.78 +  let contentDoc = view.styleDocument;
    1.79 +  let prop = contentDoc.querySelector(".property-view");
    1.80 +
    1.81 +  info("Checking that _SelectAll() then copy returns the correct clipboard value");
    1.82 +  view._onSelectAll();
    1.83 +  let expectedPattern = "color: #FF0;[\\r\\n]+" +
    1.84 +                        "font-family: helvetica,sans-serif;[\\r\\n]+" +
    1.85 +                        "font-size: 16px;[\\r\\n]+" +
    1.86 +                        "font-variant: small-caps;[\\r\\n]*";
    1.87 +
    1.88 +  return waitForClipboard(() => {
    1.89 +    fireCopyEvent(prop);
    1.90 +  }, () => {
    1.91 +    return checkClipboardData(expectedPattern);
    1.92 +  }).then(() => {}, () => {
    1.93 +    failedClipboard(expectedPattern);
    1.94 +  });
    1.95 +}
    1.96 +
    1.97 +function checkClipboardData(expectedPattern) {
    1.98 +  let actual = SpecialPowers.getClipboardData("text/unicode");
    1.99 +  let expectedRegExp = new RegExp(expectedPattern, "g");
   1.100 +  return expectedRegExp.test(actual);
   1.101 +}
   1.102 +
   1.103 +function failedClipboard(expectedPattern) {
   1.104 +  // Format expected text for comparison
   1.105 +  let terminator = osString == "WINNT" ? "\r\n" : "\n";
   1.106 +  expectedPattern = expectedPattern.replace(/\[\\r\\n\][+*]/g, terminator);
   1.107 +  expectedPattern = expectedPattern.replace(/\\\(/g, "(");
   1.108 +  expectedPattern = expectedPattern.replace(/\\\)/g, ")");
   1.109 +
   1.110 +  let actual = SpecialPowers.getClipboardData("text/unicode");
   1.111 +
   1.112 +  // Trim the right hand side of our strings. This is because expectedPattern
   1.113 +  // accounts for windows sometimes adding a newline to our copied data.
   1.114 +  expectedPattern = expectedPattern.trimRight();
   1.115 +  actual = actual.trimRight();
   1.116 +
   1.117 +  dump("TEST-UNEXPECTED-FAIL | Clipboard text does not match expected ... " +
   1.118 +    "results (escaped for accurate comparison):\n");
   1.119 +  info("Actual: " + escape(actual));
   1.120 +  info("Expected: " + escape(expectedPattern));
   1.121 +}

mercurial