Home | History | Annotate | Download | only in win
      1 // Copyright 2013 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 #include <vector>
      6 
      7 #include "base/command_line.h"
      8 #include "base/files/file_path.h"
      9 #include "base/message_loop/message_loop.h"
     10 #include "base/run_loop.h"
     11 #include "base/stl_util.h"
     12 #include "chrome/browser/browser_process.h"
     13 #include "chrome/browser/extensions/extension_service.h"
     14 #include "chrome/browser/extensions/extension_system.h"
     15 #include "chrome/browser/extensions/test_extension_system.h"
     16 #include "chrome/browser/media_galleries/media_file_system_registry.h"
     17 #include "chrome/browser/media_galleries/media_galleries_test_util.h"
     18 #include "chrome/browser/storage_monitor/storage_info.h"
     19 #include "chrome/browser/storage_monitor/storage_monitor.h"
     20 #include "chrome/browser/storage_monitor/test_portable_device_watcher_win.h"
     21 #include "chrome/browser/storage_monitor/test_storage_monitor.h"
     22 #include "chrome/browser/storage_monitor/test_storage_monitor_win.h"
     23 #include "chrome/browser/storage_monitor/test_volume_mount_watcher_win.h"
     24 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
     25 #include "chrome/test/base/testing_browser_process.h"
     26 #include "chrome/test/base/testing_profile.h"
     27 #include "content/public/browser/render_view_host.h"
     28 #include "content/public/browser/web_contents.h"
     29 #include "testing/gtest/include/gtest/gtest.h"
     30 
     31 namespace chrome {
     32 
     33 namespace {
     34 
     35 typedef std::map<MediaGalleryPrefId, MediaFileSystemInfo> FSInfoMap;
     36 
     37 void GetGalleryInfoCallback(
     38     FSInfoMap* results,
     39     const std::vector<MediaFileSystemInfo>& file_systems) {
     40   for (size_t i = 0; i < file_systems.size(); ++i) {
     41     ASSERT_FALSE(ContainsKey(*results, file_systems[i].pref_id));
     42     (*results)[file_systems[i].pref_id] = file_systems[i];
     43   }
     44 }
     45 
     46 }  // namespace
     47 
     48 class MTPDeviceDelegateImplWinTest : public ChromeRenderViewHostTestHarness {
     49  protected:
     50   void SetUp() OVERRIDE;
     51   void TearDown() OVERRIDE;
     52 
     53   void ProcessAttach(const std::string& id,
     54                      const string16& name,
     55                      const base::FilePath::StringType& location);
     56   std::string AttachDevice(StorageInfo::Type type,
     57                            const std::string& unique_id,
     58                            const base::FilePath& location);
     59   void CheckGalleryInfo(const MediaFileSystemInfo& info,
     60                         const string16& name,
     61                         const base::FilePath& path,
     62                         bool removable,
     63                         bool media_device);
     64 
     65   // Pointer to the storage monitor. Owned by TestingBrowserProcess.
     66   test::TestStorageMonitorWin* monitor_;
     67   scoped_refptr<extensions::Extension> extension_;
     68 
     69   EnsureMediaDirectoriesExists media_directories_;
     70 };
     71 
     72 void MTPDeviceDelegateImplWinTest::SetUp() {
     73   ChromeRenderViewHostTestHarness::SetUp();
     74 
     75   test::TestStorageMonitor::RemoveSingleton();
     76   test::TestPortableDeviceWatcherWin* portable_device_watcher =
     77       new test::TestPortableDeviceWatcherWin;
     78   test::TestVolumeMountWatcherWin* mount_watcher =
     79       new test::TestVolumeMountWatcherWin;
     80   portable_device_watcher->set_use_dummy_mtp_storage_info(true);
     81   scoped_ptr<test::TestStorageMonitorWin> monitor(
     82       new test::TestStorageMonitorWin(
     83           mount_watcher, portable_device_watcher));
     84   TestingBrowserProcess* browser_process = TestingBrowserProcess::GetGlobal();
     85   DCHECK(browser_process);
     86   monitor_ = monitor.get();
     87   browser_process->SetStorageMonitor(monitor.Pass());
     88 
     89   base::RunLoop runloop;
     90   monitor_->EnsureInitialized(runloop.QuitClosure());
     91   runloop.Run();
     92 
     93   extensions::TestExtensionSystem* extension_system(
     94       static_cast<extensions::TestExtensionSystem*>(
     95           extensions::ExtensionSystem::Get(profile())));
     96   extension_system->CreateExtensionService(
     97       CommandLine::ForCurrentProcess(), base::FilePath(), false);
     98 
     99   std::vector<std::string> all_permissions;
    100   all_permissions.push_back("allAutoDetected");
    101   all_permissions.push_back("read");
    102   extension_ = AddMediaGalleriesApp("all", all_permissions, profile());
    103 }
    104 
    105 void MTPDeviceDelegateImplWinTest::TearDown() {
    106   // Windows storage monitor must be destroyed on the same thread
    107   // as construction.
    108   test::TestStorageMonitor::RemoveSingleton();
    109 
    110   ChromeRenderViewHostTestHarness::TearDown();
    111 }
    112 
    113 void MTPDeviceDelegateImplWinTest::ProcessAttach(
    114     const std::string& id,
    115     const string16& label,
    116     const base::FilePath::StringType& location) {
    117   StorageInfo info(id, string16(), location, label, string16(), string16(), 0);
    118   monitor_->receiver()->ProcessAttach(info);
    119 }
    120 
    121 std::string MTPDeviceDelegateImplWinTest::AttachDevice(
    122     StorageInfo::Type type,
    123     const std::string& unique_id,
    124     const base::FilePath& location) {
    125   std::string device_id = StorageInfo::MakeDeviceId(type, unique_id);
    126   DCHECK(StorageInfo::IsRemovableDevice(device_id));
    127   string16 label = location.LossyDisplayName();
    128   ProcessAttach(device_id, label, location.value());
    129   base::RunLoop().RunUntilIdle();
    130   return device_id;
    131 }
    132 
    133 void MTPDeviceDelegateImplWinTest::CheckGalleryInfo(
    134     const MediaFileSystemInfo& info,
    135     const string16& name,
    136     const base::FilePath& path,
    137     bool removable,
    138     bool media_device) {
    139   EXPECT_EQ(name, info.name);
    140   EXPECT_EQ(path, info.path);
    141   EXPECT_EQ(removable, info.removable);
    142   EXPECT_EQ(media_device, info.media_device);
    143   EXPECT_NE(0UL, info.pref_id);
    144 
    145   if (removable)
    146     EXPECT_NE(0UL, info.transient_device_id.size());
    147   else
    148     EXPECT_EQ(0UL, info.transient_device_id.size());
    149 }
    150 
    151 TEST_F(MTPDeviceDelegateImplWinTest, GalleryNameMTP) {
    152   base::FilePath location(
    153       PortableDeviceWatcherWin::GetStoragePathFromStorageId(
    154           test::TestPortableDeviceWatcherWin::kStorageUniqueIdA));
    155   AttachDevice(StorageInfo::MTP_OR_PTP, "mtp_fake_id", location);
    156 
    157   content::RenderViewHost* rvh = web_contents()->GetRenderViewHost();
    158   FSInfoMap results;
    159   MediaFileSystemRegistry* registry =
    160       g_browser_process->media_file_system_registry();
    161   registry->GetMediaFileSystemsForExtension(
    162       rvh, extension_.get(),
    163       base::Bind(&GetGalleryInfoCallback, base::Unretained(&results)));
    164   base::RunLoop().RunUntilIdle();
    165 
    166   ASSERT_EQ(media_directories_.num_galleries() + 1, results.size());
    167   bool checked = false;
    168   for (FSInfoMap::iterator i = results.begin(); i != results.end(); ++i) {
    169     MediaFileSystemInfo info = i->second;
    170     if (info.path == location) {
    171       CheckGalleryInfo(info, location.LossyDisplayName(), location, true, true);
    172       checked = true;
    173       break;
    174     }
    175   }
    176   EXPECT_TRUE(checked);
    177 }
    178 
    179 }  // namespace chrome
    180