Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: chrome/browser/system_monitor/removable_device_notifications_mac_unittest.mm

Issue 11573048: [Media Galleries] Move RemovableStorageInfo notifications to chrome namespace (part 2) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase to head Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
16 #include "chrome/browser/system_monitor/removable_device_constants.h" 14 #include "chrome/browser/system_monitor/removable_device_constants.h"
17 #include "content/public/test/test_browser_thread.h" 15 #include "content/public/test/test_browser_thread.h"
18 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
19 17
20 namespace chrome { 18 namespace chrome {
21 19
22 uint64 kTestSize = 1000000ULL; 20 uint64 kTestSize = 1000000ULL;
23 21
(...skipping 18 matching lines...) Expand all
42 forKey:base::mac::CFToNSCast(kDADiskDescriptionVolumePathKey)]; 40 forKey:base::mac::CFToNSCast(kDADiskDescriptionVolumePathKey)];
43 [dict setObject:base::SysUTF16ToNSString(display_name) 41 [dict setObject:base::SysUTF16ToNSString(display_name)
44 forKey:base::mac::CFToNSCast(kDADiskDescriptionVolumeNameKey)]; 42 forKey:base::mac::CFToNSCast(kDADiskDescriptionVolumeNameKey)];
45 [dict setObject:[NSNumber numberWithBool:YES] 43 [dict setObject:[NSNumber numberWithBool:YES]
46 forKey:base::mac::CFToNSCast(kDADiskDescriptionMediaRemovableKey)]; 44 forKey:base::mac::CFToNSCast(kDADiskDescriptionMediaRemovableKey)];
47 [dict setObject:[NSNumber numberWithInt:size_bytes] 45 [dict setObject:[NSNumber numberWithInt:size_bytes]
48 forKey:base::mac::CFToNSCast(kDADiskDescriptionMediaSizeKey)]; 46 forKey:base::mac::CFToNSCast(kDADiskDescriptionMediaSizeKey)];
49 return DiskInfoMac::BuildDiskInfoOnFileThread(base::mac::NSToCFCast(dict)); 47 return DiskInfoMac::BuildDiskInfoOnFileThread(base::mac::NSToCFCast(dict));
50 } 48 }
51 49
50 class MockStorageObserver
vandebo (ex-Chrome) 2013/01/04 21:06:03 Duplicate... pull this into a test util file.
Greg Billock 2013/01/07 21:55:18 Done.
51 : public RemovableStorageNotifications::RemovableStorageObserver {
52 public:
53 MockStorageObserver() {}
54 ~MockStorageObserver() {}
55
56 MOCK_METHOD3(OnRemovableStorageAttached,
57 void(const std::string& id,
58 const string16& name,
59 const FilePath::StringType& location));
60 MOCK_METHOD1(OnRemovableStorageDetached, void(const std::string& id));
61 };
62
52 } // namespace 63 } // namespace
53 64
54 class RemovableDeviceNotificationsMacTest : public testing::Test { 65 class RemovableDeviceNotificationsMacTest : public testing::Test {
55 public: 66 public:
56 RemovableDeviceNotificationsMacTest() 67 RemovableDeviceNotificationsMacTest()
57 : message_loop_(MessageLoop::TYPE_IO), 68 : message_loop_(MessageLoop::TYPE_IO),
58 file_thread_(content::BrowserThread::FILE, &message_loop_) { 69 file_thread_(content::BrowserThread::FILE, &message_loop_) {
59 } 70 }
60 71
61 virtual void SetUp() OVERRIDE { 72 virtual void SetUp() OVERRIDE {
73 notifications_ = new RemovableDeviceNotificationsMac;
62 base::SystemMonitor::AllocateSystemIOPorts(); 74 base::SystemMonitor::AllocateSystemIOPorts();
63 system_monitor_.reset(new base::SystemMonitor());
64 75
65 mock_devices_changed_observer_.reset(new base::MockDevicesChangedObserver); 76 mock_storage_observer_.reset(new MockStorageObserver);
66 system_monitor_->AddDevicesChangedObserver( 77 notifications_->AddRemovableStorageObserver(
67 mock_devices_changed_observer_.get()); 78 mock_storage_observer_.get());
68 79
69 notifications_ = new RemovableDeviceNotificationsMac;
70 80
71 unique_id_ = "test_id"; 81 unique_id_ = "test_id";
72 display_name_ = ASCIIToUTF16("977 KB Test Display Name"); 82 display_name_ = ASCIIToUTF16("977 KB Test Display Name");
73 mount_point_ = FilePath("/unused_test_directory"); 83 mount_point_ = FilePath("/unused_test_directory");
74 device_id_ = MediaStorageUtil::MakeDeviceId( 84 device_id_ = MediaStorageUtil::MakeDeviceId(
75 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM, unique_id_); 85 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM, unique_id_);
76 disk_info_ = CreateDiskInfoMac(unique_id_, "", 86 disk_info_ = CreateDiskInfoMac(unique_id_, "",
77 ASCIIToUTF16("Test Display Name"), 87 ASCIIToUTF16("Test Display Name"),
78 mount_point_, kTestSize); 88 mount_point_, kTestSize);
79 } 89 }
80 90
81 protected: 91 protected:
82 // The message loop and file thread to run tests on. 92 // The message loop and file thread to run tests on.
83 MessageLoop message_loop_; 93 MessageLoop message_loop_;
84 content::TestBrowserThread file_thread_; 94 content::TestBrowserThread file_thread_;
85 95
86 // SystemMonitor and DevicesChangedObserver to hook together to test. 96 scoped_ptr<MockStorageObserver> mock_storage_observer_;
87 scoped_ptr<base::SystemMonitor> system_monitor_;
88 scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_;
89 97
90 // Information about the disk. 98 // Information about the disk.
91 std::string unique_id_; 99 std::string unique_id_;
92 string16 display_name_; 100 string16 display_name_;
93 FilePath mount_point_; 101 FilePath mount_point_;
94 std::string device_id_; 102 std::string device_id_;
95 DiskInfoMac disk_info_; 103 DiskInfoMac disk_info_;
96 104
97 scoped_refptr<RemovableDeviceNotificationsMac> notifications_; 105 scoped_refptr<RemovableDeviceNotificationsMac> notifications_;
98 }; 106 };
99 107
100 TEST_F(RemovableDeviceNotificationsMacTest, AddRemove) { 108 TEST_F(RemovableDeviceNotificationsMacTest, AddRemove) {
101 { 109 {
102 EXPECT_CALL(*mock_devices_changed_observer_, 110 EXPECT_CALL(*mock_storage_observer_,
103 OnRemovableStorageAttached(device_id_, 111 OnRemovableStorageAttached(device_id_,
104 display_name_, 112 display_name_,
105 mount_point_.value())); 113 mount_point_.value()));
106 notifications_->UpdateDisk( 114 notifications_->UpdateDisk(
107 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); 115 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED);
108 message_loop_.RunUntilIdle(); 116 message_loop_.RunUntilIdle();
109 } 117 }
110 118
111 { 119 {
112 EXPECT_CALL(*mock_devices_changed_observer_, 120 EXPECT_CALL(*mock_storage_observer_,
113 OnRemovableStorageDetached(device_id_)); 121 OnRemovableStorageDetached(device_id_));
114 notifications_->UpdateDisk( 122 notifications_->UpdateDisk(
115 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_REMOVED); 123 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_REMOVED);
116 message_loop_.RunUntilIdle(); 124 message_loop_.RunUntilIdle();
117 } 125 }
118 } 126 }
119 127
120 TEST_F(RemovableDeviceNotificationsMacTest, UpdateVolumeName) { 128 TEST_F(RemovableDeviceNotificationsMacTest, UpdateVolumeName) {
121 { 129 {
122 EXPECT_CALL(*mock_devices_changed_observer_, 130 EXPECT_CALL(*mock_storage_observer_,
123 OnRemovableStorageAttached(device_id_, 131 OnRemovableStorageAttached(device_id_,
124 display_name_, 132 display_name_,
125 mount_point_.value())); 133 mount_point_.value()));
126 notifications_->UpdateDisk( 134 notifications_->UpdateDisk(
127 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); 135 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED);
128 message_loop_.RunUntilIdle(); 136 message_loop_.RunUntilIdle();
129 } 137 }
130 138
131 { 139 {
132 string16 new_display_name(ASCIIToUTF16("977 KB Test Display Name")); 140 string16 new_display_name(ASCIIToUTF16("977 KB Test Display Name"));
133 DiskInfoMac info2 = CreateDiskInfoMac( 141 DiskInfoMac info2 = CreateDiskInfoMac(
134 unique_id_, "", ASCIIToUTF16("Test Display Name"), mount_point_, 142 unique_id_, "", ASCIIToUTF16("Test Display Name"), mount_point_,
135 kTestSize); 143 kTestSize);
136 EXPECT_CALL(*mock_devices_changed_observer_, 144 EXPECT_CALL(*mock_storage_observer_,
137 OnRemovableStorageDetached(device_id_)); 145 OnRemovableStorageDetached(device_id_));
138 EXPECT_CALL(*mock_devices_changed_observer_, 146 EXPECT_CALL(*mock_storage_observer_,
139 OnRemovableStorageAttached(device_id_, 147 OnRemovableStorageAttached(device_id_,
140 new_display_name, 148 new_display_name,
141 mount_point_.value())); 149 mount_point_.value()));
142 notifications_->UpdateDisk( 150 notifications_->UpdateDisk(
143 info2, RemovableDeviceNotificationsMac::UPDATE_DEVICE_CHANGED); 151 info2, RemovableDeviceNotificationsMac::UPDATE_DEVICE_CHANGED);
144 message_loop_.RunUntilIdle(); 152 message_loop_.RunUntilIdle();
145 } 153 }
146 } 154 }
147 155
148 TEST_F(RemovableDeviceNotificationsMacTest, DCIM) { 156 TEST_F(RemovableDeviceNotificationsMacTest, DCIM) {
149 base::ScopedTempDir temp_dir; 157 base::ScopedTempDir temp_dir;
150 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 158 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
151 file_util::CreateDirectory(temp_dir.path().Append(kDCIMDirectoryName)); 159 file_util::CreateDirectory(temp_dir.path().Append(kDCIMDirectoryName));
152 160
153 FilePath mount_point = temp_dir.path(); 161 FilePath mount_point = temp_dir.path();
154 DiskInfoMac info = CreateDiskInfoMac( 162 DiskInfoMac info = CreateDiskInfoMac(
155 unique_id_, "", ASCIIToUTF16("Test Display Name"), mount_point, 163 unique_id_, "", ASCIIToUTF16("Test Display Name"), mount_point,
156 kTestSize); 164 kTestSize);
157 std::string device_id = MediaStorageUtil::MakeDeviceId( 165 std::string device_id = MediaStorageUtil::MakeDeviceId(
158 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, unique_id_); 166 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, unique_id_);
159 167
160 { 168 {
161 EXPECT_CALL(*mock_devices_changed_observer_, 169 EXPECT_CALL(*mock_storage_observer_,
162 OnRemovableStorageAttached(device_id, 170 OnRemovableStorageAttached(device_id,
163 display_name_, 171 display_name_,
164 mount_point.value())); 172 mount_point.value()));
165 notifications_->UpdateDisk( 173 notifications_->UpdateDisk(
166 info, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); 174 info, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED);
167 message_loop_.RunUntilIdle(); 175 message_loop_.RunUntilIdle();
168 } 176 }
169 } 177 }
170 178
171 TEST_F(RemovableDeviceNotificationsMacTest, GetDeviceInfo) { 179 TEST_F(RemovableDeviceNotificationsMacTest, GetDeviceInfo) {
172 { 180 {
173 EXPECT_CALL(*mock_devices_changed_observer_, 181 EXPECT_CALL(*mock_storage_observer_,
174 OnRemovableStorageAttached(device_id_, 182 OnRemovableStorageAttached(device_id_,
175 display_name_, 183 display_name_,
176 mount_point_.value())); 184 mount_point_.value()));
177 notifications_->UpdateDisk( 185 notifications_->UpdateDisk(
178 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); 186 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED);
179 message_loop_.RunUntilIdle(); 187 message_loop_.RunUntilIdle();
180 } 188 }
181 189
182 base::SystemMonitor::RemovableStorageInfo info; 190 RemovableStorageNotifications::RemovableStorageInfo info;
183 EXPECT_TRUE(notifications_->GetDeviceInfoForPath( 191 EXPECT_TRUE(notifications_->GetDeviceInfoForPath(
184 mount_point_.AppendASCII("foo"), &info)); 192 mount_point_.AppendASCII("foo"), &info));
185 EXPECT_EQ(info.device_id, device_id_); 193 EXPECT_EQ(info.device_id, device_id_);
186 EXPECT_EQ(info.name, ASCIIToUTF16("Test Display Name")); 194 EXPECT_EQ(info.name, ASCIIToUTF16("Test Display Name"));
187 EXPECT_EQ(info.location, mount_point_.value()); 195 EXPECT_EQ(info.location, mount_point_.value());
188 196
189 EXPECT_FALSE(notifications_->GetDeviceInfoForPath( 197 EXPECT_FALSE(notifications_->GetDeviceInfoForPath(
190 FilePath("/non/matching/path"), &info)); 198 FilePath("/non/matching/path"), &info));
191 } 199 }
192 200
193 TEST_F(RemovableDeviceNotificationsMacTest, GetStorageSize) { 201 TEST_F(RemovableDeviceNotificationsMacTest, GetStorageSize) {
194 EXPECT_CALL(*mock_devices_changed_observer_, 202 EXPECT_CALL(*mock_storage_observer_,
195 OnRemovableStorageAttached(testing::_, 203 OnRemovableStorageAttached(testing::_,
196 testing::_, 204 testing::_,
197 testing::_)); 205 testing::_));
198 notifications_->UpdateDisk( 206 notifications_->UpdateDisk(
199 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); 207 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED);
200 message_loop_.RunUntilIdle(); 208 message_loop_.RunUntilIdle();
201 209
202 EXPECT_EQ(kTestSize, 210 EXPECT_EQ(kTestSize,
203 notifications_->GetStorageSize("/unused_test_directory")); 211 notifications_->GetStorageSize("/unused_test_directory"));
204 } 212 }
205 213
206 // Test that mounting a DMG doesn't send a notification. 214 // Test that mounting a DMG doesn't send a notification.
207 TEST_F(RemovableDeviceNotificationsMacTest, DMG) { 215 TEST_F(RemovableDeviceNotificationsMacTest, DMG) {
208 EXPECT_CALL(*mock_devices_changed_observer_, 216 EXPECT_CALL(*mock_storage_observer_,
209 OnRemovableStorageAttached(testing::_, 217 OnRemovableStorageAttached(testing::_,
210 testing::_, 218 testing::_,
211 testing::_)).Times(0); 219 testing::_)).Times(0);
212 DiskInfoMac info = CreateDiskInfoMac( 220 DiskInfoMac info = CreateDiskInfoMac(
213 unique_id_, "Disk Image", display_name_, mount_point_, kTestSize); 221 unique_id_, "Disk Image", display_name_, mount_point_, kTestSize);
214 notifications_->UpdateDisk( 222 notifications_->UpdateDisk(
215 info, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED); 223 info, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED);
216 message_loop_.RunUntilIdle(); 224 message_loop_.RunUntilIdle();
217 } 225 }
218 226
219 } // namespace chrome 227 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698