afa0286d1977c96fd31af3b9e0c4664ae2f81e16
[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 "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"
22
23 namespace {
24
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";
29
30 #if !defined(OS_CHROMEOS)
31 // JS commands.
32 const char kGetAllWatchedGalleryIdsCmd[] = "getAllWatchedGalleryIds()";
33 const char kGetMediaFileSystemsCmd[] = "getMediaFileSystems()";
34 const char kSetupWatchOnValidGalleriesCmd[] = "setupWatchOnValidGalleries()";
35 #if defined(OS_WIN)
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)
43
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";
50 #if defined(OS_WIN)
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)
57
58 // Test reply messages.
59 const char kGetAllGalleryWatchResultA[] = "gallery_watchers_does_not_exists";
60 const char kAddGalleryWatchRequestFailed[] = "add_watch_request_failed";
61 #if defined(OS_WIN)
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)
68
69 }  // namespace
70
71
72 ///////////////////////////////////////////////////////////////////////////////
73 //                 MediaGalleriesPrivateGalleryWatchApiTest                  //
74 ///////////////////////////////////////////////////////////////////////////////
75
76 class MediaGalleriesPrivateGalleryWatchApiTest : public ExtensionApiTest {
77  public:
78   MediaGalleriesPrivateGalleryWatchApiTest() {}
79   virtual ~MediaGalleriesPrivateGalleryWatchApiTest() {}
80
81  protected:
82   // ExtensionApiTest overrides.
83   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
84     ExtensionApiTest::SetUpCommandLine(command_line);
85     command_line->AppendSwitchASCII(
86         extensions::switches::kWhitelistedExtensionID,
87         kTestExtensionId);
88   }
89
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());
96   }
97
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))
102       return false;
103
104     base::FilePath gallery_dir;
105     if (!PathService::Get(gallery_directory_key, &gallery_dir))
106       return false;
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(),
111                                      content.length());
112     return (write_size == static_cast<int>(content.length()));
113   }
114
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));
120     if (!extension)
121       return NULL;
122     return extensions::ExtensionSystem::Get(browser()->profile())->
123         process_manager()->GetBackgroundHostForExtension(extension->id())->
124            render_view_host();
125   }
126
127  private:
128   DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPrivateGalleryWatchApiTest);
129 };
130
131
132 ///////////////////////////////////////////////////////////////////////////////
133 //                               TESTS                                       //
134 ///////////////////////////////////////////////////////////////////////////////
135 #if defined(OS_WIN)
136 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
137                        BasicGalleryWatch) {
138   EnsureMediaDirectoriesExists media_directories;
139   content::RenderViewHost* host = GetBackgroundHostForTestExtension();
140   ASSERT_TRUE(host);
141
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());
148
149   // Set up gallery watch.
150   ExtensionTestMessageListener add_gallery_watch_finished(
151       kAddGalleryWatchRequestSucceeded, false  /* no reply */);
152   ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
153                           kAddGalleryWatchOK);
154   EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
155
156   // Add gallery watch listener.
157   ExecuteCmdAndCheckReply(host, kAddGalleryChangedListenerCmd,
158                           kAddGalleryChangedListenerOK);
159
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());
165
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());
170
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());
175
176   // Remove gallery watch listener.
177   ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
178                           kRemoveGalleryChangedListenerOK);
179
180   // Remove gallery watch request.
181   ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
182 }
183
184 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
185                        RemoveListenerAndModifyGallery) {
186   EnsureMediaDirectoriesExists media_directories;
187   content::RenderViewHost* host = GetBackgroundHostForTestExtension();
188   ASSERT_TRUE(host);
189
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());
196
197   // Set up gallery watch.
198   ExtensionTestMessageListener add_gallery_watch_finished(
199       kAddGalleryWatchRequestSucceeded, false  /* no reply */);
200   ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
201                          kAddGalleryWatchOK);
202   EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
203
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());
212
213   // Remove gallery watch listener.
214   ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
215                           kRemoveGalleryChangedListenerOK);
216
217   // No listener, modify gallery contents.
218   ASSERT_TRUE(AddNewFileInGallery(chrome::DIR_USER_MUSIC));
219
220   // Remove gallery watch.
221   ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
222 }
223
224 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
225                        SetupGalleryWatchWithoutListeners) {
226   EnsureMediaDirectoriesExists media_directories;
227   content::RenderViewHost* host = GetBackgroundHostForTestExtension();
228   ASSERT_TRUE(host);
229
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());
236
237   // Set up gallery watch.
238   ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
239                           kAddGalleryWatchOK);
240
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));
245
246   // Remove gallery watch.
247   ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
248 }
249
250 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
251                        SetupGalleryChangedListenerWithoutWatchers) {
252   EnsureMediaDirectoriesExists media_directories;
253   content::RenderViewHost* host = GetBackgroundHostForTestExtension();
254   ASSERT_TRUE(host);
255
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());
262
263   // Add gallery watch listener.
264   ExecuteCmdAndCheckReply(host, kAddGalleryChangedListenerCmd,
265                           kAddGalleryChangedListenerOK);
266
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));
272
273   // Remove gallery watch listener.
274   ExecuteCmdAndCheckReply(host, kRemoveGalleryChangedListenerCmd,
275                           kRemoveGalleryChangedListenerOK);
276 }
277
278 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
279                        SetupWatchOnInvalidGallery) {
280   content::RenderViewHost* host = GetBackgroundHostForTestExtension();
281   ASSERT_TRUE(host);
282
283   // Set up a invalid gallery watch.
284   ExtensionTestMessageListener invalid_gallery_watch_request_finished(
285       kAddGalleryWatchRequestFailed, false  /* no reply */);
286   ExecuteCmdAndCheckReply(host, kSetupWatchOnInvalidGalleryCmd,
287                           kAddGalleryWatchOK);
288   EXPECT_TRUE(invalid_gallery_watch_request_finished.WaitUntilSatisfied());
289 }
290
291 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
292                        GetAllGalleryWatch) {
293   EnsureMediaDirectoriesExists media_directories;
294   content::RenderViewHost* host = GetBackgroundHostForTestExtension();
295   ASSERT_TRUE(host);
296
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());
303
304   // Gallery watchers are not yet added.
305   // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return an empty
306   // list.
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());
312
313   // Set up gallery watchers.
314   ExtensionTestMessageListener add_gallery_watch_finished(
315       kAddGalleryWatchRequestSucceeded, false  /* no reply */);
316   ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
317                           kAddGalleryWatchOK);
318   EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
319
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());
327
328   // Remove gallery watch request.
329   ExecuteCmdAndCheckReply(host, kRemoveGalleryWatchCmd, kRemoveGalleryWatchOK);
330
331   // Gallery watchers removed.
332   // chrome.mediaGalleriesPrivate.getAllGalleryWatch() should return an empty
333   // list.
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());
339 }
340
341 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateGalleryWatchApiTest,
342                        RemoveAllGalleryWatch) {
343   EnsureMediaDirectoriesExists media_directories;
344   content::RenderViewHost* host = GetBackgroundHostForTestExtension();
345   ASSERT_TRUE(host);
346
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());
353
354   // Set up gallery watchers.
355   ExtensionTestMessageListener add_gallery_watch_finished(
356       kAddGalleryWatchRequestSucceeded, false  /* no reply */);
357   ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
358                           kAddGalleryWatchOK);
359   EXPECT_TRUE(add_gallery_watch_finished.WaitUntilSatisfied());
360
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());
368
369   // Remove all gallery watchers.
370   ExecuteCmdAndCheckReply(host, kRemoveAllGalleryWatchCmd,
371                           kRemoveAllGalleryWatchOK);
372
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());
380 }
381 #endif
382
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,
387                        SetupGalleryWatch) {
388   EnsureMediaDirectoriesExists media_directories;
389   content::RenderViewHost* host = GetBackgroundHostForTestExtension();
390   ASSERT_TRUE(host);
391
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());
398
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,
404                           kAddGalleryWatchOK);
405   EXPECT_TRUE(gallery_watch_request_finished.WaitUntilSatisfied());
406 }
407
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();
414   ASSERT_TRUE(host);
415
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());
422
423   // Set up gallery watch.
424   ExecuteCmdAndCheckReply(host, kSetupWatchOnValidGalleriesCmd,
425                           kAddGalleryWatchOK);
426
427   // Gallery watchers does not exists.
428   // chrome.mediaGalleriesPrivate.getAllGalleryWatch should return an empty
429   // list.
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());
435 }
436 #endif