blob: 38eac136ac831698a33b2f717fd4a5932c64e17e [file] [log] [blame]
Chris Manton4eb56312019-08-13 14:45:06 -07001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jack He580337d2019-10-01 20:44:04 -070017#include "l2cap/internal/fixed_channel_allocator.h"
Jack Heff38d892019-10-03 17:11:07 -070018#include "l2cap/classic/internal/fixed_channel_impl_mock.h"
19#include "l2cap/classic/internal/link_mock.h"
Jack He6a9b4662019-08-20 22:07:26 -070020#include "l2cap/internal/parameter_provider_mock.h"
Chris Manton4eb56312019-08-13 14:45:06 -070021
Jack He6a9b4662019-08-20 22:07:26 -070022#include <gmock/gmock.h>
Chris Manton4eb56312019-08-13 14:45:06 -070023#include <gtest/gtest.h>
24
25namespace bluetooth {
26namespace l2cap {
27namespace internal {
28
Jack Heff38d892019-10-03 17:11:07 -070029using l2cap::classic::internal::testing::MockFixedChannelImpl;
30using l2cap::classic::internal::testing::MockLink;
Jack He6a9b4662019-08-20 22:07:26 -070031using testing::MockParameterProvider;
32using ::testing::Return;
33
Hansong Zhang47dec642019-11-25 12:01:15 -080034const hci::AddressWithType device{{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}}, hci::AddressType::PUBLIC_IDENTITY_ADDRESS};
Jack He6a9b4662019-08-20 22:07:26 -070035
Jack He580337d2019-10-01 20:44:04 -070036class L2capFixedChannelAllocatorTest : public ::testing::Test {
Chris Manton4eb56312019-08-13 14:45:06 -070037 protected:
38 void SetUp() override {
39 thread_ = new os::Thread("test_thread", os::Thread::Priority::NORMAL);
40 handler_ = new os::Handler(thread_);
Jack He6a9b4662019-08-20 22:07:26 -070041 mock_parameter_provider_ = new MockParameterProvider();
Jack Heff38d892019-10-03 17:11:07 -070042 mock_classic_link_ = new MockLink(handler_, mock_parameter_provider_);
Jack He6a9b4662019-08-20 22:07:26 -070043 EXPECT_CALL(*mock_classic_link_, GetDevice()).WillRepeatedly(Return(device));
Jack He580337d2019-10-01 20:44:04 -070044 // Use classic as a place holder
Jack Heff38d892019-10-03 17:11:07 -070045 channel_allocator_ =
46 std::make_unique<FixedChannelAllocator<MockFixedChannelImpl, MockLink>>(mock_classic_link_, handler_);
Chris Manton4eb56312019-08-13 14:45:06 -070047 }
48
49 void TearDown() override {
50 channel_allocator_.reset();
Jack He6a9b4662019-08-20 22:07:26 -070051 delete mock_classic_link_;
52 delete mock_parameter_provider_;
Chris Manton4eb56312019-08-13 14:45:06 -070053 handler_->Clear();
54 delete handler_;
55 delete thread_;
56 }
57
58 os::Thread* thread_{nullptr};
59 os::Handler* handler_{nullptr};
Jack He6a9b4662019-08-20 22:07:26 -070060 MockParameterProvider* mock_parameter_provider_{nullptr};
Jack Heff38d892019-10-03 17:11:07 -070061 MockLink* mock_classic_link_{nullptr};
62 std::unique_ptr<FixedChannelAllocator<MockFixedChannelImpl, MockLink>> channel_allocator_;
Chris Manton4eb56312019-08-13 14:45:06 -070063};
64
Jack He580337d2019-10-01 20:44:04 -070065TEST_F(L2capFixedChannelAllocatorTest, precondition) {
Chris Manton4eb56312019-08-13 14:45:06 -070066 Cid cid = kFirstFixedChannel;
Jack He6a9b4662019-08-20 22:07:26 -070067 EXPECT_FALSE(channel_allocator_->IsChannelAllocated(cid));
Chris Manton4eb56312019-08-13 14:45:06 -070068}
69
Jack He580337d2019-10-01 20:44:04 -070070TEST_F(L2capFixedChannelAllocatorTest, allocate_and_free_channel) {
Chris Manton4eb56312019-08-13 14:45:06 -070071 Cid cid = kFirstFixedChannel;
Jack He6a9b4662019-08-20 22:07:26 -070072 auto channel = channel_allocator_->AllocateChannel(cid, {});
73 EXPECT_TRUE(channel_allocator_->IsChannelAllocated(cid));
Chris Manton4eb56312019-08-13 14:45:06 -070074 EXPECT_EQ(channel, channel_allocator_->FindChannel(cid));
Jack He6a9b4662019-08-20 22:07:26 -070075 ASSERT_NO_FATAL_FAILURE(channel_allocator_->FreeChannel(cid));
76 EXPECT_FALSE(channel_allocator_->IsChannelAllocated(cid));
Chris Manton4eb56312019-08-13 14:45:06 -070077}
78
79} // namespace internal
80} // namespace l2cap
81} // namespace bluetooth