1 // Copyright (c) 2012 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 // MediaGalleriesPrivate gallery watch API browser tests.
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/path_service.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "build/build_config.h"
12 #include "chrome/browser/extensions/extension_apitest.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/extension_test_message_listener.h"
15 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "content/public/browser/render_frame_host.h"
18 #include "content/public/browser/render_view_host.h"
19 #include "extensions/browser/extension_system.h"
20 #include "extensions/common/extension.h"
21 #include "extensions/common/switches.h"
25 // Id of test extension from
26 // chrome/test/data/extensions/api_test/|kTestExtensionPath|
27 const char kTestExtensionId[] = "gceegfkgibmgpfopknlcgleimclbknie";
28 const char kTestExtensionPath[] = "media_galleries_private/gallerywatch";
30 #if !defined(OS_CHROMEOS)
32 const char kGetAllWatchedGalleryIdsCmd[] = "getAllWatchedGalleryIds()";
33 const char kGetMediaFileSystemsCmd[] = "getMediaFileSystems()";
34 const char kSetupWatchOnValidGalleriesCmd[] = "setupWatchOnValidGalleries()";
36 const char kAddGalleryChangedListenerCmd[] = "addGalleryChangedListener()";
37 const char kRemoveAllGalleryWatchCmd[] = "removeAllGalleryWatch()";
38 const char kRemoveGalleryChangedListenerCmd[] =
39 "removeGalleryChangedListener()";
40 const char kRemoveGalleryWatchCmd[] = "removeGalleryWatch()";
41 const char kSetupWatchOnInvalidGalleryCmd[] = "setupWatchOnInvalidGallery()";
42 #endif // defined(OS_WIN)
44 // And JS reply messages.
45 const char kAddGalleryWatchOK[] = "add_gallery_watch_ok";
46 const char kGetAllGalleryWatchOK[] = "get_all_gallery_watch_ok";
47 const char kGetMediaFileSystemsCallbackOK[] =
48 "get_media_file_systems_callback_ok";
49 const char kGetMediaFileSystemsOK[] = "get_media_file_systems_ok";
51 const char kAddGalleryChangedListenerOK[] = "add_gallery_changed_listener_ok";
52 const char kRemoveAllGalleryWatchOK[] = "remove_all_gallery_watch_ok";
53 const char kRemoveGalleryChangedListenerOK[] =
54 "remove_gallery_changed_listener_ok";
55 const char kRemoveGalleryWatchOK[] = "remove_gallery_watch_ok";
56 #endif // defined(OS_WIN)
58 // Test reply messages.
59 const char kGetAllGalleryWatchResultA[] = "gallery_watchers_does_not_exists";
60 const char kAddGalleryWatchRequestFailed[] = "add_watch_request_failed";
62 const char kAddGalleryWatchRequestSucceeded[] = "add_watch_request_succeeded";
63 const char kGalleryChangedEventReceived[] = "gallery_changed_event_received";
64 const char kGetAllGalleryWatchResultB[] =
65 "watchers_for_galleries_{1, 2, 3}_found";
66 #endif // defined(OS_WIN)
67 #endif // !defined(OS_CHROMEOS)
72 ///////////////////////////////////////////////////////////////////////////////
73 // MediaGalleriesPrivateGalleryWatchApiTest //
74 ///////////////////////////////////////////////////////////////////////////////
76 class MediaGalleriesPrivateGalleryWatchApiTest : public ExtensionApiTest {
78 MediaGalleriesPrivateGalleryWatchApiTest() {}
79 virtual ~MediaGalleriesPrivateGalleryWatchApiTest() {}
82 // ExtensionApiTest overrides.
83 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
84 ExtensionApiTest::SetUpCommandLine(command_line);
85 command_line->AppendSwitchASCII(
86 extensions::switches::kWhitelistedExtensionID,
90 void ExecuteCmdAndCheckReply(content::RenderViewHost* host,
91 const std::string& js_command,
92 const std::string& ok_message) {
93 ExtensionTestMessageListener listener(ok_message, false);
94 host->GetMainFrame()->ExecuteJavaScript(base::ASCIIToUTF16(js_command));
95 EXPECT_TRUE(listener.WaitUntilSatisfied());
98 bool AddNewFileInGallery(int gallery_directory_key) {
99 if ((gallery_directory_key != chrome::DIR_USER_MUSIC) &&
100 (gallery_directory_key != chrome::DIR_USER_PICTURES) &&
101 (gallery_directory_key != chrome::DIR_USER_VIDEOS))
104 base::FilePath gallery_dir;
105 if (!PathService::Get(gallery_directory_key, &gallery_dir))
107 base::FilePath gallery_file =
108 gallery_dir.Append(FILE_PATH_LITERAL("test1.txt"));
109 std::string content("new content");
110 int write_size = base::WriteFile(gallery_file, content.c_str(),
112 return (write_size == static_cast<int>(content.length()));
115 // Loads the test extension and returns the RenderViewHost of the extension.
116 // Returns NULL if the extension load operation failed.
117 content::RenderViewHost* GetBackgroundHostForTestExtension() {
118 const extensions::Extension* extension =
119 LoadExtension(test_data_dir_.AppendASCII(kTestExtensionPath));
122 return extensions::ExtensionSystem::Get(browser()->profile())->
123 process_manager()->GetBackgroundHostForExtension(extension->id())->
128 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPrivateGalleryWatchApiTest);
132 ///////////////////////////////////////////////////////////////////////////////
134 ///////////////////////////////////////////////////////////////////////////////
136 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
138 EnsureMediaDirectoriesExists media_directories;
139 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
142 // Get media file systems.
143 ExtensionTestMessageListener get_media_systems_finished(
144 kGetMediaFileSystemsCallbackOK, false /* no reply */);
145 ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
146 kGetMediaFileSystemsOK);
147 EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
149 // Set up gallery watch.
150 ExtensionTestMessageListener add_gallery_watch_finished(
151 kAddGalleryWatchRequestSucceeded, false /* no reply */);
152 ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
154 EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
156 // Add gallery watch listener.
157 ExecuteCmdAndCheckReply(host, kAddGalleryChangedListenerCmd,
158 kAddGalleryChangedListenerOK);
160 // Modify gallery contents.
161 ExtensionTestMessageListener music_gallery_change_event_received(
162 kGalleryChangedEventReceived, false /* no reply */);
163 ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
164 EXPECT_TRUE(music_gallery_change_event_received.WaitUntilSatisfied());
166 ExtensionTestMessageListener pictures_gallery_change_event_received(
167 kGalleryChangedEventReceived, false /* no reply */);
168 ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_PICTURES));
169 EXPECT_TRUE(pictures_gallery_change_event_received.WaitUntilSatisfied());
171 ExtensionTestMessageListener videos_gallery_change_event_received(
172 kGalleryChangedEventReceived, false /* no reply */);
173 ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_VIDEOS));
174 EXPECT_TRUE(videos_gallery_change_event_received.WaitUntilSatisfied());
176 // Remove gallery watch listener.
177 ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
178 kRemoveGalleryChangedListenerOK);
180 // Remove gallery watch request.
181 ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
184 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
185 RemoveListenerAndModifyGallery) {
186 EnsureMediaDirectoriesExists media_directories;
187 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
190 // Get media file systems.
191 ExtensionTestMessageListener get_media_systems_finished(
192 kGetMediaFileSystemsCallbackOK, false /* no reply */);
193 ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
194 kGetMediaFileSystemsOK);
195 EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
197 // Set up gallery watch.
198 ExtensionTestMessageListener add_gallery_watch_finished(
199 kAddGalleryWatchRequestSucceeded, false /* no reply */);
200 ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
202 EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
204 // Add a gallery watch listener.
205 ExecuteCmdAndCheckReply(host, kAddGalleryChangedListenerCmd,
206 kAddGalleryChangedListenerOK);
207 // Modify gallery contents.
208 ExtensionTestMessageListener music_gallery_change_event_received(
209 kGalleryChangedEventReceived, false /* no reply */);
210 ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
211 EXPECT_TRUE(music_gallery_change_event_received.WaitUntilSatisfied());
213 // Remove gallery watch listener.
214 ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
215 kRemoveGalleryChangedListenerOK);
217 // No listener, modify gallery contents.
218 ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
220 // Remove gallery watch.
221 ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
224 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
225 SetupGalleryWatchWithoutListeners) {
226 EnsureMediaDirectoriesExists media_directories;
227 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
230 // Get media file systems.
231 ExtensionTestMessageListener get_media_systems_finished(
232 kGetMediaFileSystemsCallbackOK, false /* no reply */);
233 ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
234 kGetMediaFileSystemsOK);
235 EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
237 // Set up gallery watch.
238 ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
241 // No listeners, modify gallery contents.
242 ExtensionTestMessageListener music_gallery_change_event_received(
243 kGalleryChangedEventReceived, false /* no reply */);
244 ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
246 // Remove gallery watch.
247 ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
250 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
251 SetupGalleryChangedListenerWithoutWatchers) {
252 EnsureMediaDirectoriesExists media_directories;
253 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
256 // Get media file systems.
257 ExtensionTestMessageListener get_media_systems_finished(
258 kGetMediaFileSystemsCallbackOK, false /* no reply */);
259 ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
260 kGetMediaFileSystemsOK);
261 EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
263 // Add gallery watch listener.
264 ExecuteCmdAndCheckReply(host, kAddGalleryChangedListenerCmd,
265 kAddGalleryChangedListenerOK);
267 // Modify gallery contents. Listener should not get called because add watch
268 // request was not called.
269 ExtensionTestMessageListener music_gallery_change_event_received(
270 kGalleryChangedEventReceived, false /* no reply */);
271 ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
273 // Remove gallery watch listener.
274 ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
275 kRemoveGalleryChangedListenerOK);
278 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
279 SetupWatchOnInvalidGallery) {
280 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
283 // Set up a invalid gallery watch.
284 ExtensionTestMessageListener invalid_gallery_watch_request_finished(
285 kAddGalleryWatchRequestFailed, false /* no reply */);
286 ExecuteCmdAndCheckReply(host, kSetupWatchOnInvalidGalleryCmd,
288 EXPECT_TRUE(invalid_gallery_watch_request_finished.WaitUntilSatisfied());
291 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
292 GetAllGalleryWatch) {
293 EnsureMediaDirectoriesExists media_directories;
294 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
297 // Get media file systems.
298 ExtensionTestMessageListener get_media_systems_finished(
299 kGetMediaFileSystemsCallbackOK, false /* no reply */);
300 ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
301 kGetMediaFileSystemsOK);
302 EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
304 // Gallery watchers are not yet added.
305 // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return an empty
307 ExtensionTestMessageListener initial_get_all_check_finished(
308 kGetAllGalleryWatchResultA, false /* no reply */);
309 ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
310 kGetAllGalleryWatchOK);
311 EXPECT_TRUE(initial_get_all_check_finished.WaitUntilSatisfied());
313 // Set up gallery watchers.
314 ExtensionTestMessageListener add_gallery_watch_finished(
315 kAddGalleryWatchRequestSucceeded, false /* no reply */);
316 ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
318 EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
320 // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return the
321 // gallery identifiers.
322 ExtensionTestMessageListener get_all_watched_galleries_finished(
323 kGetAllGalleryWatchResultB, false /* no reply */);
324 ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
325 kGetAllGalleryWatchOK);
326 EXPECT_TRUE(get_all_watched_galleries_finished.WaitUntilSatisfied());
328 // Remove gallery watch request.
329 ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
331 // Gallery watchers removed.
332 // chrome.mediaGalleriesPrivate.getAllGalleryWatch() should return an empty
334 ExtensionTestMessageListener final_get_all_check_finished(
335 kGetAllGalleryWatchResultA, false /* no reply */);
336 ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
337 kGetAllGalleryWatchOK);
338 EXPECT_TRUE(final_get_all_check_finished.WaitUntilSatisfied());
341 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
342 RemoveAllGalleryWatch) {
343 EnsureMediaDirectoriesExists media_directories;
344 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
347 // Get media file systems.
348 ExtensionTestMessageListener get_media_systems_finished(
349 kGetMediaFileSystemsCallbackOK, false /* no reply */);
350 ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
351 kGetMediaFileSystemsOK);
352 EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
354 // Set up gallery watchers.
355 ExtensionTestMessageListener add_gallery_watch_finished(
356 kAddGalleryWatchRequestSucceeded, false /* no reply */);
357 ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
359 EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
361 // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return the watched
362 // gallery identifiers.
363 ExtensionTestMessageListener get_all_watched_galleries_finished(
364 kGetAllGalleryWatchResultB, false /* no reply */);
365 ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
366 kGetAllGalleryWatchOK);
367 EXPECT_TRUE(get_all_watched_galleries_finished.WaitUntilSatisfied());
369 // Remove all gallery watchers.
370 ExecuteCmdAndCheckReply(host, kRemoveAllGalleryWatchCmd,
371 kRemoveAllGalleryWatchOK);
373 // Gallery watchers removed. chrome.mediaGalleriesPrivate.getAllGalleryWatch
374 // should return an empty list.
375 ExtensionTestMessageListener final_get_all_check_finished(
376 kGetAllGalleryWatchResultA, false /* no reply */);
377 ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
378 kGetAllGalleryWatchOK);
379 EXPECT_TRUE(final_get_all_check_finished.WaitUntilSatisfied());
383 #if !defined(OS_WIN) && !defined(OS_CHROMEOS)
384 // Gallery watch request is not enabled on non-windows platforms.
385 // Please refer to crbug.com/144491.
386 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
388 EnsureMediaDirectoriesExists media_directories;
389 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
392 // Get media file systems.
393 ExtensionTestMessageListener get_media_systems_finished(
394 kGetMediaFileSystemsCallbackOK, false /* no reply */);
395 ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
396 kGetMediaFileSystemsOK);
397 EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
399 // Set up a invalid gallery watch.
400 ExtensionTestMessageListener gallery_watch_request_finished(
401 kAddGalleryWatchRequestFailed, false /* no reply */);
402 // Set up gallery watch.
403 ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
405 EXPECT_TRUE(gallery_watch_request_finished.WaitUntilSatisfied());
408 // Gallery watch request is not enabled on non-windows platforms.
409 // Please refer to crbug.com/144491.
410 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
411 GetAllGalleryWatch) {
412 EnsureMediaDirectoriesExists media_directories;
413 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
416 // Get media file systems.
417 ExtensionTestMessageListener get_media_systems_finished(
418 kGetMediaFileSystemsCallbackOK, false /* no reply */);
419 ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
420 kGetMediaFileSystemsOK);
421 EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
423 // Set up gallery watch.
424 ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
427 // Gallery watchers does not exists.
428 // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return an empty
430 ExtensionTestMessageListener get_all_gallery_watch_finished(
431 kGetAllGalleryWatchResultA, false /* no reply */);
432 ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
433 kGetAllGalleryWatchOK);
434 EXPECT_TRUE(get_all_gallery_watch_finished.WaitUntilSatisfied());