OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "chrome/browser/chrome_notification_types.h" |
| 12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" |
| 13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" |
| 14 #include "chrome/browser/extensions/event_names.h" |
| 15 #include "chrome/browser/extensions/extension_system_factory.h" |
| 16 #include "chrome/browser/extensions/test_extension_system.h" |
| 17 #include "chrome/common/extensions/api/bluetooth.h" |
| 18 #include "chrome/test/base/testing_browser_process.h" |
| 19 #include "chrome/test/base/testing_profile.h" |
| 20 #include "chrome/test/base/testing_profile_manager.h" |
| 21 #include "content/public/browser/notification_service.h" |
| 22 #include "content/public/test/test_browser_thread_bundle.h" |
| 23 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 24 #include "device/bluetooth/test/mock_bluetooth_device.h" |
| 25 #include "device/bluetooth/test/mock_bluetooth_profile.h" |
| 26 #include "device/bluetooth/test/mock_bluetooth_socket.h" |
| 27 #include "extensions/browser/event_router.h" |
| 28 #include "extensions/common/extension_builder.h" |
| 29 #include "testing/gmock/include/gmock/gmock.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" |
| 31 |
| 32 namespace { |
| 33 |
| 34 const char kTestExtensionId[] = "test extension id"; |
| 35 const char kAudioProfileUuid[] = "audio profile uuid"; |
| 36 |
| 37 static KeyedService* ApiResourceManagerTestFactory( |
| 38 content::BrowserContext* context) { |
| 39 content::BrowserThread::ID id; |
| 40 CHECK(content::BrowserThread::GetCurrentThreadIdentifier(&id)); |
| 41 return extensions::ApiResourceManager< |
| 42 extensions::BluetoothApiSocket>::CreateApiResourceManagerForTest(context, |
| 43 id); |
| 44 } |
| 45 |
| 46 static KeyedService* BluetoothAPITestFactory(content::BrowserContext* context) { |
| 47 content::BrowserThread::ID id; |
| 48 CHECK(content::BrowserThread::GetCurrentThreadIdentifier(&id)); |
| 49 return new extensions::BluetoothAPI(context); |
| 50 } |
| 51 |
| 52 class FakeEventRouter : public extensions::EventRouter { |
| 53 public: |
| 54 explicit FakeEventRouter(Profile* profile) : EventRouter(profile, NULL) {} |
| 55 |
| 56 virtual void DispatchEventToExtension(const std::string& extension_id, |
| 57 scoped_ptr<extensions::Event> event) |
| 58 OVERRIDE { |
| 59 extension_id_ = extension_id; |
| 60 event_ = event.Pass(); |
| 61 } |
| 62 |
| 63 std::string extension_id() const { return extension_id_; } |
| 64 |
| 65 const extensions::Event* event() const { return event_.get(); } |
| 66 |
| 67 private: |
| 68 std::string extension_id_; |
| 69 scoped_ptr<extensions::Event> event_; |
| 70 |
| 71 DISALLOW_COPY_AND_ASSIGN(FakeEventRouter); |
| 72 }; |
| 73 |
| 74 class FakeExtensionSystem : public extensions::TestExtensionSystem { |
| 75 public: |
| 76 explicit FakeExtensionSystem(Profile* profile) |
| 77 : extensions::TestExtensionSystem(profile) {} |
| 78 |
| 79 virtual extensions::EventRouter* event_router() OVERRIDE { |
| 80 if (!fake_event_router_) |
| 81 fake_event_router_.reset(new FakeEventRouter(profile_)); |
| 82 return fake_event_router_.get(); |
| 83 } |
| 84 |
| 85 private: |
| 86 scoped_ptr<FakeEventRouter> fake_event_router_; |
| 87 |
| 88 DISALLOW_COPY_AND_ASSIGN(FakeExtensionSystem); |
| 89 }; |
| 90 |
| 91 KeyedService* BuildFakeExtensionSystem(content::BrowserContext* profile) { |
| 92 return new FakeExtensionSystem(static_cast<Profile*>(profile)); |
| 93 } |
| 94 |
| 95 } // namespace |
| 96 |
| 97 namespace extensions { |
| 98 |
| 99 namespace bluetooth = api::bluetooth; |
| 100 |
| 101 class ExtensionBluetoothApiTest : public testing::Test { |
| 102 public: |
| 103 ExtensionBluetoothApiTest() |
| 104 : profile_manager_(TestingBrowserProcess::GetGlobal()), |
| 105 mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>()), |
| 106 test_profile_(NULL) {} |
| 107 |
| 108 virtual void SetUp() OVERRIDE { |
| 109 testing::Test::SetUp(); |
| 110 ASSERT_TRUE(profile_manager_.SetUp()); |
| 111 test_profile_ = profile_manager_.CreateTestingProfile("test"); |
| 112 ExtensionSystemFactory::GetInstance()->SetTestingFactoryAndUse( |
| 113 test_profile_, &BuildFakeExtensionSystem); |
| 114 ApiResourceManager<BluetoothApiSocket>::GetFactoryInstance() |
| 115 ->SetTestingFactoryAndUse(test_profile_, ApiResourceManagerTestFactory); |
| 116 BluetoothAPI::GetFactoryInstance()->SetTestingFactoryAndUse( |
| 117 test_profile_, BluetoothAPITestFactory); |
| 118 BluetoothAPI::Get(test_profile_)->event_router()->SetAdapterForTest( |
| 119 mock_adapter_); |
| 120 } |
| 121 |
| 122 virtual void TearDown() OVERRIDE { |
| 123 // Some profile-dependent services rely on UI thread to clean up. We make |
| 124 // sure they are properly cleaned up by running the UI message loop until |
| 125 // idle. |
| 126 test_profile_ = NULL; |
| 127 profile_manager_.DeleteTestingProfile("test"); |
| 128 base::RunLoop run_loop; |
| 129 run_loop.RunUntilIdle(); |
| 130 } |
| 131 |
| 132 protected: |
| 133 content::TestBrowserThreadBundle thread_bundle_; |
| 134 TestingProfileManager profile_manager_; |
| 135 scoped_refptr<testing::StrictMock<device::MockBluetoothAdapter> > |
| 136 mock_adapter_; |
| 137 testing::NiceMock<device::MockBluetoothProfile> mock_audio_profile_; |
| 138 testing::NiceMock<device::MockBluetoothProfile> mock_health_profile_; |
| 139 // Profiles are weak pointers, owned by |profile_manager_|. |
| 140 TestingProfile* test_profile_; |
| 141 }; |
| 142 |
| 143 TEST_F(ExtensionBluetoothApiTest, DispatchConnectionEvent) { |
| 144 testing::NiceMock<device::MockBluetoothDevice> mock_device( |
| 145 mock_adapter_.get(), 0, "device name", "device address", true, false); |
| 146 EXPECT_CALL(*mock_adapter_, GetDevice(mock_device.GetAddress())) |
| 147 .WillOnce(testing::Return(&mock_device)); |
| 148 scoped_refptr<testing::NiceMock<device::MockBluetoothSocket> > mock_socket( |
| 149 new testing::NiceMock<device::MockBluetoothSocket>()); |
| 150 |
| 151 BluetoothAPI::Get(test_profile_)->event_router()->AddProfile( |
| 152 kAudioProfileUuid, kTestExtensionId, &mock_audio_profile_); |
| 153 |
| 154 BluetoothAPI::Get(test_profile_)->DispatchConnectionEvent( |
| 155 kTestExtensionId, kAudioProfileUuid, &mock_device, mock_socket); |
| 156 // Connection events are dispatched using a couple of PostTask to the UI |
| 157 // thread. Waiting until idle ensures the event is dispatched to the |
| 158 // receiver(s). |
| 159 base::RunLoop().RunUntilIdle(); |
| 160 |
| 161 FakeEventRouter* fake_event_router = static_cast<FakeEventRouter*>( |
| 162 ExtensionSystem::Get(test_profile_)->event_router()); |
| 163 |
| 164 ASSERT_TRUE(fake_event_router->event()); |
| 165 EXPECT_STREQ(kTestExtensionId, fake_event_router->extension_id().c_str()); |
| 166 EXPECT_STREQ(bluetooth::OnConnection::kEventName, |
| 167 fake_event_router->event()->event_name.c_str()); |
| 168 |
| 169 base::ListValue* event_args = fake_event_router->event()->event_args.get(); |
| 170 base::DictionaryValue* socket_value = NULL; |
| 171 ASSERT_TRUE(event_args->GetDictionary(0, &socket_value)); |
| 172 int socket_id; |
| 173 ASSERT_TRUE(socket_value->GetInteger("id", &socket_id)); |
| 174 |
| 175 ASSERT_TRUE(BluetoothAPI::Get(test_profile_)->socket_data()->Get( |
| 176 kTestExtensionId, socket_id)); |
| 177 |
| 178 std::string profile_uuid; |
| 179 ASSERT_TRUE(socket_value->GetString("uuid", &profile_uuid)); |
| 180 EXPECT_STREQ(kAudioProfileUuid, profile_uuid.c_str()); |
| 181 |
| 182 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 183 BluetoothAPI::Get(test_profile_)->socket_data()->Remove(kTestExtensionId, |
| 184 socket_id); |
| 185 } |
| 186 |
| 187 TEST_F(ExtensionBluetoothApiTest, DoNotDispatchConnectionEvent) { |
| 188 testing::NiceMock<device::MockBluetoothDevice> mock_device( |
| 189 mock_adapter_.get(), 0, "device name", "device address", true, false); |
| 190 scoped_refptr<testing::NiceMock<device::MockBluetoothSocket> > mock_socket( |
| 191 new testing::NiceMock<device::MockBluetoothSocket>()); |
| 192 |
| 193 // Connection event won't be dispatched for non-registered profiles. |
| 194 BluetoothAPI::Get(test_profile_)->DispatchConnectionEvent( |
| 195 "test extension id", kAudioProfileUuid, &mock_device, mock_socket); |
| 196 // Connection events are dispatched using a couple of PostTask to the UI |
| 197 // thread. Waiting until idle ensures the event is dispatched to the |
| 198 // receiver(s). |
| 199 base::RunLoop().RunUntilIdle(); |
| 200 |
| 201 FakeEventRouter* fake_event_router = static_cast<FakeEventRouter*>( |
| 202 ExtensionSystem::Get(test_profile_)->event_router()); |
| 203 EXPECT_TRUE(fake_event_router->event() == NULL); |
| 204 |
| 205 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 206 } |
| 207 |
| 208 } // namespace extensions |
OLD | NEW |