| 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 21 matching lines...) Expand all Loading... |
| 51 return event_.get(); | 52 return event_.get(); |
| 52 } | 53 } |
| 53 | 54 |
| 54 private: | 55 private: |
| 55 std::string extension_id_; | 56 std::string extension_id_; |
| 56 scoped_ptr<extensions::Event> event_; | 57 scoped_ptr<extensions::Event> event_; |
| 57 | 58 |
| 58 DISALLOW_COPY_AND_ASSIGN(FakeEventRouter); | 59 DISALLOW_COPY_AND_ASSIGN(FakeEventRouter); |
| 59 }; | 60 }; |
| 60 | 61 |
| 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 BrowserContextKeyedService* BuildFakeExtensionSystem( | |
| 79 content::BrowserContext* profile) { | |
| 80 return new FakeExtensionSystem(static_cast<Profile*>(profile)); | |
| 81 } | |
| 82 | |
| 83 } // namespace | 62 } // namespace |
| 84 | 63 |
| 85 namespace extensions { | 64 namespace extensions { |
| 86 | 65 |
| 87 namespace bluetooth = api::bluetooth; | 66 namespace bluetooth = api::bluetooth; |
| 88 | 67 |
| 89 class ExtensionBluetoothEventRouterTest : public testing::Test { | 68 class ExtensionBluetoothEventRouterTest : public testing::Test { |
| 90 public: | 69 public: |
| 91 ExtensionBluetoothEventRouterTest() | 70 ExtensionBluetoothEventRouterTest() |
| 92 : mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>()), | 71 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 72 mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>()), |
| 93 test_profile_(new TestingProfile()), | 73 test_profile_(new TestingProfile()), |
| 94 router_(test_profile_.get()), | 74 router_(test_profile_.get()) { |
| 95 ui_thread_(content::BrowserThread::UI, &message_loop_) { | |
| 96 router_.SetAdapterForTest(mock_adapter_); | 75 router_.SetAdapterForTest(mock_adapter_); |
| 97 } | 76 } |
| 98 | 77 |
| 99 virtual void TearDown() OVERRIDE { | 78 virtual void TearDown() OVERRIDE { |
| 100 // Some profile-dependent services rely on UI thread to clean up. We make | 79 // Some profile-dependent services rely on UI thread to clean up. We make |
| 101 // sure they are properly cleaned up by running the UI message loop until | 80 // sure they are properly cleaned up by running the UI message loop until |
| 102 // idle. | 81 // idle. |
| 103 test_profile_.reset(NULL); | 82 test_profile_.reset(NULL); |
| 104 base::RunLoop run_loop; | 83 base::RunLoop run_loop; |
| 105 run_loop.RunUntilIdle(); | 84 run_loop.RunUntilIdle(); |
| 106 } | 85 } |
| 107 | 86 |
| 108 protected: | 87 protected: |
| 88 base::MessageLoopForUI message_loop_; |
| 89 // Note: |ui_thread_| must be declared before |router_|. |
| 90 content::TestBrowserThread ui_thread_; |
| 109 testing::StrictMock<device::MockBluetoothAdapter>* mock_adapter_; | 91 testing::StrictMock<device::MockBluetoothAdapter>* mock_adapter_; |
| 110 testing::NiceMock<device::MockBluetoothProfile> mock_audio_profile_; | 92 testing::NiceMock<device::MockBluetoothProfile> mock_audio_profile_; |
| 111 testing::NiceMock<device::MockBluetoothProfile> mock_health_profile_; | 93 testing::NiceMock<device::MockBluetoothProfile> mock_health_profile_; |
| 112 scoped_ptr<TestingProfile> test_profile_; | 94 scoped_ptr<TestingProfile> test_profile_; |
| 113 ExtensionBluetoothEventRouter router_; | 95 ExtensionBluetoothEventRouter router_; |
| 114 base::MessageLoopForUI message_loop_; | |
| 115 content::TestBrowserThread ui_thread_; | |
| 116 }; | 96 }; |
| 117 | 97 |
| 118 TEST_F(ExtensionBluetoothEventRouterTest, BluetoothEventListener) { | 98 TEST_F(ExtensionBluetoothEventRouterTest, BluetoothEventListener) { |
| 119 router_.OnListenerAdded(); | 99 router_.OnListenerAdded(); |
| 120 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | 100 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 121 router_.OnListenerRemoved(); | 101 router_.OnListenerRemoved(); |
| 122 } | 102 } |
| 123 | 103 |
| 124 TEST_F(ExtensionBluetoothEventRouterTest, MultipleBluetoothEventListeners) { | 104 TEST_F(ExtensionBluetoothEventRouterTest, MultipleBluetoothEventListeners) { |
| 125 router_.OnListenerAdded(); | 105 router_.OnListenerAdded(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 extension, UnloadedExtensionInfo::REASON_DISABLE); | 159 extension, UnloadedExtensionInfo::REASON_DISABLE); |
| 180 notifier->Notify(chrome::NOTIFICATION_EXTENSION_UNLOADED, | 160 notifier->Notify(chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 181 content::Source<Profile>(test_profile_.get()), | 161 content::Source<Profile>(test_profile_.get()), |
| 182 content::Details<UnloadedExtensionInfo>(&details)); | 162 content::Details<UnloadedExtensionInfo>(&details)); |
| 183 | 163 |
| 184 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid)); | 164 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid)); |
| 185 EXPECT_FALSE(router_.HasProfile(kHealthProfileUuid)); | 165 EXPECT_FALSE(router_.HasProfile(kHealthProfileUuid)); |
| 186 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | 166 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 187 } | 167 } |
| 188 | 168 |
| 189 TEST_F(ExtensionBluetoothEventRouterTest, DispatchConnectionEvent) { | |
| 190 router_.AddProfile( | |
| 191 kAudioProfileUuid, kTestExtensionId, &mock_audio_profile_); | |
| 192 | |
| 193 FakeExtensionSystem* fake_extension_system = | |
| 194 static_cast<FakeExtensionSystem*>(ExtensionSystemFactory::GetInstance()-> | |
| 195 SetTestingFactoryAndUse(test_profile_.get(), | |
| 196 &BuildFakeExtensionSystem)); | |
| 197 | |
| 198 testing::NiceMock<device::MockBluetoothDevice> mock_device( | |
| 199 mock_adapter_, 0, "device name", "device address", true, false); | |
| 200 scoped_refptr<testing::NiceMock<device::MockBluetoothSocket> > mock_socket( | |
| 201 new testing::NiceMock<device::MockBluetoothSocket>()); | |
| 202 | |
| 203 router_.DispatchConnectionEvent(kTestExtensionId, | |
| 204 kAudioProfileUuid, | |
| 205 &mock_device, | |
| 206 mock_socket); | |
| 207 | |
| 208 FakeEventRouter* fake_event_router = | |
| 209 static_cast<FakeEventRouter*>(fake_extension_system->event_router()); | |
| 210 | |
| 211 EXPECT_STREQ(kTestExtensionId, fake_event_router->extension_id().c_str()); | |
| 212 EXPECT_STREQ(bluetooth::OnConnection::kEventName, | |
| 213 fake_event_router->event()->event_name.c_str()); | |
| 214 | |
| 215 base::ListValue* event_args = fake_event_router->event()->event_args.get(); | |
| 216 base::DictionaryValue* socket_value = NULL; | |
| 217 ASSERT_TRUE(event_args->GetDictionary(0, &socket_value)); | |
| 218 int socket_id; | |
| 219 ASSERT_TRUE(socket_value->GetInteger("id", &socket_id)); | |
| 220 EXPECT_EQ(mock_socket.get(), router_.GetSocket(socket_id).get()); | |
| 221 | |
| 222 base::DictionaryValue* profile_value = NULL; | |
| 223 ASSERT_TRUE(socket_value->GetDictionary("profile", &profile_value)); | |
| 224 std::string profile_uuid; | |
| 225 ASSERT_TRUE(profile_value->GetString("uuid", &profile_uuid)); | |
| 226 EXPECT_STREQ(kAudioProfileUuid, profile_uuid.c_str()); | |
| 227 | |
| 228 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | |
| 229 router_.ReleaseSocket(socket_id); | |
| 230 } | |
| 231 | |
| 232 TEST_F(ExtensionBluetoothEventRouterTest, DoNotDispatchConnectionEvent) { | |
| 233 FakeExtensionSystem* fake_extension_system = | |
| 234 static_cast<FakeExtensionSystem*>(ExtensionSystemFactory::GetInstance()-> | |
| 235 SetTestingFactoryAndUse(test_profile_.get(), | |
| 236 &BuildFakeExtensionSystem)); | |
| 237 testing::NiceMock<device::MockBluetoothDevice> mock_device( | |
| 238 mock_adapter_, 0, "device name", "device address", true, false); | |
| 239 scoped_refptr<testing::NiceMock<device::MockBluetoothSocket> > mock_socket( | |
| 240 new testing::NiceMock<device::MockBluetoothSocket>()); | |
| 241 | |
| 242 // Connection event won't be dispatched for non-registered profiles. | |
| 243 router_.DispatchConnectionEvent("test extension id", | |
| 244 kAudioProfileUuid, | |
| 245 &mock_device, | |
| 246 mock_socket); | |
| 247 FakeEventRouter* fake_event_router = | |
| 248 static_cast<FakeEventRouter*>(fake_extension_system->event_router()); | |
| 249 EXPECT_TRUE(fake_event_router->event() == NULL); | |
| 250 | |
| 251 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | |
| 252 } | |
| 253 | |
| 254 } // namespace extensions | 169 } // namespace extensions |
| OLD | NEW |