| 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 #include "chrome/browser/storage_monitor/removable_device_notifications_mac.h" | 5 #include "chrome/browser/storage_monitor/storage_monitor_mac.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/sys_string_conversions.h" | 11 #include "base/sys_string_conversions.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/storage_monitor/media_storage_util.h" | 13 #include "chrome/browser/storage_monitor/media_storage_util.h" |
| 14 #include "chrome/browser/storage_monitor/mock_removable_storage_observer.h" | 14 #include "chrome/browser/storage_monitor/mock_removable_storage_observer.h" |
| 15 #include "chrome/browser/storage_monitor/removable_device_constants.h" | 15 #include "chrome/browser/storage_monitor/removable_device_constants.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 43 forKey:base::mac::CFToNSCast(kDADiskDescriptionVolumeNameKey)]; | 43 forKey:base::mac::CFToNSCast(kDADiskDescriptionVolumeNameKey)]; |
| 44 [dict setObject:[NSNumber numberWithBool:YES] | 44 [dict setObject:[NSNumber numberWithBool:YES] |
| 45 forKey:base::mac::CFToNSCast(kDADiskDescriptionMediaRemovableKey)]; | 45 forKey:base::mac::CFToNSCast(kDADiskDescriptionMediaRemovableKey)]; |
| 46 [dict setObject:[NSNumber numberWithInt:size_bytes] | 46 [dict setObject:[NSNumber numberWithInt:size_bytes] |
| 47 forKey:base::mac::CFToNSCast(kDADiskDescriptionMediaSizeKey)]; | 47 forKey:base::mac::CFToNSCast(kDADiskDescriptionMediaSizeKey)]; |
| 48 return DiskInfoMac::BuildDiskInfoOnFileThread(base::mac::NSToCFCast(dict)); | 48 return DiskInfoMac::BuildDiskInfoOnFileThread(base::mac::NSToCFCast(dict)); |
| 49 } | 49 } |
| 50 | 50 |
| 51 } // namespace | 51 } // namespace |
| 52 | 52 |
| 53 class RemovableDeviceNotificationsMacTest : public testing::Test { | 53 class StorageMonitorMacTest : public testing::Test { |
| 54 public: | 54 public: |
| 55 RemovableDeviceNotificationsMacTest() | 55 StorageMonitorMacTest() |
| 56 : message_loop_(MessageLoop::TYPE_IO), | 56 : message_loop_(MessageLoop::TYPE_IO), |
| 57 file_thread_(content::BrowserThread::FILE, &message_loop_) { | 57 file_thread_(content::BrowserThread::FILE, &message_loop_) { |
| 58 } | 58 } |
| 59 | 59 |
| 60 virtual void SetUp() OVERRIDE { | 60 virtual void SetUp() OVERRIDE { |
| 61 notifications_ = new RemovableDeviceNotificationsMac; | 61 monitor_ = new StorageMonitorMac; |
| 62 | 62 |
| 63 mock_storage_observer_.reset(new MockRemovableStorageObserver); | 63 mock_storage_observer_.reset(new MockRemovableStorageObserver); |
| 64 notifications_->AddObserver(mock_storage_observer_.get()); | 64 monitor_->AddObserver(mock_storage_observer_.get()); |
| 65 | |
| 66 | 65 |
| 67 unique_id_ = "test_id"; | 66 unique_id_ = "test_id"; |
| 68 display_name_ = ASCIIToUTF16("977 KB Test Display Name"); | 67 display_name_ = ASCIIToUTF16("977 KB Test Display Name"); |
| 69 mount_point_ = base::FilePath("/unused_test_directory"); | 68 mount_point_ = base::FilePath("/unused_test_directory"); |
| 70 device_id_ = MediaStorageUtil::MakeDeviceId( | 69 device_id_ = MediaStorageUtil::MakeDeviceId( |
| 71 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM, unique_id_); | 70 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM, unique_id_); |
| 72 disk_info_ = CreateDiskInfoMac(unique_id_, "", | 71 disk_info_ = CreateDiskInfoMac(unique_id_, "", |
| 73 ASCIIToUTF16("Test Display Name"), | 72 ASCIIToUTF16("Test Display Name"), |
| 74 mount_point_, kTestSize); | 73 mount_point_, kTestSize); |
| 75 } | 74 } |
| 76 | 75 |
| 77 protected: | 76 protected: |
| 78 // The message loop and file thread to run tests on. | 77 // The message loop and file thread to run tests on. |
| 79 MessageLoop message_loop_; | 78 MessageLoop message_loop_; |
| 80 content::TestBrowserThread file_thread_; | 79 content::TestBrowserThread file_thread_; |
| 81 | 80 |
| 82 scoped_ptr<MockRemovableStorageObserver> mock_storage_observer_; | 81 scoped_ptr<MockRemovableStorageObserver> mock_storage_observer_; |
| 83 | 82 |
| 84 // Information about the disk. | 83 // Information about the disk. |
| 85 std::string unique_id_; | 84 std::string unique_id_; |
| 86 string16 display_name_; | 85 string16 display_name_; |
| 87 base::FilePath mount_point_; | 86 base::FilePath mount_point_; |
| 88 std::string device_id_; | 87 std::string device_id_; |
| 89 DiskInfoMac disk_info_; | 88 DiskInfoMac disk_info_; |
| 90 | 89 |
| 91 scoped_refptr<RemovableDeviceNotificationsMac> notifications_; | 90 scoped_refptr<StorageMonitorMac> monitor_; |
| 92 }; | 91 }; |
| 93 | 92 |
| 94 TEST_F(RemovableDeviceNotificationsMacTest, AddRemove) { | 93 TEST_F(StorageMonitorMacTest, AddRemove) { |
| 95 notifications_->UpdateDisk( | 94 monitor_->UpdateDisk(disk_info_, StorageMonitorMac::UPDATE_DEVICE_ADDED); |
| 96 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); | |
| 97 message_loop_.RunUntilIdle(); | 95 message_loop_.RunUntilIdle(); |
| 98 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); | 96 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); |
| 99 EXPECT_EQ(0, mock_storage_observer_->detach_calls()); | 97 EXPECT_EQ(0, mock_storage_observer_->detach_calls()); |
| 100 EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id); | 98 EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id); |
| 101 EXPECT_EQ(display_name_, mock_storage_observer_->last_attached().name); | 99 EXPECT_EQ(display_name_, mock_storage_observer_->last_attached().name); |
| 102 EXPECT_EQ(mount_point_.value(), | 100 EXPECT_EQ(mount_point_.value(), |
| 103 mock_storage_observer_->last_attached().location); | 101 mock_storage_observer_->last_attached().location); |
| 104 | 102 |
| 105 notifications_->UpdateDisk( | 103 monitor_->UpdateDisk(disk_info_, StorageMonitorMac::UPDATE_DEVICE_REMOVED); |
| 106 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_REMOVED); | |
| 107 message_loop_.RunUntilIdle(); | 104 message_loop_.RunUntilIdle(); |
| 108 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); | 105 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); |
| 109 EXPECT_EQ(1, mock_storage_observer_->detach_calls()); | 106 EXPECT_EQ(1, mock_storage_observer_->detach_calls()); |
| 110 EXPECT_EQ(device_id_, mock_storage_observer_->last_detached().device_id); | 107 EXPECT_EQ(device_id_, mock_storage_observer_->last_detached().device_id); |
| 111 } | 108 } |
| 112 | 109 |
| 113 TEST_F(RemovableDeviceNotificationsMacTest, UpdateVolumeName) { | 110 TEST_F(StorageMonitorMacTest, UpdateVolumeName) { |
| 114 notifications_->UpdateDisk( | 111 monitor_->UpdateDisk(disk_info_, StorageMonitorMac::UPDATE_DEVICE_ADDED); |
| 115 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); | |
| 116 message_loop_.RunUntilIdle(); | 112 message_loop_.RunUntilIdle(); |
| 117 | 113 |
| 118 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); | 114 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); |
| 119 EXPECT_EQ(0, mock_storage_observer_->detach_calls()); | 115 EXPECT_EQ(0, mock_storage_observer_->detach_calls()); |
| 120 EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id); | 116 EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id); |
| 121 EXPECT_EQ(display_name_, mock_storage_observer_->last_attached().name); | 117 EXPECT_EQ(display_name_, mock_storage_observer_->last_attached().name); |
| 122 EXPECT_EQ(mount_point_.value(), | 118 EXPECT_EQ(mount_point_.value(), |
| 123 mock_storage_observer_->last_attached().location); | 119 mock_storage_observer_->last_attached().location); |
| 124 | 120 |
| 125 string16 new_display_name(ASCIIToUTF16("977 KB Test Display Name")); | 121 string16 new_display_name(ASCIIToUTF16("977 KB Test Display Name")); |
| 126 DiskInfoMac info2 = CreateDiskInfoMac( | 122 DiskInfoMac info2 = CreateDiskInfoMac( |
| 127 unique_id_, "", ASCIIToUTF16("Test Display Name"), mount_point_, | 123 unique_id_, "", ASCIIToUTF16("Test Display Name"), mount_point_, |
| 128 kTestSize); | 124 kTestSize); |
| 129 notifications_->UpdateDisk( | 125 monitor_->UpdateDisk(info2, StorageMonitorMac::UPDATE_DEVICE_CHANGED); |
| 130 info2, RemovableDeviceNotificationsMac::UPDATE_DEVICE_CHANGED); | |
| 131 message_loop_.RunUntilIdle(); | 126 message_loop_.RunUntilIdle(); |
| 132 | 127 |
| 133 EXPECT_EQ(1, mock_storage_observer_->detach_calls()); | 128 EXPECT_EQ(1, mock_storage_observer_->detach_calls()); |
| 134 EXPECT_EQ(device_id_, mock_storage_observer_->last_detached().device_id); | 129 EXPECT_EQ(device_id_, mock_storage_observer_->last_detached().device_id); |
| 135 EXPECT_EQ(2, mock_storage_observer_->attach_calls()); | 130 EXPECT_EQ(2, mock_storage_observer_->attach_calls()); |
| 136 EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id); | 131 EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id); |
| 137 EXPECT_EQ(new_display_name, mock_storage_observer_->last_attached().name); | 132 EXPECT_EQ(new_display_name, mock_storage_observer_->last_attached().name); |
| 138 EXPECT_EQ(mount_point_.value(), | 133 EXPECT_EQ(mount_point_.value(), |
| 139 mock_storage_observer_->last_attached().location); | 134 mock_storage_observer_->last_attached().location); |
| 140 } | 135 } |
| 141 | 136 |
| 142 TEST_F(RemovableDeviceNotificationsMacTest, DCIM) { | 137 TEST_F(StorageMonitorMacTest, DCIM) { |
| 143 base::ScopedTempDir temp_dir; | 138 base::ScopedTempDir temp_dir; |
| 144 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 139 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 145 ASSERT_TRUE(file_util::CreateDirectory( | 140 ASSERT_TRUE(file_util::CreateDirectory( |
| 146 temp_dir.path().Append(kDCIMDirectoryName))); | 141 temp_dir.path().Append(kDCIMDirectoryName))); |
| 147 | 142 |
| 148 base::FilePath mount_point = temp_dir.path(); | 143 base::FilePath mount_point = temp_dir.path(); |
| 149 DiskInfoMac info = CreateDiskInfoMac( | 144 DiskInfoMac info = CreateDiskInfoMac( |
| 150 unique_id_, "", ASCIIToUTF16("Test Display Name"), mount_point, | 145 unique_id_, "", ASCIIToUTF16("Test Display Name"), mount_point, |
| 151 kTestSize); | 146 kTestSize); |
| 152 std::string device_id = MediaStorageUtil::MakeDeviceId( | 147 std::string device_id = MediaStorageUtil::MakeDeviceId( |
| 153 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, unique_id_); | 148 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, unique_id_); |
| 154 | 149 |
| 155 notifications_->UpdateDisk( | 150 monitor_->UpdateDisk(info, StorageMonitorMac::UPDATE_DEVICE_ADDED); |
| 156 info, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); | |
| 157 message_loop_.RunUntilIdle(); | 151 message_loop_.RunUntilIdle(); |
| 158 | 152 |
| 159 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); | 153 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); |
| 160 EXPECT_EQ(0, mock_storage_observer_->detach_calls()); | 154 EXPECT_EQ(0, mock_storage_observer_->detach_calls()); |
| 161 EXPECT_EQ(device_id, mock_storage_observer_->last_attached().device_id); | 155 EXPECT_EQ(device_id, mock_storage_observer_->last_attached().device_id); |
| 162 EXPECT_EQ(display_name_, mock_storage_observer_->last_attached().name); | 156 EXPECT_EQ(display_name_, mock_storage_observer_->last_attached().name); |
| 163 EXPECT_EQ(mount_point.value(), | 157 EXPECT_EQ(mount_point.value(), |
| 164 mock_storage_observer_->last_attached().location); | 158 mock_storage_observer_->last_attached().location); |
| 165 } | 159 } |
| 166 | 160 |
| 167 TEST_F(RemovableDeviceNotificationsMacTest, GetStorageInfo) { | 161 TEST_F(StorageMonitorMacTest, GetStorageInfo) { |
| 168 notifications_->UpdateDisk( | 162 monitor_->UpdateDisk(disk_info_, StorageMonitorMac::UPDATE_DEVICE_ADDED); |
| 169 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); | |
| 170 message_loop_.RunUntilIdle(); | 163 message_loop_.RunUntilIdle(); |
| 171 | 164 |
| 172 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); | 165 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); |
| 173 EXPECT_EQ(0, mock_storage_observer_->detach_calls()); | 166 EXPECT_EQ(0, mock_storage_observer_->detach_calls()); |
| 174 EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id); | 167 EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id); |
| 175 EXPECT_EQ(display_name_, mock_storage_observer_->last_attached().name); | 168 EXPECT_EQ(display_name_, mock_storage_observer_->last_attached().name); |
| 176 EXPECT_EQ(mount_point_.value(), | 169 EXPECT_EQ(mount_point_.value(), |
| 177 mock_storage_observer_->last_attached().location); | 170 mock_storage_observer_->last_attached().location); |
| 178 | 171 |
| 179 StorageMonitor::StorageInfo info; | 172 StorageMonitor::StorageInfo info; |
| 180 EXPECT_TRUE(notifications_->GetStorageInfoForPath( | 173 EXPECT_TRUE(monitor_->GetStorageInfoForPath(mount_point_.AppendASCII("foo"), |
| 181 mount_point_.AppendASCII("foo"), &info)); | 174 &info)); |
| 182 EXPECT_EQ(info.device_id, device_id_); | 175 EXPECT_EQ(info.device_id, device_id_); |
| 183 EXPECT_EQ(info.name, ASCIIToUTF16("Test Display Name")); | 176 EXPECT_EQ(info.name, ASCIIToUTF16("Test Display Name")); |
| 184 EXPECT_EQ(info.location, mount_point_.value()); | 177 EXPECT_EQ(info.location, mount_point_.value()); |
| 185 | 178 |
| 186 EXPECT_FALSE(notifications_->GetStorageInfoForPath( | 179 EXPECT_FALSE(monitor_->GetStorageInfoForPath( |
| 187 base::FilePath("/non/matching/path"), &info)); | 180 base::FilePath("/non/matching/path"), &info)); |
| 188 } | 181 } |
| 189 | 182 |
| 190 TEST_F(RemovableDeviceNotificationsMacTest, GetStorageSize) { | 183 TEST_F(StorageMonitorMacTest, GetStorageSize) { |
| 191 notifications_->UpdateDisk( | 184 monitor_->UpdateDisk(disk_info_, StorageMonitorMac::UPDATE_DEVICE_ADDED); |
| 192 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); | |
| 193 message_loop_.RunUntilIdle(); | 185 message_loop_.RunUntilIdle(); |
| 194 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); | 186 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); |
| 195 | 187 |
| 196 EXPECT_EQ(kTestSize, | 188 EXPECT_EQ(kTestSize, |
| 197 notifications_->GetStorageSize("/unused_test_directory")); | 189 monitor_->GetStorageSize("/unused_test_directory")); |
| 198 } | 190 } |
| 199 | 191 |
| 200 // Test that mounting a DMG doesn't send a notification. | 192 // Test that mounting a DMG doesn't send a notification. |
| 201 TEST_F(RemovableDeviceNotificationsMacTest, DMG) { | 193 TEST_F(StorageMonitorMacTest, DMG) { |
| 202 DiskInfoMac info = CreateDiskInfoMac( | 194 DiskInfoMac info = CreateDiskInfoMac( |
| 203 unique_id_, "Disk Image", display_name_, mount_point_, kTestSize); | 195 unique_id_, "Disk Image", display_name_, mount_point_, kTestSize); |
| 204 notifications_->UpdateDisk( | 196 monitor_->UpdateDisk(info, StorageMonitorMac::UPDATE_DEVICE_ADDED); |
| 205 info, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); | |
| 206 message_loop_.RunUntilIdle(); | 197 message_loop_.RunUntilIdle(); |
| 207 EXPECT_EQ(0, mock_storage_observer_->attach_calls()); | 198 EXPECT_EQ(0, mock_storage_observer_->attach_calls()); |
| 208 } | 199 } |
| 209 | 200 |
| 210 } // namespace chrome | 201 } // namespace chrome |
| OLD | NEW |