Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / activity_log_private / test / test.js
1 // Copyright 2013 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 // The extension ID for the .../activity_log_private/friend extension, which
6 // this extension communicates with.  This should correspond to the public key
7 // defined in .../activity_log_private/friend/manifest.json.
8 var FRIEND_EXTENSION_ID = 'pknkgggnfecklokoggaggchhaebkajji';
9
10 // Setup the test cases.
11 var testCases = [];
12 testCases.push({
13   func: function triggerApiCall() {
14     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
15                                'api_call', function response() { });
16   },
17   expected_activity: ['cookies.set']
18 });
19 testCases.push({
20   func: function triggerSpecialCall() {
21     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
22                                'special_call', function response() { });
23   },
24   expected_activity: [
25     'extension.getURL',
26     'extension.getViews'
27   ]
28 });
29 testCases.push({
30   func: function triggerDouble() {
31     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
32                                'double', function response() {});
33   },
34   expected_activity: ['omnibox.setDefaultSuggestion']
35 });
36 testCases.push({
37   func: function triggerAppBindings() {
38     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
39                                'app_bindings', function response() { });
40   },
41   expected_activity: [
42     'app.GetDetails',
43     'app.GetIsInstalled',
44     'app.getInstallState'
45   ]
46 });
47 testCases.push({
48   func: function triggerObjectMethods() {
49     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
50                                'object_methods', function response() { });
51   },
52   expected_activity: ['storage.clear']
53 });
54 testCases.push({
55   func: function triggerMessageSelf() {
56     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
57                                'message_self', function response() { });
58   },
59   expected_activity: [
60     'runtime.connect',
61     'runtime.sendMessage'
62   ]
63 });
64 testCases.push({
65   func: function triggerMessageOther() {
66     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
67                                'message_other', function response() { });
68   },
69   expected_activity: [
70     'runtime.connect',
71     'runtime.sendMessage'
72   ]
73 });
74 testCases.push({
75   func: function triggerConnectOther() {
76     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
77                                'connect_other', function response() { });
78   },
79   expected_activity: ['runtime.connect']
80 });
81 testCases.push({
82   func: function triggerBackgroundXHR() {
83     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
84                                'background_xhr', function response() { });
85   },
86   expected_activity: [
87     'XMLHttpRequest.open',
88     'XMLHttpRequest.setRequestHeader'
89   ]
90 });
91 testCases.push({
92   func: function triggerTabIds() {
93     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
94                                'tab_ids', function response() { });
95   },
96   expected_activity: [
97     'tabs.onUpdated',
98     'tabs.onUpdated',
99     'tabs.executeScript',
100     'tabs.onUpdated',
101     'tabs.onUpdated',
102     'tabs.move',
103     'tabs.remove'
104   ]
105 });
106 testCases.push({
107   func: function triggerTabIdsIncognito() {
108     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
109                                'tab_ids_incognito', function response() { });
110   },
111   is_incognito: true,
112   expected_activity: [
113     'windows.create',
114     'tabs.onUpdated',
115     'tabs.onUpdated',
116     'tabs.executeScript',
117     'windows.create',
118     'tabs.onUpdated',
119     'tabs.onUpdated',
120     'tabs.move',
121     'tabs.remove'
122   ]
123 });
124
125 testCases.push({
126   func: function triggerWebRequest() {
127     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
128                                'webrequest', function response() { });
129   },
130   expected_activity: [
131     'webRequestInternal.addEventListener',
132     'webRequestInternal.addEventListener',
133     'webRequest.onBeforeSendHeaders/1',
134     'webRequestInternal.eventHandled',
135     'webRequest.onBeforeSendHeaders',
136     'webRequest.onHeadersReceived/2',
137     'webRequestInternal.eventHandled',
138     'webRequest.onHeadersReceived',
139     'tabs.onUpdated',
140     'tabs.onUpdated',
141     'tabs.remove'
142   ],
143 });
144
145 testCases.push({
146   func: function triggerWebRequestIncognito() {
147     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
148                                'webrequest_incognito', function response() { });
149   },
150   is_incognito: true,
151   expected_activity: [
152     'webRequestInternal.addEventListener',
153     'webRequestInternal.addEventListener',
154     'windows.create',
155     'webRequest.onBeforeSendHeaders/3',
156     'webRequestInternal.eventHandled',
157     'webRequest.onBeforeSendHeaders',
158     'webRequest.onHeadersReceived/4',
159     'webRequestInternal.eventHandled',
160     'webRequest.onHeadersReceived',
161     'tabs.onUpdated',
162     'tabs.onUpdated',
163     'tabs.remove'
164   ],
165 });
166
167 testCases.push({
168   func: function triggerApiCallsOnTabsUpdated() {
169     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
170                                'api_tab_updated', function response() { });
171   },
172   expected_activity: [
173     'tabs.onUpdated',
174     'tabs.onUpdated',
175     'tabs.connect',
176     'tabs.sendMessage',
177     'tabs.executeScript',
178     'tabs.executeScript',
179     'HTMLDocument.write',
180     'tabs.remove'
181   ]
182 });
183 testCases.push({
184   func: function triggerApiCallsOnTabsUpdatedIncognito() {
185     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
186                                'api_tab_updated_incognito',
187                                function response() { });
188   },
189   is_incognito: true,
190   expected_activity: [
191     'windows.create',
192     'tabs.onUpdated',
193     'tabs.onUpdated',
194     'tabs.connect',
195     'tabs.sendMessage',
196     'tabs.executeScript',
197     'tabs.executeScript',
198     'HTMLDocument.write',
199     'tabs.remove'
200   ]
201 });
202 testCases.push({
203   func: function triggerFullscreen() {
204     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
205                                'launch_dom_fullscreen',
206                                function response() { });
207   },
208   expected_activity: [
209     'extension.getURL',
210     'test.getConfig',
211     'Element.webkitRequestFullscreen'
212   ]
213 });
214
215 var domExpectedActivity = [
216     'tabs.onUpdated',
217     'tabs.onUpdated',
218     'tabs.executeScript',
219      // Location access
220     'Window.location',
221     'Document.location',
222     'Window.location',
223     'Location.assign',
224     'Location.replace',
225      // Dom mutations
226     'Document.createElement',
227     'Document.createElement',
228     'Node.appendChild',
229     'Node.insertBefore',
230     'Node.replaceChild',
231     //'Document.location',
232     'HTMLDocument.write',
233     'HTMLDocument.writeln',
234     'Element.innerHTML',
235     // Navigator access
236     'Window.navigator',
237     'Geolocation.getCurrentPosition',
238     'Geolocation.watchPosition',
239     // Web store access - session storage
240     'Window.sessionStorage',
241     'Storage.setItem',
242     'Storage.getItem',
243     'Storage.removeItem',
244     'Storage.clear',
245     // Web store access - local storage
246     'Window.localStorage',
247     'Storage.setItem',
248     'Storage.getItem',
249     'Storage.removeItem',
250     'Storage.clear',
251     // Notification access
252     'Window.webkitNotifications',
253     'NotificationCenter.createNotification',
254     // Cache access
255     'Window.applicationCache',
256     // Web database access
257     'Window.openDatabase',
258     // Canvas access
259     'Document.createElement',
260     'HTMLCanvasElement.getContext',
261     // XHR from content script.
262     'XMLHttpRequest.open',
263     'XMLHttpRequest.setRequestHeader',
264     'HTMLDocument.write'
265 ];
266
267 // add the hook activity
268 hookNames = ['onclick', 'ondblclick', 'ondrag', 'ondragend', 'ondragenter',
269              'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'oninput',
270              'onkeydown', 'onkeypress', 'onkeyup', 'onmousedown',
271              'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout',
272              'onmouseover', 'onmouseup', 'onmousewheel'];
273
274 for (var i = 0; i < hookNames.length; i++) {
275   domExpectedActivity.push('HTMLElement.' + hookNames[i]);
276   domExpectedActivity.push('Document.' + hookNames[i]);
277   domExpectedActivity.push('Window.' + hookNames[i]);
278 }
279
280 // Close the tab.
281 domExpectedActivity.push('tabs.remove');
282
283 testCases.push({
284   func: function triggerDOMChangesOnTabsUpdated() {
285     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
286                                'dom_tab_updated', function response() { });
287   },
288   expected_activity: domExpectedActivity
289 });
290
291 testCases.push({
292   func: function triggerDOMChangesOnTabsUpdatedIncognito() {
293     chrome.runtime.sendMessage(FRIEND_EXTENSION_ID,
294                                'dom_tab_updated_incognito',
295                                function response() { });
296   },
297   is_incognito: true,
298   expected_activity: ['windows.create'].concat(domExpectedActivity)
299 });
300
301 testCases.push({
302   func: function checkSavedHistory() {
303     var filter = new Object();
304     filter.extensionId = FRIEND_EXTENSION_ID;
305     filter.activityType = 'any';
306     filter.apiCall = 'tabs.onUpdated';
307     chrome.activityLogPrivate.getExtensionActivities(
308         filter,
309         function(result) {
310           chrome.test.assertEq(FRIEND_EXTENSION_ID,
311               result['activities'][0]['extensionId']);
312           chrome.test.assertEq('tabs.onUpdated',
313               result['activities'][0]['apiCall']);
314           chrome.test.succeed();
315         });
316   }
317 });
318
319 testCases.push({
320   func: function checkHistoryForURL() {
321     var filter = new Object();
322     filter.extensionId = FRIEND_EXTENSION_ID;
323     filter.activityType = 'any';
324     filter.pageUrl = 'http://www.google.com';
325     chrome.activityLogPrivate.getExtensionActivities(
326         filter,
327         function(result) {
328           chrome.test.assertEq(FRIEND_EXTENSION_ID,
329               result['activities'][0]['extensionId']);
330           chrome.test.succeed();
331         });
332   }
333 });
334
335 testCases.push({
336   func: function checkOtherObject() {
337     var filter = new Object();
338     filter.extensionId = FRIEND_EXTENSION_ID;
339     filter.activityType = 'dom_access';
340     filter.apiCall = 'Document.location';
341     chrome.activityLogPrivate.getExtensionActivities(
342         filter,
343         function(result) {
344           chrome.test.assertEq(FRIEND_EXTENSION_ID,
345               result['activities'][0]['extensionId']);
346           chrome.test.assertEq('Document.location',
347               result['activities'][0]['apiCall']);
348           chrome.test.assertEq('setter',
349               result['activities'][0]['other']['domVerb']);
350           chrome.test.succeed();
351         });
352     var filter = new Object();
353     filter.extensionId = FRIEND_EXTENSION_ID;
354     filter.activityType = 'any';
355     filter.apiCall = 'webRequest.onHeadersReceived';
356     chrome.activityLogPrivate.getExtensionActivities(
357         filter,
358         function(result) {
359           chrome.test.assertEq(FRIEND_EXTENSION_ID,
360               result['activities'][0]['extensionId']);
361           chrome.test.assertEq('webRequest.onHeadersReceived',
362               result['activities'][0]['apiCall']);
363           chrome.test.assertEq('{"added_request_headers":true}',
364               result['activities'][0]['other']['webRequest']);
365           chrome.test.succeed();
366         });
367   }
368 });
369
370 testCases.push({
371   func: function deleteActivities() {
372     var activityIds = [];
373     var filter = new Object();
374     filter.extensionId = FRIEND_EXTENSION_ID;
375     filter.activityType = 'any';
376     filter.apiCall = 'tabs.executeScript';
377     chrome.activityLogPrivate.getExtensionActivities(filter, function(result) {
378       chrome.test.assertEq(6, result['activities'].length);
379       for (var i = 0; i < result['activities'].length; i++)
380         activityIds.push(result['activities'][i]['activityId']);
381       chrome.test.assertEq(6, activityIds.length);
382       chrome.activityLogPrivate.deleteActivities(['-1', '-2', '-3']);
383       chrome.activityLogPrivate.getExtensionActivities(filter,
384           function(result) {
385             chrome.test.assertEq(6, result['activities'].length);
386             chrome.activityLogPrivate.deleteActivities([activityIds[0]]);
387             chrome.activityLogPrivate.getExtensionActivities(filter,
388                 function(result) {
389                   chrome.test.assertEq(5, result['activities'].length);
390                   for (var i = 0; i < result['activities'].length; i++)
391                     chrome.test.assertFalse(activityIds[0] ==
392                         result['activities'][i]['activityId']);
393                   chrome.activityLogPrivate.deleteActivities(activityIds);
394                   chrome.activityLogPrivate.getExtensionActivities(filter,
395                       function(result) {
396                         chrome.test.assertEq(0, result['activities'].length);
397                         chrome.test.succeed();
398                       });
399                 });
400           });
401     });
402   }
403 });
404
405 testCases.push({
406   func: function deleteGoogleUrls() {
407     chrome.test.getConfig(function(config) {
408       chrome.activityLogPrivate.deleteUrls(
409           ['http://www.google.com:' + config.testServer.port]);
410
411       var filter = new Object();
412       filter.extensionId = FRIEND_EXTENSION_ID;
413       filter.activityType = 'any';
414       filter.pageUrl = 'http://www.google.com';
415       chrome.activityLogPrivate.getExtensionActivities(
416         filter,
417         function(result) {
418           chrome.test.assertEq(0, result['activities'].length);
419           chrome.test.succeed();
420         });
421      });
422   }
423 });
424
425 testCases.push({
426   func: function deleteAllUrls() {
427     chrome.activityLogPrivate.deleteUrls([]);
428     var filter = new Object();
429     filter.extensionId = FRIEND_EXTENSION_ID;
430     filter.activityType = 'any';
431     filter.pageUrl = 'http://';
432     chrome.activityLogPrivate.getExtensionActivities(
433         filter,
434         function(result) {
435           chrome.test.assertEq(0, result['activities'].length);
436           chrome.test.succeed();
437         });
438   }
439 });
440
441 testCases.push({
442   func: function deleteAllHistory() {
443     chrome.activityLogPrivate.deleteDatabase();
444     var filter = new Object();
445     filter.extensionId = FRIEND_EXTENSION_ID;
446     filter.activityType = 'any';
447     filter.apiCall = '';
448     chrome.activityLogPrivate.getExtensionActivities(
449         filter,
450         function(result) {
451           chrome.test.assertEq(0, result['activities'].length);
452           chrome.test.succeed();
453         });
454   }
455 });
456
457 function checkIncognito(url, incognitoExpected) {
458   if (url) {
459     incognitoExpected = Boolean(incognitoExpected);
460     var kIncognitoMarker = '<incognito>';
461     var isIncognito =
462         (url.substr(0, kIncognitoMarker.length) == kIncognitoMarker);
463     chrome.test.assertEq(incognitoExpected, isIncognito,
464                          'Bad incognito state for URL ' + url);
465   }
466 }
467
468 // Listener to check the expected logging is done in the test cases.
469 var testCaseIndx = 0;
470 var callIndx = -1;
471 var enabledTestCases = [];
472
473 chrome.activityLogPrivate.onExtensionActivity.addListener(
474     function(activity) {
475       var activityId = activity['extensionId'];
476       chrome.test.assertEq(FRIEND_EXTENSION_ID, activityId);
477
478       // Check the api call is the one we expected next.
479       var apiCall = activity['apiCall'];
480       expectedCall = 'runtime.onMessageExternal';
481       var testCase = enabledTestCases[testCaseIndx];
482       if (callIndx > -1) {
483         expectedCall = testCase.expected_activity[callIndx];
484       }
485       console.log('Logged:' + apiCall + ' Expected:' + expectedCall);
486       chrome.test.assertEq(expectedCall, apiCall);
487
488       // Check that no real URLs are logged in incognito-mode tests.  Ignore
489       // the initial call to windows.create opening the tab.
490       if (apiCall != 'windows.create') {
491         checkIncognito(activity['pageUrl'], testCase.is_incognito);
492         checkIncognito(activity['argUrl'], testCase.is_incognito);
493       }
494
495       // If all the expected calls have been logged for this test case then
496       // mark as suceeded and move to the next. Otherwise look for the next
497       // expected api call.
498       if (callIndx == testCase.expected_activity.length - 1) {
499         chrome.test.succeed();
500         callIndx = -1;
501         testCaseIndx++;
502       } else {
503         callIndx++;
504       }
505     }
506 );
507
508 function setupTestCasesAndRun() {
509   chrome.runtime.getPlatformInfo(function(info) {
510     var tests = [];
511     for (var i = 0; i < testCases.length; i++) {
512       // Ignore test case if disabled for this OS.
513       if (testCases[i].disabled != undefined &&
514           info.os in testCases[i].disabled &&
515           testCases[i].disabled[info.os]) {
516         console.log('Test case disabled for this OS: ' + info.os);
517         continue;
518       }
519
520       // Add the test case to the enabled list and set the expected activity
521       // appriorate for this OS.
522       if (testCases[i].func != undefined) {
523         tests.push(testCases[i].func);
524         var enabledTestCase = testCases[i];
525         var activityListForOS = 'expected_activity_' + info.os;
526         if (activityListForOS in enabledTestCase) {
527           console.log('Expecting OS specific activity for: ' + info.os);
528           enabledTestCase.expected_activity =
529               enabledTestCase[activityListForOS];
530         }
531         enabledTestCases.push(enabledTestCase);
532       }
533     }
534     chrome.test.runTests(tests);
535   });
536 }
537
538 setupTestCasesAndRun();