| OLD | NEW |
| 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 "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
| 6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "base/synchronization/waitable_event.h" | 7 #include "base/synchronization/waitable_event.h" |
| 8 #include "chrome/browser/storage_monitor/mock_removable_storage_observer.h" | 8 #include "chrome/browser/storage_monitor/mock_removable_storage_observer.h" |
| 9 #include "chrome/browser/storage_monitor/storage_monitor.h" | 9 #include "chrome/browser/storage_monitor/storage_monitor.h" |
| 10 #include "chrome/browser/storage_monitor/test_storage_monitor.h" | 10 #include "chrome/browser/storage_monitor/test_storage_monitor.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 void SetLatch(bool* called) { | 15 void SetLatch(bool* called) { |
| 16 *called = true; | 16 *called = true; |
| 17 } | 17 } |
| 18 | 18 |
| 19 } // namespace | 19 } // namespace |
| 20 | 20 |
| 21 namespace chrome { | |
| 22 | |
| 23 TEST(StorageMonitorTest, TestInitialize) { | 21 TEST(StorageMonitorTest, TestInitialize) { |
| 24 test::TestStorageMonitor::RemoveSingleton(); | 22 TestStorageMonitor::RemoveSingleton(); |
| 25 test::TestStorageMonitor monitor; | 23 TestStorageMonitor monitor; |
| 26 EXPECT_FALSE(monitor.init_called()); | 24 EXPECT_FALSE(monitor.init_called()); |
| 27 | 25 |
| 28 bool initialized = false; | 26 bool initialized = false; |
| 29 monitor.EnsureInitialized(base::Bind(&SetLatch, &initialized)); | 27 monitor.EnsureInitialized(base::Bind(&SetLatch, &initialized)); |
| 30 EXPECT_TRUE(monitor.init_called()); | 28 EXPECT_TRUE(monitor.init_called()); |
| 31 EXPECT_FALSE(initialized); | 29 EXPECT_FALSE(initialized); |
| 32 monitor.MarkInitialized(); | 30 monitor.MarkInitialized(); |
| 33 EXPECT_TRUE(initialized); | 31 EXPECT_TRUE(initialized); |
| 34 } | 32 } |
| 35 | 33 |
| 36 TEST(StorageMonitorTest, DeviceAttachDetachNotifications) { | 34 TEST(StorageMonitorTest, DeviceAttachDetachNotifications) { |
| 37 test::TestStorageMonitor::RemoveSingleton(); | 35 TestStorageMonitor::RemoveSingleton(); |
| 38 base::MessageLoop message_loop; | 36 base::MessageLoop message_loop; |
| 39 const string16 kDeviceName = ASCIIToUTF16("media device"); | 37 const string16 kDeviceName = ASCIIToUTF16("media device"); |
| 40 const std::string kDeviceId1 = "dcim:UUID:FFF0-0001"; | 38 const std::string kDeviceId1 = "dcim:UUID:FFF0-0001"; |
| 41 const std::string kDeviceId2 = "dcim:UUID:FFF0-0002"; | 39 const std::string kDeviceId2 = "dcim:UUID:FFF0-0002"; |
| 42 MockRemovableStorageObserver observer1; | 40 MockRemovableStorageObserver observer1; |
| 43 MockRemovableStorageObserver observer2; | 41 MockRemovableStorageObserver observer2; |
| 44 test::TestStorageMonitor monitor; | 42 TestStorageMonitor monitor; |
| 45 monitor.AddObserver(&observer1); | 43 monitor.AddObserver(&observer1); |
| 46 monitor.AddObserver(&observer2); | 44 monitor.AddObserver(&observer2); |
| 47 | 45 |
| 48 StorageInfo info(kDeviceId1, kDeviceName, FILE_PATH_LITERAL("path"), | 46 StorageInfo info(kDeviceId1, kDeviceName, FILE_PATH_LITERAL("path"), |
| 49 string16(), string16(), string16(), 0); | 47 string16(), string16(), string16(), 0); |
| 50 monitor.receiver()->ProcessAttach(info); | 48 monitor.receiver()->ProcessAttach(info); |
| 51 message_loop.RunUntilIdle(); | 49 message_loop.RunUntilIdle(); |
| 52 | 50 |
| 53 EXPECT_EQ(kDeviceId1, observer1.last_attached().device_id()); | 51 EXPECT_EQ(kDeviceId1, observer1.last_attached().device_id()); |
| 54 EXPECT_EQ(kDeviceName, observer1.last_attached().name()); | 52 EXPECT_EQ(kDeviceName, observer1.last_attached().name()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 75 | 73 |
| 76 // The kDeviceId2 won't be notified since it was never attached. | 74 // The kDeviceId2 won't be notified since it was never attached. |
| 77 EXPECT_EQ(1, observer1.detach_calls()); | 75 EXPECT_EQ(1, observer1.detach_calls()); |
| 78 EXPECT_EQ(1, observer2.detach_calls()); | 76 EXPECT_EQ(1, observer2.detach_calls()); |
| 79 | 77 |
| 80 monitor.RemoveObserver(&observer1); | 78 monitor.RemoveObserver(&observer1); |
| 81 monitor.RemoveObserver(&observer2); | 79 monitor.RemoveObserver(&observer2); |
| 82 } | 80 } |
| 83 | 81 |
| 84 TEST(StorageMonitorTest, GetAllAvailableStoragesEmpty) { | 82 TEST(StorageMonitorTest, GetAllAvailableStoragesEmpty) { |
| 85 test::TestStorageMonitor::RemoveSingleton(); | 83 TestStorageMonitor::RemoveSingleton(); |
| 86 base::MessageLoop message_loop; | 84 base::MessageLoop message_loop; |
| 87 test::TestStorageMonitor monitor; | 85 TestStorageMonitor monitor; |
| 88 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages(); | 86 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages(); |
| 89 EXPECT_EQ(0U, devices.size()); | 87 EXPECT_EQ(0U, devices.size()); |
| 90 } | 88 } |
| 91 | 89 |
| 92 TEST(StorageMonitorTest, GetAllAvailableStorageAttachDetach) { | 90 TEST(StorageMonitorTest, GetAllAvailableStorageAttachDetach) { |
| 93 test::TestStorageMonitor::RemoveSingleton(); | 91 TestStorageMonitor::RemoveSingleton(); |
| 94 base::MessageLoop message_loop; | 92 base::MessageLoop message_loop; |
| 95 test::TestStorageMonitor monitor; | 93 TestStorageMonitor monitor; |
| 96 const std::string kDeviceId1 = "dcim:UUID:FFF0-0042"; | 94 const std::string kDeviceId1 = "dcim:UUID:FFF0-0042"; |
| 97 const string16 kDeviceName1 = ASCIIToUTF16("test"); | 95 const string16 kDeviceName1 = ASCIIToUTF16("test"); |
| 98 const base::FilePath kDevicePath1(FILE_PATH_LITERAL("/testfoo")); | 96 const base::FilePath kDevicePath1(FILE_PATH_LITERAL("/testfoo")); |
| 99 StorageInfo info1(kDeviceId1, kDeviceName1, kDevicePath1.value(), | 97 StorageInfo info1(kDeviceId1, kDeviceName1, kDevicePath1.value(), |
| 100 string16(), string16(), string16(), 0); | 98 string16(), string16(), string16(), 0); |
| 101 monitor.receiver()->ProcessAttach(info1); | 99 monitor.receiver()->ProcessAttach(info1); |
| 102 message_loop.RunUntilIdle(); | 100 message_loop.RunUntilIdle(); |
| 103 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages(); | 101 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages(); |
| 104 ASSERT_EQ(1U, devices.size()); | 102 ASSERT_EQ(1U, devices.size()); |
| 105 EXPECT_EQ(kDeviceId1, devices[0].device_id()); | 103 EXPECT_EQ(kDeviceId1, devices[0].device_id()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 128 ASSERT_EQ(1U, devices.size()); | 126 ASSERT_EQ(1U, devices.size()); |
| 129 EXPECT_EQ(kDeviceId2, devices[0].device_id()); | 127 EXPECT_EQ(kDeviceId2, devices[0].device_id()); |
| 130 EXPECT_EQ(kDeviceName2, devices[0].name()); | 128 EXPECT_EQ(kDeviceName2, devices[0].name()); |
| 131 EXPECT_EQ(kDevicePath2.value(), devices[0].location()); | 129 EXPECT_EQ(kDevicePath2.value(), devices[0].location()); |
| 132 | 130 |
| 133 monitor.receiver()->ProcessDetach(kDeviceId2); | 131 monitor.receiver()->ProcessDetach(kDeviceId2); |
| 134 message_loop.RunUntilIdle(); | 132 message_loop.RunUntilIdle(); |
| 135 devices = monitor.GetAllAvailableStorages(); | 133 devices = monitor.GetAllAvailableStorages(); |
| 136 EXPECT_EQ(0U, devices.size()); | 134 EXPECT_EQ(0U, devices.size()); |
| 137 } | 135 } |
| 138 | |
| 139 } // namespace chrome | |
| OLD | NEW |