Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / clipboard / extension_no_permission / test.js
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.
4
5 // Clipboard permission test for Chrome.
6 // browser_tests.exe --gtest_filter=ClipboardApiTest.ExtensionNoPermission
7
8 // TODO(kalman): Consolidate this test script with the other clipboard tests.
9
10 var pass = chrome.test.callbackPass;
11
12 function testDomCopy() {
13   if (document.execCommand('copy'))
14     chrome.test.succeed();
15   else
16     chrome.test.fail('execCommand("copy") failed');
17 }
18
19 function testDomPaste() {
20   if (document.execCommand('paste'))
21     chrome.test.fail('execCommand("paste") succeeded');
22   else
23     chrome.test.succeed();
24 }
25
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>');
31 }
32
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>');
38 }
39
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();
47   else
48     chrome.test.fail();
49 }
50
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);
60       }));
61     }));
62   }
63
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') {
68         runScript(newTab.id);
69         done();
70       }
71     });
72   }));
73 }
74
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);
82     }));
83   }
84
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') {
89         runScript(newTab.id);
90         done();
91       }
92     });
93   }));
94 }
95
96 function bindTest(test, param) {
97   var result = test.bind(null, param);
98   result.generatedName = test.name;
99   return result;
100 }
101
102 chrome.test.getConfig(function(config) {
103   var baseUrl = 'http://localhost:' + config.testServer.port + '/extensions';
104   chrome.test.runTests([
105     testDomCopy,
106     testDomPaste,
107     testCopyInIframe,
108     testPasteInIframe,
109     bindTest(testExecuteScriptCopyPaste, baseUrl),
110     bindTest(testContentScriptCopyPaste, baseUrl)
111   ]);
112 });