1 /*
2 * Copyright 2022 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "pc/data_channel_controller.h"
12
13 #include <memory>
14
15 #include "pc/peer_connection_internal.h"
16 #include "pc/sctp_data_channel.h"
17 #include "pc/test/mock_peer_connection_internal.h"
18 #include "test/gmock.h"
19 #include "test/gtest.h"
20
21 namespace webrtc {
22
23 namespace {
24
25 using ::testing::NiceMock;
26 using ::testing::Return;
27
28 class DataChannelControllerTest : public ::testing::Test {
29 protected:
DataChannelControllerTest()30 DataChannelControllerTest() {
31 pc_ = rtc::make_ref_counted<NiceMock<MockPeerConnectionInternal>>();
32 ON_CALL(*pc_, signaling_thread)
33 .WillByDefault(Return(rtc::Thread::Current()));
34 }
35
36 rtc::AutoThread main_thread_;
37 rtc::scoped_refptr<NiceMock<MockPeerConnectionInternal>> pc_;
38 };
39
TEST_F(DataChannelControllerTest,CreateAndDestroy)40 TEST_F(DataChannelControllerTest, CreateAndDestroy) {
41 DataChannelController dcc(pc_.get());
42 }
43
TEST_F(DataChannelControllerTest,CreateDataChannelEarlyRelease)44 TEST_F(DataChannelControllerTest, CreateDataChannelEarlyRelease) {
45 DataChannelController dcc(pc_.get());
46 auto channel = dcc.InternalCreateDataChannelWithProxy(
47 "label",
48 std::make_unique<InternalDataChannelInit>(DataChannelInit()).get());
49 channel = nullptr; // dcc holds a reference to channel, so not destroyed yet
50 }
51
TEST_F(DataChannelControllerTest,CreateDataChannelLateRelease)52 TEST_F(DataChannelControllerTest, CreateDataChannelLateRelease) {
53 auto dcc = std::make_unique<DataChannelController>(pc_.get());
54 auto channel = dcc->InternalCreateDataChannelWithProxy(
55 "label",
56 std::make_unique<InternalDataChannelInit>(DataChannelInit()).get());
57 dcc.reset();
58 channel = nullptr;
59 }
60
TEST_F(DataChannelControllerTest,CloseAfterControllerDestroyed)61 TEST_F(DataChannelControllerTest, CloseAfterControllerDestroyed) {
62 auto dcc = std::make_unique<DataChannelController>(pc_.get());
63 auto channel = dcc->InternalCreateDataChannelWithProxy(
64 "label",
65 std::make_unique<InternalDataChannelInit>(DataChannelInit()).get());
66 // Connect to provider
67 auto inner_channel =
68 DowncastProxiedDataChannelInterfaceToSctpDataChannelForTesting(
69 channel.get());
70 dcc->ConnectDataChannel(inner_channel);
71 dcc.reset();
72 channel->Close();
73 }
74
75 } // namespace
76 } // namespace webrtc
77