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.h" | 5 #include "base/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 { |
| 14 |
| 15 void SetLatch(bool* called) { |
| 16 *called = true; |
| 17 } |
| 18 |
| 19 } // namespace |
| 20 |
13 namespace chrome { | 21 namespace chrome { |
14 | 22 |
15 TEST(StorageMonitorTest, TestInitialize) { | 23 TEST(StorageMonitorTest, TestInitialize) { |
| 24 test::TestStorageMonitor::RemoveSingleton(); |
16 test::TestStorageMonitor monitor; | 25 test::TestStorageMonitor monitor; |
17 EXPECT_FALSE(monitor.init_called_); | 26 EXPECT_FALSE(monitor.init_called_); |
18 | 27 |
19 base::WaitableEvent event(false, false); | 28 bool initialized = false; |
20 monitor.EnsureInitialized(base::Bind(&base::WaitableEvent::Signal, | 29 monitor.EnsureInitialized(base::Bind(&SetLatch, &initialized)); |
21 base::Unretained(&event))); | |
22 EXPECT_TRUE(monitor.init_called_); | 30 EXPECT_TRUE(monitor.init_called_); |
23 EXPECT_FALSE(event.IsSignaled()); | 31 EXPECT_FALSE(initialized); |
24 monitor.MarkInitialized(); | 32 monitor.MarkInitialized(); |
25 EXPECT_TRUE(event.IsSignaled()); | 33 EXPECT_TRUE(initialized); |
26 } | 34 } |
27 | 35 |
28 TEST(StorageMonitorTest, DeviceAttachDetachNotifications) { | 36 TEST(StorageMonitorTest, DeviceAttachDetachNotifications) { |
| 37 test::TestStorageMonitor::RemoveSingleton(); |
29 base::MessageLoop message_loop; | 38 base::MessageLoop message_loop; |
30 const string16 kDeviceName = ASCIIToUTF16("media device"); | 39 const string16 kDeviceName = ASCIIToUTF16("media device"); |
31 const std::string kDeviceId1 = "dcim:UUID:FFF0-0001"; | 40 const std::string kDeviceId1 = "dcim:UUID:FFF0-0001"; |
32 const std::string kDeviceId2 = "dcim:UUID:FFF0-0002"; | 41 const std::string kDeviceId2 = "dcim:UUID:FFF0-0002"; |
33 MockRemovableStorageObserver observer1; | 42 MockRemovableStorageObserver observer1; |
34 MockRemovableStorageObserver observer2; | 43 MockRemovableStorageObserver observer2; |
35 test::TestStorageMonitor monitor; | 44 test::TestStorageMonitor monitor; |
36 monitor.AddObserver(&observer1); | 45 monitor.AddObserver(&observer1); |
37 monitor.AddObserver(&observer2); | 46 monitor.AddObserver(&observer2); |
38 | 47 |
(...skipping 27 matching lines...) Expand all Loading... |
66 | 75 |
67 // The kDeviceId2 won't be notified since it was never attached. | 76 // The kDeviceId2 won't be notified since it was never attached. |
68 EXPECT_EQ(1, observer1.detach_calls()); | 77 EXPECT_EQ(1, observer1.detach_calls()); |
69 EXPECT_EQ(1, observer2.detach_calls()); | 78 EXPECT_EQ(1, observer2.detach_calls()); |
70 | 79 |
71 monitor.RemoveObserver(&observer1); | 80 monitor.RemoveObserver(&observer1); |
72 monitor.RemoveObserver(&observer2); | 81 monitor.RemoveObserver(&observer2); |
73 } | 82 } |
74 | 83 |
75 TEST(StorageMonitorTest, GetAllAvailableStoragesEmpty) { | 84 TEST(StorageMonitorTest, GetAllAvailableStoragesEmpty) { |
| 85 test::TestStorageMonitor::RemoveSingleton(); |
76 base::MessageLoop message_loop; | 86 base::MessageLoop message_loop; |
77 test::TestStorageMonitor monitor; | 87 test::TestStorageMonitor monitor; |
78 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages(); | 88 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages(); |
79 EXPECT_EQ(0U, devices.size()); | 89 EXPECT_EQ(0U, devices.size()); |
80 } | 90 } |
81 | 91 |
82 TEST(StorageMonitorTest, GetAllAvailableStorageAttachDetach) { | 92 TEST(StorageMonitorTest, GetAllAvailableStorageAttachDetach) { |
| 93 test::TestStorageMonitor::RemoveSingleton(); |
83 base::MessageLoop message_loop; | 94 base::MessageLoop message_loop; |
84 test::TestStorageMonitor monitor; | 95 test::TestStorageMonitor monitor; |
85 const std::string kDeviceId1 = "dcim:UUID:FFF0-0042"; | 96 const std::string kDeviceId1 = "dcim:UUID:FFF0-0042"; |
86 const string16 kDeviceName1 = ASCIIToUTF16("test"); | 97 const string16 kDeviceName1 = ASCIIToUTF16("test"); |
87 const base::FilePath kDevicePath1(FILE_PATH_LITERAL("/testfoo")); | 98 const base::FilePath kDevicePath1(FILE_PATH_LITERAL("/testfoo")); |
88 StorageInfo info1(kDeviceId1, kDeviceName1, kDevicePath1.value(), | 99 StorageInfo info1(kDeviceId1, kDeviceName1, kDevicePath1.value(), |
89 string16(), string16(), string16(), 0); | 100 string16(), string16(), string16(), 0); |
90 monitor.receiver()->ProcessAttach(info1); | 101 monitor.receiver()->ProcessAttach(info1); |
91 message_loop.RunUntilIdle(); | 102 message_loop.RunUntilIdle(); |
92 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages(); | 103 std::vector<StorageInfo> devices = monitor.GetAllAvailableStorages(); |
(...skipping 26 matching lines...) Expand all Loading... |
119 EXPECT_EQ(kDeviceName2, devices[0].name()); | 130 EXPECT_EQ(kDeviceName2, devices[0].name()); |
120 EXPECT_EQ(kDevicePath2.value(), devices[0].location()); | 131 EXPECT_EQ(kDevicePath2.value(), devices[0].location()); |
121 | 132 |
122 monitor.receiver()->ProcessDetach(kDeviceId2); | 133 monitor.receiver()->ProcessDetach(kDeviceId2); |
123 message_loop.RunUntilIdle(); | 134 message_loop.RunUntilIdle(); |
124 devices = monitor.GetAllAvailableStorages(); | 135 devices = monitor.GetAllAvailableStorages(); |
125 EXPECT_EQ(0U, devices.size()); | 136 EXPECT_EQ(0U, devices.size()); |
126 } | 137 } |
127 | 138 |
128 } // namespace chrome | 139 } // namespace chrome |
OLD | NEW |