OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // RemovableDeviceNotificationsLinux unit tests. | 5 // StorageMonitorLinux unit tests. |
6 | 6 |
7 #include "chrome/browser/storage_monitor/removable_device_notifications_linux.h" | 7 #include "chrome/browser/storage_monitor/storage_monitor_linux.h" |
8 | 8 |
9 #include <mntent.h> | 9 #include <mntent.h> |
10 #include <stdio.h> | 10 #include <stdio.h> |
11 | 11 |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
15 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 } | 127 } |
128 | 128 |
129 string16 GetDeviceName(const std::string& device) { | 129 string16 GetDeviceName(const std::string& device) { |
130 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) { | 130 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) { |
131 if (device == kTestDeviceData[i].device_path) | 131 if (device == kTestDeviceData[i].device_path) |
132 return ASCIIToUTF16(kTestDeviceData[i].device_name); | 132 return ASCIIToUTF16(kTestDeviceData[i].device_name); |
133 } | 133 } |
134 return string16(); | 134 return string16(); |
135 } | 135 } |
136 | 136 |
137 class RemovableDeviceNotificationsLinuxTestWrapper | 137 class StorageMonitorLinuxTestWrapper |
vandebo (ex-Chrome)
2013/03/01 19:47:39
A better name for this would probably be TestStora
| |
138 : public RemovableDeviceNotificationsLinux { | 138 : public StorageMonitorLinux { |
139 public: | 139 public: |
140 RemovableDeviceNotificationsLinuxTestWrapper(const base::FilePath& path, | 140 StorageMonitorLinuxTestWrapper(const base::FilePath& path, |
141 MessageLoop* message_loop) | 141 MessageLoop* message_loop) |
142 : RemovableDeviceNotificationsLinux(path, &GetDeviceInfo), | 142 : StorageMonitorLinux(path, &GetDeviceInfo), |
143 message_loop_(message_loop) { | 143 message_loop_(message_loop) { |
144 } | 144 } |
145 | 145 |
146 private: | 146 private: |
147 // Avoids code deleting the object while there are references to it. | 147 // Avoids code deleting the object while there are references to it. |
148 // Aside from the base::RefCountedThreadSafe friend class, any attempts to | 148 // Aside from the base::RefCountedThreadSafe friend class, any attempts to |
149 // call this dtor will result in a compile-time error. | 149 // call this dtor will result in a compile-time error. |
150 virtual ~RemovableDeviceNotificationsLinuxTestWrapper() {} | 150 virtual ~StorageMonitorLinuxTestWrapper() {} |
151 | 151 |
152 virtual void OnFilePathChanged(const base::FilePath& path, | 152 virtual void OnFilePathChanged(const base::FilePath& path, |
153 bool error) OVERRIDE { | 153 bool error) OVERRIDE { |
154 RemovableDeviceNotificationsLinux::OnFilePathChanged(path, error); | 154 StorageMonitorLinux::OnFilePathChanged(path, error); |
155 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 155 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
156 } | 156 } |
157 | 157 |
158 MessageLoop* message_loop_; | 158 MessageLoop* message_loop_; |
159 | 159 |
160 DISALLOW_COPY_AND_ASSIGN(RemovableDeviceNotificationsLinuxTestWrapper); | 160 DISALLOW_COPY_AND_ASSIGN(StorageMonitorLinuxTestWrapper); |
161 }; | 161 }; |
162 | 162 |
163 class RemovableDeviceNotificationLinuxTest : public testing::Test { | 163 class RemovableDeviceNotificationLinuxTest : public testing::Test { |
vandebo (ex-Chrome)
2013/03/01 19:47:39
Missed this class.
| |
164 public: | 164 public: |
165 struct MtabTestData { | 165 struct MtabTestData { |
166 MtabTestData(const std::string& mount_device, | 166 MtabTestData(const std::string& mount_device, |
167 const std::string& mount_point, | 167 const std::string& mount_point, |
168 const std::string& mount_type) | 168 const std::string& mount_type) |
169 : mount_device(mount_device), | 169 : mount_device(mount_device), |
170 mount_point(mount_point), | 170 mount_point(mount_point), |
171 mount_type(mount_type) { | 171 mount_type(mount_type) { |
172 } | 172 } |
173 | 173 |
(...skipping 16 matching lines...) Expand all Loading... | |
190 ASSERT_TRUE(file_util::CreateDirectory(test_dir)); | 190 ASSERT_TRUE(file_util::CreateDirectory(test_dir)); |
191 mtab_file_ = test_dir.AppendASCII("test_mtab"); | 191 mtab_file_ = test_dir.AppendASCII("test_mtab"); |
192 MtabTestData initial_test_data[] = { | 192 MtabTestData initial_test_data[] = { |
193 MtabTestData("dummydevice", "dummydir", kInvalidFS), | 193 MtabTestData("dummydevice", "dummydir", kInvalidFS), |
194 }; | 194 }; |
195 WriteToMtab(initial_test_data, | 195 WriteToMtab(initial_test_data, |
196 arraysize(initial_test_data), | 196 arraysize(initial_test_data), |
197 true /* overwrite */); | 197 true /* overwrite */); |
198 | 198 |
199 // Initialize the test subject. | 199 // Initialize the test subject. |
200 notifications_ = new RemovableDeviceNotificationsLinuxTestWrapper( | 200 notifications_ = new StorageMonitorLinuxTestWrapper( |
201 mtab_file_, &message_loop_); | 201 mtab_file_, &message_loop_); |
202 mock_storage_observer_.reset(new MockRemovableStorageObserver); | 202 mock_storage_observer_.reset(new MockRemovableStorageObserver); |
203 notifications_->AddObserver(mock_storage_observer_.get()); | 203 notifications_->AddObserver(mock_storage_observer_.get()); |
204 | 204 |
205 notifications_->Init(); | 205 notifications_->Init(); |
206 message_loop_.RunUntilIdle(); | 206 message_loop_.RunUntilIdle(); |
207 } | 207 } |
208 | 208 |
209 virtual void TearDown() OVERRIDE { | 209 virtual void TearDown() OVERRIDE { |
210 message_loop_.RunUntilIdle(); | 210 message_loop_.RunUntilIdle(); |
(...skipping 16 matching lines...) Expand all Loading... | |
227 } | 227 } |
228 | 228 |
229 // Simplied version of OverwriteMtabAndRunLoop() that just deletes all the | 229 // Simplied version of OverwriteMtabAndRunLoop() that just deletes all the |
230 // entries in the mtab file. | 230 // entries in the mtab file. |
231 void WriteEmptyMtabAndRunLoop() { | 231 void WriteEmptyMtabAndRunLoop() { |
232 OverwriteMtabAndRunLoop(NULL, // No data. | 232 OverwriteMtabAndRunLoop(NULL, // No data. |
233 0); // No data length. | 233 0); // No data length. |
234 } | 234 } |
235 | 235 |
236 // Create a directory named |dir| relative to the test directory. | 236 // Create a directory named |dir| relative to the test directory. |
237 // It has a DCIM directory, so RemovableDeviceNotificationsLinux recognizes it | 237 // It has a DCIM directory, so StorageMonitorLinux recognizes it |
238 // as a media directory. | 238 // as a media directory. |
239 base::FilePath CreateMountPointWithDCIMDir(const std::string& dir) { | 239 base::FilePath CreateMountPointWithDCIMDir(const std::string& dir) { |
240 return CreateMountPoint(dir, true /* create DCIM dir */); | 240 return CreateMountPoint(dir, true /* create DCIM dir */); |
241 } | 241 } |
242 | 242 |
243 // Create a directory named |dir| relative to the test directory. | 243 // Create a directory named |dir| relative to the test directory. |
244 // It does not have a DCIM directory, so RemovableDeviceNotificationsLinux | 244 // It does not have a DCIM directory, so StorageMonitorLinux |
245 // does not recognizes it as a media directory. | 245 // does not recognizes it as a media directory. |
246 base::FilePath CreateMountPointWithoutDCIMDir(const std::string& dir) { | 246 base::FilePath CreateMountPointWithoutDCIMDir(const std::string& dir) { |
247 return CreateMountPoint(dir, false /* do not create DCIM dir */); | 247 return CreateMountPoint(dir, false /* do not create DCIM dir */); |
248 } | 248 } |
249 | 249 |
250 void RemoveDCIMDirFromMountPoint(const std::string& dir) { | 250 void RemoveDCIMDirFromMountPoint(const std::string& dir) { |
251 base::FilePath dcim = | 251 base::FilePath dcim = |
252 scoped_temp_dir_.path().AppendASCII(dir).Append(kDCIMDirectoryName); | 252 scoped_temp_dir_.path().AppendASCII(dir).Append(kDCIMDirectoryName); |
253 file_util::Delete(dcim, false); | 253 file_util::Delete(dcim, false); |
254 } | 254 } |
255 | 255 |
256 MockRemovableStorageObserver& observer() { | 256 MockRemovableStorageObserver& observer() { |
257 return *mock_storage_observer_; | 257 return *mock_storage_observer_; |
258 } | 258 } |
259 | 259 |
260 RemovableDeviceNotificationsLinux* notifier() { | 260 StorageMonitorLinux* notifier() { |
261 return notifications_.get(); | 261 return notifications_.get(); |
262 } | 262 } |
263 | 263 |
264 private: | 264 private: |
265 // Create a directory named |dir| relative to the test directory. | 265 // Create a directory named |dir| relative to the test directory. |
266 // Set |with_dcim_dir| to true if the created directory will have a "DCIM" | 266 // Set |with_dcim_dir| to true if the created directory will have a "DCIM" |
267 // subdirectory. | 267 // subdirectory. |
268 // Returns the full path to the created directory on success, or an empty | 268 // Returns the full path to the created directory on success, or an empty |
269 // path on failure. | 269 // path on failure. |
270 base::FilePath CreateMountPoint(const std::string& dir, bool with_dcim_dir) { | 270 base::FilePath CreateMountPoint(const std::string& dir, bool with_dcim_dir) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
315 MessageLoop message_loop_; | 315 MessageLoop message_loop_; |
316 content::TestBrowserThread file_thread_; | 316 content::TestBrowserThread file_thread_; |
317 | 317 |
318 scoped_ptr<MockRemovableStorageObserver> mock_storage_observer_; | 318 scoped_ptr<MockRemovableStorageObserver> mock_storage_observer_; |
319 | 319 |
320 // Temporary directory for created test data. | 320 // Temporary directory for created test data. |
321 base::ScopedTempDir scoped_temp_dir_; | 321 base::ScopedTempDir scoped_temp_dir_; |
322 // Path to the test mtab file. | 322 // Path to the test mtab file. |
323 base::FilePath mtab_file_; | 323 base::FilePath mtab_file_; |
324 | 324 |
325 scoped_refptr<RemovableDeviceNotificationsLinuxTestWrapper> notifications_; | 325 scoped_refptr<StorageMonitorLinuxTestWrapper> notifications_; |
326 | 326 |
327 DISALLOW_COPY_AND_ASSIGN(RemovableDeviceNotificationLinuxTest); | 327 DISALLOW_COPY_AND_ASSIGN(RemovableDeviceNotificationLinuxTest); |
328 }; | 328 }; |
329 | 329 |
330 // Simple test case where we attach and detach a media device. | 330 // Simple test case where we attach and detach a media device. |
331 TEST_F(RemovableDeviceNotificationLinuxTest, BasicAttachDetach) { | 331 TEST_F(RemovableDeviceNotificationLinuxTest, BasicAttachDetach) { |
332 base::FilePath test_path = CreateMountPointWithDCIMDir(kMountPointA); | 332 base::FilePath test_path = CreateMountPointWithDCIMDir(kMountPointA); |
333 ASSERT_FALSE(test_path.empty()); | 333 ASSERT_FALSE(test_path.empty()); |
334 MtabTestData test_data[] = { | 334 MtabTestData test_data[] = { |
335 MtabTestData(kDeviceDCIM2, test_path.value(), kValidFS), | 335 MtabTestData(kDeviceDCIM2, test_path.value(), kValidFS), |
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
693 notifier()->GetStorageSize(test_path_a.value())); | 693 notifier()->GetStorageSize(test_path_a.value())); |
694 EXPECT_EQ(GetDevicePartitionSize(kDeviceNoDCIM), | 694 EXPECT_EQ(GetDevicePartitionSize(kDeviceNoDCIM), |
695 notifier()->GetStorageSize(test_path_b.value())); | 695 notifier()->GetStorageSize(test_path_b.value())); |
696 EXPECT_EQ(GetDevicePartitionSize(kInvalidPath), | 696 EXPECT_EQ(GetDevicePartitionSize(kInvalidPath), |
697 notifier()->GetStorageSize(kInvalidPath)); | 697 notifier()->GetStorageSize(kInvalidPath)); |
698 } | 698 } |
699 | 699 |
700 } // namespace | 700 } // namespace |
701 | 701 |
702 } // namespace chrome | 702 } // namespace chrome |
OLD | NEW |