| Index: chrome/browser/extensions/api/bluetooth/bluetooth_api_unittest.cc
|
| diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_api_unittest.cc b/chrome/browser/extensions/api/bluetooth/bluetooth_api_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..8739b6d16a2e8eefc4bac17d976fafeb93f4d3c2
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/api/bluetooth/bluetooth_api_unittest.cc
|
| @@ -0,0 +1,213 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include <string>
|
| +
|
| +#include "base/memory/ref_counted.h"
|
| +#include "base/memory/scoped_ptr.h"
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "base/run_loop.h"
|
| +#include "chrome/browser/chrome_notification_types.h"
|
| +#include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h"
|
| +#include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h"
|
| +#include "chrome/browser/extensions/event_names.h"
|
| +#include "chrome/browser/extensions/extension_system_factory.h"
|
| +#include "chrome/browser/extensions/test_extension_system.h"
|
| +#include "chrome/common/extensions/api/bluetooth.h"
|
| +#include "chrome/test/base/testing_browser_process.h"
|
| +#include "chrome/test/base/testing_profile.h"
|
| +#include "chrome/test/base/testing_profile_manager.h"
|
| +#include "content/public/browser/notification_service.h"
|
| +#include "content/public/test/test_browser_thread_bundle.h"
|
| +#include "device/bluetooth/test/mock_bluetooth_adapter.h"
|
| +#include "device/bluetooth/test/mock_bluetooth_device.h"
|
| +#include "device/bluetooth/test/mock_bluetooth_profile.h"
|
| +#include "device/bluetooth/test/mock_bluetooth_socket.h"
|
| +#include "extensions/browser/event_router.h"
|
| +#include "extensions/common/extension_builder.h"
|
| +#include "testing/gmock/include/gmock/gmock.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace {
|
| +
|
| +const char kTestExtensionId[] = "test extension id";
|
| +const char kAudioProfileUuid[] = "audio profile uuid";
|
| +
|
| +static BrowserContextKeyedService* ApiResourceManagerTestFactory(
|
| + content::BrowserContext* context) {
|
| + content::BrowserThread::ID id;
|
| + CHECK(content::BrowserThread::GetCurrentThreadIdentifier(&id));
|
| + return extensions::ApiResourceManager<
|
| + extensions::BluetoothApiSocket>::CreateApiResourceManagerForTest(context,
|
| + id);
|
| +}
|
| +
|
| +class FakeEventRouter : public extensions::EventRouter {
|
| + public:
|
| + explicit FakeEventRouter(Profile* profile) : EventRouter(profile, NULL) {}
|
| +
|
| + virtual void DispatchEventToExtension(const std::string& extension_id,
|
| + scoped_ptr<extensions::Event> event)
|
| + OVERRIDE {
|
| + extension_id_ = extension_id;
|
| + event_ = event.Pass();
|
| + }
|
| +
|
| + std::string extension_id() const { return extension_id_; }
|
| +
|
| + const extensions::Event* event() const { return event_.get(); }
|
| +
|
| + private:
|
| + std::string extension_id_;
|
| + scoped_ptr<extensions::Event> event_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(FakeEventRouter);
|
| +};
|
| +
|
| +class FakeExtensionSystem : public extensions::TestExtensionSystem {
|
| + public:
|
| + explicit FakeExtensionSystem(Profile* profile)
|
| + : extensions::TestExtensionSystem(profile) {}
|
| +
|
| + virtual extensions::EventRouter* event_router() OVERRIDE {
|
| + if (!fake_event_router_)
|
| + fake_event_router_.reset(new FakeEventRouter(profile_));
|
| + return fake_event_router_.get();
|
| + }
|
| +
|
| + private:
|
| + scoped_ptr<FakeEventRouter> fake_event_router_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(FakeExtensionSystem);
|
| +};
|
| +
|
| +BrowserContextKeyedService* BuildFakeExtensionSystem(
|
| + content::BrowserContext* profile) {
|
| + return new FakeExtensionSystem(static_cast<Profile*>(profile));
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +namespace extensions {
|
| +
|
| +namespace bluetooth = api::bluetooth;
|
| +
|
| +class ExtensionBluetoothApiTest : public testing::Test {
|
| + public:
|
| + ExtensionBluetoothApiTest()
|
| + : profile_manager_(TestingBrowserProcess::GetGlobal()),
|
| + mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>()),
|
| + test_profile_(NULL) {}
|
| +
|
| + virtual void SetUp() OVERRIDE {
|
| + testing::Test::SetUp();
|
| + ASSERT_TRUE(profile_manager_.SetUp());
|
| + test_profile_ = profile_manager_.CreateTestingProfile("test");
|
| + ApiResourceManager<BluetoothApiSocket>::GetFactoryInstance()
|
| + ->SetTestingFactoryAndUse(test_profile_, ApiResourceManagerTestFactory);
|
| + }
|
| +
|
| + virtual void TearDown() OVERRIDE {
|
| + // Some profile-dependent services rely on UI thread to clean up. We make
|
| + // sure they are properly cleaned up by running the UI message loop until
|
| + // idle.
|
| + test_profile_ = NULL;
|
| + profile_manager_.DeleteTestingProfile("test");
|
| + base::RunLoop run_loop;
|
| + run_loop.RunUntilIdle();
|
| + }
|
| +
|
| + protected:
|
| + BluetoothAPI* bluetooth_api() {
|
| + if (!bluetooth_api_) {
|
| + bluetooth_api_.reset(new BluetoothAPI(test_profile_));
|
| + }
|
| + return bluetooth_api_.get();
|
| + }
|
| +
|
| + content::TestBrowserThreadBundle thread_bundle_;
|
| + TestingProfileManager profile_manager_;
|
| + scoped_refptr<testing::StrictMock<device::MockBluetoothAdapter> >
|
| + mock_adapter_;
|
| + testing::NiceMock<device::MockBluetoothProfile> mock_audio_profile_;
|
| + testing::NiceMock<device::MockBluetoothProfile> mock_health_profile_;
|
| + // Profiles are weak pointers, owned by |profile_manager_|.
|
| + TestingProfile* test_profile_;
|
| +
|
| + private:
|
| + scoped_ptr<BluetoothAPI> bluetooth_api_;
|
| +};
|
| +
|
| +TEST_F(ExtensionBluetoothApiTest, DispatchConnectionEvent) {
|
| + FakeExtensionSystem* fake_extension_system =
|
| + static_cast<FakeExtensionSystem*>(
|
| + ExtensionSystemFactory::GetInstance()->SetTestingFactoryAndUse(
|
| + test_profile_, &BuildFakeExtensionSystem));
|
| +
|
| + testing::NiceMock<device::MockBluetoothDevice> mock_device(
|
| + mock_adapter_.get(), 0, "device name", "device address", true, false);
|
| + scoped_refptr<testing::NiceMock<device::MockBluetoothSocket> > mock_socket(
|
| + new testing::NiceMock<device::MockBluetoothSocket>());
|
| +
|
| + bluetooth_api()->event_router()->AddProfile(
|
| + kAudioProfileUuid, kTestExtensionId, &mock_audio_profile_);
|
| +
|
| + bluetooth_api()->DispatchConnectionEvent(
|
| + kTestExtensionId, kAudioProfileUuid, &mock_device, mock_socket);
|
| + base::RunLoop().RunUntilIdle();
|
| +
|
| + FakeEventRouter* fake_event_router =
|
| + static_cast<FakeEventRouter*>(fake_extension_system->event_router());
|
| +
|
| + ASSERT_TRUE(fake_event_router->event());
|
| + EXPECT_STREQ(kTestExtensionId, fake_event_router->extension_id().c_str());
|
| + EXPECT_STREQ(bluetooth::OnConnection::kEventName,
|
| + fake_event_router->event()->event_name.c_str());
|
| +
|
| + base::ListValue* event_args = fake_event_router->event()->event_args.get();
|
| + base::DictionaryValue* socket_value = NULL;
|
| + ASSERT_TRUE(event_args->GetDictionary(0, &socket_value));
|
| + int socket_id;
|
| + ASSERT_TRUE(socket_value->GetInteger("id", &socket_id));
|
| +
|
| + // TODO(rpaquay)
|
| + // EXPECT_EQ(mock_socket.get(),
|
| + // bluetooth_api_.bluetooth_socket_manager()->Get(kTestExtensionId,
|
| + // socket_id));
|
| +
|
| + base::DictionaryValue* profile_value = NULL;
|
| + ASSERT_TRUE(socket_value->GetDictionary("profile", &profile_value));
|
| + std::string profile_uuid;
|
| + ASSERT_TRUE(profile_value->GetString("uuid", &profile_uuid));
|
| + EXPECT_STREQ(kAudioProfileUuid, profile_uuid.c_str());
|
| +
|
| + // EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
|
| + // TODO(rpaquay)
|
| + bluetooth_api()->socket_data()->Remove(kTestExtensionId, socket_id);
|
| +}
|
| +
|
| +TEST_F(ExtensionBluetoothApiTest, DoNotDispatchConnectionEvent) {
|
| + FakeExtensionSystem* fake_extension_system =
|
| + static_cast<FakeExtensionSystem*>(
|
| + ExtensionSystemFactory::GetInstance()->SetTestingFactoryAndUse(
|
| + test_profile_, &BuildFakeExtensionSystem));
|
| + testing::NiceMock<device::MockBluetoothDevice> mock_device(
|
| + mock_adapter_.get(), 0, "device name", "device address", true, false);
|
| + scoped_refptr<testing::NiceMock<device::MockBluetoothSocket> > mock_socket(
|
| + new testing::NiceMock<device::MockBluetoothSocket>());
|
| +
|
| + // Connection event won't be dispatched for non-registered profiles.
|
| + bluetooth_api()->DispatchConnectionEvent(
|
| + "test extension id", kAudioProfileUuid, &mock_device, mock_socket);
|
| + base::RunLoop().RunUntilIdle();
|
| +
|
| + FakeEventRouter* fake_event_router =
|
| + static_cast<FakeEventRouter*>(fake_extension_system->event_router());
|
| + EXPECT_TRUE(fake_event_router->event() == NULL);
|
| +
|
| + // TODO(rpaquay)
|
| + // EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
|
| +}
|
| +
|
| +} // namespace extensions
|
|
|