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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "chrome/browser/chrome_notification_types.h" |
11 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" | 12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" |
12 #include "chrome/browser/extensions/event_names.h" | 13 #include "chrome/browser/extensions/event_names.h" |
13 #include "chrome/browser/extensions/extension_system_factory.h" | 14 #include "chrome/browser/extensions/extension_system_factory.h" |
14 #include "chrome/browser/extensions/test_extension_system.h" | 15 #include "chrome/browser/extensions/test_extension_system.h" |
15 #include "chrome/common/extensions/api/bluetooth.h" | 16 #include "chrome/common/extensions/api/bluetooth.h" |
16 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 18 #include "content/public/browser/notification_service.h" |
17 #include "content/public/test/test_browser_thread.h" | 19 #include "content/public/test/test_browser_thread.h" |
18 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 20 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
19 #include "device/bluetooth/test/mock_bluetooth_device.h" | 21 #include "device/bluetooth/test/mock_bluetooth_device.h" |
20 #include "device/bluetooth/test/mock_bluetooth_profile.h" | 22 #include "device/bluetooth/test/mock_bluetooth_profile.h" |
21 #include "device/bluetooth/test/mock_bluetooth_socket.h" | 23 #include "device/bluetooth/test/mock_bluetooth_socket.h" |
22 #include "extensions/browser/event_router.h" | 24 #include "extensions/browser/event_router.h" |
| 25 #include "extensions/common/extension_builder.h" |
23 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
25 | 28 |
26 namespace { | 29 namespace { |
27 | 30 |
| 31 const char kTestExtensionId[] = "test extension id"; |
28 const char kAudioProfileUuid[] = "audio profile uuid"; | 32 const char kAudioProfileUuid[] = "audio profile uuid"; |
29 const char kHealthProfileUuid[] = "health profile uuid"; | 33 const char kHealthProfileUuid[] = "health profile uuid"; |
30 | 34 |
31 class FakeEventRouter : public extensions::EventRouter { | 35 class FakeEventRouter : public extensions::EventRouter { |
32 public: | 36 public: |
33 explicit FakeEventRouter(Profile* profile) : EventRouter(profile, NULL) {} | 37 explicit FakeEventRouter(Profile* profile) : EventRouter(profile, NULL) {} |
34 | 38 |
35 virtual void DispatchEventToExtension( | 39 virtual void DispatchEventToExtension( |
36 const std::string& extension_id, | 40 const std::string& extension_id, |
37 scoped_ptr<extensions::Event> event) OVERRIDE { | 41 scoped_ptr<extensions::Event> event) OVERRIDE { |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 router_.OnListenerRemoved(); | 128 router_.OnListenerRemoved(); |
125 router_.OnListenerRemoved(); | 129 router_.OnListenerRemoved(); |
126 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | 130 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
127 router_.OnListenerRemoved(); | 131 router_.OnListenerRemoved(); |
128 } | 132 } |
129 | 133 |
130 TEST_F(ExtensionBluetoothEventRouterTest, Profiles) { | 134 TEST_F(ExtensionBluetoothEventRouterTest, Profiles) { |
131 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid)); | 135 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid)); |
132 EXPECT_FALSE(router_.HasProfile(kHealthProfileUuid)); | 136 EXPECT_FALSE(router_.HasProfile(kHealthProfileUuid)); |
133 | 137 |
134 router_.AddProfile(kAudioProfileUuid, &mock_audio_profile_); | 138 router_.AddProfile( |
135 router_.AddProfile(kHealthProfileUuid, &mock_health_profile_); | 139 kAudioProfileUuid, kTestExtensionId, &mock_audio_profile_); |
| 140 router_.AddProfile( |
| 141 kHealthProfileUuid, kTestExtensionId, &mock_health_profile_); |
136 EXPECT_TRUE(router_.HasProfile(kAudioProfileUuid)); | 142 EXPECT_TRUE(router_.HasProfile(kAudioProfileUuid)); |
137 EXPECT_TRUE(router_.HasProfile(kHealthProfileUuid)); | 143 EXPECT_TRUE(router_.HasProfile(kHealthProfileUuid)); |
138 | 144 |
139 EXPECT_CALL(mock_audio_profile_, Unregister()).Times(1); | 145 EXPECT_CALL(mock_audio_profile_, Unregister()).Times(1); |
140 router_.RemoveProfile(kAudioProfileUuid); | 146 router_.RemoveProfile(kAudioProfileUuid); |
141 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid)); | 147 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid)); |
142 EXPECT_TRUE(router_.HasProfile(kHealthProfileUuid)); | 148 EXPECT_TRUE(router_.HasProfile(kHealthProfileUuid)); |
143 | 149 |
144 // Make sure remaining profiles are unregistered in destructor. | 150 // Make sure remaining profiles are unregistered in destructor. |
145 EXPECT_CALL(mock_health_profile_, Unregister()).Times(1); | 151 EXPECT_CALL(mock_health_profile_, Unregister()).Times(1); |
146 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | 152 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
147 } | 153 } |
148 | 154 |
| 155 TEST_F(ExtensionBluetoothEventRouterTest, UnloadExtension) { |
| 156 scoped_refptr<const extensions::Extension> extension = |
| 157 extensions::ExtensionBuilder() |
| 158 .SetManifest(extensions::DictionaryBuilder() |
| 159 .Set("name", "BT event router test") |
| 160 .Set("version", "1.0") |
| 161 .Set("manifest_version", 2)) |
| 162 .SetID(kTestExtensionId) |
| 163 .Build(); |
| 164 |
| 165 router_.AddProfile( |
| 166 kAudioProfileUuid, kTestExtensionId, &mock_audio_profile_); |
| 167 router_.AddProfile( |
| 168 kHealthProfileUuid, kTestExtensionId, &mock_health_profile_); |
| 169 EXPECT_TRUE(router_.HasProfile(kAudioProfileUuid)); |
| 170 EXPECT_TRUE(router_.HasProfile(kHealthProfileUuid)); |
| 171 |
| 172 // Unloading the extension should unregister all profiles added by it. |
| 173 EXPECT_CALL(mock_audio_profile_, Unregister()).Times(1); |
| 174 EXPECT_CALL(mock_health_profile_, Unregister()).Times(1); |
| 175 |
| 176 content::NotificationService* notifier = |
| 177 content::NotificationService::current(); |
| 178 UnloadedExtensionInfo details( |
| 179 extension, UnloadedExtensionInfo::REASON_DISABLE); |
| 180 notifier->Notify(chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 181 content::Source<Profile>(test_profile_.get()), |
| 182 content::Details<UnloadedExtensionInfo>(&details)); |
| 183 |
| 184 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid)); |
| 185 EXPECT_FALSE(router_.HasProfile(kHealthProfileUuid)); |
| 186 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 187 } |
| 188 |
149 TEST_F(ExtensionBluetoothEventRouterTest, DispatchConnectionEvent) { | 189 TEST_F(ExtensionBluetoothEventRouterTest, DispatchConnectionEvent) { |
150 router_.AddProfile(kAudioProfileUuid, &mock_audio_profile_); | 190 router_.AddProfile( |
| 191 kAudioProfileUuid, kTestExtensionId, &mock_audio_profile_); |
151 | 192 |
152 FakeExtensionSystem* fake_extension_system = | 193 FakeExtensionSystem* fake_extension_system = |
153 static_cast<FakeExtensionSystem*>(ExtensionSystemFactory::GetInstance()-> | 194 static_cast<FakeExtensionSystem*>(ExtensionSystemFactory::GetInstance()-> |
154 SetTestingFactoryAndUse(test_profile_.get(), | 195 SetTestingFactoryAndUse(test_profile_.get(), |
155 &BuildFakeExtensionSystem)); | 196 &BuildFakeExtensionSystem)); |
156 | 197 |
157 const char test_extension_id[] = "test extension id"; | |
158 testing::NiceMock<device::MockBluetoothDevice> mock_device( | 198 testing::NiceMock<device::MockBluetoothDevice> mock_device( |
159 mock_adapter_, 0, "device name", "device address", true, false); | 199 mock_adapter_, 0, "device name", "device address", true, false); |
160 scoped_refptr<testing::NiceMock<device::MockBluetoothSocket> > mock_socket( | 200 scoped_refptr<testing::NiceMock<device::MockBluetoothSocket> > mock_socket( |
161 new testing::NiceMock<device::MockBluetoothSocket>()); | 201 new testing::NiceMock<device::MockBluetoothSocket>()); |
162 | 202 |
163 router_.DispatchConnectionEvent(test_extension_id, | 203 router_.DispatchConnectionEvent(kTestExtensionId, |
164 kAudioProfileUuid, | 204 kAudioProfileUuid, |
165 &mock_device, | 205 &mock_device, |
166 mock_socket); | 206 mock_socket); |
167 | 207 |
168 FakeEventRouter* fake_event_router = | 208 FakeEventRouter* fake_event_router = |
169 static_cast<FakeEventRouter*>(fake_extension_system->event_router()); | 209 static_cast<FakeEventRouter*>(fake_extension_system->event_router()); |
170 | 210 |
171 EXPECT_STREQ(test_extension_id, fake_event_router->extension_id().c_str()); | 211 EXPECT_STREQ(kTestExtensionId, fake_event_router->extension_id().c_str()); |
172 EXPECT_STREQ(bluetooth::OnConnection::kEventName, | 212 EXPECT_STREQ(bluetooth::OnConnection::kEventName, |
173 fake_event_router->event()->event_name.c_str()); | 213 fake_event_router->event()->event_name.c_str()); |
174 | 214 |
175 base::ListValue* event_args = fake_event_router->event()->event_args.get(); | 215 base::ListValue* event_args = fake_event_router->event()->event_args.get(); |
176 base::DictionaryValue* socket_value = NULL; | 216 base::DictionaryValue* socket_value = NULL; |
177 ASSERT_TRUE(event_args->GetDictionary(0, &socket_value)); | 217 ASSERT_TRUE(event_args->GetDictionary(0, &socket_value)); |
178 int socket_id; | 218 int socket_id; |
179 ASSERT_TRUE(socket_value->GetInteger("id", &socket_id)); | 219 ASSERT_TRUE(socket_value->GetInteger("id", &socket_id)); |
180 EXPECT_EQ(mock_socket.get(), router_.GetSocket(socket_id).get()); | 220 EXPECT_EQ(mock_socket.get(), router_.GetSocket(socket_id).get()); |
181 | 221 |
(...skipping 23 matching lines...) Expand all Loading... |
205 &mock_device, | 245 &mock_device, |
206 mock_socket); | 246 mock_socket); |
207 FakeEventRouter* fake_event_router = | 247 FakeEventRouter* fake_event_router = |
208 static_cast<FakeEventRouter*>(fake_extension_system->event_router()); | 248 static_cast<FakeEventRouter*>(fake_extension_system->event_router()); |
209 EXPECT_TRUE(fake_event_router->event() == NULL); | 249 EXPECT_TRUE(fake_event_router->event() == NULL); |
210 | 250 |
211 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | 251 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
212 } | 252 } |
213 | 253 |
214 } // namespace extensions | 254 } // namespace extensions |
OLD | NEW |