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.
6 var invalidGalleryId = '11000';
8 // chrome.mediaGalleries.getMediaFileSystems callback.
9 var mediaFileSystemsListCallback = function (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');
24 chrome.test.sendMessage('add_watch_request_failed');
28 // chrome.mediaGalleries.getAllGalleryWatch callback.
29 var getAllGalleryWatchCallback = function (results) {
31 chrome.test.sendMessage('get_all_gallery_watch_failed');
34 if (results.length == 0) {
35 chrome.test.sendMessage('gallery_watchers_does_not_exists');
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');
44 chrome.test.sendMessage('gallery_watcher_checks');
48 var onGalleryChangedCheckingCallback = function(result) {
49 if (result.galleryId != '' && result.type == 'contents_changed') {
50 chrome.test.sendMessage('on_gallery_changed_checking_ok');
56 * Generates a callback function which notifies the apitest when the given
57 * number of runtime errors has occurred.
59 * @param {number} expectedNumCalls The number of calls to this callback to
61 * @return {function()}
63 var createUnlistenedAddWatchCallback = function(expectedNumCalls) {
68 if (chrome.runtime.lastError) {
72 if (numCalls == expectedNumCalls && numErrors == expectedNumCalls) {
73 chrome.test.sendMessage('add_watch_request_runtime_error');
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);
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);
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);
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');