Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / image_writer_private / removable_storage_provider_chromeos_unittest.cc
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.
4
5 #include "base/bind.h"
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"
9
10 namespace extensions {
11
12 namespace {
13
14 using namespace chromeos::disks;
15 using namespace api::image_writer_private;
16
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;
25
26 const char kUnknownSDDiskModel[] = "SD Card";
27 const char kUnknownUSBDiskModel[] = "USB Drive";
28
29 class RemovableStorageProviderChromeOsUnitTest : public testing::Test {
30  public:
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();
35   }
36
37   virtual void TearDown() OVERRIDE { DiskMountManager::Shutdown(); }
38
39   void DevicesCallback(scoped_refptr<StorageDeviceList> devices, bool success) {
40     devices_ = devices;
41   }
42
43   void CreateDisk(const std::string& device_path,
44                   chromeos::DeviceType device_type,
45                   bool is_parent,
46                   bool has_media,
47                   bool on_boot_device) {
48     return CreateDisk(device_path,
49                       kVendorName,
50                       kProductName,
51                       device_type,
52                       is_parent,
53                       has_media,
54                       on_boot_device);
55   }
56
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,
61                   bool is_parent,
62                   bool has_media,
63                   bool on_boot_device) {
64     DiskMountManager::MountPointInfo mount_info(
65         device_path,
66         kMountPath,
67         chromeos::MOUNT_TYPE_DEVICE,
68         chromeos::disks::MOUNT_CONDITION_NONE);
69     disk_mount_manager_mock_->CreateDiskEntryForMountDevice(mount_info,
70                                                             kDeviceId,
71                                                             kDeviceName,
72                                                             vendor_name,
73                                                             product_name,
74                                                             device_type,
75                                                             kDeviceSize,
76                                                             is_parent,
77                                                             has_media,
78                                                             on_boot_device);
79   }
80
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 =
85              list->data.begin();
86          iter != list->data.end();
87          ++iter) {
88       if ((*iter)->storage_unit_id == file_path) {
89         return (*iter).get();
90       }
91     }
92     return NULL;
93   }
94
95   void ExpectDevice(StorageDeviceList* list,
96                     const std::string& device_path,
97                     const std::string& vendor,
98                     const std::string& model,
99                     uint64 capacity) {
100     RemovableStorageDevice* device = FindDevice(devices_, device_path);
101
102     ASSERT_TRUE(device != NULL);
103
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);
108   }
109
110   MockDiskMountManager* disk_mount_manager_mock_;
111   scoped_refptr<StorageDeviceList> devices_;
112 };
113
114 }  // namespace
115
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);
125
126   RemovableStorageProvider::GetAllDevices(
127       base::Bind(&RemovableStorageProviderChromeOsUnitTest::DevicesCallback,
128                  base::Unretained(this)));
129
130   ASSERT_EQ(2U, devices_->data.size());
131
132   ExpectDevice(
133       devices_, kDevicePathUSB, kVendorName, kProductName, kDeviceSize);
134   ExpectDevice(devices_, kDevicePathSD, kVendorName, kProductName, kDeviceSize);
135 }
136
137 // Tests that a USB drive with an empty vendor and product gets a generic name.
138 TEST_F(RemovableStorageProviderChromeOsUnitTest, EmptyProductAndModel) {
139   CreateDisk(
140       kDevicePathUSB, "", "", chromeos::DEVICE_TYPE_USB, true, true, false);
141   CreateDisk(
142       kDevicePathSD, "", "", chromeos::DEVICE_TYPE_SD, true, true, false);
143
144   RemovableStorageProvider::GetAllDevices(
145       base::Bind(&RemovableStorageProviderChromeOsUnitTest::DevicesCallback,
146                  base::Unretained(this)));
147
148   ASSERT_EQ(2U, devices_->data.size());
149
150   ExpectDevice(devices_, kDevicePathUSB, "", kUnknownUSBDiskModel, kDeviceSize);
151   ExpectDevice(devices_, kDevicePathSD, "", kUnknownSDDiskModel, kDeviceSize);
152 }
153
154 }  // namespace extensions