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