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_system.h"
15 #include "chrome/browser/extensions/extension_test_message_listener.h"
16 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/extensions/extension.h"
20 #include "content/public/browser/render_view_host.h"
24 // Id of test extension from
25 // chrome/test/data/extensions/api_test/|kTestExtensionPath|
26 const char kTestExtensionId[] = "gceegfkgibmgpfopknlcgleimclbknie";
27 const char kTestExtensionPath[] = "media_galleries_private/gallerywatch";
29 #if !defined(OS_CHROMEOS)
31 const char kGetAllWatchedGalleryIdsCmd[] = "getAllWatchedGalleryIds()";
32 const char kGetMediaFileSystemsCmd[] = "getMediaFileSystems()";
33 const char kSetupWatchOnValidGalleriesCmd[] = "setupWatchOnValidGalleries()";
35 const char kAddGalleryChangedListenerCmd[] = "addGalleryChangedListener()";
36 const char kRemoveAllGalleryWatchCmd[] = "removeAllGalleryWatch()";
37 const char kRemoveGalleryChangedListenerCmd[] =
38 "removeGalleryChangedListener()";
39 const char kRemoveGalleryWatchCmd[] = "removeGalleryWatch()";
40 const char kSetupWatchOnInvalidGalleryCmd[] = "setupWatchOnInvalidGallery()";
41 #endif // defined(OS_WIN)
43 // And JS reply messages.
44 const char kAddGalleryWatchOK[] = "add_gallery_watch_ok";
45 const char kGetAllGalleryWatchOK[] = "get_all_gallery_watch_ok";
46 const char kGetMediaFileSystemsCallbackOK[] =
47 "get_media_file_systems_callback_ok";
48 const char kGetMediaFileSystemsOK[] = "get_media_file_systems_ok";
50 const char kAddGalleryChangedListenerOK[] = "add_gallery_changed_listener_ok";
51 const char kRemoveAllGalleryWatchOK[] = "remove_all_gallery_watch_ok";
52 const char kRemoveGalleryChangedListenerOK[] =
53 "remove_gallery_changed_listener_ok";
54 const char kRemoveGalleryWatchOK[] = "remove_gallery_watch_ok";
55 #endif // defined(OS_WIN)
57 // Test reply messages.
58 const char kGetAllGalleryWatchResultA[] = "gallery_watchers_does_not_exists";
59 const char kAddGalleryWatchRequestFailed[] = "add_watch_request_failed";
61 const char kAddGalleryWatchRequestSucceeded[] = "add_watch_request_succeeded";
62 const char kGalleryChangedEventReceived[] = "gallery_changed_event_received";
63 const char kGetAllGalleryWatchResultB[] =
64 "watchers_for_galleries_{1, 2, 3}_found";
65 #endif // defined(OS_WIN)
66 #endif // !defined(OS_CHROMEOS)
71 ///////////////////////////////////////////////////////////////////////////////
72 // MediaGalleriesPrivateGalleryWatchApiTest //
73 ///////////////////////////////////////////////////////////////////////////////
75 class MediaGalleriesPrivateGalleryWatchApiTest : public ExtensionApiTest {
77 MediaGalleriesPrivateGalleryWatchApiTest() {}
78 virtual ~MediaGalleriesPrivateGalleryWatchApiTest() {}
81 // ExtensionApiTest overrides.
82 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
83 ExtensionApiTest::SetUpCommandLine(command_line);
84 command_line->AppendSwitchASCII(switches::kWhitelistedExtensionID,
88 void ExecuteCmdAndCheckReply(content::RenderViewHost* host,
89 const std::string& js_command,
90 const std::string& ok_message) {
91 ExtensionTestMessageListener listener(ok_message, false);
92 host->ExecuteJavascriptInWebFrame(string16(), ASCIIToUTF16(js_command));
93 EXPECT_TRUE(listener.WaitUntilSatisfied());
96 bool AddNewFileInGallery(int gallery_directory_key) {
97 if ((gallery_directory_key != chrome::DIR_USER_MUSIC) &&
98 (gallery_directory_key != chrome::DIR_USER_PICTURES) &&
99 (gallery_directory_key != chrome::DIR_USER_VIDEOS))
102 base::FilePath gallery_dir;
103 if (!PathService::Get(gallery_directory_key, &gallery_dir))
105 base::FilePath gallery_file =
106 gallery_dir.Append(FILE_PATH_LITERAL("test1.txt"));
107 std::string content("new content");
108 int write_size = file_util::WriteFile(gallery_file, content.c_str(),
110 return (write_size == static_cast<int>(content.length()));
113 // Loads the test extension and returns the RenderViewHost of the extension.
114 // Returns NULL if the extension load operation failed.
115 content::RenderViewHost* GetBackgroundHostForTestExtension() {
116 const extensions::Extension* extension =
117 LoadExtension(test_data_dir_.AppendASCII(kTestExtensionPath));
120 return extensions::ExtensionSystem::Get(browser()->profile())->
121 process_manager()->GetBackgroundHostForExtension(extension->id())->
126 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPrivateGalleryWatchApiTest);
130 ///////////////////////////////////////////////////////////////////////////////
132 ///////////////////////////////////////////////////////////////////////////////
134 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
136 EnsureMediaDirectoriesExists media_directories;
137 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
140 // Get media file systems.
141 ExtensionTestMessageListener get_media_systems_finished(
142 kGetMediaFileSystemsCallbackOK, false /* no reply */);
143 ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
144 kGetMediaFileSystemsOK);
145 EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
147 // Set up gallery watch.
148 ExtensionTestMessageListener add_gallery_watch_finished(
149 kAddGalleryWatchRequestSucceeded, false /* no reply */);
150 ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
152 EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
154 // Add gallery watch listener.
155 ExecuteCmdAndCheckReply(host, kAddGalleryChangedListenerCmd,
156 kAddGalleryChangedListenerOK);
158 // Modify gallery contents.
159 ExtensionTestMessageListener music_gallery_change_event_received(
160 kGalleryChangedEventReceived, false /* no reply */);
161 ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
162 EXPECT_TRUE(music_gallery_change_event_received.WaitUntilSatisfied());
164 ExtensionTestMessageListener pictures_gallery_change_event_received(
165 kGalleryChangedEventReceived, false /* no reply */);
166 ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_PICTURES));
167 EXPECT_TRUE(pictures_gallery_change_event_received.WaitUntilSatisfied());
169 ExtensionTestMessageListener videos_gallery_change_event_received(
170 kGalleryChangedEventReceived, false /* no reply */);
171 ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_VIDEOS));
172 EXPECT_TRUE(videos_gallery_change_event_received.WaitUntilSatisfied());
174 // Remove gallery watch listener.
175 ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
176 kRemoveGalleryChangedListenerOK);
178 // Remove gallery watch request.
179 ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
182 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
183 RemoveListenerAndModifyGallery) {
184 EnsureMediaDirectoriesExists media_directories;
185 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
188 // Get media file systems.
189 ExtensionTestMessageListener get_media_systems_finished(
190 kGetMediaFileSystemsCallbackOK, false /* no reply */);
191 ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
192 kGetMediaFileSystemsOK);
193 EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
195 // Set up gallery watch.
196 ExtensionTestMessageListener add_gallery_watch_finished(
197 kAddGalleryWatchRequestSucceeded, false /* no reply */);
198 ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
200 EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
202 // Add a gallery watch listener.
203 ExecuteCmdAndCheckReply(host, kAddGalleryChangedListenerCmd,
204 kAddGalleryChangedListenerOK);
205 // Modify gallery contents.
206 ExtensionTestMessageListener music_gallery_change_event_received(
207 kGalleryChangedEventReceived, false /* no reply */);
208 ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
209 EXPECT_TRUE(music_gallery_change_event_received.WaitUntilSatisfied());
211 // Remove gallery watch listener.
212 ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
213 kRemoveGalleryChangedListenerOK);
215 // No listener, modify gallery contents.
216 ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
218 // Remove gallery watch.
219 ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
222 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
223 SetupGalleryWatchWithoutListeners) {
224 EnsureMediaDirectoriesExists media_directories;
225 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
228 // Get media file systems.
229 ExtensionTestMessageListener get_media_systems_finished(
230 kGetMediaFileSystemsCallbackOK, false /* no reply */);
231 ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
232 kGetMediaFileSystemsOK);
233 EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
235 // Set up gallery watch.
236 ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
239 // No listeners, modify gallery contents.
240 ExtensionTestMessageListener music_gallery_change_event_received(
241 kGalleryChangedEventReceived, false /* no reply */);
242 ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
244 // Remove gallery watch.
245 ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
248 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
249 SetupGalleryChangedListenerWithoutWatchers) {
250 EnsureMediaDirectoriesExists media_directories;
251 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
254 // Get media file systems.
255 ExtensionTestMessageListener get_media_systems_finished(
256 kGetMediaFileSystemsCallbackOK, false /* no reply */);
257 ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
258 kGetMediaFileSystemsOK);
259 EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
261 // Add gallery watch listener.
262 ExecuteCmdAndCheckReply(host, kAddGalleryChangedListenerCmd,
263 kAddGalleryChangedListenerOK);
265 // Modify gallery contents. Listener should not get called because add watch
266 // request was not called.
267 ExtensionTestMessageListener music_gallery_change_event_received(
268 kGalleryChangedEventReceived, false /* no reply */);
269 ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
271 // Remove gallery watch listener.
272 ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
273 kRemoveGalleryChangedListenerOK);
276 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
277 SetupWatchOnInvalidGallery) {
278 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
281 // Set up a invalid gallery watch.
282 ExtensionTestMessageListener invalid_gallery_watch_request_finished(
283 kAddGalleryWatchRequestFailed, false /* no reply */);
284 ExecuteCmdAndCheckReply(host, kSetupWatchOnInvalidGalleryCmd,
286 EXPECT_TRUE(invalid_gallery_watch_request_finished.WaitUntilSatisfied());
289 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
290 GetAllGalleryWatch) {
291 EnsureMediaDirectoriesExists media_directories;
292 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
295 // Get media file systems.
296 ExtensionTestMessageListener get_media_systems_finished(
297 kGetMediaFileSystemsCallbackOK, false /* no reply */);
298 ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
299 kGetMediaFileSystemsOK);
300 EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
302 // Gallery watchers are not yet added.
303 // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return an empty
305 ExtensionTestMessageListener initial_get_all_check_finished(
306 kGetAllGalleryWatchResultA, false /* no reply */);
307 ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
308 kGetAllGalleryWatchOK);
309 EXPECT_TRUE(initial_get_all_check_finished.WaitUntilSatisfied());
311 // Set up gallery watchers.
312 ExtensionTestMessageListener add_gallery_watch_finished(
313 kAddGalleryWatchRequestSucceeded, false /* no reply */);
314 ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
316 EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
318 // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return the
319 // gallery identifiers.
320 ExtensionTestMessageListener get_all_watched_galleries_finished(
321 kGetAllGalleryWatchResultB, false /* no reply */);
322 ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
323 kGetAllGalleryWatchOK);
324 EXPECT_TRUE(get_all_watched_galleries_finished.WaitUntilSatisfied());
326 // Remove gallery watch request.
327 ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
329 // Gallery watchers removed.
330 // chrome.mediaGalleriesPrivate.getAllGalleryWatch() should return an empty
332 ExtensionTestMessageListener final_get_all_check_finished(
333 kGetAllGalleryWatchResultA, false /* no reply */);
334 ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
335 kGetAllGalleryWatchOK);
336 EXPECT_TRUE(final_get_all_check_finished.WaitUntilSatisfied());
339 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
340 RemoveAllGalleryWatch) {
341 EnsureMediaDirectoriesExists media_directories;
342 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
345 // Get media file systems.
346 ExtensionTestMessageListener get_media_systems_finished(
347 kGetMediaFileSystemsCallbackOK, false /* no reply */);
348 ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
349 kGetMediaFileSystemsOK);
350 EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
352 // Set up gallery watchers.
353 ExtensionTestMessageListener add_gallery_watch_finished(
354 kAddGalleryWatchRequestSucceeded, false /* no reply */);
355 ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
357 EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
359 // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return the watched
360 // gallery identifiers.
361 ExtensionTestMessageListener get_all_watched_galleries_finished(
362 kGetAllGalleryWatchResultB, false /* no reply */);
363 ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
364 kGetAllGalleryWatchOK);
365 EXPECT_TRUE(get_all_watched_galleries_finished.WaitUntilSatisfied());
367 // Remove all gallery watchers.
368 ExecuteCmdAndCheckReply(host, kRemoveAllGalleryWatchCmd,
369 kRemoveAllGalleryWatchOK);
371 // Gallery watchers removed. chrome.mediaGalleriesPrivate.getAllGalleryWatch
372 // should return an empty list.
373 ExtensionTestMessageListener final_get_all_check_finished(
374 kGetAllGalleryWatchResultA, false /* no reply */);
375 ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
376 kGetAllGalleryWatchOK);
377 EXPECT_TRUE(final_get_all_check_finished.WaitUntilSatisfied());
381 #if !defined(OS_WIN) && !defined(OS_CHROMEOS)
382 // Gallery watch request is not enabled on non-windows platforms.
383 // Please refer to crbug.com/144491.
384 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
386 EnsureMediaDirectoriesExists media_directories;
387 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
390 // Get media file systems.
391 ExtensionTestMessageListener get_media_systems_finished(
392 kGetMediaFileSystemsCallbackOK, false /* no reply */);
393 ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
394 kGetMediaFileSystemsOK);
395 EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
397 // Set up a invalid gallery watch.
398 ExtensionTestMessageListener gallery_watch_request_finished(
399 kAddGalleryWatchRequestFailed, false /* no reply */);
400 // Set up gallery watch.
401 ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
403 EXPECT_TRUE(gallery_watch_request_finished.WaitUntilSatisfied());
406 // Gallery watch request is not enabled on non-windows platforms.
407 // Please refer to crbug.com/144491.
408 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
409 GetAllGalleryWatch) {
410 EnsureMediaDirectoriesExists media_directories;
411 content::RenderViewHost* host = GetBackgroundHostForTestExtension();
414 // Get media file systems.
415 ExtensionTestMessageListener get_media_systems_finished(
416 kGetMediaFileSystemsCallbackOK, false /* no reply */);
417 ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
418 kGetMediaFileSystemsOK);
419 EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
421 // Set up gallery watch.
422 ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
425 // Gallery watchers does not exists.
426 // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return an empty
428 ExtensionTestMessageListener get_all_gallery_watch_finished(
429 kGetAllGalleryWatchResultA, false /* no reply */);
430 ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
431 kGetAllGalleryWatchOK);
432 EXPECT_TRUE(get_all_gallery_watch_finished.WaitUntilSatisfied());