| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/system_monitor/removable_device_notifications_mac.h" |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/mac/foundation_util.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "base/scoped_temp_dir.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" |
| 15 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 16 #include "content/public/test/test_browser_thread.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace chrome { |
| 20 |
| 21 namespace { |
| 22 |
| 23 DiskInfoMac CreateDiskInfoMac(const std::string& unique_id, |
| 24 const string16& display_name, |
| 25 const FilePath& mount_point) { |
| 26 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; |
| 27 [dict setObject:@"dummy_bsd_name" |
| 28 forKey:base::mac::CFToNSCast(kDADiskDescriptionMediaBSDNameKey)]; |
| 29 [dict setObject:base::SysUTF8ToNSString(unique_id) |
| 30 forKey:base::mac::CFToNSCast(kDADiskDescriptionDeviceRevisionKey)]; |
| 31 NSString* path = base::mac::FilePathToNSString(mount_point); |
| 32 [dict setObject:[NSURL fileURLWithPath:path] |
| 33 forKey:base::mac::CFToNSCast(kDADiskDescriptionVolumePathKey)]; |
| 34 [dict setObject:base::SysUTF16ToNSString(display_name) |
| 35 forKey:base::mac::CFToNSCast(kDADiskDescriptionVolumeNameKey)]; |
| 36 [dict setObject:[NSNumber numberWithBool:YES] |
| 37 forKey:base::mac::CFToNSCast(kDADiskDescriptionMediaRemovableKey)]; |
| 38 return DiskInfoMac::BuildDiskInfoOnFileThread(base::mac::NSToCFCast(dict)); |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
| 43 class RemovableDeviceNotificationsMacTest : public testing::Test { |
| 44 public: |
| 45 RemovableDeviceNotificationsMacTest() |
| 46 : message_loop_(MessageLoop::TYPE_IO), |
| 47 file_thread_(content::BrowserThread::FILE, &message_loop_) { |
| 48 } |
| 49 |
| 50 virtual void SetUp() OVERRIDE { |
| 51 base::SystemMonitor::AllocateSystemIOPorts(); |
| 52 system_monitor_.reset(new base::SystemMonitor()); |
| 53 |
| 54 mock_devices_changed_observer_.reset(new base::MockDevicesChangedObserver); |
| 55 system_monitor_->AddDevicesChangedObserver( |
| 56 mock_devices_changed_observer_.get()); |
| 57 |
| 58 notifications_.reset(new RemovableDeviceNotificationsMac); |
| 59 |
| 60 unique_id_ = "test_id"; |
| 61 display_name_ = ASCIIToUTF16("Test Display Name"); |
| 62 mount_point_ = FilePath("/unused_test_directory"); |
| 63 device_id_ = MediaStorageUtil::MakeDeviceId( |
| 64 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM, unique_id_); |
| 65 disk_info_ = CreateDiskInfoMac(unique_id_, display_name_, mount_point_); |
| 66 } |
| 67 |
| 68 protected: |
| 69 // The message loop and file thread to run tests on. |
| 70 MessageLoop message_loop_; |
| 71 content::TestBrowserThread file_thread_; |
| 72 |
| 73 // SystemMonitor and DevicesChangedObserver to hook together to test. |
| 74 scoped_ptr<base::SystemMonitor> system_monitor_; |
| 75 scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_; |
| 76 |
| 77 // Information about the disk. |
| 78 std::string unique_id_; |
| 79 string16 display_name_; |
| 80 FilePath mount_point_; |
| 81 std::string device_id_; |
| 82 DiskInfoMac disk_info_; |
| 83 |
| 84 scoped_ptr<RemovableDeviceNotificationsMac> notifications_; |
| 85 }; |
| 86 |
| 87 TEST_F(RemovableDeviceNotificationsMacTest, AddRemove) { |
| 88 { |
| 89 EXPECT_CALL(*mock_devices_changed_observer_, |
| 90 OnRemovableStorageAttached(device_id_, |
| 91 display_name_, |
| 92 mount_point_.value())); |
| 93 notifications_->UpdateDisk( |
| 94 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); |
| 95 message_loop_.RunAllPending(); |
| 96 } |
| 97 |
| 98 { |
| 99 EXPECT_CALL(*mock_devices_changed_observer_, |
| 100 OnRemovableStorageDetached(device_id_)); |
| 101 notifications_->UpdateDisk( |
| 102 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_REMOVED); |
| 103 message_loop_.RunAllPending(); |
| 104 } |
| 105 } |
| 106 |
| 107 TEST_F(RemovableDeviceNotificationsMacTest, UpdateVolumeName) { |
| 108 { |
| 109 EXPECT_CALL(*mock_devices_changed_observer_, |
| 110 OnRemovableStorageAttached(device_id_, |
| 111 display_name_, |
| 112 mount_point_.value())); |
| 113 notifications_->UpdateDisk( |
| 114 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); |
| 115 message_loop_.RunAllPending(); |
| 116 } |
| 117 |
| 118 { |
| 119 string16 new_display_name(ASCIIToUTF16("Test Display Name")); |
| 120 DiskInfoMac info2 = CreateDiskInfoMac( |
| 121 unique_id_, new_display_name, mount_point_); |
| 122 EXPECT_CALL(*mock_devices_changed_observer_, |
| 123 OnRemovableStorageDetached(device_id_)); |
| 124 EXPECT_CALL(*mock_devices_changed_observer_, |
| 125 OnRemovableStorageAttached(device_id_, |
| 126 new_display_name, |
| 127 mount_point_.value())); |
| 128 notifications_->UpdateDisk( |
| 129 info2, RemovableDeviceNotificationsMac::UPDATE_DEVICE_CHANGED); |
| 130 message_loop_.RunAllPending(); |
| 131 } |
| 132 } |
| 133 |
| 134 TEST_F(RemovableDeviceNotificationsMacTest, DCIM) { |
| 135 ScopedTempDir temp_dir; |
| 136 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 137 file_util::CreateDirectory(temp_dir.path().AppendASCII("DCIM")); |
| 138 |
| 139 FilePath mount_point = temp_dir.path(); |
| 140 DiskInfoMac info = CreateDiskInfoMac(unique_id_, display_name_, mount_point); |
| 141 std::string device_id = MediaStorageUtil::MakeDeviceId( |
| 142 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, unique_id_); |
| 143 |
| 144 { |
| 145 EXPECT_CALL(*mock_devices_changed_observer_, |
| 146 OnRemovableStorageAttached(device_id, |
| 147 display_name_, |
| 148 mount_point.value())); |
| 149 notifications_->UpdateDisk( |
| 150 info, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); |
| 151 message_loop_.RunAllPending(); |
| 152 } |
| 153 } |
| 154 |
| 155 TEST_F(RemovableDeviceNotificationsMacTest, GetDeviceInfo) { |
| 156 { |
| 157 EXPECT_CALL(*mock_devices_changed_observer_, |
| 158 OnRemovableStorageAttached(device_id_, |
| 159 display_name_, |
| 160 mount_point_.value())); |
| 161 notifications_->UpdateDisk( |
| 162 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); |
| 163 message_loop_.RunAllPending(); |
| 164 } |
| 165 |
| 166 base::SystemMonitor::RemovableStorageInfo info; |
| 167 EXPECT_TRUE(notifications_->GetDeviceInfoForPath( |
| 168 mount_point_.AppendASCII("foo"), &info)); |
| 169 EXPECT_EQ(info.device_id, device_id_); |
| 170 EXPECT_EQ(info.name, display_name_); |
| 171 EXPECT_EQ(info.location, mount_point_.value()); |
| 172 |
| 173 EXPECT_FALSE(notifications_->GetDeviceInfoForPath( |
| 174 FilePath("/non/matching/path"), &info)); |
| 175 } |
| 176 |
| 177 } // namespace chrome |
| OLD | NEW |