1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/api/bluetooth/bluetooth_event_router.h"
#include <memory>
#include <string>
#include <utility>
#include "base/functional/callback_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "content/public/test/test_browser_context.h"
#include "device/bluetooth/bluetooth_common.h"
#include "device/bluetooth/public/cpp/bluetooth_uuid.h"
#include "device/bluetooth/test/mock_bluetooth_adapter.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extensions_test.h"
#include "extensions/browser/unloaded_extension_reason.h"
#include "extensions/common/api/bluetooth.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 device::BluetoothUUID kAudioProfileUuid("1234");
const device::BluetoothUUID kHealthProfileUuid("4321");
MATCHER_P(IsFilterEqual, a, "") {
return arg.Equals(*a);
}
} // namespace
namespace extensions {
namespace bluetooth = api::bluetooth;
class BluetoothEventRouterTest : public ExtensionsTest {
public:
BluetoothEventRouterTest()
: mock_adapter_(base::MakeRefCounted<
testing::StrictMock<device::MockBluetoothAdapter>>()) {}
void SetUp() override {
ExtensionsTest::SetUp();
router_ = std::make_unique<BluetoothEventRouter>(browser_context());
router_->SetAdapterForTest(mock_adapter_.get());
}
void TearDown() override {
// It's important to destroy the router before the browser context keyed
// services so it removes itself as an ExtensionRegistry observer.
router_.reset();
mock_adapter_.reset();
ExtensionsTest::TearDown();
}
protected:
scoped_refptr<testing::StrictMock<device::MockBluetoothAdapter>>
mock_adapter_;
std::unique_ptr<BluetoothEventRouter> router_;
};
TEST_F(BluetoothEventRouterTest, BluetoothEventListener) {
EventListenerInfo info("", "", GURL(), nullptr);
router_->OnListenerAdded(info);
EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
router_->OnListenerRemoved(info);
}
TEST_F(BluetoothEventRouterTest, MultipleBluetoothEventListeners) {
// TODO(rkc/stevenjb): Test multiple extensions and WebUI.
EventListenerInfo info("", "", GURL(), nullptr);
router_->OnListenerAdded(info);
router_->OnListenerAdded(info);
router_->OnListenerAdded(info);
router_->OnListenerRemoved(info);
router_->OnListenerRemoved(info);
EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
router_->OnListenerRemoved(info);
}
TEST_F(BluetoothEventRouterTest, UnloadExtension) {
scoped_refptr<const Extension> extension =
ExtensionBuilder()
.SetManifest(base::Value::Dict()
.Set("name", "BT event router test")
.Set("version", "1.0")
.Set("manifest_version", 2))
.SetID(kTestExtensionId)
.Build();
ExtensionRegistry::Get(browser_context())
->TriggerOnUnloaded(extension.get(), UnloadedExtensionReason::DISABLE);
EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
}
// This test check that calling SetDiscoveryFilter before StartDiscoverySession
// for given extension will start session with proper filter.
TEST_F(BluetoothEventRouterTest, SetDiscoveryFilter) {
auto discovery_filter = std::make_unique<device::BluetoothDiscoveryFilter>(
device::BLUETOOTH_TRANSPORT_LE);
discovery_filter->SetRSSI(-80);
device::BluetoothDiscoveryFilter::DeviceInfoFilter device_filter;
device_filter.uuids.insert(device::BluetoothUUID("1000"));
discovery_filter->AddDeviceFilter(std::move(device_filter));
device::BluetoothDiscoveryFilter df(device::BLUETOOTH_TRANSPORT_LE);
df.CopyFrom(*discovery_filter);
router_->SetDiscoveryFilter(std::move(discovery_filter), mock_adapter_.get(),
kTestExtensionId, base::DoNothing(),
base::DoNothing());
EXPECT_CALL(
*mock_adapter_,
StartScanWithFilter_(testing::Pointee(IsFilterEqual(&df)), testing::_))
.WillOnce(testing::Invoke(
[](const device::BluetoothDiscoveryFilter* filter,
base::OnceCallback<void(
/*is_error*/ bool,
device::UMABluetoothDiscoverySessionOutcome)>& callback) {
std::move(callback).Run(
false, device::UMABluetoothDiscoverySessionOutcome::SUCCESS);
}));
// RemoveDiscoverySession will be called when the BluetoothDiscoverySession
// is destroyed
EXPECT_CALL(*mock_adapter_, StopScan(testing::_)).Times(1);
router_->StartDiscoverySession(mock_adapter_.get(), kTestExtensionId,
base::DoNothing(), base::DoNothing());
EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1);
}
} // namespace extensions
|