| 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/event_names.h" | 13 #include "chrome/browser/extensions/event_names.h" |
| 14 #include "chrome/browser/extensions/extension_system_factory.h" | 14 #include "chrome/browser/extensions/extension_system_factory.h" |
| 15 #include "chrome/browser/extensions/test_extension_system.h" | 15 #include "chrome/browser/extensions/test_extension_system.h" |
| 16 #include "chrome/common/extensions/api/bluetooth.h" | 16 #include "chrome/common/extensions/api/bluetooth.h" |
| 17 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 18 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/test/test_browser_thread.h" | 19 #include "content/public/test/test_browser_thread.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.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 { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 | 82 |
| 82 } // namespace | 83 } // namespace |
| 83 | 84 |
| 84 namespace extensions { | 85 namespace extensions { |
| 85 | 86 |
| 86 namespace bluetooth = api::bluetooth; | 87 namespace bluetooth = api::bluetooth; |
| 87 | 88 |
| 88 class ExtensionBluetoothEventRouterTest : public testing::Test { | 89 class ExtensionBluetoothEventRouterTest : public testing::Test { |
| 89 public: | 90 public: |
| 90 ExtensionBluetoothEventRouterTest() | 91 ExtensionBluetoothEventRouterTest() |
| 91 : mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>()), | 92 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 93 mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>()), |
| 92 test_profile_(new TestingProfile()), | 94 test_profile_(new TestingProfile()), |
| 93 router_(test_profile_.get()), | 95 router_(test_profile_.get()) { |
| 94 ui_thread_(content::BrowserThread::UI, &message_loop_) { | |
| 95 router_.SetAdapterForTest(mock_adapter_); | 96 router_.SetAdapterForTest(mock_adapter_); |
| 96 } | 97 } |
| 97 | 98 |
| 98 virtual void TearDown() OVERRIDE { | 99 virtual void TearDown() OVERRIDE { |
| 99 // Some profile-dependent services rely on UI thread to clean up. We make | 100 // 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 | 101 // sure they are properly cleaned up by running the UI message loop until |
| 101 // idle. | 102 // idle. |
| 102 test_profile_.reset(NULL); | 103 test_profile_.reset(NULL); |
| 103 base::RunLoop run_loop; | 104 base::RunLoop run_loop; |
| 104 run_loop.RunUntilIdle(); | 105 run_loop.RunUntilIdle(); |
| 105 } | 106 } |
| 106 | 107 |
| 107 protected: | 108 protected: |
| 109 base::MessageLoopForUI message_loop_; |
| 110 // Note: |ui_thread_| must be declared before |router_|. |
| 111 content::TestBrowserThread ui_thread_; |
| 108 testing::StrictMock<device::MockBluetoothAdapter>* mock_adapter_; | 112 testing::StrictMock<device::MockBluetoothAdapter>* mock_adapter_; |
| 109 testing::NiceMock<device::MockBluetoothProfile> mock_audio_profile_; | 113 testing::NiceMock<device::MockBluetoothProfile> mock_audio_profile_; |
| 110 testing::NiceMock<device::MockBluetoothProfile> mock_health_profile_; | 114 testing::NiceMock<device::MockBluetoothProfile> mock_health_profile_; |
| 111 scoped_ptr<TestingProfile> test_profile_; | 115 scoped_ptr<TestingProfile> test_profile_; |
| 112 ExtensionBluetoothEventRouter router_; | 116 ExtensionBluetoothEventRouter router_; |
| 113 base::MessageLoopForUI message_loop_; | |
| 114 content::TestBrowserThread ui_thread_; | |
| 115 }; | 117 }; |
| 116 | 118 |
| 117 TEST_F(ExtensionBluetoothEventRouterTest, BluetoothEventListener) { | 119 TEST_F(ExtensionBluetoothEventRouterTest, BluetoothEventListener) { |
| 118 router_.OnListenerAdded(); | 120 router_.OnListenerAdded(); |
| 119 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | 121 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 120 router_.OnListenerRemoved(); | 122 router_.OnListenerRemoved(); |
| 121 } | 123 } |
| 122 | 124 |
| 123 TEST_F(ExtensionBluetoothEventRouterTest, MultipleBluetoothEventListeners) { | 125 TEST_F(ExtensionBluetoothEventRouterTest, MultipleBluetoothEventListeners) { |
| 124 router_.OnListenerAdded(); | 126 router_.OnListenerAdded(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 extension, UnloadedExtensionInfo::REASON_DISABLE); | 180 extension, UnloadedExtensionInfo::REASON_DISABLE); |
| 179 notifier->Notify(chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 181 notifier->Notify(chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, |
| 180 content::Source<Profile>(test_profile_.get()), | 182 content::Source<Profile>(test_profile_.get()), |
| 181 content::Details<UnloadedExtensionInfo>(&details)); | 183 content::Details<UnloadedExtensionInfo>(&details)); |
| 182 | 184 |
| 183 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid)); | 185 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid)); |
| 184 EXPECT_FALSE(router_.HasProfile(kHealthProfileUuid)); | 186 EXPECT_FALSE(router_.HasProfile(kHealthProfileUuid)); |
| 185 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | 187 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 186 } | 188 } |
| 187 | 189 |
| 188 TEST_F(ExtensionBluetoothEventRouterTest, 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, uuid.c_str()); | |
| 226 | |
| 227 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | |
| 228 router_.ReleaseSocket(socket_id); | |
| 229 } | |
| 230 | |
| 231 TEST_F(ExtensionBluetoothEventRouterTest, 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 | 190 } // namespace extensions |
| OLD | NEW |