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 #pragma once
16 
17 #include <pw_async_fuchsia/dispatcher.h>
18 
19 #include "pw_bluetooth_sapphire/internal/host/common/macros.h"
20 #include "pw_bluetooth_sapphire/internal/host/gap/adapter.h"
21 #include "pw_bluetooth_sapphire/internal/host/gatt/fake_layer.h"
22 #include "pw_bluetooth_sapphire/internal/host/l2cap/fake_l2cap.h"
23 #include "pw_bluetooth_sapphire/internal/host/testing/controller_test.h"
24 #include "pw_bluetooth_sapphire/internal/host/testing/fake_controller.h"
25 #include "pw_bluetooth_sapphire/internal/host/testing/loop_fixture.h"
26 
27 namespace bthost::testing {
28 
29 // This test fixture provides an instance of the Bluetooth stack with mock data
30 // plane (L2CAP) and GATT test doubles. The fixture is backed by a
31 // FakeController and an event loop which can be used to test interactions with
32 // the Bluetooth controller.
33 class AdapterTestFixture
34     : public bt::testing::TestLoopFixture,
35       public bt::testing::ControllerTest<bt::testing::FakeController> {
36  public:
AdapterTestFixture()37   AdapterTestFixture()
38       : bt::testing::ControllerTest<bt::testing::FakeController>(
39             pw_dispatcher_),
40         pw_dispatcher_(dispatcher()) {}
41   ~AdapterTestFixture() override = default;
42 
pw_dispatcher()43   pw::async::Dispatcher& pw_dispatcher() { return pw_dispatcher_; }
44 
45  protected:
46   void SetUp() override;
47   void SetUp(bt::testing::FakeController::Settings settings,
48              pw::bluetooth::Controller::FeaturesBits features =
49                  pw::bluetooth::Controller::FeaturesBits{0});
50   void TearDown() override;
51 
adapter()52   bt::gap::Adapter::WeakPtr adapter() const { return adapter_->AsWeakPtr(); }
gatt()53   bt::gatt::testing::FakeLayer* gatt() const { return gatt_.get(); }
take_gatt()54   std::unique_ptr<bt::gatt::testing::FakeLayer> take_gatt() {
55     return std::move(gatt_);
56   }
l2cap()57   bt::l2cap::testing::FakeL2cap* l2cap() const { return l2cap_; }
58 
59  private:
60   pw::async_fuchsia::FuchsiaDispatcher pw_dispatcher_;
61   std::unique_ptr<bt::gap::Adapter> adapter_;
62   bt::l2cap::testing::FakeL2cap* l2cap_;
63   std::unique_ptr<bt::gatt::testing::FakeLayer> gatt_;
64 
65   BT_DISALLOW_COPY_ASSIGN_AND_MOVE(AdapterTestFixture);
66 };
67 
68 }  // namespace bthost::testing
69