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 "chrome/browser/extensions/api/image_writer_private/removable_storage_provider.h"
7 #include "chromeos/disks/mock_disk_mount_manager.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace extensions {
14 using namespace chromeos::disks;
15 using namespace api::image_writer_private;
17 const char kDevicePathUSB[] = "/dev/test-usb";
18 const char kDevicePathSD[] = "/dev/test-sd";
19 const char kMountPath[] = "/test-mount";
20 const char kDeviceId[] = "FFFF-FFFF";
21 const char kDeviceName[] = "Test Device Name";
22 const char kVendorName[] = "Test Vendor";
23 const char kProductName[] = "Test Product";
24 const uint64 kDeviceSize = 1024 * 1024 * 1024;
26 const char kUnknownSDDiskModel[] = "SD Card";
27 const char kUnknownUSBDiskModel[] = "USB Drive";
29 class RemovableStorageProviderChromeOsUnitTest : public testing::Test {
31 virtual void SetUp() OVERRIDE {
32 disk_mount_manager_mock_ = new MockDiskMountManager();
33 DiskMountManager::InitializeForTesting(disk_mount_manager_mock_);
34 disk_mount_manager_mock_->SetupDefaultReplies();
37 virtual void TearDown() OVERRIDE { DiskMountManager::Shutdown(); }
39 void DevicesCallback(scoped_refptr<StorageDeviceList> devices, bool success) {
43 void CreateDisk(const std::string& device_path,
44 chromeos::DeviceType device_type,
47 bool on_boot_device) {
48 return CreateDisk(device_path,
57 void CreateDisk(const std::string& device_path,
58 const std::string& vendor_name,
59 const std::string& product_name,
60 chromeos::DeviceType device_type,
63 bool on_boot_device) {
64 DiskMountManager::MountPointInfo mount_info(
67 chromeos::MOUNT_TYPE_DEVICE,
68 chromeos::disks::MOUNT_CONDITION_NONE);
69 disk_mount_manager_mock_->CreateDiskEntryForMountDevice(mount_info,
81 // Checks if the DeviceList has a specific entry.
82 RemovableStorageDevice* FindDevice(StorageDeviceList* list,
83 const std::string& file_path) {
84 for (std::vector<linked_ptr<RemovableStorageDevice> >::const_iterator iter =
86 iter != list->data.end();
88 if ((*iter)->storage_unit_id == file_path) {
95 void ExpectDevice(StorageDeviceList* list,
96 const std::string& device_path,
97 const std::string& vendor,
98 const std::string& model,
100 RemovableStorageDevice* device = FindDevice(devices_, device_path);
102 ASSERT_TRUE(device != NULL);
104 EXPECT_EQ(device_path, device->storage_unit_id);
105 EXPECT_EQ(vendor, device->vendor);
106 EXPECT_EQ(model, device->model);
107 EXPECT_EQ(capacity, device->capacity);
110 MockDiskMountManager* disk_mount_manager_mock_;
111 scoped_refptr<StorageDeviceList> devices_;
116 // Tests that GetAllDevices works as expected, only exposing USB and SD cards
117 // that are parents, have media and are not boot devices. Other flags are
118 // uninteresting or should not occur for these device types.
119 TEST_F(RemovableStorageProviderChromeOsUnitTest, GetAllDevices) {
120 CreateDisk(kDevicePathUSB, chromeos::DEVICE_TYPE_USB, true, true, false);
121 CreateDisk(kDevicePathSD, chromeos::DEVICE_TYPE_SD, true, true, false);
122 CreateDisk("/dev/NotParent", chromeos::DEVICE_TYPE_USB, false, true, false);
123 CreateDisk("/dev/NoMedia", chromeos::DEVICE_TYPE_USB, true, false, false);
124 CreateDisk("/dev/OnBootDevice", chromeos::DEVICE_TYPE_USB, true, true, true);
126 RemovableStorageProvider::GetAllDevices(
127 base::Bind(&RemovableStorageProviderChromeOsUnitTest::DevicesCallback,
128 base::Unretained(this)));
130 ASSERT_EQ(2U, devices_->data.size());
133 devices_, kDevicePathUSB, kVendorName, kProductName, kDeviceSize);
134 ExpectDevice(devices_, kDevicePathSD, kVendorName, kProductName, kDeviceSize);
137 // Tests that a USB drive with an empty vendor and product gets a generic name.
138 TEST_F(RemovableStorageProviderChromeOsUnitTest, EmptyProductAndModel) {
140 kDevicePathUSB, "", "", chromeos::DEVICE_TYPE_USB, true, true, false);
142 kDevicePathSD, "", "", chromeos::DEVICE_TYPE_SD, true, true, false);
144 RemovableStorageProvider::GetAllDevices(
145 base::Bind(&RemovableStorageProviderChromeOsUnitTest::DevicesCallback,
146 base::Unretained(this)));
148 ASSERT_EQ(2U, devices_->data.size());
150 ExpectDevice(devices_, kDevicePathUSB, "", kUnknownUSBDiskModel, kDeviceSize);
151 ExpectDevice(devices_, kDevicePathSD, "", kUnknownSDDiskModel, kDeviceSize);
154 } // namespace extensions