| 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/system_monitor/removable_device_notifications_mac.h" | 5 #include "chrome/browser/system_monitor/removable_device_notifications_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/system_monitor/system_monitor.h" | |
| 13 #include "base/test/mock_devices_changed_observer.h" | |
| 14 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 15 #include "chrome/browser/system_monitor/media_storage_util.h" | 13 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 14 #include "chrome/browser/system_monitor/mock_removable_storage_observer.h" |
| 16 #include "chrome/browser/system_monitor/removable_device_constants.h" | 15 #include "chrome/browser/system_monitor/removable_device_constants.h" |
| 17 #include "content/public/test/test_browser_thread.h" | 16 #include "content/public/test/test_browser_thread.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 18 |
| 20 namespace chrome { | 19 namespace chrome { |
| 21 | 20 |
| 22 uint64 kTestSize = 1000000ULL; | 21 uint64 kTestSize = 1000000ULL; |
| 23 | 22 |
| 24 namespace { | 23 namespace { |
| 25 | 24 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 52 } // namespace | 51 } // namespace |
| 53 | 52 |
| 54 class RemovableDeviceNotificationsMacTest : public testing::Test { | 53 class RemovableDeviceNotificationsMacTest : public testing::Test { |
| 55 public: | 54 public: |
| 56 RemovableDeviceNotificationsMacTest() | 55 RemovableDeviceNotificationsMacTest() |
| 57 : message_loop_(MessageLoop::TYPE_IO), | 56 : message_loop_(MessageLoop::TYPE_IO), |
| 58 file_thread_(content::BrowserThread::FILE, &message_loop_) { | 57 file_thread_(content::BrowserThread::FILE, &message_loop_) { |
| 59 } | 58 } |
| 60 | 59 |
| 61 virtual void SetUp() OVERRIDE { | 60 virtual void SetUp() OVERRIDE { |
| 61 notifications_ = new RemovableDeviceNotificationsMac; |
| 62 base::SystemMonitor::AllocateSystemIOPorts(); | 62 base::SystemMonitor::AllocateSystemIOPorts(); |
| 63 system_monitor_.reset(new base::SystemMonitor()); | |
| 64 | 63 |
| 65 mock_devices_changed_observer_.reset(new base::MockDevicesChangedObserver); | 64 mock_storage_observer_.reset(new MockRemovableStorageObserver); |
| 66 system_monitor_->AddDevicesChangedObserver( | 65 notifications_->AddObserver(mock_storage_observer_.get()); |
| 67 mock_devices_changed_observer_.get()); | |
| 68 | 66 |
| 69 notifications_ = new RemovableDeviceNotificationsMac; | |
| 70 | 67 |
| 71 unique_id_ = "test_id"; | 68 unique_id_ = "test_id"; |
| 72 display_name_ = ASCIIToUTF16("977 KB Test Display Name"); | 69 display_name_ = ASCIIToUTF16("977 KB Test Display Name"); |
| 73 mount_point_ = FilePath("/unused_test_directory"); | 70 mount_point_ = FilePath("/unused_test_directory"); |
| 74 device_id_ = MediaStorageUtil::MakeDeviceId( | 71 device_id_ = MediaStorageUtil::MakeDeviceId( |
| 75 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM, unique_id_); | 72 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM, unique_id_); |
| 76 disk_info_ = CreateDiskInfoMac(unique_id_, "", | 73 disk_info_ = CreateDiskInfoMac(unique_id_, "", |
| 77 ASCIIToUTF16("Test Display Name"), | 74 ASCIIToUTF16("Test Display Name"), |
| 78 mount_point_, kTestSize); | 75 mount_point_, kTestSize); |
| 79 } | 76 } |
| 80 | 77 |
| 81 protected: | 78 protected: |
| 82 // The message loop and file thread to run tests on. | 79 // The message loop and file thread to run tests on. |
| 83 MessageLoop message_loop_; | 80 MessageLoop message_loop_; |
| 84 content::TestBrowserThread file_thread_; | 81 content::TestBrowserThread file_thread_; |
| 85 | 82 |
| 86 // SystemMonitor and DevicesChangedObserver to hook together to test. | 83 scoped_ptr<MockRemovableStorageObserver> mock_storage_observer_; |
| 87 scoped_ptr<base::SystemMonitor> system_monitor_; | |
| 88 scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_; | |
| 89 | 84 |
| 90 // Information about the disk. | 85 // Information about the disk. |
| 91 std::string unique_id_; | 86 std::string unique_id_; |
| 92 string16 display_name_; | 87 string16 display_name_; |
| 93 FilePath mount_point_; | 88 FilePath mount_point_; |
| 94 std::string device_id_; | 89 std::string device_id_; |
| 95 DiskInfoMac disk_info_; | 90 DiskInfoMac disk_info_; |
| 96 | 91 |
| 97 scoped_refptr<RemovableDeviceNotificationsMac> notifications_; | 92 scoped_refptr<RemovableDeviceNotificationsMac> notifications_; |
| 98 }; | 93 }; |
| 99 | 94 |
| 100 TEST_F(RemovableDeviceNotificationsMacTest, AddRemove) { | 95 TEST_F(RemovableDeviceNotificationsMacTest, AddRemove) { |
| 101 { | 96 { |
| 102 EXPECT_CALL(*mock_devices_changed_observer_, | |
| 103 OnRemovableStorageAttached(device_id_, | |
| 104 display_name_, | |
| 105 mount_point_.value())); | |
| 106 notifications_->UpdateDisk( | 97 notifications_->UpdateDisk( |
| 107 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); | 98 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); |
| 108 message_loop_.RunUntilIdle(); | 99 message_loop_.RunUntilIdle(); |
| 100 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); |
| 101 EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id); |
| 102 EXPECT_EQ(display_name_, mock_storage_observer_->last_attached().name); |
| 103 EXPECT_EQ(mount_point_.value(), |
| 104 mock_storage_observer_->last_attached().location); |
| 109 } | 105 } |
| 110 | 106 |
| 111 { | 107 { |
| 112 EXPECT_CALL(*mock_devices_changed_observer_, | |
| 113 OnRemovableStorageDetached(device_id_)); | |
| 114 notifications_->UpdateDisk( | 108 notifications_->UpdateDisk( |
| 115 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_REMOVED); | 109 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_REMOVED); |
| 116 message_loop_.RunUntilIdle(); | 110 message_loop_.RunUntilIdle(); |
| 111 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); |
| 112 EXPECT_EQ(1, mock_storage_observer_->detach_calls()); |
| 113 EXPECT_EQ(device_id_, mock_storage_observer_->last_detached().device_id); |
| 117 } | 114 } |
| 118 } | 115 } |
| 119 | 116 |
| 120 TEST_F(RemovableDeviceNotificationsMacTest, UpdateVolumeName) { | 117 TEST_F(RemovableDeviceNotificationsMacTest, UpdateVolumeName) { |
| 121 { | 118 { |
| 122 EXPECT_CALL(*mock_devices_changed_observer_, | |
| 123 OnRemovableStorageAttached(device_id_, | |
| 124 display_name_, | |
| 125 mount_point_.value())); | |
| 126 notifications_->UpdateDisk( | 119 notifications_->UpdateDisk( |
| 127 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); | 120 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); |
| 128 message_loop_.RunUntilIdle(); | 121 message_loop_.RunUntilIdle(); |
| 122 |
| 123 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); |
| 124 EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id); |
| 125 EXPECT_EQ(display_name_, mock_storage_observer_->last_attached().name); |
| 126 EXPECT_EQ(mount_point_.value(), |
| 127 mock_storage_observer_->last_attached().location); |
| 129 } | 128 } |
| 130 | 129 |
| 131 { | 130 { |
| 132 string16 new_display_name(ASCIIToUTF16("977 KB Test Display Name")); | 131 string16 new_display_name(ASCIIToUTF16("977 KB Test Display Name")); |
| 133 DiskInfoMac info2 = CreateDiskInfoMac( | 132 DiskInfoMac info2 = CreateDiskInfoMac( |
| 134 unique_id_, "", ASCIIToUTF16("Test Display Name"), mount_point_, | 133 unique_id_, "", ASCIIToUTF16("Test Display Name"), mount_point_, |
| 135 kTestSize); | 134 kTestSize); |
| 136 EXPECT_CALL(*mock_devices_changed_observer_, | |
| 137 OnRemovableStorageDetached(device_id_)); | |
| 138 EXPECT_CALL(*mock_devices_changed_observer_, | |
| 139 OnRemovableStorageAttached(device_id_, | |
| 140 new_display_name, | |
| 141 mount_point_.value())); | |
| 142 notifications_->UpdateDisk( | 135 notifications_->UpdateDisk( |
| 143 info2, RemovableDeviceNotificationsMac::UPDATE_DEVICE_CHANGED); | 136 info2, RemovableDeviceNotificationsMac::UPDATE_DEVICE_CHANGED); |
| 144 message_loop_.RunUntilIdle(); | 137 message_loop_.RunUntilIdle(); |
| 138 |
| 139 EXPECT_EQ(1, mock_storage_observer_->detach_calls()); |
| 140 EXPECT_EQ(device_id_, mock_storage_observer_->last_detached().device_id); |
| 141 EXPECT_EQ(2, mock_storage_observer_->attach_calls()); |
| 142 EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id); |
| 143 EXPECT_EQ(new_display_name, mock_storage_observer_->last_attached().name); |
| 144 EXPECT_EQ(mount_point_.value(), |
| 145 mock_storage_observer_->last_attached().location); |
| 145 } | 146 } |
| 146 } | 147 } |
| 147 | 148 |
| 148 TEST_F(RemovableDeviceNotificationsMacTest, DCIM) { | 149 TEST_F(RemovableDeviceNotificationsMacTest, DCIM) { |
| 149 base::ScopedTempDir temp_dir; | 150 base::ScopedTempDir temp_dir; |
| 150 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 151 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 151 file_util::CreateDirectory(temp_dir.path().Append(kDCIMDirectoryName)); | 152 file_util::CreateDirectory(temp_dir.path().Append(kDCIMDirectoryName)); |
| 152 | 153 |
| 153 FilePath mount_point = temp_dir.path(); | 154 FilePath mount_point = temp_dir.path(); |
| 154 DiskInfoMac info = CreateDiskInfoMac( | 155 DiskInfoMac info = CreateDiskInfoMac( |
| 155 unique_id_, "", ASCIIToUTF16("Test Display Name"), mount_point, | 156 unique_id_, "", ASCIIToUTF16("Test Display Name"), mount_point, |
| 156 kTestSize); | 157 kTestSize); |
| 157 std::string device_id = MediaStorageUtil::MakeDeviceId( | 158 std::string device_id = MediaStorageUtil::MakeDeviceId( |
| 158 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, unique_id_); | 159 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, unique_id_); |
| 159 | 160 |
| 160 { | 161 { |
| 161 EXPECT_CALL(*mock_devices_changed_observer_, | |
| 162 OnRemovableStorageAttached(device_id, | |
| 163 display_name_, | |
| 164 mount_point.value())); | |
| 165 notifications_->UpdateDisk( | 162 notifications_->UpdateDisk( |
| 166 info, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); | 163 info, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); |
| 167 message_loop_.RunUntilIdle(); | 164 message_loop_.RunUntilIdle(); |
| 165 |
| 166 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); |
| 167 EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id); |
| 168 EXPECT_EQ(display_name_, mock_storage_observer_->last_attached().name); |
| 169 EXPECT_EQ(mount_point_.value(), |
| 170 mock_storage_observer_->last_attached().location); |
| 168 } | 171 } |
| 169 } | 172 } |
| 170 | 173 |
| 171 TEST_F(RemovableDeviceNotificationsMacTest, GetDeviceInfo) { | 174 TEST_F(RemovableDeviceNotificationsMacTest, GetDeviceInfo) { |
| 172 { | 175 { |
| 173 EXPECT_CALL(*mock_devices_changed_observer_, | |
| 174 OnRemovableStorageAttached(device_id_, | |
| 175 display_name_, | |
| 176 mount_point_.value())); | |
| 177 notifications_->UpdateDisk( | 176 notifications_->UpdateDisk( |
| 178 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); | 177 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); |
| 179 message_loop_.RunUntilIdle(); | 178 message_loop_.RunUntilIdle(); |
| 179 |
| 180 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); |
| 181 EXPECT_EQ(device_id_, mock_storage_observer_->last_attached().device_id); |
| 182 EXPECT_EQ(display_name_, mock_storage_observer_->last_attached().name); |
| 183 EXPECT_EQ(mount_point_.value(), |
| 184 mock_storage_observer_->last_attached().location); |
| 180 } | 185 } |
| 181 | 186 |
| 182 base::SystemMonitor::RemovableStorageInfo info; | 187 RemovableStorageNotifications::StorageInfo info; |
| 183 EXPECT_TRUE(notifications_->GetDeviceInfoForPath( | 188 EXPECT_TRUE(notifications_->GetDeviceInfoForPath( |
| 184 mount_point_.AppendASCII("foo"), &info)); | 189 mount_point_.AppendASCII("foo"), &info)); |
| 185 EXPECT_EQ(info.device_id, device_id_); | 190 EXPECT_EQ(info.device_id, device_id_); |
| 186 EXPECT_EQ(info.name, ASCIIToUTF16("Test Display Name")); | 191 EXPECT_EQ(info.name, ASCIIToUTF16("Test Display Name")); |
| 187 EXPECT_EQ(info.location, mount_point_.value()); | 192 EXPECT_EQ(info.location, mount_point_.value()); |
| 188 | 193 |
| 189 EXPECT_FALSE(notifications_->GetDeviceInfoForPath( | 194 EXPECT_FALSE(notifications_->GetDeviceInfoForPath( |
| 190 FilePath("/non/matching/path"), &info)); | 195 FilePath("/non/matching/path"), &info)); |
| 191 } | 196 } |
| 192 | 197 |
| 193 TEST_F(RemovableDeviceNotificationsMacTest, GetStorageSize) { | 198 TEST_F(RemovableDeviceNotificationsMacTest, GetStorageSize) { |
| 194 EXPECT_CALL(*mock_devices_changed_observer_, | |
| 195 OnRemovableStorageAttached(testing::_, | |
| 196 testing::_, | |
| 197 testing::_)); | |
| 198 notifications_->UpdateDisk( | 199 notifications_->UpdateDisk( |
| 199 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); | 200 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); |
| 200 message_loop_.RunUntilIdle(); | 201 message_loop_.RunUntilIdle(); |
| 202 EXPECT_EQ(1, mock_storage_observer_->attach_calls()); |
| 201 | 203 |
| 202 EXPECT_EQ(kTestSize, | 204 EXPECT_EQ(kTestSize, |
| 203 notifications_->GetStorageSize("/unused_test_directory")); | 205 notifications_->GetStorageSize("/unused_test_directory")); |
| 204 } | 206 } |
| 205 | 207 |
| 206 // Test that mounting a DMG doesn't send a notification. | 208 // Test that mounting a DMG doesn't send a notification. |
| 207 TEST_F(RemovableDeviceNotificationsMacTest, DMG) { | 209 TEST_F(RemovableDeviceNotificationsMacTest, DMG) { |
| 208 EXPECT_CALL(*mock_devices_changed_observer_, | |
| 209 OnRemovableStorageAttached(testing::_, | |
| 210 testing::_, | |
| 211 testing::_)).Times(0); | |
| 212 DiskInfoMac info = CreateDiskInfoMac( | 210 DiskInfoMac info = CreateDiskInfoMac( |
| 213 unique_id_, "Disk Image", display_name_, mount_point_, kTestSize); | 211 unique_id_, "Disk Image", display_name_, mount_point_, kTestSize); |
| 214 notifications_->UpdateDisk( | 212 notifications_->UpdateDisk( |
| 215 info, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); | 213 info, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); |
| 216 message_loop_.RunUntilIdle(); | 214 message_loop_.RunUntilIdle(); |
| 215 EXPECT_EQ(0, mock_storage_observer_->attach_calls()); |
| 217 } | 216 } |
| 218 | 217 |
| 219 } // namespace chrome | 218 } // namespace chrome |
| OLD | NEW |