Chromium Code Reviews| Index: chrome/browser/extensions/api/image_writer_private/removable_storage_provider_chromeos_unittest.cc |
| diff --git a/chrome/browser/extensions/api/image_writer_private/removable_storage_provider_chromeos_unittest.cc b/chrome/browser/extensions/api/image_writer_private/removable_storage_provider_chromeos_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..890622a3c2d5c7a2ad12286b469cc68422f5092b |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/image_writer_private/removable_storage_provider_chromeos_unittest.cc |
| @@ -0,0 +1,156 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/bind.h" |
| +#include "chrome/browser/extensions/api/image_writer_private/removable_storage_provider.h" |
| +#include "chromeos/disks/mock_disk_mount_manager.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace extensions { |
| + |
| +namespace { |
| + |
| +using namespace chromeos::disks; |
| +using namespace api::image_writer_private; |
| + |
| +const char kMountPath[] = "/mnt"; |
| +const char kDeviceId[] = "FFFF-FFFF"; |
| +const char kDeviceName[] = "USB Disk 1"; |
| +const char kVendorName[] = "Vendor"; |
| +const char kProductName[] = "Product"; |
| +const uint64 kDeviceSize = 1024 * 1024 * 1024; |
| + |
| +const char kUnknownSDDiskModel[] = "SD Card"; |
| +const char kUnknownUSBDiskModel[] = "USB Drive"; |
| + |
| +class RemovableStorageProviderChromeOsUnitTest : public testing::Test { |
| + public: |
| + virtual void SetUp() OVERRIDE { |
| + disk_mount_manager_mock_ = new MockDiskMountManager(); |
| + DiskMountManager::InitializeForTesting(disk_mount_manager_mock_); |
| + disk_mount_manager_mock_->SetupDefaultReplies(); |
| + } |
| + |
| + virtual void TearDown() OVERRIDE { DiskMountManager::Shutdown(); } |
| + |
| + void DevicesCallback(scoped_refptr<StorageDeviceList> devices, bool success) { |
| + devices_ = devices; |
| + } |
| + |
| + void CreateDisk(const std::string& device_path, |
| + chromeos::DeviceType device_type, |
| + bool is_parent, |
| + bool has_media, |
| + bool on_boot_device) { |
| + return CreateDisk(device_path, |
| + kVendorName, |
| + kProductName, |
| + device_type, |
| + is_parent, |
| + has_media, |
| + on_boot_device); |
| + } |
| + |
| + void CreateDisk(const std::string& device_path, |
| + const std::string& vendor_name, |
| + const std::string& product_name, |
| + chromeos::DeviceType device_type, |
| + bool is_parent, |
| + bool has_media, |
| + bool on_boot_device) { |
| + DiskMountManager::MountPointInfo mount_info( |
| + device_path, |
| + kMountPath, |
| + chromeos::MOUNT_TYPE_DEVICE, |
| + chromeos::disks::MOUNT_CONDITION_NONE); |
| + disk_mount_manager_mock_->CreateDiskEntryForMountDevice(mount_info, |
| + kDeviceId, |
| + kDeviceName, |
| + vendor_name, |
| + product_name, |
| + device_type, |
| + kDeviceSize, |
| + is_parent, |
| + has_media, |
| + on_boot_device); |
| + } |
| + |
| + // Checks if the DeviceList has a specific entry. |
| + bool DeviceListHasEntry(StorageDeviceList* list, |
|
tbarzic
2014/03/26 00:33:56
storage_unit_id should be unique, so it should be
Drew Haven
2014/03/27 01:47:34
Done. It looks reasonably clean to factor out the
|
| + const std::string& file_path, |
| + int64 capacity, |
| + const std::string& vendor_name, |
| + const std::string& product_name) { |
| + for (std::vector<linked_ptr<RemovableStorageDevice> >::const_iterator iter = |
| + list->data.begin(); |
| + iter != list->data.end(); |
| + ++iter) { |
| + if ((*iter)->storage_unit_id == file_path && |
| + (*iter)->capacity == capacity && (*iter)->vendor == vendor_name && |
| + (*iter)->model == product_name) { |
| + return true; |
| + } |
| + } |
| + return false; |
| + } |
| + |
| + MockDiskMountManager* disk_mount_manager_mock_; |
| + scoped_refptr<StorageDeviceList> devices_; |
| +}; |
| + |
| +} // namespace |
| + |
| +// Tests that GetAllDevices works as expected, only exposing USB and SD cards |
| +// that are parents, have media and are not boot devices. Other flags are |
| +// uninteresting or should not occur for these device types. |
| +TEST_F(RemovableStorageProviderChromeOsUnitTest, GetAllDevices) { |
| + CreateDisk("/dev/usb", chromeos::DEVICE_TYPE_USB, true, true, false); |
| + CreateDisk("/dev/sd", chromeos::DEVICE_TYPE_SD, true, true, false); |
| + CreateDisk("/dev/NotParent", chromeos::DEVICE_TYPE_USB, false, true, false); |
| + CreateDisk("/dev/NoMedia", chromeos::DEVICE_TYPE_USB, true, false, false); |
| + CreateDisk("/dev/OnBootDevice", chromeos::DEVICE_TYPE_USB, true, true, true); |
| + |
| + RemovableStorageProvider::GetAllDevices( |
| + base::Bind(&RemovableStorageProviderChromeOsUnitTest::DevicesCallback, |
| + base::Unretained(this))); |
| + |
| + ASSERT_EQ(2U, devices_->data.size()); |
| + |
| + EXPECT_TRUE(DeviceListHasEntry( |
| + devices_.get(), "/dev/usb", kDeviceSize, kVendorName, kProductName)); |
| + EXPECT_TRUE(DeviceListHasEntry( |
| + devices_.get(), "/dev/sd", kDeviceSize, kVendorName, kProductName)); |
| +} |
| + |
| +// Tests that a USB drive with an empty vendor and product gets a generic name. |
| +TEST_F(RemovableStorageProviderChromeOsUnitTest, |
| + EmptyProductAndModelForUsbDrive) { |
| + CreateDisk("/dev/usb", "", "", chromeos::DEVICE_TYPE_USB, true, true, false); |
| + |
| + RemovableStorageProvider::GetAllDevices( |
| + base::Bind(&RemovableStorageProviderChromeOsUnitTest::DevicesCallback, |
| + base::Unretained(this))); |
| + |
| + ASSERT_EQ(1U, devices_->data.size()); |
| + |
| + EXPECT_TRUE(DeviceListHasEntry( |
| + devices_.get(), "/dev/usb", kDeviceSize, "", kUnknownUSBDiskModel)); |
| +} |
| + |
| +// Tests that a SD card with an empty vendor and product gets a generic name. |
| +TEST_F(RemovableStorageProviderChromeOsUnitTest, |
| + EmptyProductAndModelForSdCard) { |
| + CreateDisk("/dev/sd", "", "", chromeos::DEVICE_TYPE_SD, true, true, false); |
| + |
| + RemovableStorageProvider::GetAllDevices( |
| + base::Bind(&RemovableStorageProviderChromeOsUnitTest::DevicesCallback, |
| + base::Unretained(this))); |
| + |
| + ASSERT_EQ(1U, devices_->data.size()); |
| + |
| + EXPECT_TRUE(DeviceListHasEntry( |
| + devices_.get(), "/dev/sd", kDeviceSize, "", kUnknownSDDiskModel)); |
| +} |
| + |
| +} // namespace extensions |