Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / media_galleries_private / media_galleries_watch_apitest.cc
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.
4 //
5 // MediaGalleriesPrivate gallery watch API browser tests.
6
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 "chrome/common/chrome_switches.h"
18 #include "content/public/browser/render_view_host.h"
19 #include "extensions/browser/extension_system.h"
20 #include "extensions/common/extension.h"
21
22 namespace {
23
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";
28
29 #if !defined(OS_CHROMEOS)
30 // JS commands.
31 const char kGetAllWatchedGalleryIdsCmd[] = "getAllWatchedGalleryIds()";
32 const char kGetMediaFileSystemsCmd[] = "getMediaFileSystems()";
33 const char kSetupWatchOnValidGalleriesCmd[] = "setupWatchOnValidGalleries()";
34 #if defined(OS_WIN)
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)
42
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";
49 #if defined(OS_WIN)
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)
56
57 // Test reply messages.
58 const char kGetAllGalleryWatchResultA[] = "gallery_watchers_does_not_exists";
59 const char kAddGalleryWatchRequestFailed[] = "add_watch_request_failed";
60 #if defined(OS_WIN)
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)
67
68 }  // namespace
69
70
71 ///////////////////////////////////////////////////////////////////////////////
72 //                 MediaGalleriesPrivateGalleryWatchApiTest                  //
73 ///////////////////////////////////////////////////////////////////////////////
74
75 class MediaGalleriesPrivateGalleryWatchApiTest : public ExtensionApiTest {
76  public:
77   MediaGalleriesPrivateGalleryWatchApiTest() {}
78   virtual ~MediaGalleriesPrivateGalleryWatchApiTest() {}
79
80  protected:
81   // ExtensionApiTest overrides.
82   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
83     ExtensionApiTest::SetUpCommandLine(command_line);
84     command_line->AppendSwitchASCII(switches::kWhitelistedExtensionID,
85                                     kTestExtensionId);
86   }
87
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(base::string16(),
93                                       base::ASCIIToUTF16(js_command));
94     EXPECT_TRUE(listener.WaitUntilSatisfied());
95   }
96
97   bool AddNewFileInGallery(int gallery_directory_key) {
98     if ((gallery_directory_key != chrome::DIR_USER_MUSIC) &&
99         (gallery_directory_key != chrome::DIR_USER_PICTURES) &&
100         (gallery_directory_key != chrome::DIR_USER_VIDEOS))
101       return false;
102
103     base::FilePath gallery_dir;
104     if (!PathService::Get(gallery_directory_key, &gallery_dir))
105       return false;
106     base::FilePath gallery_file =
107         gallery_dir.Append(FILE_PATH_LITERAL("test1.txt"));
108     std::string content("new content");
109     int write_size = file_util::WriteFile(gallery_file, content.c_str(),
110                                           content.length());
111     return (write_size == static_cast<int>(content.length()));
112   }
113
114   // Loads the test extension and returns the RenderViewHost of the extension.
115   // Returns NULL if the extension load operation failed.
116   content::RenderViewHost* GetBackgroundHostForTestExtension() {
117     const extensions::Extension* extension =
118         LoadExtension(test_data_dir_.AppendASCII(kTestExtensionPath));
119     if (!extension)
120       return NULL;
121     return extensions::ExtensionSystem::Get(browser()->profile())->
122         process_manager()->GetBackgroundHostForExtension(extension->id())->
123            render_view_host();
124   }
125
126  private:
127   DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPrivateGalleryWatchApiTest);
128 };
129
130
131 ///////////////////////////////////////////////////////////////////////////////
132 //                               TESTS                                       //
133 ///////////////////////////////////////////////////////////////////////////////
134 #if defined(OS_WIN)
135 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
136                        BasicGalleryWatch) {
137   EnsureMediaDirectoriesExists media_directories;
138   content::RenderViewHost* host = GetBackgroundHostForTestExtension();
139   ASSERT_TRUE(host);
140
141   // Get media file systems.
142   ExtensionTestMessageListener get_media_systems_finished(
143       kGetMediaFileSystemsCallbackOK, false  /* no reply */);
144   ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
145                           kGetMediaFileSystemsOK);
146   EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
147
148   // Set up gallery watch.
149   ExtensionTestMessageListener add_gallery_watch_finished(
150       kAddGalleryWatchRequestSucceeded, false  /* no reply */);
151   ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
152                           kAddGalleryWatchOK);
153   EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
154
155   // Add gallery watch listener.
156   ExecuteCmdAndCheckReply(host, kAddGalleryChangedListenerCmd,
157                           kAddGalleryChangedListenerOK);
158
159   // Modify gallery contents.
160   ExtensionTestMessageListener music_gallery_change_event_received(
161       kGalleryChangedEventReceived, false  /* no reply */);
162   ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
163   EXPECT_TRUE(music_gallery_change_event_received.WaitUntilSatisfied());
164
165   ExtensionTestMessageListener pictures_gallery_change_event_received(
166       kGalleryChangedEventReceived, false  /* no reply */);
167   ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_PICTURES));
168   EXPECT_TRUE(pictures_gallery_change_event_received.WaitUntilSatisfied());
169
170   ExtensionTestMessageListener videos_gallery_change_event_received(
171       kGalleryChangedEventReceived, false  /* no reply */);
172   ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_VIDEOS));
173   EXPECT_TRUE(videos_gallery_change_event_received.WaitUntilSatisfied());
174
175   // Remove gallery watch listener.
176   ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
177                           kRemoveGalleryChangedListenerOK);
178
179   // Remove gallery watch request.
180   ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
181 }
182
183 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
184                        RemoveListenerAndModifyGallery) {
185   EnsureMediaDirectoriesExists media_directories;
186   content::RenderViewHost* host = GetBackgroundHostForTestExtension();
187   ASSERT_TRUE(host);
188
189   // Get media file systems.
190   ExtensionTestMessageListener get_media_systems_finished(
191       kGetMediaFileSystemsCallbackOK, false  /* no reply */);
192   ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
193                           kGetMediaFileSystemsOK);
194   EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
195
196   // Set up gallery watch.
197   ExtensionTestMessageListener add_gallery_watch_finished(
198       kAddGalleryWatchRequestSucceeded, false  /* no reply */);
199   ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
200                          kAddGalleryWatchOK);
201   EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
202
203   // Add a gallery watch listener.
204   ExecuteCmdAndCheckReply(host, kAddGalleryChangedListenerCmd,
205                           kAddGalleryChangedListenerOK);
206   // Modify gallery contents.
207   ExtensionTestMessageListener music_gallery_change_event_received(
208       kGalleryChangedEventReceived, false  /* no reply */);
209   ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
210   EXPECT_TRUE(music_gallery_change_event_received.WaitUntilSatisfied());
211
212   // Remove gallery watch listener.
213   ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
214                           kRemoveGalleryChangedListenerOK);
215
216   // No listener, modify gallery contents.
217   ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
218
219   // Remove gallery watch.
220   ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
221 }
222
223 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
224                        SetupGalleryWatchWithoutListeners) {
225   EnsureMediaDirectoriesExists media_directories;
226   content::RenderViewHost* host = GetBackgroundHostForTestExtension();
227   ASSERT_TRUE(host);
228
229   // Get media file systems.
230   ExtensionTestMessageListener get_media_systems_finished(
231       kGetMediaFileSystemsCallbackOK, false  /* no reply */);
232   ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
233                           kGetMediaFileSystemsOK);
234   EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
235
236   // Set up gallery watch.
237   ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
238                           kAddGalleryWatchOK);
239
240   // No listeners, modify gallery contents.
241   ExtensionTestMessageListener music_gallery_change_event_received(
242       kGalleryChangedEventReceived, false  /* no reply */);
243   ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
244
245   // Remove gallery watch.
246   ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
247 }
248
249 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
250                        SetupGalleryChangedListenerWithoutWatchers) {
251   EnsureMediaDirectoriesExists media_directories;
252   content::RenderViewHost* host = GetBackgroundHostForTestExtension();
253   ASSERT_TRUE(host);
254
255   // Get media file systems.
256   ExtensionTestMessageListener get_media_systems_finished(
257       kGetMediaFileSystemsCallbackOK, false  /* no reply */);
258   ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
259                           kGetMediaFileSystemsOK);
260   EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
261
262   // Add gallery watch listener.
263   ExecuteCmdAndCheckReply(host, kAddGalleryChangedListenerCmd,
264                           kAddGalleryChangedListenerOK);
265
266   // Modify gallery contents. Listener should not get called because add watch
267   // request was not called.
268   ExtensionTestMessageListener music_gallery_change_event_received(
269       kGalleryChangedEventReceived, false  /* no reply */);
270   ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
271
272   // Remove gallery watch listener.
273   ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
274                           kRemoveGalleryChangedListenerOK);
275 }
276
277 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
278                        SetupWatchOnInvalidGallery) {
279   content::RenderViewHost* host = GetBackgroundHostForTestExtension();
280   ASSERT_TRUE(host);
281
282   // Set up a invalid gallery watch.
283   ExtensionTestMessageListener invalid_gallery_watch_request_finished(
284       kAddGalleryWatchRequestFailed, false  /* no reply */);
285   ExecuteCmdAndCheckReply(host, kSetupWatchOnInvalidGalleryCmd,
286                           kAddGalleryWatchOK);
287   EXPECT_TRUE(invalid_gallery_watch_request_finished.WaitUntilSatisfied());
288 }
289
290 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
291                        GetAllGalleryWatch) {
292   EnsureMediaDirectoriesExists media_directories;
293   content::RenderViewHost* host = GetBackgroundHostForTestExtension();
294   ASSERT_TRUE(host);
295
296   // Get media file systems.
297   ExtensionTestMessageListener get_media_systems_finished(
298       kGetMediaFileSystemsCallbackOK, false  /* no reply */);
299   ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
300                           kGetMediaFileSystemsOK);
301   EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
302
303   // Gallery watchers are not yet added.
304   // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return an empty
305   // list.
306   ExtensionTestMessageListener initial_get_all_check_finished(
307       kGetAllGalleryWatchResultA, false  /* no reply */);
308   ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
309                           kGetAllGalleryWatchOK);
310   EXPECT_TRUE(initial_get_all_check_finished.WaitUntilSatisfied());
311
312   // Set up gallery watchers.
313   ExtensionTestMessageListener add_gallery_watch_finished(
314       kAddGalleryWatchRequestSucceeded, false  /* no reply */);
315   ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
316                           kAddGalleryWatchOK);
317   EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
318
319   // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return the
320   // gallery identifiers.
321   ExtensionTestMessageListener get_all_watched_galleries_finished(
322       kGetAllGalleryWatchResultB, false  /* no reply */);
323   ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
324                           kGetAllGalleryWatchOK);
325   EXPECT_TRUE(get_all_watched_galleries_finished.WaitUntilSatisfied());
326
327   // Remove gallery watch request.
328   ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
329
330   // Gallery watchers removed.
331   // chrome.mediaGalleriesPrivate.getAllGalleryWatch() should return an empty
332   // list.
333   ExtensionTestMessageListener final_get_all_check_finished(
334       kGetAllGalleryWatchResultA, false  /* no reply */);
335   ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
336                           kGetAllGalleryWatchOK);
337   EXPECT_TRUE(final_get_all_check_finished.WaitUntilSatisfied());
338 }
339
340 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
341                        RemoveAllGalleryWatch) {
342   EnsureMediaDirectoriesExists media_directories;
343   content::RenderViewHost* host = GetBackgroundHostForTestExtension();
344   ASSERT_TRUE(host);
345
346   // Get media file systems.
347   ExtensionTestMessageListener get_media_systems_finished(
348       kGetMediaFileSystemsCallbackOK, false  /* no reply */);
349   ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
350                           kGetMediaFileSystemsOK);
351   EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
352
353   // Set up gallery watchers.
354   ExtensionTestMessageListener add_gallery_watch_finished(
355       kAddGalleryWatchRequestSucceeded, false  /* no reply */);
356   ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
357                           kAddGalleryWatchOK);
358   EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
359
360   // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return the watched
361   // gallery identifiers.
362   ExtensionTestMessageListener get_all_watched_galleries_finished(
363       kGetAllGalleryWatchResultB, false  /* no reply */);
364   ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
365                           kGetAllGalleryWatchOK);
366   EXPECT_TRUE(get_all_watched_galleries_finished.WaitUntilSatisfied());
367
368   // Remove all gallery watchers.
369   ExecuteCmdAndCheckReply(host, kRemoveAllGalleryWatchCmd,
370                           kRemoveAllGalleryWatchOK);
371
372   // Gallery watchers removed. chrome.mediaGalleriesPrivate.getAllGalleryWatch
373   // should return an empty list.
374   ExtensionTestMessageListener final_get_all_check_finished(
375       kGetAllGalleryWatchResultA, false  /* no reply */);
376   ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
377                           kGetAllGalleryWatchOK);
378   EXPECT_TRUE(final_get_all_check_finished.WaitUntilSatisfied());
379 }
380 #endif
381
382 #if !defined(OS_WIN) && !defined(OS_CHROMEOS)
383 // Gallery watch request is not enabled on non-windows platforms.
384 // Please refer to crbug.com/144491.
385 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
386                        SetupGalleryWatch) {
387   EnsureMediaDirectoriesExists media_directories;
388   content::RenderViewHost* host = GetBackgroundHostForTestExtension();
389   ASSERT_TRUE(host);
390
391   // Get media file systems.
392   ExtensionTestMessageListener get_media_systems_finished(
393       kGetMediaFileSystemsCallbackOK, false  /* no reply */);
394   ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
395                           kGetMediaFileSystemsOK);
396   EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
397
398   // Set up a invalid gallery watch.
399   ExtensionTestMessageListener gallery_watch_request_finished(
400       kAddGalleryWatchRequestFailed, false  /* no reply */);
401   // Set up gallery watch.
402   ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
403                           kAddGalleryWatchOK);
404   EXPECT_TRUE(gallery_watch_request_finished.WaitUntilSatisfied());
405 }
406
407 // Gallery watch request is not enabled on non-windows platforms.
408 // Please refer to crbug.com/144491.
409 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
410                        GetAllGalleryWatch) {
411   EnsureMediaDirectoriesExists media_directories;
412   content::RenderViewHost* host = GetBackgroundHostForTestExtension();
413   ASSERT_TRUE(host);
414
415   // Get media file systems.
416   ExtensionTestMessageListener get_media_systems_finished(
417       kGetMediaFileSystemsCallbackOK, false  /* no reply */);
418   ExecuteCmdAndCheckReply(host, kGetMediaFileSystemsCmd,
419                           kGetMediaFileSystemsOK);
420   EXPECT_TRUE(get_media_systems_finished.WaitUntilSatisfied());
421
422   // Set up gallery watch.
423   ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
424                           kAddGalleryWatchOK);
425
426   // Gallery watchers does not exists.
427   // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return an empty
428   // list.
429   ExtensionTestMessageListener get_all_gallery_watch_finished(
430       kGetAllGalleryWatchResultA, false  /* no reply */);
431   ExecuteCmdAndCheckReply(host, kGetAllWatchedGalleryIdsCmd,
432                           kGetAllGalleryWatchOK);
433   EXPECT_TRUE(get_all_gallery_watch_finished.WaitUntilSatisfied());
434 }
435 #endif