1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Clipboard permission test for Chrome.
6 // browser_tests.exe --gtest_filter=ClipboardApiTest.ExtensionNoPermission
8 // TODO(kalman): Consolidate this test script with the other clipboard tests.
10 var pass
= chrome
.test
.callbackPass
;
12 function testDomCopy() {
13 if (document
.execCommand('copy'))
14 chrome
.test
.succeed();
16 chrome
.test
.fail('execCommand("copy") failed');
19 function testDomPaste() {
20 if (document
.execCommand('paste'))
21 chrome
.test
.fail('execCommand("paste") succeeded');
23 chrome
.test
.succeed();
26 function testCopyInIframe() {
27 var ifr
= document
.createElement('iframe');
28 document
.body
.appendChild(ifr
);
29 window
.command
= 'copy';
30 ifr
.contentDocument
.write('<script src="iframe.js"></script>');
33 function testPasteInIframe() {
34 var ifr
= document
.createElement('iframe');
35 document
.body
.appendChild(ifr
);
36 window
.command
= 'paste';
37 ifr
.contentDocument
.write('<script src="iframe.js"></script>');
40 function testDone(result
) {
41 // 'copy' should always succeed regardless of the clipboardWrite permission,
42 // for backwards compatibility. 'paste' should always fail because the
43 // extension doesn't have clipboardRead.
44 var expected
= window
.command
=== 'copy';
45 if (result
=== expected
)
46 chrome
.test
.succeed();
51 function testExecuteScriptCopyPaste(baseUrl
) {
52 var tabUrl
= baseUrl
+ '/test_file.html';
53 function runScript(tabId
) {
54 chrome
.tabs
.executeScript(tabId
, {file
: 'content_script.js'},
55 chrome
.test
.callbackPass(function() {
56 chrome
.tabs
.sendMessage(tabId
, "run",
57 chrome
.test
.callbackPass(function(result
) {
58 chrome
.tabs
.remove(tabId
);
59 chrome
.test
.assertEq('', result
);
64 chrome
.tabs
.create({url
: tabUrl
}, pass(function(newTab
) {
65 var done
= chrome
.test
.listenForever(chrome
.tabs
.onUpdated
,
66 function(_
, info
, updatedTab
) {
67 if (updatedTab
.id
== newTab
.id
&& info
.status
== 'complete') {
75 function testContentScriptCopyPaste(baseUrl
) {
76 var tabUrl
= baseUrl
+ '/test_file_with_body.html';
77 function runScript(tabId
) {
78 chrome
.tabs
.sendMessage(tabId
, "run",
79 chrome
.test
.callbackPass(function(result
) {
80 chrome
.tabs
.remove(tabId
);
81 chrome
.test
.assertEq('', result
);
85 chrome
.tabs
.create({url
: tabUrl
}, chrome
.test
.callbackPass(function(newTab
) {
86 var done
= chrome
.test
.listenForever(chrome
.tabs
.onUpdated
,
87 function(_
, info
, updatedTab
) {
88 if (updatedTab
.id
== newTab
.id
&& info
.status
== 'complete') {
96 function bindTest(test
, param
) {
97 var result
= test
.bind(null, param
);
98 result
.generatedName
= test
.name
;
102 chrome
.test
.getConfig(function(config
) {
103 var baseUrl
= 'http://localhost:' + config
.testServer
.port
+ '/extensions';
104 chrome
.test
.runTests([
109 bindTest(testExecuteScriptCopyPaste
, baseUrl
),
110 bindTest(testContentScriptCopyPaste
, baseUrl
)