| 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" | |
| 20 #include "device/bluetooth/bluetooth_uuid.h" | 19 #include "device/bluetooth/bluetooth_uuid.h" |
| 21 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 20 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 22 #include "device/bluetooth/test/mock_bluetooth_device.h" | 21 #include "device/bluetooth/test/mock_bluetooth_device.h" |
| 23 #include "device/bluetooth/test/mock_bluetooth_profile.h" | 22 #include "device/bluetooth/test/mock_bluetooth_profile.h" |
| 24 #include "device/bluetooth/test/mock_bluetooth_socket.h" | 23 #include "device/bluetooth/test/mock_bluetooth_socket.h" |
| 25 #include "extensions/browser/event_router.h" | 24 #include "extensions/browser/event_router.h" |
| 26 #include "extensions/common/extension_builder.h" | 25 #include "extensions/common/extension_builder.h" |
| 27 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 28 |
| 30 namespace { | 29 namespace { |
| 31 | 30 |
| 32 const char kTestExtensionId[] = "test extension id"; | 31 const char kTestExtensionId[] = "test extension id"; |
| 33 const device::BluetoothUUID kAudioProfileUuid("1234"); | 32 const device::BluetoothUUID kAudioProfileUuid("1234"); |
| 34 const device::BluetoothUUID kHealthProfileUuid("4321"); | 33 const device::BluetoothUUID kHealthProfileUuid("4321"); |
| 35 | 34 |
| 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 |
| 36 } // namespace | 82 } // namespace |
| 37 | 83 |
| 38 namespace extensions { | 84 namespace extensions { |
| 39 | 85 |
| 40 namespace bluetooth = api::bluetooth; | 86 namespace bluetooth = api::bluetooth; |
| 41 | 87 |
| 42 class BluetoothEventRouterTest : public testing::Test { | 88 class BluetoothEventRouterTest : public testing::Test { |
| 43 public: | 89 public: |
| 44 BluetoothEventRouterTest() | 90 BluetoothEventRouterTest() |
| 45 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 91 : mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>()), |
| 46 mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>()), | |
| 47 test_profile_(new TestingProfile()), | 92 test_profile_(new TestingProfile()), |
| 48 router_(test_profile_.get()) { | 93 router_(test_profile_.get()), |
| 94 ui_thread_(content::BrowserThread::UI, &message_loop_) { |
| 49 router_.SetAdapterForTest(mock_adapter_); | 95 router_.SetAdapterForTest(mock_adapter_); |
| 50 } | 96 } |
| 51 | 97 |
| 52 virtual void TearDown() OVERRIDE { | 98 virtual void TearDown() OVERRIDE { |
| 53 // Some profile-dependent services rely on UI thread to clean up. We make | 99 // Some profile-dependent services rely on UI thread to clean up. We make |
| 54 // sure they are properly cleaned up by running the UI message loop until | 100 // sure they are properly cleaned up by running the UI message loop until |
| 55 // idle. | 101 // idle. |
| 56 test_profile_.reset(NULL); | 102 test_profile_.reset(NULL); |
| 57 base::RunLoop run_loop; | 103 base::RunLoop run_loop; |
| 58 run_loop.RunUntilIdle(); | 104 run_loop.RunUntilIdle(); |
| 59 } | 105 } |
| 60 | 106 |
| 61 protected: | 107 protected: |
| 62 base::MessageLoopForUI message_loop_; | |
| 63 // Note: |ui_thread_| must be declared before |router_|. | |
| 64 content::TestBrowserThread ui_thread_; | |
| 65 testing::StrictMock<device::MockBluetoothAdapter>* mock_adapter_; | 108 testing::StrictMock<device::MockBluetoothAdapter>* mock_adapter_; |
| 66 testing::NiceMock<device::MockBluetoothProfile> mock_audio_profile_; | 109 testing::NiceMock<device::MockBluetoothProfile> mock_audio_profile_; |
| 67 testing::NiceMock<device::MockBluetoothProfile> mock_health_profile_; | 110 testing::NiceMock<device::MockBluetoothProfile> mock_health_profile_; |
| 68 scoped_ptr<TestingProfile> test_profile_; | 111 scoped_ptr<TestingProfile> test_profile_; |
| 69 BluetoothEventRouter router_; | 112 BluetoothEventRouter router_; |
| 113 base::MessageLoopForUI message_loop_; |
| 114 content::TestBrowserThread ui_thread_; |
| 70 }; | 115 }; |
| 71 | 116 |
| 72 TEST_F(BluetoothEventRouterTest, BluetoothEventListener) { | 117 TEST_F(BluetoothEventRouterTest, BluetoothEventListener) { |
| 73 router_.OnListenerAdded(); | 118 router_.OnListenerAdded(); |
| 74 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | 119 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 75 router_.OnListenerRemoved(); | 120 router_.OnListenerRemoved(); |
| 76 } | 121 } |
| 77 | 122 |
| 78 TEST_F(BluetoothEventRouterTest, MultipleBluetoothEventListeners) { | 123 TEST_F(BluetoothEventRouterTest, MultipleBluetoothEventListeners) { |
| 79 router_.OnListenerAdded(); | 124 router_.OnListenerAdded(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 extension, UnloadedExtensionInfo::REASON_DISABLE); | 178 extension, UnloadedExtensionInfo::REASON_DISABLE); |
| 134 notifier->Notify(chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 179 notifier->Notify(chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, |
| 135 content::Source<Profile>(test_profile_.get()), | 180 content::Source<Profile>(test_profile_.get()), |
| 136 content::Details<UnloadedExtensionInfo>(&details)); | 181 content::Details<UnloadedExtensionInfo>(&details)); |
| 137 | 182 |
| 138 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid)); | 183 EXPECT_FALSE(router_.HasProfile(kAudioProfileUuid)); |
| 139 EXPECT_FALSE(router_.HasProfile(kHealthProfileUuid)); | 184 EXPECT_FALSE(router_.HasProfile(kHealthProfileUuid)); |
| 140 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); | 185 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 141 } | 186 } |
| 142 | 187 |
| 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 |
| 143 } // namespace extensions | 253 } // namespace extensions |
| OLD | NEW |