Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / platform_apps / web_view / context_menus / basic / embedder.js
1 // Copyright 2014 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 var LOG = function(msg) { window.console.log(msg); };
6
7 function ContextMenuTester() {
8   this.webview_ = null;
9   this.id_ = '';
10
11   this.inlineClickCalled_ = false;
12   this.globalClickCalled_ = false;
13
14   // Used for createThreeMenuItems().
15   this.numItemsCreated_ = 0;
16
17   this.failed_ = false;
18 }
19
20 ContextMenuTester.prototype.setWebview = function(webview) {
21   this.webview_ = webview;
22 };
23
24 ContextMenuTester.prototype.testProperties = function() {
25   LOG('testProperties');
26   if (!this.webview_) {
27     this.fail_();
28     return;
29   }
30
31   var w = this.webview_;
32   this.assertTrue_(!!w.contextMenus);
33   this.assertEq_('function', typeof w.contextMenus.create);
34   this.assertEq_('function', typeof w.contextMenus.update);
35   this.assertEq_('function', typeof w.contextMenus.remove);
36   this.assertEq_('function', typeof w.contextMenus.removeAll);
37
38   var onClicked = w.contextMenus.onClicked;
39   this.assertTrue_(!!onClicked);
40   this.assertEq_('function', typeof onClicked.addListener);
41   this.assertEq_('function', typeof onClicked.hasListener);
42   this.assertEq_('function', typeof onClicked.hasListeners);
43   this.assertEq_('function', typeof onClicked.removeListener);
44
45   if (!this.failed_) {
46     this.proceedTest_('ITEM_CHECKED');
47   }
48 };
49
50 ContextMenuTester.prototype.testCreateMenuItem = function() {
51   LOG('testCreateMenuItem');
52   if (!this.webview_) {
53     this.fail_();
54     return;
55   }
56
57   var self = this;
58   this.id_ = this.webview_.contextMenus.create({
59     'title': 'initial-title',
60     'onclick': function() { self.onClick_('inline'); }
61   }, function createdCallback() {
62     LOG('ITEM_CREATED');
63     self.proceedTest_('ITEM_CREATED');
64   });
65   this.webview_.contextMenus.onClicked.addListener(function() {
66     self.onClick_('global');
67   });
68 };
69
70 ContextMenuTester.prototype.testUpdateMenuItem = function() {
71   LOG('testUpdateMenuItem');
72   if (!this.id_) {
73     this.fail_();
74     return;
75   }
76
77   var self = this;
78   this.webview_.contextMenus.update(this.id_,
79     {'title': 'new_title'},
80     function() { self.proceedTest_('ITEM_UPDATED'); });
81 };
82
83 ContextMenuTester.prototype.testRemoveItem = function() {
84   LOG('testRemoveItem');
85   var self = this;
86   this.webview_.contextMenus.remove(
87       this.id_,
88       function() { self.proceedTest_('ITEM_REMOVED'); });
89 };
90
91 ContextMenuTester.prototype.createThreeMenuItems = function() {
92   LOG('createThreeMenuItems');
93   var self = this;
94   var createdCallback = function() { self.onCreate_(); };
95   this.webview_.contextMenus.create({'title': 'a'}, createdCallback);
96   this.webview_.contextMenus.create({'title': 'b'}, createdCallback);
97   this.webview_.contextMenus.create({'title': 'c'}, createdCallback);
98 };
99
100 ContextMenuTester.prototype.testRemoveAllItems = function() {
101   LOG('testRemoveAllItems');
102   var self = this;
103   this.webview_.contextMenus.removeAll(function() {
104     self.proceedTest_('ITEM_ALL_REMOVED');
105   });
106 };
107
108 ContextMenuTester.prototype.onClick_ = function(type) {
109   if (type == 'global') {
110     this.globalClickCalled_ = true;
111   } else if (type == 'inline') {
112     this.inlineClickCalled_ = true;
113   }
114   if (this.inlineClickCalled_ && this.globalClickCalled_) {
115     this.proceedTest_('ITEM_CLICKED');
116   }
117 };
118
119 ContextMenuTester.prototype.onCreate_ = function() {
120   ++this.numItemsCreated_;
121   if (this.numItemsCreated_ == 3) {
122     this.proceedTest_('ITEM_MULTIPLE_CREATED');
123   }
124 };
125
126 ContextMenuTester.prototype.proceedTest_ = function(step) {
127   switch (step) {
128     case 'ITEM_CHECKED':
129       document.title = 'ITEM_CHECKED';
130       break;
131     case 'ITEM_CREATED':
132       document.title = 'ITEM_CREATED';
133       break;
134     case 'ITEM_CLICKED':
135       chrome.test.sendMessage('ITEM_CLICKED');
136       break;
137     case 'ITEM_UPDATED':
138       document.title = 'ITEM_UPDATED';
139       break;
140     case 'ITEM_REMOVED':
141       document.title = 'ITEM_REMOVED';
142       break;
143     case 'ITEM_MULTIPLE_CREATED':
144       document.title = 'ITEM_MULTIPLE_CREATED';
145       break;
146     case 'ITEM_ALL_REMOVED':
147       document.title = 'ITEM_ALL_REMOVED';
148       break;
149     default:
150       break;
151   }
152 };
153
154 ContextMenuTester.prototype.fail_ = function() {
155   this.failed_ = true;
156   document.title = 'error';
157 };
158
159 ContextMenuTester.prototype.assertEq_ = function(e, a) {
160   if (e != a) {
161     this.fail_();
162   }
163 };
164
165 ContextMenuTester.prototype.assertTrue_ = function(c) {
166   if (!c) {
167     this.fail_();
168   }
169 };
170
171 var tester = new ContextMenuTester();
172
173 // window.* exported functions begin.
174 window.checkProperties = function() {
175   tester.testProperties();
176 };
177 window.createMenuItem = function() {
178   tester.testCreateMenuItem();
179 };
180 window.updateMenuItem = function() {
181   tester.testUpdateMenuItem();
182 };
183 window.removeItem = function() {
184   tester.testRemoveItem();
185 };
186 window.createThreeMenuItems = function() {
187   tester.createThreeMenuItems();
188 };
189 window.removeAllItems = function() {
190   LOG('window.testRemoveAllItems');
191   tester.testRemoveAllItems();
192 };
193 // window.* exported functions end.
194
195 function setUpTest(messageCallback) {
196   var guestUrl = 'data:text/html,<html><body>guest</body></html>';
197   var webview = document.createElement('webview');
198
199   var onLoadStop = function(e) {
200     LOG('webview has loaded.');
201     webview.executeScript(
202       {file: 'guest.js'},
203       function(results) {
204         if (!results || !results.length) {
205           chrome.test.sendMessage('WebViewTest.FAILURE');
206           return;
207         }
208         LOG('Script has been injected into webview.');
209         // Establish a communication channel with the guest.
210         var msg = ['connect'];
211         webview.contentWindow.postMessage(JSON.stringify(msg), '*');
212       });
213   };
214   webview.addEventListener('loadstop', onLoadStop);
215
216   window.addEventListener('message', function(e) {
217     var data = JSON.parse(e.data);
218     if (data[0] == 'connected') {
219       console.log('A communication channel has been established with webview.');
220     }
221     messageCallback(webview);
222   });
223
224   webview.setAttribute('src', guestUrl);
225   document.body.appendChild(webview);
226 }
227
228 onload = function() {
229   chrome.test.getConfig(function(config) {
230     setUpTest(function(webview) {
231       LOG('Guest load completed.');
232       chrome.test.sendMessage('WebViewTest.LAUNCHED');
233       tester.setWebview(webview);
234     });
235   });
236 };