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