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/chrome_notification_types.h" |
12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" | 12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" |
13 #include "chrome/browser/extensions/extension_system_factory.h" | 13 #include "chrome/browser/extensions/extension_system_factory.h" |
14 #include "chrome/browser/extensions/test_extension_system.h" | 14 #include "chrome/browser/extensions/test_extension_system.h" |
15 #include "chrome/common/extensions/api/bluetooth.h" | 15 #include "chrome/common/extensions/api/bluetooth.h" |
16 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
17 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
18 #include "content/public/test/test_browser_thread.h" | 18 #include "content/public/test/test_browser_thread.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" |
19 #include "device/bluetooth/bluetooth_uuid.h" | 20 #include "device/bluetooth/bluetooth_uuid.h" |
20 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 21 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
21 #include "device/bluetooth/test/mock_bluetooth_device.h" | 22 #include "device/bluetooth/test/mock_bluetooth_device.h" |
22 #include "device/bluetooth/test/mock_bluetooth_profile.h" | 23 #include "device/bluetooth/test/mock_bluetooth_profile.h" |
23 #include "device/bluetooth/test/mock_bluetooth_socket.h" | 24 #include "device/bluetooth/test/mock_bluetooth_socket.h" |
24 #include "extensions/browser/event_router.h" | 25 #include "extensions/browser/event_router.h" |
25 #include "extensions/common/extension_builder.h" | 26 #include "extensions/common/extension_builder.h" |
26 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 const char kTestExtensionId[] = "test extension id"; | 32 const char kTestExtensionId[] = "test extension id"; |
32 const device::BluetoothUUID kAudioProfileUuid("1234"); | 33 const device::BluetoothUUID kAudioProfileUuid("1234"); |
33 const device::BluetoothUUID kHealthProfileUuid("4321"); | 34 const device::BluetoothUUID kHealthProfileUuid("4321"); |
34 | 35 |
35 class FakeEventRouter : public extensions::EventRouter { | |
36 public: | |
37 explicit FakeEventRouter(Profile* profile) : EventRouter(profile, NULL) {} | |
38 | |
39 virtual void DispatchEventToExtension( | |
40 const std::string& extension_id, | |
41 scoped_ptr<extensions::Event> event) OVERRIDE { | |
42 extension_id_ = extension_id; | |
43 event_ = event.Pass(); | |
44 } | |
45 | |
46 std::string extension_id() const { | |
47 return extension_id_; | |
48 } | |
49 | |
50 const extensions::Event* event() const { | |
51 return event_.get(); | |
52 } | |
53 | |
54 private: | |
55 std::string extension_id_; | |
56 scoped_ptr<extensions::Event> event_; | |
57 | |
58 DISALLOW_COPY_AND_ASSIGN(FakeEventRouter); | |
59 }; | |
60 | |
61 class FakeExtensionSystem : public extensions::TestExtensionSystem { | |
62 public: | |
63 explicit FakeExtensionSystem(Profile* profile) | |
64 : extensions::TestExtensionSystem(profile) {} | |
65 | |
66 virtual extensions::EventRouter* event_router() OVERRIDE { | |
67 if (!fake_event_router_) | |
68 fake_event_router_.reset(new FakeEventRouter(profile_)); | |
69 return fake_event_router_.get(); | |
70 } | |
71 | |
72 private: | |
73 scoped_ptr<FakeEventRouter> fake_event_router_; | |
74 | |
75 DISALLOW_COPY_AND_ASSIGN(FakeExtensionSystem); | |
76 }; | |
77 | |
78 KeyedService* BuildFakeExtensionSystem(content::BrowserContext* profile) { | |
79 return new FakeExtensionSystem(static_cast<Profile*>(profile)); | |
80 } | |
81 | |
82 } // namespace | 36 } // namespace |
83 | 37 |
84 namespace extensions { | 38 namespace extensions { |
85 | 39 |
86 namespace bluetooth = api::bluetooth; | 40 namespace bluetooth = api::bluetooth; |
87 | 41 |
88 class BluetoothEventRouterTest : public testing::Test { | 42 class BluetoothEventRouterTest : public testing::Test { |
89 public: | 43 public: |
90 BluetoothEventRouterTest() | 44 BluetoothEventRouterTest() |
91 : mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>()), | 45 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 46 mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>()), |
92 test_profile_(new TestingProfile()), | 47 test_profile_(new TestingProfile()), |
93 router_(test_profile_.get()), | 48 router_(test_profile_.get()) { |
94 ui_thread_(content::BrowserThread::UI, &message_loop_) { | |
95 router_.SetAdapterForTest(mock_adapter_); | 49 router_.SetAdapterForTest(mock_adapter_); |
96 } | 50 } |
97 | 51 |
98 virtual void TearDown() OVERRIDE { | 52 virtual void TearDown() OVERRIDE { |
99 // Some profile-dependent services rely on UI thread to clean up. We make | 53 // Some profile-dependent services rely on UI thread to clean up. We make |
100 // sure they are properly cleaned up by running the UI message loop until | 54 // sure they are properly cleaned up by running the UI message loop until |
101 // idle. | 55 // idle. |
102 test_profile_.reset(NULL); | 56 test_profile_.reset(NULL); |
103 base::RunLoop run_loop; | 57 base::RunLoop run_loop; |
104 run_loop.RunUntilIdle(); | 58 run_loop.RunUntilIdle(); |
105 } | 59 } |
106 | 60 |
107 protected: | 61 protected: |
| 62 base::MessageLoopForUI message_loop_; |
| 63 // Note: |ui_thread_| must be declared before |router_|. |
| 64 content::TestBrowserThread ui_thread_; |
108 testing::StrictMock<device::MockBluetoothAdapter>* mock_adapter_; | 65 testing::StrictMock<device::MockBluetoothAdapter>* mock_adapter_; |
109 testing::NiceMock<device::MockBluetoothProfile> mock_audio_profile_; | 66 testing::NiceMock<device::MockBluetoothProfile> mock_audio_profile_; |
110 testing::NiceMock<device::MockBluetoothProfile> mock_health_profile_; | 67 testing::NiceMock<device::MockBluetoothProfile> mock_health_profile_; |
111 scoped_ptr<TestingProfile> test_profile_; | 68 scoped_ptr<TestingProfile> test_profile_; |
112 BluetoothEventRouter router_; | 69 BluetoothEventRouter router_; |
113 base::MessageLoopForUI message_loop_; | |
114 content::TestBrowserThread ui_thread_; | |
115 }; | 70 }; |
116 | 71 |
117 TEST_F(BluetoothEventRouterTest, BluetoothEventListener) { | 72 TEST_F(BluetoothEventRouterTest, BluetoothEventListener) { |
118 router_.OnListenerAdded(); | 73 router_.OnListenerAdded(); |
119 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | 74 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
120 router_.OnListenerRemoved(); | 75 router_.OnListenerRemoved(); |
121 } | 76 } |
122 | 77 |
123 TEST_F(BluetoothEventRouterTest, MultipleBluetoothEventListeners) { | 78 TEST_F(BluetoothEventRouterTest, MultipleBluetoothEventListeners) { |
124 router_.OnListenerAdded(); | 79 router_.OnListenerAdded(); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 extension, UnloadedExtensionInfo::REASON_DISABLE); | 133 extension, UnloadedExtensionInfo::REASON_DISABLE); |
179 notifier->Notify(chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 134 notifier->Notify(chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, |
180 content::Source<Profile>(test_profile_.get()), | 135 content::Source<Profile>(test_profile_.get()), |
181 content::Details<UnloadedExtensionInfo>(&details)); | 136 content::Details<UnloadedExtensionInfo>(&details)); |
182 | 137 |
183 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid)); | 138 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid)); |
184 EXPECT_FALSE(router_.HasProfile(kHealthProfileUuid)); | 139 EXPECT_FALSE(router_.HasProfile(kHealthProfileUuid)); |
185 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | 140 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
186 } | 141 } |
187 | 142 |
188 TEST_F(BluetoothEventRouterTest, DispatchConnectionEvent) { | |
189 router_.AddProfile( | |
190 kAudioProfileUuid, kTestExtensionId, &mock_audio_profile_); | |
191 | |
192 FakeExtensionSystem* fake_extension_system = | |
193 static_cast<FakeExtensionSystem*>(ExtensionSystemFactory::GetInstance()-> | |
194 SetTestingFactoryAndUse(test_profile_.get(), | |
195 &BuildFakeExtensionSystem)); | |
196 | |
197 testing::NiceMock<device::MockBluetoothDevice> mock_device( | |
198 mock_adapter_, 0, "device name", "device address", true, false); | |
199 scoped_refptr<testing::NiceMock<device::MockBluetoothSocket> > mock_socket( | |
200 new testing::NiceMock<device::MockBluetoothSocket>()); | |
201 | |
202 router_.DispatchConnectionEvent(kTestExtensionId, | |
203 kAudioProfileUuid, | |
204 &mock_device, | |
205 mock_socket); | |
206 | |
207 FakeEventRouter* fake_event_router = | |
208 static_cast<FakeEventRouter*>(fake_extension_system->event_router()); | |
209 | |
210 EXPECT_STREQ(kTestExtensionId, fake_event_router->extension_id().c_str()); | |
211 EXPECT_STREQ(bluetooth::OnConnection::kEventName, | |
212 fake_event_router->event()->event_name.c_str()); | |
213 | |
214 base::ListValue* event_args = fake_event_router->event()->event_args.get(); | |
215 base::DictionaryValue* socket_value = NULL; | |
216 ASSERT_TRUE(event_args->GetDictionary(0, &socket_value)); | |
217 int socket_id; | |
218 ASSERT_TRUE(socket_value->GetInteger("id", &socket_id)); | |
219 EXPECT_EQ(mock_socket.get(), router_.GetSocket(socket_id).get()); | |
220 | |
221 base::DictionaryValue* profile_value = NULL; | |
222 ASSERT_TRUE(socket_value->GetDictionary("profile", &profile_value)); | |
223 std::string uuid; | |
224 ASSERT_TRUE(profile_value->GetString("uuid", &uuid)); | |
225 EXPECT_STREQ(kAudioProfileUuid.canonical_value().c_str(), uuid.c_str()); | |
226 | |
227 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | |
228 router_.ReleaseSocket(socket_id); | |
229 } | |
230 | |
231 TEST_F(BluetoothEventRouterTest, DoNotDispatchConnectionEvent) { | |
232 FakeExtensionSystem* fake_extension_system = | |
233 static_cast<FakeExtensionSystem*>(ExtensionSystemFactory::GetInstance()-> | |
234 SetTestingFactoryAndUse(test_profile_.get(), | |
235 &BuildFakeExtensionSystem)); | |
236 testing::NiceMock<device::MockBluetoothDevice> mock_device( | |
237 mock_adapter_, 0, "device name", "device address", true, false); | |
238 scoped_refptr<testing::NiceMock<device::MockBluetoothSocket> > mock_socket( | |
239 new testing::NiceMock<device::MockBluetoothSocket>()); | |
240 | |
241 // Connection event won't be dispatched for non-registered profiles. | |
242 router_.DispatchConnectionEvent("test extension id", | |
243 kAudioProfileUuid, | |
244 &mock_device, | |
245 mock_socket); | |
246 FakeEventRouter* fake_event_router = | |
247 static_cast<FakeEventRouter*>(fake_extension_system->event_router()); | |
248 EXPECT_TRUE(fake_event_router->event() == NULL); | |
249 | |
250 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | |
251 } | |
252 | |
253 } // namespace extensions | 143 } // namespace extensions |
OLD | NEW |