Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / media_galleries / gallerywatch / test.js
bloba4e14f75af3ad11a0886df1215355ecf70a827d5
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 galleries;
6 var invalidGalleryId = '11000';
8 // chrome.mediaGalleries.getMediaFileSystems callback.
9 var mediaFileSystemsListCallback = function (results) {
10   galleries = results;
11   chrome.test.sendMessage('get_media_file_systems_callback_ok');
14 // Gallery changed event handler.
15 var onGalleryChangedCallback = function (details) {
16   chrome.test.sendMessage('gallery_changed_event_received');
19 // Add watch request callback.
20 var onAddWatchRequestCallback = function (details) {
21   if (details.success) {
22     chrome.test.sendMessage('add_watch_request_succeeded');
23   } else {
24     chrome.test.sendMessage('add_watch_request_failed');
25   }
28 // chrome.mediaGalleries.getAllGalleryWatch callback.
29 var getAllGalleryWatchCallback = function (results) {
30   if (!results) {
31     chrome.test.sendMessage('get_all_gallery_watch_failed');
32     return;
33   }
34   if (results.length == 0) {
35     chrome.test.sendMessage('gallery_watchers_does_not_exists');
36   } else {
37     for (var i = 0; i < results.length; ++i) {
38       var info = chrome.mediaGalleries.getMediaFileSystemMetadata(galleries[i]);
39       if (results[i] !== info.galleryId) {
40         chrome.test.sendMessage('gallery_watcher_mismatch');
41         return;
42       }
43     }
44     chrome.test.sendMessage('gallery_watcher_checks');
45   }
48 var onGalleryChangedCheckingCallback = function(result) {
49   if (result.galleryId != '' && result.type == 'contents_changed') {
50     chrome.test.sendMessage('on_gallery_changed_checking_ok');
51   }
55 /**
56  * Generates a callback function which notifies the apitest when the given
57  * number of runtime errors has occurred.
58  *
59  * @param {number} expectedNumCalls The number of calls to this callback to
60  *     expect.
61  * @return {function()}
62  */
63 var createUnlistenedAddWatchCallback = function(expectedNumCalls) {
64   var numCalls = 0;
65   var numErrors = 0;
66   return function() {
67     numCalls++;
68     if (chrome.runtime.lastError) {
69       numErrors++;
70     }
72     if (numCalls == expectedNumCalls && numErrors == expectedNumCalls) {
73       chrome.test.sendMessage('add_watch_request_runtime_error');
74     }
75   };
78 // Helpers to add and remove event listeners.
79 function addGalleryChangedListener() {
80   chrome.mediaGalleries.onGalleryChanged.addListener(
81       onGalleryChangedCallback);
82   chrome.test.sendMessage('add_gallery_changed_listener_ok');
85 function addCheckingGalleryChangedListener() {
86   chrome.mediaGalleries.onGalleryChanged.addListener(
87       onGalleryChangedCheckingCallback);
88   chrome.test.sendMessage('add_gallery_changed_listener_ok');
91 function setupWatchOnValidGalleries() {
92   for (var i = 0; i < galleries.length; ++i) {
93     var info = chrome.mediaGalleries.getMediaFileSystemMetadata(galleries[i]);
94     chrome.mediaGalleries.addGalleryWatch(info.galleryId,
95                                           onAddWatchRequestCallback);
96   }
97   chrome.test.sendMessage('add_gallery_watch_ok');
100 function setupWatchOnUnlistenedValidGalleries() {
101   var callback = createUnlistenedAddWatchCallback(galleries.length);
102   for (var i = 0; i < galleries.length; ++i) {
103     var info = chrome.mediaGalleries.getMediaFileSystemMetadata(galleries[i]);
104     chrome.mediaGalleries.addGalleryWatch(info.galleryId, callback);
105   }
108 function setupWatchOnInvalidGallery() {
109   chrome.mediaGalleries.addGalleryWatch(invalidGalleryId,
110                                         onAddWatchRequestCallback);
113 function getMediaFileSystems() {
114   chrome.mediaGalleries.getMediaFileSystems(mediaFileSystemsListCallback);
115   chrome.test.sendMessage('get_media_file_systems_ok');
118 function removeGalleryWatch() {
119   for (var i = 0; i < galleries.length; ++i) {
120     var info = chrome.mediaGalleries.getMediaFileSystemMetadata(galleries[i]);
121     chrome.mediaGalleries.removeGalleryWatch(info.galleryId);
122   }
123   chrome.test.sendMessage('remove_gallery_watch_ok');
126 function removeGalleryChangedListener() {
127   chrome.mediaGalleries.onGalleryChanged.removeListener(
128       onGalleryChangedCallback);
129   chrome.test.sendMessage('remove_gallery_changed_listener_ok');
132 function getAllWatchedGalleryIds() {
133   chrome.mediaGalleries.getAllGalleryWatch(getAllGalleryWatchCallback);
134   chrome.test.sendMessage('get_all_gallery_watch_ok');
137 function removeAllGalleryWatch() {
138   chrome.mediaGalleries.removeAllGalleryWatch();
139   chrome.test.sendMessage('remove_all_gallery_watch_ok');