Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / storage_monitor / storage_monitor_mac_unittest.mm
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 "components/storage_monitor/storage_monitor_mac.h"
6
7 #include "base/bind_helpers.h"
8 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/mac/foundation_util.h"
11 #include "base/run_loop.h"
12 #include "base/strings/sys_string_conversions.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "components/storage_monitor/mock_removable_storage_observer.h"
15 #include "components/storage_monitor/removable_device_constants.h"
16 #include "components/storage_monitor/storage_info.h"
17 #include "components/storage_monitor/test_storage_monitor.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 uint64 kTestSize = 1000000ULL;
23
24 namespace {
25
26 StorageInfo CreateStorageInfo(
27     const std::string& device_id,
28     const std::string& model_name,
29     const base::FilePath& mount_point,
30     uint64 size_bytes) {
31   return StorageInfo(
32       device_id, base::string16(), mount_point.value(),
33       base::string16(), base::string16(), base::UTF8ToUTF16(model_name),
34       size_bytes);
35 }
36
37 }  // namespace
38
39 class StorageMonitorMacTest : public testing::Test {
40  public:
41   StorageMonitorMacTest() {}
42
43   virtual void SetUp() OVERRIDE {
44     monitor_.reset(new StorageMonitorMac);
45
46     mock_storage_observer_.reset(new MockRemovableStorageObserver);
47     monitor_->AddObserver(mock_storage_observer_.get());
48
49     unique_id_ = "test_id";
50     mount_point_ = base::FilePath("/unused_test_directory");
51     device_id_ = StorageInfo::MakeDeviceId(
52         StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM, unique_id_);
53     disk_info_ = CreateStorageInfo(device_id_, "",
54                                    mount_point_, kTestSize);
55   }
56
57   void UpdateDisk(StorageInfo info, StorageMonitorMac::UpdateType update_type) {
58     content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
59         base::Bind(&StorageMonitorMac::UpdateDisk,
60                    base::Unretained(monitor_.get()),
61                    "dummy_bsd_name", info, update_type));
62     base::RunLoop().RunUntilIdle();
63   }
64
65  protected:
66   content::TestBrowserThreadBundle thread_bundle_;
67
68   scoped_ptr<MockRemovableStorageObserver> mock_storage_observer_;
69
70   // Information about the disk.
71   std::string unique_id_;
72   base::FilePath mount_point_;
73   std::string device_id_;
74   StorageInfo disk_info_;
75
76   scoped_ptr<StorageMonitorMac> monitor_;
77 };
78
79 TEST_F(StorageMonitorMacTest, AddRemove) {
80   UpdateDisk(disk_info_, StorageMonitorMac::UPDATE_DEVICE_ADDED);
81
82   EXPECT_EQ(1, mock_storage_observer_->attach_calls());
83   EXPECT_EQ(0, mock_storage_observer_->detach_calls());
84   EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id());
85   EXPECT_EQ(base::string16(), mock_storage_observer_->last_attached().name());
86   EXPECT_EQ(mount_point_.value(),
87             mock_storage_observer_->last_attached().location());
88
89   UpdateDisk(disk_info_, StorageMonitorMac::UPDATE_DEVICE_REMOVED);
90
91   EXPECT_EQ(1, mock_storage_observer_->attach_calls());
92   EXPECT_EQ(1, mock_storage_observer_->detach_calls());
93   EXPECT_EQ(device_id_, mock_storage_observer_->last_detached().device_id());
94 }
95
96 TEST_F(StorageMonitorMacTest, UpdateVolumeName) {
97   UpdateDisk(disk_info_, StorageMonitorMac::UPDATE_DEVICE_ADDED);
98
99   EXPECT_EQ(1, mock_storage_observer_->attach_calls());
100   EXPECT_EQ(0, mock_storage_observer_->detach_calls());
101   EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id());
102   EXPECT_EQ(base::string16(), mock_storage_observer_->last_attached().name());
103   EXPECT_EQ(kTestSize,
104             mock_storage_observer_->last_attached().total_size_in_bytes());
105   EXPECT_EQ(mount_point_.value(),
106             mock_storage_observer_->last_attached().location());
107
108   StorageInfo info2 = CreateStorageInfo(
109       device_id_, "", mount_point_, kTestSize * 2);
110   UpdateDisk(info2, StorageMonitorMac::UPDATE_DEVICE_CHANGED);
111   base::RunLoop().RunUntilIdle();
112
113   EXPECT_EQ(1, mock_storage_observer_->detach_calls());
114   EXPECT_EQ(device_id_, mock_storage_observer_->last_detached().device_id());
115   EXPECT_EQ(2, mock_storage_observer_->attach_calls());
116   EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id());
117   EXPECT_EQ(base::string16(), mock_storage_observer_->last_attached().name());
118   EXPECT_EQ(kTestSize * 2,
119             mock_storage_observer_->last_attached().total_size_in_bytes());
120   EXPECT_EQ(mount_point_.value(),
121             mock_storage_observer_->last_attached().location());
122 }
123
124 TEST_F(StorageMonitorMacTest, DCIM) {
125   base::ScopedTempDir temp_dir;
126   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
127   ASSERT_TRUE(base::CreateDirectory(
128       temp_dir.path().Append(kDCIMDirectoryName)));
129
130   base::FilePath mount_point = temp_dir.path();
131   std::string device_id = StorageInfo::MakeDeviceId(
132       StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, unique_id_);
133   StorageInfo info = CreateStorageInfo(device_id, "", mount_point, kTestSize);
134
135   UpdateDisk(info, StorageMonitorMac::UPDATE_DEVICE_ADDED);
136
137   EXPECT_EQ(1, mock_storage_observer_->attach_calls());
138   EXPECT_EQ(0, mock_storage_observer_->detach_calls());
139   EXPECT_EQ(device_id, mock_storage_observer_->last_attached().device_id());
140   EXPECT_EQ(base::string16(), mock_storage_observer_->last_attached().name());
141   EXPECT_EQ(mount_point.value(),
142             mock_storage_observer_->last_attached().location());
143 }
144
145 TEST_F(StorageMonitorMacTest, GetStorageInfo) {
146   UpdateDisk(disk_info_, StorageMonitorMac::UPDATE_DEVICE_ADDED);
147
148   EXPECT_EQ(1, mock_storage_observer_->attach_calls());
149   EXPECT_EQ(0, mock_storage_observer_->detach_calls());
150   EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id());
151   EXPECT_EQ(base::string16(), mock_storage_observer_->last_attached().name());
152   EXPECT_EQ(mount_point_.value(),
153             mock_storage_observer_->last_attached().location());
154
155   StorageInfo info;
156   EXPECT_TRUE(monitor_->GetStorageInfoForPath(mount_point_.AppendASCII("foo"),
157                                               &info));
158   EXPECT_EQ(device_id_, info.device_id());
159   EXPECT_EQ(base::string16(), info.name());
160   EXPECT_EQ(mount_point_.value(), info.location());
161   EXPECT_EQ(kTestSize, info.total_size_in_bytes());
162
163   EXPECT_FALSE(monitor_->GetStorageInfoForPath(
164       base::FilePath("/non/matching/path"), &info));
165 }
166
167 // Test that mounting a DMG doesn't send a notification.
168 TEST_F(StorageMonitorMacTest, DMG) {
169   StorageInfo info = CreateStorageInfo(
170       device_id_, "Disk Image", mount_point_, kTestSize);
171   UpdateDisk(info, StorageMonitorMac::UPDATE_DEVICE_ADDED);
172   EXPECT_EQ(0, mock_storage_observer_->attach_calls());
173 }