OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Clipboard permission test for Chrome. | 5 // Clipboard permission test for Chrome. |
6 // browser_tests.exe --gtest_filter=ClipboardApiTest.Extension | 6 // browser_tests.exe --gtest_filter=ClipboardApiTest.Extension |
7 | 7 |
8 chrome.test.runTests([ | 8 // TODO(kalman): Consolidate this test script with the other clipboard tests. |
9 function domCopy() { | |
10 if (document.execCommand('copy')) | |
11 chrome.test.succeed(); | |
12 else | |
13 chrome.test.fail('execCommand("copy") failed'); | |
14 }, | |
15 function domPaste() { | |
16 if (document.execCommand('paste')) | |
17 chrome.test.succeed(); | |
18 else | |
19 chrome.test.fail('execCommand("paste") failed'); | |
20 }, | |
21 function copyInIframe() { | |
22 var ifr = document.createElement('iframe'); | |
23 document.body.appendChild(ifr); | |
24 window.command = 'copy'; | |
25 ifr.contentDocument.write('<script src="iframe.js"></script>'); | |
26 }, | |
27 function pasteInIframe() { | |
28 var ifr = document.createElement('iframe'); | |
29 document.body.appendChild(ifr); | |
30 window.command = 'paste'; | |
31 ifr.contentDocument.write('<script src="iframe.js"></script>'); | |
32 } | |
33 ]); | |
34 | 9 |
| 10 function testDomCopy() { |
| 11 if (document.execCommand('copy')) |
| 12 chrome.test.succeed(); |
| 13 else |
| 14 chrome.test.fail('execCommand("copy") failed'); |
| 15 } |
| 16 |
| 17 function testDomPaste() { |
| 18 if (document.execCommand('paste')) |
| 19 chrome.test.succeed(); |
| 20 else |
| 21 chrome.test.fail('execCommand("paste") failed'); |
| 22 } |
| 23 |
| 24 function testCopyInIframe() { |
| 25 var ifr = document.createElement('iframe'); |
| 26 document.body.appendChild(ifr); |
| 27 window.command = 'copy'; |
| 28 ifr.contentDocument.write('<script src="iframe.js"></script>'); |
| 29 } |
| 30 |
| 31 function testPasteInIframe() { |
| 32 var ifr = document.createElement('iframe'); |
| 33 document.body.appendChild(ifr); |
| 34 window.command = 'paste'; |
| 35 ifr.contentDocument.write('<script src="iframe.js"></script>'); |
| 36 } |
35 | 37 |
36 function testDone(result) { | 38 function testDone(result) { |
37 if (result) | 39 if (result) |
38 chrome.test.succeed(); | 40 chrome.test.succeed(); |
39 else | 41 else |
40 chrome.test.fail(); | 42 chrome.test.fail(); |
41 } | 43 } |
| 44 |
| 45 function testExecuteScriptCopyPaste(baseUrl) { |
| 46 var tabUrl = baseUrl + '/test_file.html'; |
| 47 function runScript(tabId) { |
| 48 chrome.tabs.executeScript(tabId, {file: 'content_script.js'}, |
| 49 chrome.test.callbackPass(function() { |
| 50 chrome.tabs.sendMessage(tabId, "run", |
| 51 chrome.test.callbackPass(function(result) { |
| 52 chrome.tabs.remove(tabId); |
| 53 chrome.test.assertEq('', result); |
| 54 })); |
| 55 })); |
| 56 } |
| 57 |
| 58 chrome.tabs.create({url: tabUrl}, chrome.test.callbackPass(function(newTab) { |
| 59 var done = chrome.test.listenForever(chrome.tabs.onUpdated, |
| 60 function(_, info, updatedTab) { |
| 61 if (updatedTab.id == newTab.id && info.status == 'complete') { |
| 62 runScript(newTab.id); |
| 63 done(); |
| 64 } |
| 65 }); |
| 66 })); |
| 67 } |
| 68 |
| 69 function testContentScriptCopyPaste(baseUrl) { |
| 70 var tabUrl = baseUrl + '/test_file_with_body.html'; |
| 71 function runScript(tabId) { |
| 72 chrome.tabs.sendMessage(tabId, "run", |
| 73 chrome.test.callbackPass(function(result) { |
| 74 chrome.tabs.remove(tabId); |
| 75 chrome.test.assertEq('', result); |
| 76 })); |
| 77 } |
| 78 |
| 79 chrome.tabs.create({url: tabUrl}, chrome.test.callbackPass(function(newTab) { |
| 80 var done = chrome.test.listenForever(chrome.tabs.onUpdated, |
| 81 function(_, info, updatedTab) { |
| 82 if (updatedTab.id == newTab.id && info.status == 'complete') { |
| 83 runScript(newTab.id); |
| 84 done(); |
| 85 } |
| 86 }); |
| 87 })); |
| 88 } |
| 89 |
| 90 function bindTest(test, param) { |
| 91 var result = test.bind(null, param); |
| 92 result.generatedName = test.name; |
| 93 return result; |
| 94 } |
| 95 |
| 96 chrome.test.getConfig(function(config) { |
| 97 var baseUrl = 'http://localhost:' + config.testServer.port + '/extensions'; |
| 98 chrome.test.runTests([ |
| 99 testDomCopy, |
| 100 testDomPaste, |
| 101 testCopyInIframe, |
| 102 testPasteInIframe, |
| 103 bindTest(testExecuteScriptCopyPaste, baseUrl), |
| 104 bindTest(testContentScriptCopyPaste, baseUrl) |
| 105 ]); |
| 106 }) |
OLD | NEW |