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

Side by Side Diff: chrome/browser/system_monitor/removable_device_notifications_window_win_unittest.cc

Issue 11297002: [Media Gallery] Added code to support mtp device media file system on Windows. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removed DCHECK, added lock in PortableDeviceWatcherWin and fixed tests. Created 8 years 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_window_wi n.h" 5 #include "chrome/browser/system_monitor/removable_device_notifications_window_wi n.h"
6 6
7 #include <dbt.h> 7 #include <dbt.h>
8 8
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/bind.h"
12 #include "base/file_util.h" 13 #include "base/file_util.h"
13 #include "base/files/scoped_temp_dir.h" 14 #include "base/files/scoped_temp_dir.h"
14 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h" 16 #include "base/memory/scoped_ptr.h"
16 #include "base/message_loop.h" 17 #include "base/message_loop.h"
18 #include "base/sequenced_task_runner.h"
19 #include "base/synchronization/waitable_event.h"
17 #include "base/system_monitor/system_monitor.h" 20 #include "base/system_monitor/system_monitor.h"
18 #include "base/test/mock_devices_changed_observer.h" 21 #include "base/test/mock_devices_changed_observer.h"
22 #include "base/threading/sequenced_worker_pool.h"
19 #include "base/utf_string_conversions.h" 23 #include "base/utf_string_conversions.h"
20 #include "chrome/browser/system_monitor/media_storage_util.h" 24 #include "chrome/browser/system_monitor/media_storage_util.h"
21 #include "chrome/browser/system_monitor/portable_device_watcher_win.h" 25 #include "chrome/browser/system_monitor/portable_device_watcher_win.h"
22 #include "chrome/browser/system_monitor/removable_device_constants.h" 26 #include "chrome/browser/system_monitor/removable_device_constants.h"
23 #include "chrome/browser/system_monitor/volume_mount_watcher_win.h" 27 #include "chrome/browser/system_monitor/volume_mount_watcher_win.h"
24 #include "content/public/test/test_browser_thread.h" 28 #include "content/public/test/test_browser_thread.h"
25 #include "testing/gmock/include/gmock/gmock.h" 29 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
27 31
28 namespace chrome { 32 namespace chrome {
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 // Runs all the pending tasks on UI thread, FILE thread and blocking thread. 323 // Runs all the pending tasks on UI thread, FILE thread and blocking thread.
320 void RunUntilIdle(); 324 void RunUntilIdle();
321 325
322 void DoMassStorageDeviceAttachedTest(const DeviceIndices& device_indices); 326 void DoMassStorageDeviceAttachedTest(const DeviceIndices& device_indices);
323 void DoMassStorageDevicesDetachedTest(const DeviceIndices& device_indices); 327 void DoMassStorageDevicesDetachedTest(const DeviceIndices& device_indices);
324 328
325 // Injects a device attach or detach change (depending on the value of 329 // Injects a device attach or detach change (depending on the value of
326 // |test_attach|) and tests that the appropriate handler is called. 330 // |test_attach|) and tests that the appropriate handler is called.
327 void DoMTPDeviceTest(const string16& pnp_device_id, bool test_attach); 331 void DoMTPDeviceTest(const string16& pnp_device_id, bool test_attach);
328 332
333 // Gets the MTP details of the storage specified by the |storage_device_id|.
334 // On success, returns true and fills in |pnp_device_id| and
335 // |storage_object_id|.
336 bool GetMTPStorageInfo(const std::string& storage_device_id,
337 string16* pnp_device_id,
338 string16* storage_object_id);
339
340 scoped_ptr<TestRemovableDeviceNotificationsWindowWin> window_;
341
342 private:
343 // Gets the MTP details of the storage on the blocking pool thread.
344 void GetMTPStorageInfoOnBlockingThread(const std::string& storage_device_id,
345 string16* pnp_device_id,
346 string16* storage_object_id,
347 bool* result,
348 base::WaitableEvent* completion);
349
329 MessageLoopForUI message_loop_; 350 MessageLoopForUI message_loop_;
330 content::TestBrowserThread ui_thread_; 351 content::TestBrowserThread ui_thread_;
331 content::TestBrowserThread file_thread_; 352 content::TestBrowserThread file_thread_;
353 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
332 354
333 base::SystemMonitor system_monitor_; 355 base::SystemMonitor system_monitor_;
334 base::MockDevicesChangedObserver observer_; 356 base::MockDevicesChangedObserver observer_;
335 scoped_ptr<TestRemovableDeviceNotificationsWindowWin> window_;
336 scoped_refptr<TestVolumeMountWatcherWin> volume_mount_watcher_; 357 scoped_refptr<TestVolumeMountWatcherWin> volume_mount_watcher_;
337 }; 358 };
338 359
339 RemovableDeviceNotificationsWindowWinTest:: 360 RemovableDeviceNotificationsWindowWinTest::
340 RemovableDeviceNotificationsWindowWinTest() 361 RemovableDeviceNotificationsWindowWinTest()
341 : ui_thread_(BrowserThread::UI, &message_loop_), 362 : ui_thread_(BrowserThread::UI, &message_loop_),
342 file_thread_(BrowserThread::FILE, &message_loop_) { 363 file_thread_(BrowserThread::FILE, &message_loop_) {
343 } 364 }
344 365
345 RemovableDeviceNotificationsWindowWinTest:: 366 RemovableDeviceNotificationsWindowWinTest::
346 ~RemovableDeviceNotificationsWindowWinTest() { 367 ~RemovableDeviceNotificationsWindowWinTest() {
347 } 368 }
348 369
349 void RemovableDeviceNotificationsWindowWinTest::SetUp() { 370 void RemovableDeviceNotificationsWindowWinTest::SetUp() {
350 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); 371 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI));
351 volume_mount_watcher_ = new TestVolumeMountWatcherWin; 372 volume_mount_watcher_ = new TestVolumeMountWatcherWin;
352 window_.reset(new TestRemovableDeviceNotificationsWindowWin( 373 window_.reset(new TestRemovableDeviceNotificationsWindowWin(
353 volume_mount_watcher_.get(), new TestPortableDeviceWatcherWin)); 374 volume_mount_watcher_.get(), new TestPortableDeviceWatcherWin));
375 scoped_refptr<base::SequencedWorkerPool> pool =
376 content::BrowserThread::GetBlockingPool();
377 blocking_task_runner_ =
378 pool->GetSequencedTaskRunner(pool->GetSequenceToken());
354 window_->InitWithTestData(false); 379 window_->InitWithTestData(false);
355 RunUntilIdle(); 380 RunUntilIdle();
356 system_monitor_.AddDevicesChangedObserver(&observer_); 381 system_monitor_.AddDevicesChangedObserver(&observer_);
357 } 382 }
358 383
359 void RemovableDeviceNotificationsWindowWinTest::TearDown() { 384 void RemovableDeviceNotificationsWindowWinTest::TearDown() {
360 RunUntilIdle(); 385 RunUntilIdle();
361 system_monitor_.RemoveDevicesChangedObserver(&observer_); 386 system_monitor_.RemoveDevicesChangedObserver(&observer_);
362 } 387 }
363 388
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 .Times((name.empty() || unique_id.empty()) ? 0 : 1); 509 .Times((name.empty() || unique_id.empty()) ? 0 : 1);
485 } 510 }
486 } 511 }
487 } 512 }
488 window_->InjectDeviceChange( 513 window_->InjectDeviceChange(
489 test_attach ? DBT_DEVICEARRIVAL : DBT_DEVICEREMOVECOMPLETE, 514 test_attach ? DBT_DEVICEARRIVAL : DBT_DEVICEREMOVECOMPLETE,
490 reinterpret_cast<DWORD>(dev_interface_broadcast.get())); 515 reinterpret_cast<DWORD>(dev_interface_broadcast.get()));
491 RunUntilIdle(); 516 RunUntilIdle();
492 } 517 }
493 518
519 bool RemovableDeviceNotificationsWindowWinTest::GetMTPStorageInfo(
520 const std::string& storage_device_id,
521 string16* pnp_device_id,
522 string16* storage_object_id) {
523 base::WaitableEvent completion(false, false);
524 bool result = false;
525 blocking_task_runner_->PostTask(
526 FROM_HERE,
527 base::Bind(&RemovableDeviceNotificationsWindowWinTest::
528 GetMTPStorageInfoOnBlockingThread,
529 base::Unretained(this),
530 storage_device_id,
531 pnp_device_id,
532 storage_object_id,
533 &result,
534 &completion));
535 completion.Wait();
536 return result;
537 }
538
539 void RemovableDeviceNotificationsWindowWinTest::
540 GetMTPStorageInfoOnBlockingThread(
541 const std::string& storage_device_id,
542 string16* pnp_device_id,
543 string16* storage_object_id,
544 bool* result,
545 base::WaitableEvent* completion) {
546 *result = window_->GetMTPStorageInfoFromDeviceId(storage_device_id,
547 pnp_device_id,
548 storage_object_id);
549 completion->Signal();
550 }
551
494 TEST_F(RemovableDeviceNotificationsWindowWinTest, RandomMessage) { 552 TEST_F(RemovableDeviceNotificationsWindowWinTest, RandomMessage) {
495 window_->InjectDeviceChange(DBT_DEVICEQUERYREMOVE, NULL); 553 window_->InjectDeviceChange(DBT_DEVICEQUERYREMOVE, NULL);
496 RunUntilIdle(); 554 RunUntilIdle();
497 } 555 }
498 556
499 TEST_F(RemovableDeviceNotificationsWindowWinTest, DevicesAttached) { 557 TEST_F(RemovableDeviceNotificationsWindowWinTest, DevicesAttached) {
500 DeviceIndices device_indices; 558 DeviceIndices device_indices;
501 device_indices.push_back(1); 559 device_indices.push_back(1);
502 device_indices.push_back(5); 560 device_indices.push_back(5);
503 device_indices.push_back(7); 561 device_indices.push_back(7);
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
635 } 693 }
636 694
637 // Attach a device with two data partitions. Verify that attach/detach 695 // Attach a device with two data partitions. Verify that attach/detach
638 // notifications are sent out for each removable storage. 696 // notifications are sent out for each removable storage.
639 TEST_F(RemovableDeviceNotificationsWindowWinTest, 697 TEST_F(RemovableDeviceNotificationsWindowWinTest,
640 MTPDeviceWithMultipleStorageObjects) { 698 MTPDeviceWithMultipleStorageObjects) {
641 DoMTPDeviceTest(kMTPDeviceWithMultipleStorageObjects, true); 699 DoMTPDeviceTest(kMTPDeviceWithMultipleStorageObjects, true);
642 DoMTPDeviceTest(kMTPDeviceWithMultipleStorageObjects, false); 700 DoMTPDeviceTest(kMTPDeviceWithMultipleStorageObjects, false);
643 } 701 }
644 702
703 // Given a MTP storage persistent id, GetMTPStorageInfo() should fetch the
704 // device interface path and local storage object identifier.
705 TEST_F(RemovableDeviceNotificationsWindowWinTest, GetMTPStorageInfo) {
706 DoMTPDeviceTest(kMTPDeviceWithValidInfo, true);
707 PortableDeviceWatcherWin::StorageObjects storage_objects =
708 GetDeviceStorageObjects(kMTPDeviceWithValidInfo);
709 for (PortableDeviceWatcherWin::StorageObjects::const_iterator it =
710 storage_objects.begin(); it != storage_objects.end(); ++it) {
711 string16 pnp_device_id;
712 string16 storage_object_id;
713 ASSERT_TRUE(GetMTPStorageInfo(it->object_persistent_id, &pnp_device_id,
714 &storage_object_id));
715 EXPECT_EQ(kMTPDeviceWithValidInfo, pnp_device_id);
716 EXPECT_EQ(it->object_persistent_id,
717 GetMTPStorageUniqueId(pnp_device_id, storage_object_id));
718 }
719
720 DoMTPDeviceTest(kMTPDeviceWithValidInfo, false);
721 }
722
645 } // namespace chrome 723 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698