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

Side by Side Diff: chrome/browser/media_gallery/removable_device_notifications_linux_unittest.cc

Issue 10882039: Make the Linux System Monitor implementation track all devices (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix CrOS build 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
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 // MediaDeviceNotificationsLinux unit tests. 5 // RemovableDeviceNotificationsLinux unit tests.
6 6
7 #include "chrome/browser/media_gallery/media_device_notifications_linux.h" 7 #include "chrome/browser/media_gallery/removable_device_notifications_linux.h"
8 8
9 #include <mntent.h> 9 #include <mntent.h>
10 #include <stdio.h> 10 #include <stdio.h>
11 11
12 #include <string> 12 #include <string>
13 13
14 #include "base/file_util.h" 14 #include "base/file_util.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/memory/scoped_ptr.h" 16 #include "base/memory/scoped_ptr.h"
17 #include "base/message_loop.h" 17 #include "base/message_loop.h"
(...skipping 12 matching lines...) Expand all
30 using testing::_; 30 using testing::_;
31 31
32 const char kValidFS[] = "vfat"; 32 const char kValidFS[] = "vfat";
33 const char kInvalidFS[] = "invalidfs"; 33 const char kInvalidFS[] = "invalidfs";
34 34
35 const char kInvalidPath[] = "invalid path does not exist"; 35 const char kInvalidPath[] = "invalid path does not exist";
36 36
37 const char kDevice1[] = "d1"; 37 const char kDevice1[] = "d1";
38 const char kDevice2[] = "d2"; 38 const char kDevice2[] = "d2";
39 const char kDevice3[] = "d3"; 39 const char kDevice3[] = "d3";
40 const char kDevice4[] = "d4";
41 const char kDevice5[] = "d5";
40 42
41 const char kDeviceId1[] = "UUID:FFF0-000F"; 43 const char kInvalidDevice[] = "invalid_device";
42 const char kDeviceId2[] = "VendorModelSerial:ComName:Model2010:898989898989";
43 const char kDeviceId3[] = "VendorModelSerial:::WEM319X792";
44
45 const char kDeviceLabel1[] = "TEST_USB_MODEL_1";
46 const char kDeviceLabel2[] = "TEST_USB_MODEL_2";
47 const char kDeviceLabel3[] = "TEST_USB_MODEL_3";
48 44
49 const char kMountPointA[] = "mnt_a"; 45 const char kMountPointA[] = "mnt_a";
50 const char kMountPointB[] = "mnt_b"; 46 const char kMountPointB[] = "mnt_b";
47 const char kMountPointC[] = "mnt_c";
51 48
52 std::string GetDCIMDeviceId(std::string unique_id) { 49 const char kDCIM[] = "DCIM";
53 return chrome::MediaStorageUtil::MakeDeviceId( 50
54 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, unique_id); 51 struct TestDeviceData {
52 const char* device_path;
53 const char* unique_id;
54 const char* device_name;
55 MediaStorageUtil::Type type;
56 };
57
58 const TestDeviceData kTestDeviceData[] = {
59 {kDevice1, "UUID:FFF0-000F", "TEST_USB_MODEL_1",
60 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM},
61 {kDevice2, "VendorModelSerial:ComName:Model2010:89898989", "TEST_USB_MODEL_2",
62 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM},
63 {kDevice3, "VendorModelSerial:::WEM319X792", "TEST_USB_MODEL_3",
64 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM},
65 {kDevice4, "UUID:ABCD-1234", "TEST_USB_MODEL_4",
66 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM},
67 {kDevice5, "UUID:743A91FD2349", "TEST_USB_MODEL_5",
68 MediaStorageUtil::FIXED_MASS_STORAGE},
69 };
70
71 bool GetDeviceInfo(const FilePath& device_path, std::string* id,
72 string16* name, bool* removable) {
73 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) {
74 if (device_path.value() == kTestDeviceData[i].device_path) {
75 if (id)
76 *id = kTestDeviceData[i].unique_id;
77 if (name)
78 *name = ASCIIToUTF16(kTestDeviceData[i].device_name);
79 if (removable) {
80 MediaStorageUtil::Type type = kTestDeviceData[i].type;
81 *removable =
82 (type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM) ||
83 (type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM);
84 }
85 return true;
86 }
87 }
88 return false;
55 } 89 }
56 90
57 bool GetDeviceInfo(const std::string& dev_path, std::string* id, 91 std::string GetDeviceId(const std::string& device) {
58 string16* name) { 92 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) {
59 std::string device_name; 93 if (device == kTestDeviceData[i].device_path) {
60 if (dev_path == kDevice1) { 94 return MediaStorageUtil::MakeDeviceId(kTestDeviceData[i].type,
61 *id = GetDCIMDeviceId(kDeviceId1); 95 kTestDeviceData[i].unique_id);
62 device_name = kDeviceLabel1; 96 }
63 } else if (dev_path == kDevice2) {
64 *id = GetDCIMDeviceId(kDeviceId2);
65 device_name = kDeviceLabel2;
66 } else if (dev_path == kDevice3) {
67 *id = GetDCIMDeviceId(kDeviceId3);
68 device_name = kDeviceLabel3;
69 } else {
70 return false;
71 } 97 }
72 98 if (device == kInvalidDevice) {
73 *name = ASCIIToUTF16(device_name); 99 return MediaStorageUtil::MakeDeviceId(MediaStorageUtil::FIXED_MASS_STORAGE,
74 return true; 100 kInvalidDevice);
101 }
102 return std::string();
103 }
104 string16 GetDeviceName(const std::string& device) {
105 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) {
106 if (device == kTestDeviceData[i].device_path)
107 return ASCIIToUTF16(kTestDeviceData[i].device_name);
108 }
109 return string16();
75 } 110 }
76 111
77 class MediaDeviceNotificationsLinuxTestWrapper 112 class RemovableDeviceNotificationsLinuxTestWrapper
78 : public MediaDeviceNotificationsLinux { 113 : public RemovableDeviceNotificationsLinux {
79 public: 114 public:
80 MediaDeviceNotificationsLinuxTestWrapper(const FilePath& path, 115 RemovableDeviceNotificationsLinuxTestWrapper(const FilePath& path,
81 MessageLoop* message_loop) 116 MessageLoop* message_loop)
82 : MediaDeviceNotificationsLinux(path, &GetDeviceInfo), 117 : RemovableDeviceNotificationsLinux(path, &GetDeviceInfo),
83 message_loop_(message_loop) { 118 message_loop_(message_loop) {
84 } 119 }
85 120
86 private: 121 private:
87 // Avoids code deleting the object while there are references to it. 122 // Avoids code deleting the object while there are references to it.
88 // Aside from the base::RefCountedThreadSafe friend class, any attempts to 123 // Aside from the base::RefCountedThreadSafe friend class, any attempts to
89 // call this dtor will result in a compile-time error. 124 // call this dtor will result in a compile-time error.
90 ~MediaDeviceNotificationsLinuxTestWrapper() {} 125 ~RemovableDeviceNotificationsLinuxTestWrapper() {}
91 126
92 virtual void OnFilePathChanged(const FilePath& path, bool error) OVERRIDE { 127 virtual void OnFilePathChanged(const FilePath& path, bool error) OVERRIDE {
93 MediaDeviceNotificationsLinux::OnFilePathChanged(path, error); 128 RemovableDeviceNotificationsLinux::OnFilePathChanged(path, error);
94 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 129 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
95 } 130 }
96 131
97 MessageLoop* message_loop_; 132 MessageLoop* message_loop_;
98 133
99 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinuxTestWrapper); 134 DISALLOW_COPY_AND_ASSIGN(RemovableDeviceNotificationsLinuxTestWrapper);
100 }; 135 };
101 136
102 class MediaDeviceNotificationsLinuxTest : public testing::Test { 137 class RemovableDeviceNotificationLinuxTest : public testing::Test {
103 public: 138 public:
104 struct MtabTestData { 139 struct MtabTestData {
105 MtabTestData(const std::string& mount_device, 140 MtabTestData(const std::string& mount_device,
106 const std::string& mount_point, 141 const std::string& mount_point,
107 const std::string& mount_type) 142 const std::string& mount_type)
108 : mount_device(mount_device), 143 : mount_device(mount_device),
109 mount_point(mount_point), 144 mount_point(mount_point),
110 mount_type(mount_type) { 145 mount_type(mount_type) {
111 } 146 }
112 147
113 const std::string mount_device; 148 const std::string mount_device;
114 const std::string mount_point; 149 const std::string mount_point;
115 const std::string mount_type; 150 const std::string mount_type;
116 }; 151 };
117 152
118 MediaDeviceNotificationsLinuxTest() 153 RemovableDeviceNotificationLinuxTest()
119 : message_loop_(MessageLoop::TYPE_IO), 154 : message_loop_(MessageLoop::TYPE_IO),
120 file_thread_(content::BrowserThread::FILE, &message_loop_) { 155 file_thread_(content::BrowserThread::FILE, &message_loop_) {
121 } 156 }
122 virtual ~MediaDeviceNotificationsLinuxTest() {} 157 virtual ~RemovableDeviceNotificationLinuxTest() {}
123 158
124 protected: 159 protected:
125 virtual void SetUp() OVERRIDE { 160 virtual void SetUp() OVERRIDE {
126 mock_devices_changed_observer_.reset(new base::MockDevicesChangedObserver); 161 mock_devices_changed_observer_.reset(new base::MockDevicesChangedObserver);
127 system_monitor_.AddDevicesChangedObserver( 162 system_monitor_.AddDevicesChangedObserver(
128 mock_devices_changed_observer_.get()); 163 mock_devices_changed_observer_.get());
129 164
130 // Create and set up a temp dir with files for the test. 165 // Create and set up a temp dir with files for the test.
131 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); 166 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
132 FilePath test_dir = scoped_temp_dir_.path().AppendASCII("test_etc"); 167 FilePath test_dir = scoped_temp_dir_.path().AppendASCII("test_etc");
133 ASSERT_TRUE(file_util::CreateDirectory(test_dir)); 168 ASSERT_TRUE(file_util::CreateDirectory(test_dir));
134 mtab_file_ = test_dir.AppendASCII("test_mtab"); 169 mtab_file_ = test_dir.AppendASCII("test_mtab");
135 MtabTestData initial_test_data[] = { 170 MtabTestData initial_test_data[] = {
136 MtabTestData("dummydevice", "dummydir", kInvalidFS), 171 MtabTestData("dummydevice", "dummydir", kInvalidFS),
137 }; 172 };
138 WriteToMtab(initial_test_data, 173 WriteToMtab(initial_test_data,
139 arraysize(initial_test_data), 174 arraysize(initial_test_data),
140 true /* overwrite */); 175 true /* overwrite */);
141 176
142 // Initialize the test subject. 177 // Initialize the test subject.
143 notifications_ = 178 notifications_ = new RemovableDeviceNotificationsLinuxTestWrapper(
144 new MediaDeviceNotificationsLinuxTestWrapper(mtab_file_, 179 mtab_file_, &message_loop_);
145 &message_loop_);
146 notifications_->Init(); 180 notifications_->Init();
147 message_loop_.RunAllPending(); 181 message_loop_.RunAllPending();
148 } 182 }
149 183
150 virtual void TearDown() OVERRIDE { 184 virtual void TearDown() OVERRIDE {
151 message_loop_.RunAllPending(); 185 message_loop_.RunAllPending();
152 notifications_ = NULL; 186 notifications_ = NULL;
153 system_monitor_.RemoveDevicesChangedObserver( 187 system_monitor_.RemoveDevicesChangedObserver(
154 mock_devices_changed_observer_.get()); 188 mock_devices_changed_observer_.get());
155 } 189 }
(...skipping 13 matching lines...) Expand all
169 } 203 }
170 204
171 // Simplied version of OverwriteMtabAndRunLoop() that just deletes all the 205 // Simplied version of OverwriteMtabAndRunLoop() that just deletes all the
172 // entries in the mtab file. 206 // entries in the mtab file.
173 void WriteEmptyMtabAndRunLoop() { 207 void WriteEmptyMtabAndRunLoop() {
174 OverwriteMtabAndRunLoop(NULL, // No data. 208 OverwriteMtabAndRunLoop(NULL, // No data.
175 0); // No data length. 209 0); // No data length.
176 } 210 }
177 211
178 // Create a directory named |dir| relative to the test directory. 212 // Create a directory named |dir| relative to the test directory.
179 // It has a DCIM directory, so MediaDeviceNotificationsLinux recognizes it as 213 // It has a DCIM directory, so RemovableDeviceNotificationsLinux recognizes it
180 // a media directory. 214 // as a media directory.
181 FilePath CreateMountPointWithDCIMDir(const std::string& dir) { 215 FilePath CreateMountPointWithDCIMDir(const std::string& dir) {
182 return CreateMountPoint(dir, true /* create DCIM dir */); 216 return CreateMountPoint(dir, true /* create DCIM dir */);
183 } 217 }
184 218
185 // Create a directory named |dir| relative to the test directory. 219 // Create a directory named |dir| relative to the test directory.
186 // It does not have a DCIM directory, so MediaDeviceNotificationsLinux does 220 // It does not have a DCIM directory, so RemovableDeviceNotificationsLinux
187 // not recognizes it as a media directory. 221 // does not recognizes it as a media directory.
188 FilePath CreateMountPointWithoutDCIMDir(const std::string& dir) { 222 FilePath CreateMountPointWithoutDCIMDir(const std::string& dir) {
189 return CreateMountPoint(dir, false /* do not create DCIM dir */); 223 return CreateMountPoint(dir, false /* do not create DCIM dir */);
190 } 224 }
191 225
226 void RemoveDCIMDirFromMountPoint(const std::string& dir) {
227 FilePath dcim(scoped_temp_dir_.path().AppendASCII(dir).AppendASCII(kDCIM));
228 file_util::Delete(dcim, false);
229 }
230
192 base::MockDevicesChangedObserver& observer() { 231 base::MockDevicesChangedObserver& observer() {
193 return *mock_devices_changed_observer_; 232 return *mock_devices_changed_observer_;
194 } 233 }
195 234
235 RemovableDeviceNotificationsLinux* notifier() {
236 return notifications_.get();
237 }
238
196 private: 239 private:
197 // Create a directory named |dir| relative to the test directory. 240 // Create a directory named |dir| relative to the test directory.
198 // Set |with_dcim_dir| to true if the created directory will have a "DCIM" 241 // Set |with_dcim_dir| to true if the created directory will have a "DCIM"
199 // subdirectory. 242 // subdirectory.
200 // Returns the full path to the created directory on success, or an empty 243 // Returns the full path to the created directory on success, or an empty
201 // path on failure. 244 // path on failure.
202 FilePath CreateMountPoint(const std::string& dir, bool with_dcim_dir) { 245 FilePath CreateMountPoint(const std::string& dir, bool with_dcim_dir) {
203 FilePath return_path(scoped_temp_dir_.path()); 246 FilePath return_path(scoped_temp_dir_.path());
204 return_path = return_path.AppendASCII(dir); 247 return_path = return_path.AppendASCII(dir);
205 FilePath path(return_path); 248 FilePath path(return_path);
206 if (with_dcim_dir) 249 if (with_dcim_dir)
207 path = path.AppendASCII("DCIM"); 250 path = path.AppendASCII(kDCIM);
208 if (!file_util::CreateDirectory(path)) 251 if (!file_util::CreateDirectory(path))
209 return FilePath(); 252 return FilePath();
210 return return_path; 253 return return_path;
211 } 254 }
212 255
213 // Write the test mtab data to |mtab_file_|. 256 // Write the test mtab data to |mtab_file_|.
214 // |data| is an array of mtab entries. 257 // |data| is an array of mtab entries.
215 // |data_size| is the array size of |data|. 258 // |data_size| is the array size of |data|.
216 // |overwrite| specifies whether to overwrite |mtab_file_|. 259 // |overwrite| specifies whether to overwrite |mtab_file_|.
217 void WriteToMtab(const MtabTestData* data, 260 void WriteToMtab(const MtabTestData* data,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 292
250 // SystemMonitor and DevicesChangedObserver to hook together to test. 293 // SystemMonitor and DevicesChangedObserver to hook together to test.
251 base::SystemMonitor system_monitor_; 294 base::SystemMonitor system_monitor_;
252 scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_; 295 scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_;
253 296
254 // Temporary directory for created test data. 297 // Temporary directory for created test data.
255 ScopedTempDir scoped_temp_dir_; 298 ScopedTempDir scoped_temp_dir_;
256 // Path to the test mtab file. 299 // Path to the test mtab file.
257 FilePath mtab_file_; 300 FilePath mtab_file_;
258 301
259 scoped_refptr<MediaDeviceNotificationsLinuxTestWrapper> notifications_; 302 scoped_refptr<RemovableDeviceNotificationsLinuxTestWrapper> notifications_;
260 303
261 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinuxTest); 304 DISALLOW_COPY_AND_ASSIGN(RemovableDeviceNotificationLinuxTest);
262 }; 305 };
263 306
264 // Simple test case where we attach and detach a media device. 307 // Simple test case where we attach and detach a media device.
265 TEST_F(MediaDeviceNotificationsLinuxTest, BasicAttachDetach) { 308 TEST_F(RemovableDeviceNotificationLinuxTest, BasicAttachDetach) {
266 testing::Sequence mock_sequence; 309 testing::Sequence mock_sequence;
267 FilePath test_path = CreateMountPointWithDCIMDir(kMountPointA); 310 FilePath test_path = CreateMountPointWithDCIMDir(kMountPointA);
268 ASSERT_FALSE(test_path.empty()); 311 ASSERT_FALSE(test_path.empty());
269 MtabTestData test_data[] = { 312 MtabTestData test_data[] = {
270 MtabTestData(kDevice1, kInvalidPath, kValidFS), 313 MtabTestData(kDevice1, kInvalidPath, kValidFS),
271 MtabTestData(kDevice2, test_path.value(), kValidFS), 314 MtabTestData(kDevice2, test_path.value(), kValidFS),
272 }; 315 };
273 // Only |kDevice2| should be attached, since |kDevice1| has a bad path. 316 // Only |kDevice2| should be attached, since |kDevice1| has a bad path.
274 EXPECT_CALL(observer(), 317 EXPECT_CALL(observer(), OnRemovableStorageAttached(GetDeviceId(kDevice2),
275 OnRemovableStorageAttached(GetDCIMDeviceId(kDeviceId2), 318 GetDeviceName(kDevice2),
276 ASCIIToUTF16(kDeviceLabel2), 319 test_path.value()))
277 test_path.value()))
278 .InSequence(mock_sequence); 320 .InSequence(mock_sequence);
279 AppendToMtabAndRunLoop(test_data, arraysize(test_data)); 321 AppendToMtabAndRunLoop(test_data, arraysize(test_data));
280 322
281 // |kDevice2| should be detached here. 323 // |kDevice2| should be detached here.
282 EXPECT_CALL(observer(), 324 EXPECT_CALL(observer(), OnRemovableStorageDetached(GetDeviceId(kDevice2)))
283 OnRemovableStorageDetached(GetDCIMDeviceId(kDeviceId2)))
284 .InSequence(mock_sequence); 325 .InSequence(mock_sequence);
285 WriteEmptyMtabAndRunLoop(); 326 WriteEmptyMtabAndRunLoop();
286 } 327 }
287 328
288 // Only mount points with DCIM directories are recognized. 329 // Only mount points with DCIM directories are recognized.
289 TEST_F(MediaDeviceNotificationsLinuxTest, DCIM) { 330 TEST_F(RemovableDeviceNotificationLinuxTest, DCIM) {
290 testing::Sequence mock_sequence; 331 testing::Sequence mock_sequence;
291 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); 332 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
292 ASSERT_FALSE(test_path_a.empty()); 333 ASSERT_FALSE(test_path_a.empty());
293 MtabTestData test_data1[] = { 334 MtabTestData test_data1[] = {
294 MtabTestData(kDevice1, test_path_a.value(), kValidFS), 335 MtabTestData(kDevice1, test_path_a.value(), kValidFS),
295 }; 336 };
296 // |kDevice1| should be attached as expected. 337 // |kDevice1| should be attached as expected.
297 EXPECT_CALL(observer(), 338 EXPECT_CALL(observer(), OnRemovableStorageAttached(GetDeviceId(kDevice1),
298 OnRemovableStorageAttached(GetDCIMDeviceId(kDeviceId1), 339 GetDeviceName(kDevice1),
299 ASCIIToUTF16(kDeviceLabel1), 340 test_path_a.value()))
300 test_path_a.value()))
301 .InSequence(mock_sequence); 341 .InSequence(mock_sequence);
302 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1)); 342 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1));
303 343
304 // This should do nothing, since |kMountPointB| does not have a DCIM dir. 344 // This should do nothing, since |kMountPointB| does not have a DCIM dir.
305 FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB); 345 FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB);
306 ASSERT_FALSE(test_path_b.empty()); 346 ASSERT_FALSE(test_path_b.empty());
307 MtabTestData test_data2[] = { 347 MtabTestData test_data2[] = {
308 MtabTestData(kDevice2, test_path_b.value(), kValidFS), 348 MtabTestData(kDevice2, test_path_b.value(), kValidFS),
309 }; 349 };
310 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); 350 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2));
311 351
312 // |kDevice1| should be detached as expected. 352 // |kDevice1| should be detached as expected.
313 EXPECT_CALL(observer(), 353 EXPECT_CALL(observer(), OnRemovableStorageDetached(GetDeviceId(kDevice1)))
314 OnRemovableStorageDetached(GetDCIMDeviceId(kDeviceId1)))
315 .InSequence(mock_sequence); 354 .InSequence(mock_sequence);
316 WriteEmptyMtabAndRunLoop(); 355 WriteEmptyMtabAndRunLoop();
317 } 356 }
318 357
319 // More complicated test case with multiple devices on multiple mount points. 358 // More complicated test case with multiple devices on multiple mount points.
320 TEST_F(MediaDeviceNotificationsLinuxTest, SwapMountPoints) { 359 TEST_F(RemovableDeviceNotificationLinuxTest, SwapMountPoints) {
321 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); 360 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
322 FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB); 361 FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB);
323 ASSERT_FALSE(test_path_a.empty()); 362 ASSERT_FALSE(test_path_a.empty());
324 ASSERT_FALSE(test_path_b.empty()); 363 ASSERT_FALSE(test_path_b.empty());
325 364
326 // Attach two devices. 365 // Attach two devices.
327 // kDevice1 -> kMountPointA 366 // kDevice1 -> kMountPointA
328 // kDevice2 -> kMountPointB 367 // kDevice2 -> kMountPointB
329 MtabTestData test_data1[] = { 368 MtabTestData test_data1[] = {
330 MtabTestData(kDevice1, test_path_a.value(), kValidFS), 369 MtabTestData(kDevice1, test_path_a.value(), kValidFS),
(...skipping 15 matching lines...) Expand all
346 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(2); 385 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(2);
347 OverwriteMtabAndRunLoop(test_data2, arraysize(test_data2)); 386 OverwriteMtabAndRunLoop(test_data2, arraysize(test_data2));
348 387
349 // Detach all devices. 388 // Detach all devices.
350 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0); 389 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0);
351 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(2); 390 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(2);
352 WriteEmptyMtabAndRunLoop(); 391 WriteEmptyMtabAndRunLoop();
353 } 392 }
354 393
355 // More complicated test case with multiple devices on multiple mount points. 394 // More complicated test case with multiple devices on multiple mount points.
356 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesMultiMountPoints) { 395 TEST_F(RemovableDeviceNotificationLinuxTest, MultiDevicesMultiMountPoints) {
357 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); 396 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
358 FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB); 397 FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB);
359 ASSERT_FALSE(test_path_a.empty()); 398 ASSERT_FALSE(test_path_a.empty());
360 ASSERT_FALSE(test_path_b.empty()); 399 ASSERT_FALSE(test_path_b.empty());
361 400
362 // Attach two devices. 401 // Attach two devices.
363 // kDevice1 -> kMountPointA 402 // kDevice1 -> kMountPointA *
364 // kDevice2 -> kMountPointB 403 // kDevice2 -> kMountPointB *
365 MtabTestData test_data1[] = { 404 MtabTestData test_data1[] = {
366 MtabTestData(kDevice1, test_path_a.value(), kValidFS), 405 MtabTestData(kDevice1, test_path_a.value(), kValidFS),
367 MtabTestData(kDevice2, test_path_b.value(), kValidFS), 406 MtabTestData(kDevice2, test_path_b.value(), kValidFS),
368 }; 407 };
369 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(2); 408 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(2);
370 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(0); 409 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(0);
371 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1)); 410 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1));
372 411
373 // Attach |kDevice1| to |kMountPointB|. 412 // Attach |kDevice1| to |kMountPointB|.
374 // |kDevice2| is inaccessible, so it is detached. |kDevice1| has been 413 // |kDevice2| is inaccessible, so it is detached. |kDevice1| has been
375 // re-attached at |kMountPointB|, so it is 'detached' from kMountPointA. 414 // attached at |kMountPointB|, but is still accessible from |kMountPointA|.
376 // kDevice1 -> kMountPointA 415 // kDevice1 -> kMountPointA *
377 // kDevice2 -> kMountPointB 416 // kDevice2 -> kMountPointB
378 // kDevice1 -> kMountPointB 417 // kDevice1 -> kMountPointB
379 MtabTestData test_data2[] = { 418 MtabTestData test_data2[] = {
380 MtabTestData(kDevice1, test_path_b.value(), kValidFS), 419 MtabTestData(kDevice1, test_path_b.value(), kValidFS),
381 }; 420 };
382 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(1); 421 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0);
383 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(2); 422 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(1);
384 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); 423 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2));
385 424
386 // Attach |kDevice2| to |kMountPointA|. 425 // Detach |kDevice1| from |kMountPointA|, causing a detach and attach event.
387 // kDevice1 -> kMountPointA
388 // kDevice2 -> kMountPointB 426 // kDevice2 -> kMountPointB
389 // kDevice1 -> kMountPointB 427 // kDevice1 -> kMountPointB *
390 // kDevice2 -> kMountPointA
391 MtabTestData test_data3[] = { 428 MtabTestData test_data3[] = {
392 MtabTestData(kDevice2, test_path_a.value(), kValidFS),
393 };
394 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(1);
395 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(0);
396 AppendToMtabAndRunLoop(test_data3, arraysize(test_data3));
397
398 // Detach |kDevice2| from |kMountPointA|.
399 // kDevice1 -> kMountPointA
400 // kDevice2 -> kMountPointB
401 // kDevice1 -> kMountPointB
402 MtabTestData test_data4[] = {
403 MtabTestData(kDevice1, test_path_a.value(), kValidFS),
404 MtabTestData(kDevice2, test_path_b.value(), kValidFS), 429 MtabTestData(kDevice2, test_path_b.value(), kValidFS),
405 MtabTestData(kDevice1, test_path_b.value(), kValidFS), 430 MtabTestData(kDevice1, test_path_b.value(), kValidFS),
406 }; 431 };
432 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(1);
433 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(1);
434 OverwriteMtabAndRunLoop(test_data3, arraysize(test_data3));
435
436 // Attach |kDevice1| to |kMountPointA|.
437 // kDevice2 -> kMountPointB
438 // kDevice1 -> kMountPointB *
439 // kDevice1 -> kMountPointA
440 MtabTestData test_data4[] = {
441 MtabTestData(kDevice1, test_path_a.value(), kValidFS),
442 };
407 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0); 443 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0);
408 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(1); 444 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(0);
409 OverwriteMtabAndRunLoop(test_data4, arraysize(test_data4)); 445 AppendToMtabAndRunLoop(test_data4, arraysize(test_data4));
410 446
411 // Detach |kDevice1| from |kMountPointB|. 447 // Detach |kDevice1| from |kMountPointB|.
412 // kDevice1 -> kMountPointA 448 // kDevice1 -> kMountPointA *
413 // kDevice2 -> kMountPointB 449 // kDevice2 -> kMountPointB *
414 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(2); 450 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(2);
415 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(1); 451 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(1);
416 OverwriteMtabAndRunLoop(test_data1, arraysize(test_data1)); 452 OverwriteMtabAndRunLoop(test_data1, arraysize(test_data1));
417 453
418 // Detach all devices. 454 // Detach all devices.
419 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0); 455 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0);
420 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(2); 456 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(2);
421 WriteEmptyMtabAndRunLoop(); 457 WriteEmptyMtabAndRunLoop();
422 } 458 }
423 459
424 // More complicated test case with multiple devices on one mount point. 460 TEST_F(RemovableDeviceNotificationLinuxTest,
425 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesOneMountPoint) { 461 MultipleMountPointsWithNonDCIMDevices) {
426 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); 462 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
427 FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB); 463 FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB);
428 ASSERT_FALSE(test_path_a.empty()); 464 ASSERT_FALSE(test_path_a.empty());
429 ASSERT_FALSE(test_path_b.empty()); 465 ASSERT_FALSE(test_path_b.empty());
430 466
431 // |kDevice1| is most recently mounted at |kMountPointB|. 467 // Attach to one first.
468 // kDevice1 -> kMountPointA *
469 MtabTestData test_data1[] = {
470 MtabTestData(kDevice1, test_path_a.value(), kValidFS),
471 };
472 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(1);
473 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(0);
474 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1));
475
476 // Attach |kDevice1| to |kMountPointB|.
477 // kDevice1 -> kMountPointA *
478 // kDevice1 -> kMountPointB
479 MtabTestData test_data2[] = {
480 MtabTestData(kDevice1, test_path_b.value(), kValidFS),
481 };
482 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0);
483 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(0);
484 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2));
485
486 // Attach |kDevice4| (a non-DCIM device) to |kMountPointA|.
432 // kDevice1 -> kMountPointA 487 // kDevice1 -> kMountPointA
433 // kDevice2 -> kMountPointB 488 // kDevice1 -> kMountPointB *
489 // kDevice4 -> kMountPointA
490 MtabTestData test_data3[] = {
491 MtabTestData(kDevice4, test_path_a.value(), kValidFS),
492 };
493 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(1);
494 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(1);
495 RemoveDCIMDirFromMountPoint(kMountPointA);
496 AppendToMtabAndRunLoop(test_data3, arraysize(test_data3));
497
498 // Detach |kDevice4|.
499 // kDevice1 -> kMountPointA
500 // kDevice1 -> kMountPointB *
501 MtabTestData test_data4[] = {
502 MtabTestData(kDevice1, test_path_a.value(), kValidFS),
503 MtabTestData(kDevice1, test_path_b.value(), kValidFS),
504 };
505 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0);
506 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(0);
507 OverwriteMtabAndRunLoop(test_data4, arraysize(test_data4));
508
509 // Detach |kDevice1| from |kMountPointA|.
510 // kDevice1 -> kMountPointB *
511 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0);
512 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(0);
513 OverwriteMtabAndRunLoop(test_data2, arraysize(test_data2));
514
515 // Detach all devices.
516 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0);
517 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(1);
518 WriteEmptyMtabAndRunLoop();
519 }
520
521 TEST_F(RemovableDeviceNotificationLinuxTest, MountLookUp) {
522 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
523 FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB);
524 FilePath test_path_c = CreateMountPointWithoutDCIMDir(kMountPointC);
525 ASSERT_FALSE(test_path_a.empty());
526 ASSERT_FALSE(test_path_b.empty());
527 ASSERT_FALSE(test_path_c.empty());
528
529 // Attach to one first.
530 // kDevice1 -> kMountPointA *
531 // kDevice4 -> kMountPointB
532 // kDevice5 -> kMountPointC
533 MtabTestData test_data1[] = {
534 MtabTestData(kDevice1, test_path_a.value(), kValidFS),
535 MtabTestData(kDevice4, test_path_b.value(), kValidFS),
536 MtabTestData(kDevice5, test_path_c.value(), kValidFS),
537 };
538 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(1);
539 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(0);
540 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1));
541
542 EXPECT_EQ(test_path_a,
543 notifier()->GetDeviceMountPoint(GetDeviceId(kDevice1)));
544 EXPECT_EQ(test_path_b,
545 notifier()->GetDeviceMountPoint(GetDeviceId(kDevice4)));
546 EXPECT_EQ(test_path_c,
547 notifier()->GetDeviceMountPoint(GetDeviceId(kDevice5)));
548 EXPECT_EQ(FilePath(),
549 notifier()->GetDeviceMountPoint(GetDeviceId(kInvalidDevice)));
550
551 // Make |kDevice1| mounted in multiple places.
552 // kDevice1 -> kMountPointA *
434 // kDevice1 -> kMountPointB 553 // kDevice1 -> kMountPointB
554 // kDevice1 -> kMountPointC
555 MtabTestData test_data2[] = {
556 MtabTestData(kDevice1, test_path_a.value(), kValidFS),
557 MtabTestData(kDevice1, test_path_b.value(), kValidFS),
558 MtabTestData(kDevice1, test_path_c.value(), kValidFS),
559 };
560 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0);
561 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(0);
562 // Add DCIM dirs.
563 CreateMountPointWithDCIMDir(kMountPointB);
564 CreateMountPointWithDCIMDir(kMountPointC);
565 OverwriteMtabAndRunLoop(test_data2, arraysize(test_data2));
566
567 EXPECT_EQ(test_path_a,
568 notifier()->GetDeviceMountPoint(GetDeviceId(kDevice1)));
569
570 // Unmount |kDevice1| from |kMountPointA|.
571 // kDevice1 -> kMountPointB *
572 // kDevice1 -> kMountPointC
573 // Either |kMountPointB| or |kMountPointC| is a valid new default, but
574 // it turns out that it will be B.
575 MtabTestData test_data3[] = {
576 MtabTestData(kDevice1, test_path_b.value(), kValidFS),
577 };
578 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(1);
579 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(1);
580 OverwriteMtabAndRunLoop(test_data3, arraysize(test_data3));
581
582 EXPECT_EQ(test_path_b,
583 notifier()->GetDeviceMountPoint(GetDeviceId(kDevice1)));
584
585 // Mount a non-removable device in multiple places.
586 // kDevice5 -> kMountPointA
587 // kDevice5 -> kMountPointB
588 // kDevice5 -> kMountPointC
589 MtabTestData test_data4[] = {
590 MtabTestData(kDevice5, test_path_a.value(), kValidFS),
591 MtabTestData(kDevice5, test_path_b.value(), kValidFS),
592 MtabTestData(kDevice5, test_path_c.value(), kValidFS),
593 };
594 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0);
595 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(1);
596 // Remove DCIM dirs.
597 RemoveDCIMDirFromMountPoint(kMountPointA);
598 RemoveDCIMDirFromMountPoint(kMountPointB);
599 RemoveDCIMDirFromMountPoint(kMountPointC);
600 OverwriteMtabAndRunLoop(test_data4, arraysize(test_data4));
601
602 // Any of the mount points would be valid.
603 EXPECT_EQ(test_path_a.value(),
604 notifier()->GetDeviceMountPoint(GetDeviceId(kDevice5)).value());
605 }
606
607 TEST_F(RemovableDeviceNotificationLinuxTest, DeviceLookUp) {
608 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
609 FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB);
610 FilePath test_path_c = CreateMountPointWithoutDCIMDir(kMountPointC);
611 ASSERT_FALSE(test_path_a.empty());
612 ASSERT_FALSE(test_path_b.empty());
613 ASSERT_FALSE(test_path_c.empty());
614
615 // Attach to one first.
616 // kDevice1 -> kMountPointA *
617 // kDevice4 -> kMountPointB
618 // kDevice5 -> kMountPointC
435 MtabTestData test_data1[] = { 619 MtabTestData test_data1[] = {
436 MtabTestData(kDevice1, test_path_a.value(), kValidFS), 620 MtabTestData(kDevice1, test_path_a.value(), kValidFS),
437 MtabTestData(kDevice2, test_path_b.value(), kValidFS), 621 MtabTestData(kDevice4, test_path_b.value(), kValidFS),
438 MtabTestData(kDevice1, test_path_b.value(), kValidFS), 622 MtabTestData(kDevice5, test_path_c.value(), kValidFS),
439 }; 623 };
440 EXPECT_CALL(observer(), 624 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(1);
441 OnRemovableStorageAttached(GetDCIMDeviceId(kDeviceId1), 625 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(0);
442 ASCIIToUTF16(kDeviceLabel1), 626 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1));
443 test_path_b.value())) 627
444 .Times(1); 628 FilePath mount_point;
445 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(0); 629 EXPECT_EQ(GetDeviceId(kDevice1),
446 OverwriteMtabAndRunLoop(test_data1, arraysize(test_data1)); 630 notifier()->GetDeviceIdForPath(test_path_a, &mount_point));
447 631 EXPECT_EQ(test_path_a.value(), mount_point.value());
448 // Attach |kDevice3| to |kMountPointB|. 632 EXPECT_EQ(GetDeviceId(kDevice4),
449 // |kDevice1| is inaccessible at its most recent mount point, so it is 633 notifier()->GetDeviceIdForPath(test_path_b, &mount_point));
450 // detached and unavailable, even though it is still accessible via 634 EXPECT_EQ(test_path_b.value(), mount_point.value());
451 // |kMountPointA|. 635 EXPECT_EQ(GetDeviceId(kDevice5),
452 // kDevice1 -> kMountPointA 636 notifier()->GetDeviceIdForPath(test_path_c, NULL));
453 // kDevice2 -> kMountPointB 637
454 // kDevice1 -> kMountPointB 638 // An invalid path.
455 // kDevice3 -> kMountPointB 639 EXPECT_EQ(std::string(),
640 notifier()->GetDeviceIdForPath(FilePath(kInvalidPath), NULL));
641
642 // Test filling in of the mount point.
643 EXPECT_EQ(GetDeviceId(kDevice1), notifier()->GetDeviceIdForPath(
644 test_path_a.Append("some/other/path"), &mount_point));
645 EXPECT_EQ(test_path_a.value(), mount_point.value());
646
647 // One device attached at multiple points.
648 // kDevice1 -> kMountPointA *
649 // kDevice5 -> kMountPointB
650 // kDevice5 -> kMountPointC
456 MtabTestData test_data2[] = { 651 MtabTestData test_data2[] = {
457 MtabTestData(kDevice3, test_path_b.value(), kValidFS), 652 MtabTestData(kDevice1, test_path_a.value(), kValidFS),
458 }; 653 MtabTestData(kDevice5, test_path_b.value(), kValidFS),
459 EXPECT_CALL(observer(), 654 MtabTestData(kDevice5, test_path_c.value(), kValidFS),
460 OnRemovableStorageDetached(GetDCIMDeviceId(kDeviceId1))) 655 };
461 .Times(1); 656 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0);
462 EXPECT_CALL(observer(), 657 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(0);
463 OnRemovableStorageAttached(GetDCIMDeviceId(kDeviceId3),
464 ASCIIToUTF16(kDeviceLabel3),
465 test_path_b.value()))
466 .Times(1);
467 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); 658 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2));
468 659
469 // Detach all devices. 660 EXPECT_EQ(GetDeviceId(kDevice1),
470 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0); 661 notifier()->GetDeviceIdForPath(test_path_a, NULL));
471 EXPECT_CALL(observer(), 662 EXPECT_EQ(GetDeviceId(kDevice5),
472 OnRemovableStorageDetached(GetDCIMDeviceId(kDeviceId3))) 663 notifier()->GetDeviceIdForPath(test_path_b, NULL));
473 .Times(1); 664 EXPECT_EQ(GetDeviceId(kDevice5),
474 WriteEmptyMtabAndRunLoop(); 665 notifier()->GetDeviceIdForPath(test_path_c, NULL));
475 } 666 }
476 667
477 } // namespace 668 } // namespace
478 669
479 } // namespace chrome 670 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/media_gallery/removable_device_notifications_linux.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698