summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_copyimage.html
blob: a2610e360fd4ebfc978525e752f7f2c5d8d5566e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=518249
https://bugzilla.mozilla.org/show_bug.cgi?id=952456
-->
<head>
  <title>Test for copy image</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=518249">Mozilla Bug 518249</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=952456">Mozilla Bug 952456</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">

function testCopyImage () {
  var Ci = SpecialPowers.Ci;
  var Cc = SpecialPowers.Cc;
  var clipboard = SpecialPowers.Services.clipboard;

  function getClipboardData(mime) {
    var transferable = Cc['@mozilla.org/widget/transferable;1']
                       .createInstance(Ci.nsITransferable);
    var loadingContext = SpecialPowers.wrap(window).QueryInterface(Ci.nsIInterfaceRequestor)
                                      .getInterface(Ci.nsIWebNavigation)
                                      .QueryInterface(Ci.nsILoadContext);
    transferable.init(loadingContext);
    transferable.addDataFlavor(mime);
    clipboard.getData(transferable, 1);
    var data = SpecialPowers.createBlankObject();
    transferable.getTransferData(mime, data, {});
    return data;
  }

  function testClipboardValue(mime, expected) {
    var data = SpecialPowers.wrap(getClipboardData(mime));
    var str = data.value == null ? data.value :
              data.value.QueryInterface(Ci.nsISupportsString).data;
    is(str, expected, "clipboard has correct [" + mime + "] content")
  }

  //--------- Prepare data and copy it.

  // Select the node.
  var node = document.getElementById('logo');

  // Set node and copy image.
  var webnav = SpecialPowers.wrap(window)
               .QueryInterface(Ci.nsIInterfaceRequestor)
               .getInterface(Ci.nsIWebNavigation)
  var docShell = webnav.QueryInterface(Ci.nsIDocShell);
  var documentViewer = docShell.contentViewer
                               .QueryInterface(Ci.nsIContentViewerEdit);
  documentViewer.setCommandNode(node);
  documentViewer.copyImage(documentViewer.COPY_IMAGE_ALL);

  //--------- Let's check the content of the clipboard now.

  // Does the clipboard contain text/unicode data ?
  ok(clipboard.hasDataMatchingFlavors(["text/unicode"], 1, clipboard.kGlobalClipboard), "clipboard contains unicode text");
  // Does the clipboard contain text/html data ?
  ok(clipboard.hasDataMatchingFlavors(["text/html"], 1, clipboard.kGlobalClipboard), "clipboard contains html text");
  // Does the clipboard contain image data ?
  ok(clipboard.hasDataMatchingFlavors(["image/png"], 1, clipboard.kGlobalClipboard), "clipboard contains image");

  // Is the text/uncodie data correct ?
  testClipboardValue('text/unicode', 'about:logo');
  // Is the text/html data correct ?
  var expected = '<img id="logo" src="about:logo">';
  if (navigator.platform.indexOf("Win") >= 0) {
    expected = "<html><body>\n<!--StartFragment-->" + expected + "<!--EndFragment-->\n</body>\n</html>";
  }
  testClipboardValue('text/html', expected);

  SimpleTest.finish();
}

SimpleTest.waitForExplicitFinish();
addLoadEvent(function() {
  SpecialPowers.pushPrefEnv({"set": [["clipboard.plainTextOnly", false]]}, testCopyImage);
});

</script>
</pre>
<div>
  <img id="logo" src="about:logo">
</div>
</body>
</html>