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/gatt/fake_layer.h"
21 #include "pw_bluetooth_sapphire/internal/host/testing/loop_fixture.h"
22 
23 namespace bt::fidl::testing {
24 
25 // Provides a common GTest harness base class for clients of the GATT layer and
26 // emulation of ATT behavior.
27 class FakeGattFixture : public bt::testing::TestLoopFixture {
28  public:
29   FakeGattFixture();
30   ~FakeGattFixture() override = default;
31 
32   void TearDown() override;
33 
34  protected:
gatt()35   const bt::gatt::GATT::WeakPtr& gatt() const {
36     PW_CHECK(weak_gatt_.is_alive(),
37              "fake GATT layer accessed after it was destroyed!");
38     return weak_gatt_;
39   }
40 
fake_gatt()41   const bt::gatt::testing::FakeLayer::WeakPtr& fake_gatt() const {
42     PW_CHECK(weak_fake_layer_.is_alive(),
43              "fake GATT layer accessed after it was destroyed!");
44     return weak_fake_layer_;
45   }
46 
TakeGatt()47   std::unique_ptr<bt::gatt::testing::FakeLayer> TakeGatt() {
48     return std::move(gatt_);
49   }
50 
51  private:
52   // Store both an owning and a weak pointer to allow test code to acquire
53   // ownership of the layer object for dependency injection.
54   std::unique_ptr<bt::gatt::testing::FakeLayer> gatt_;
55   const bt::gatt::GATT::WeakPtr weak_gatt_;
56   const bt::gatt::testing::FakeLayer::WeakPtr weak_fake_layer_;
57   pw::async_fuchsia::FuchsiaDispatcher pw_dispatcher_{dispatcher()};
58 
59   BT_DISALLOW_COPY_ASSIGN_AND_MOVE(FakeGattFixture);
60 };
61 
62 }  // namespace bt::fidl::testing
63