1 // Copyright 2023 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/internal/host/l2cap/scoped_channel.h"
16
17 #include "pw_bluetooth_sapphire/internal/host/l2cap/fake_channel.h"
18 #include "pw_unit_test/framework.h"
19
20 namespace bt::l2cap::testing {
21 namespace {
22
DoNothing()23 void DoNothing() {}
NopRxCallback(ByteBufferPtr)24 void NopRxCallback(ByteBufferPtr) {}
25
26 } // namespace
27
TEST(ScopedChannelTest,Close)28 TEST(ScopedChannelTest, Close) {
29 auto chan = std::make_unique<FakeChannel>(1, 1, 1, bt::LinkType::kACL);
30 ASSERT_TRUE(chan->Activate(NopRxCallback, DoNothing));
31 ASSERT_TRUE(chan->activated());
32
33 {
34 ScopedChannel scoped(chan->GetWeakPtr());
35 EXPECT_TRUE(chan->activated());
36 }
37
38 EXPECT_FALSE(chan->activated());
39 }
40
TEST(ScopedChannelTest,Reset)41 TEST(ScopedChannelTest, Reset) {
42 auto chan1 = std::make_unique<FakeChannel>(1, 1, 1, bt::LinkType::kACL);
43 auto chan2 = std::make_unique<FakeChannel>(1, 1, 1, bt::LinkType::kACL);
44 ASSERT_TRUE(chan1->Activate(NopRxCallback, DoNothing));
45 ASSERT_TRUE(chan2->Activate(NopRxCallback, DoNothing));
46 ASSERT_TRUE(chan1->activated());
47 ASSERT_TRUE(chan2->activated());
48
49 ScopedChannel scoped(chan1->GetWeakPtr());
50 EXPECT_TRUE(chan1->activated());
51
52 scoped.Reset(chan2->GetWeakPtr());
53 EXPECT_FALSE(chan1->activated());
54 EXPECT_TRUE(chan2->activated());
55
56 scoped.Reset(Channel::WeakPtr());
57 EXPECT_FALSE(chan2->activated());
58 }
59
60 } // namespace bt::l2cap::testing
61