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

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

Issue 10919185: Implement RemovableDeviceNotifications for Mac (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address review comments Created 8 years, 3 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
(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 mock_devices_changed_observer_.reset(new base::MockDevicesChangedObserver);
52 system_monitor_.AddDevicesChangedObserver(
53 mock_devices_changed_observer_.get());
54
55 notifications_.reset(new RemovableDeviceNotificationsMac);
56
57 unique_id_ = "test_id";
58 display_name_ = ASCIIToUTF16("Test Display Name");
59 mount_point_ = FilePath("/unused_test_directory");
60 device_id_ = MediaStorageUtil::MakeDeviceId(
61 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM, unique_id_);
62 disk_info_ = CreateDiskInfoMac(unique_id_, display_name_, mount_point_);
63 }
64
65 protected:
66 // The message loop and file thread to run tests on.
67 MessageLoop message_loop_;
68 content::TestBrowserThread file_thread_;
69
70 // SystemMonitor and DevicesChangedObserver to hook together to test.
71 base::SystemMonitor system_monitor_;
72 scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_;
73
74 // Information about the disk.
75 std::string unique_id_;
76 string16 display_name_;
77 FilePath mount_point_;
78 std::string device_id_;
79 DiskInfoMac disk_info_;
80
81 scoped_ptr<RemovableDeviceNotificationsMac> notifications_;
82 };
83
84 TEST_F(RemovableDeviceNotificationsMacTest, AddRemove) {
85 {
86 EXPECT_CALL(*mock_devices_changed_observer_,
87 OnRemovableStorageAttached(device_id_,
88 display_name_,
89 mount_point_.value()));
90 notifications_->UpdateDisk(
91 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED);
92 message_loop_.RunAllPending();
93 }
94
95 {
96 EXPECT_CALL(*mock_devices_changed_observer_,
97 OnRemovableStorageDetached(device_id_));
98 notifications_->UpdateDisk(
99 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_REMOVED);
100 message_loop_.RunAllPending();
101 }
102 }
103
104 TEST_F(RemovableDeviceNotificationsMacTest, UpdateVolumeName) {
105 {
106 EXPECT_CALL(*mock_devices_changed_observer_,
107 OnRemovableStorageAttached(device_id_,
108 display_name_,
109 mount_point_.value()));
110 notifications_->UpdateDisk(
111 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED);
112 message_loop_.RunAllPending();
113 }
114
115 {
116 string16 new_display_name(ASCIIToUTF16("Test Display Name"));
117 DiskInfoMac info2 = CreateDiskInfoMac(
118 unique_id_, new_display_name, mount_point_);
119 EXPECT_CALL(*mock_devices_changed_observer_,
120 OnRemovableStorageDetached(device_id_));
121 EXPECT_CALL(*mock_devices_changed_observer_,
122 OnRemovableStorageAttached(device_id_,
123 new_display_name,
124 mount_point_.value()));
125 notifications_->UpdateDisk(
126 info2, RemovableDeviceNotificationsMac::UPDATE_DEVICE_CHANGED);
127 message_loop_.RunAllPending();
128 }
129 }
130
131 TEST_F(RemovableDeviceNotificationsMacTest, DCIM) {
132 ScopedTempDir temp_dir;
133 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
134 file_util::CreateDirectory(temp_dir.path().AppendASCII("DCIM"));
135
136 FilePath mount_point = temp_dir.path();
137 DiskInfoMac info = CreateDiskInfoMac(unique_id_, display_name_, mount_point);
138 std::string device_id = MediaStorageUtil::MakeDeviceId(
139 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, unique_id_);
140
141 {
142 EXPECT_CALL(*mock_devices_changed_observer_,
143 OnRemovableStorageAttached(device_id,
144 display_name_,
145 mount_point.value()));
146 notifications_->UpdateDisk(
147 info, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED);
148 message_loop_.RunAllPending();
149 }
150 }
151
152 TEST_F(RemovableDeviceNotificationsMacTest, GetDeviceInfo) {
153 {
154 EXPECT_CALL(*mock_devices_changed_observer_,
155 OnRemovableStorageAttached(device_id_,
156 display_name_,
157 mount_point_.value()));
158 notifications_->UpdateDisk(
159 disk_info_, RemovableDeviceNotificationsMac::UPDATE_DEVICE_ADDED);
160 message_loop_.RunAllPending();
161 }
162
163 base::SystemMonitor::RemovableStorageInfo info;
164 EXPECT_TRUE(notifications_->GetDeviceInfoForPath(
165 mount_point_.AppendASCII("foo"), &info));
166 EXPECT_EQ(info.device_id, device_id_);
167 EXPECT_EQ(info.name, display_name_);
168 EXPECT_EQ(info.location, mount_point_.value());
169
170 EXPECT_FALSE(notifications_->GetDeviceInfoForPath(
171 FilePath("/non/matching/path"), &info));
172 }
173
174 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698