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.
5 var LOG = function(msg) { window.console.log(msg); };
7 function ContextMenuTester() {
11 this.inlineClickCalled_ = false;
12 this.globalClickCalled_ = false;
14 // Used for createThreeMenuItems().
15 this.numItemsCreated_ = 0;
20 ContextMenuTester.prototype.setWebview = function(webview) {
21 this.webview_ = webview;
24 ContextMenuTester.prototype.testProperties = function() {
25 LOG('testProperties');
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);
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);
46 this.proceedTest_('ITEM_CHECKED');
50 ContextMenuTester.prototype.testCreateMenuItem = function() {
51 LOG('testCreateMenuItem');
58 this.id_ = this.webview_.contextMenus.create({
59 'title': 'initial-title',
60 'onclick': function() { self.onClick_('inline'); }
61 }, function createdCallback() {
63 self.proceedTest_('ITEM_CREATED');
65 this.webview_.contextMenus.onClicked.addListener(function() {
66 self.onClick_('global');
70 ContextMenuTester.prototype.testUpdateMenuItem = function() {
71 LOG('testUpdateMenuItem');
78 this.webview_.contextMenus.update(this.id_,
79 {'title': 'new_title'},
80 function() { self.proceedTest_('ITEM_UPDATED'); });
83 ContextMenuTester.prototype.testRemoveItem = function() {
84 LOG('testRemoveItem');
86 this.webview_.contextMenus.remove(
88 function() { self.proceedTest_('ITEM_REMOVED'); });
91 ContextMenuTester.prototype.createThreeMenuItems = function() {
92 LOG('createThreeMenuItems');
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);
100 ContextMenuTester.prototype.testRemoveAllItems = function() {
101 LOG('testRemoveAllItems');
103 this.webview_.contextMenus.removeAll(function() {
104 self.proceedTest_('ITEM_ALL_REMOVED');
108 ContextMenuTester.prototype.onClick_ = function(type) {
109 if (type == 'global') {
110 this.globalClickCalled_ = true;
111 } else if (type == 'inline') {
112 this.inlineClickCalled_ = true;
114 if (this.inlineClickCalled_ && this.globalClickCalled_) {
115 this.proceedTest_('ITEM_CLICKED');
119 ContextMenuTester.prototype.onCreate_ = function() {
120 ++this.numItemsCreated_;
121 if (this.numItemsCreated_ == 3) {
122 this.proceedTest_('ITEM_MULTIPLE_CREATED');
126 ContextMenuTester.prototype.proceedTest_ = function(step) {
129 document.title = 'ITEM_CHECKED';
132 document.title = 'ITEM_CREATED';
135 chrome.test.sendMessage('ITEM_CLICKED');
138 document.title = 'ITEM_UPDATED';
141 document.title = 'ITEM_REMOVED';
143 case 'ITEM_MULTIPLE_CREATED':
144 document.title = 'ITEM_MULTIPLE_CREATED';
146 case 'ITEM_ALL_REMOVED':
147 document.title = 'ITEM_ALL_REMOVED';
154 ContextMenuTester.prototype.fail_ = function() {
156 document.title = 'error';
159 ContextMenuTester.prototype.assertEq_ = function(e, a) {
165 ContextMenuTester.prototype.assertTrue_ = function(c) {
171 var tester = new ContextMenuTester();
173 // window.* exported functions begin.
174 window.checkProperties = function() {
175 tester.testProperties();
177 window.createMenuItem = function() {
178 tester.testCreateMenuItem();
180 window.updateMenuItem = function() {
181 tester.testUpdateMenuItem();
183 window.removeItem = function() {
184 tester.testRemoveItem();
186 window.createThreeMenuItems = function() {
187 tester.createThreeMenuItems();
189 window.removeAllItems = function() {
190 LOG('window.testRemoveAllItems');
191 tester.testRemoveAllItems();
193 // window.* exported functions end.
195 function setUpTest(messageCallback) {
196 var guestUrl = 'data:text/html,<html><body>guest</body></html>';
197 var webview = document.createElement('webview');
199 var onLoadStop = function(e) {
200 LOG('webview has loaded.');
201 webview.executeScript(
204 if (!results || !results.length) {
205 chrome.test.sendMessage('WebViewTest.FAILURE');
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), '*');
214 webview.addEventListener('loadstop', onLoadStop);
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.');
221 messageCallback(webview);
224 webview.setAttribute('src', guestUrl);
225 document.body.appendChild(webview);
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);