1 // Copyright 2014 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.
6 #include "base/run_loop.h"
7 #include "chrome/browser/extensions/api/image_writer_private/removable_storage_provider.h"
8 #include "chromeos/disks/mock_disk_mount_manager.h"
9 #include "content/public/test/test_browser_thread_bundle.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace extensions {
16 using namespace chromeos::disks;
17 using namespace api::image_writer_private;
19 const char kDevicePathUSB[] = "/dev/test-usb";
20 const char kDevicePathSD[] = "/dev/test-sd";
21 const char kMountPath[] = "/test-mount";
22 const char kDeviceId[] = "FFFF-FFFF";
23 const char kDeviceName[] = "Test Device Name";
24 const char kVendorName[] = "Test Vendor";
25 const char kProductName[] = "Test Product";
26 const uint64 kDeviceSize = 1024 * 1024 * 1024;
27 const bool kOnRemovableDevice = true;
29 const char kUnknownSDDiskModel[] = "SD Card";
30 const char kUnknownUSBDiskModel[] = "USB Drive";
32 class RemovableStorageProviderChromeOsUnitTest : public testing::Test {
34 virtual void SetUp() override {
35 disk_mount_manager_mock_ = new MockDiskMountManager();
36 DiskMountManager::InitializeForTesting(disk_mount_manager_mock_);
37 disk_mount_manager_mock_->SetupDefaultReplies();
40 virtual void TearDown() override { DiskMountManager::Shutdown(); }
42 void DevicesCallback(scoped_refptr<StorageDeviceList> devices, bool success) {
46 void CreateDisk(const std::string& device_path,
47 chromeos::DeviceType device_type,
50 bool on_boot_device) {
51 return CreateDisk(device_path,
60 void CreateDisk(const std::string& device_path,
61 const std::string& vendor_name,
62 const std::string& product_name,
63 chromeos::DeviceType device_type,
66 bool on_boot_device) {
67 DiskMountManager::MountPointInfo mount_info(
70 chromeos::MOUNT_TYPE_DEVICE,
71 chromeos::disks::MOUNT_CONDITION_NONE);
72 disk_mount_manager_mock_->CreateDiskEntryForMountDevice(mount_info,
85 // Checks if the DeviceList has a specific entry.
86 RemovableStorageDevice* FindDevice(StorageDeviceList* list,
87 const std::string& file_path) {
88 for (std::vector<linked_ptr<RemovableStorageDevice> >::const_iterator iter =
90 iter != list->data.end();
92 if ((*iter)->storage_unit_id == file_path) {
99 void ExpectDevice(StorageDeviceList* list,
100 const std::string& device_path,
101 const std::string& vendor,
102 const std::string& model,
104 RemovableStorageDevice* device = FindDevice(devices_.get(), device_path);
106 ASSERT_TRUE(device != NULL);
108 EXPECT_EQ(device_path, device->storage_unit_id);
109 EXPECT_EQ(vendor, device->vendor);
110 EXPECT_EQ(model, device->model);
111 EXPECT_EQ(capacity, device->capacity);
114 MockDiskMountManager* disk_mount_manager_mock_;
115 scoped_refptr<StorageDeviceList> devices_;
118 content::TestBrowserThreadBundle thread_bundle_;
123 // Tests that GetAllDevices works as expected, only exposing USB and SD cards
124 // that are parents, have media and are not boot devices. Other flags are
125 // uninteresting or should not occur for these device types.
126 TEST_F(RemovableStorageProviderChromeOsUnitTest, GetAllDevices) {
127 CreateDisk(kDevicePathUSB, chromeos::DEVICE_TYPE_USB, true, true, false);
128 CreateDisk(kDevicePathSD, chromeos::DEVICE_TYPE_SD, true, true, false);
129 CreateDisk("/dev/NotParent", chromeos::DEVICE_TYPE_USB, false, true, false);
130 CreateDisk("/dev/NoMedia", chromeos::DEVICE_TYPE_USB, true, false, false);
131 CreateDisk("/dev/OnBootDevice", chromeos::DEVICE_TYPE_USB, true, true, true);
133 RemovableStorageProvider::GetAllDevices(
134 base::Bind(&RemovableStorageProviderChromeOsUnitTest::DevicesCallback,
135 base::Unretained(this)));
137 base::RunLoop().RunUntilIdle();
139 ASSERT_EQ(2U, devices_->data.size());
142 devices_.get(), kDevicePathUSB, kVendorName, kProductName, kDeviceSize);
144 devices_.get(), kDevicePathSD, kVendorName, kProductName, kDeviceSize);
147 // Tests that a USB drive with an empty vendor and product gets a generic name.
148 TEST_F(RemovableStorageProviderChromeOsUnitTest, EmptyProductAndModel) {
150 kDevicePathUSB, "", "", chromeos::DEVICE_TYPE_USB, true, true, false);
152 kDevicePathSD, "", "", chromeos::DEVICE_TYPE_SD, true, true, false);
154 RemovableStorageProvider::GetAllDevices(
155 base::Bind(&RemovableStorageProviderChromeOsUnitTest::DevicesCallback,
156 base::Unretained(this)));
158 base::RunLoop().RunUntilIdle();
160 ASSERT_EQ(2U, devices_->data.size());
163 devices_.get(), kDevicePathUSB, "", kUnknownUSBDiskModel, kDeviceSize);
165 devices_.get(), kDevicePathSD, "", kUnknownSDDiskModel, kDeviceSize);
168 } // namespace extensions