1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_bluetooth_sapphire/fuchsia/host/fidl/adapter_test_fixture.h"
16
17 namespace bthost::testing {
18
19 using bt::testing::FakeController;
20 using TestingBase = bt::testing::ControllerTest<bt::testing::FakeController>;
21
SetUp()22 void AdapterTestFixture::SetUp() {
23 FakeController::Settings settings;
24 settings.ApplyDualModeDefaults();
25 SetUp(settings);
26 }
27
SetUp(FakeController::Settings settings,pw::bluetooth::Controller::FeaturesBits features)28 void AdapterTestFixture::SetUp(
29 FakeController::Settings settings,
30 pw::bluetooth::Controller::FeaturesBits features) {
31 TestingBase::Initialize(features, /*initialize_transport=*/false);
32
33 auto l2cap = std::make_unique<bt::l2cap::testing::FakeL2cap>(pw_dispatcher());
34 l2cap_ = l2cap.get();
35 gatt_ = std::make_unique<bt::gatt::testing::FakeLayer>(pw_dispatcher());
36 bt::gap::Adapter::Config config = {
37 .legacy_pairing_enabled = false,
38 };
39 adapter_ = bt::gap::Adapter::Create(pw_dispatcher(),
40 transport()->GetWeakPtr(),
41 gatt_->GetWeakPtr(),
42 config,
43 std::move(l2cap));
44
45 test_device()->set_settings(settings);
46
47 bool success = false;
48 adapter_->Initialize([&](bool result) { success = result; }, [] {});
49 RunLoopUntilIdle();
50 ASSERT_TRUE(success);
51 ASSERT_TRUE(adapter_->le());
52 ASSERT_TRUE(adapter_->bredr());
53 }
54
TearDown()55 void AdapterTestFixture::TearDown() {
56 // Drain all scheduled tasks.
57 RunLoopUntilIdle();
58
59 // Cleanly shut down the stack.
60 l2cap_ = nullptr;
61 adapter_ = nullptr;
62 RunLoopUntilIdle();
63
64 gatt_ = nullptr;
65 }
66
67 } // namespace bthost::testing
68