File: rtc_sctp_transport_test.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (119 lines) | stat: -rw-r--r-- 4,210 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/peerconnection/rtc_sctp_transport.h"

#include "base/memory/raw_ptr.h"
#include "base/test/test_simple_task_runner.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/web/web_heap.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"
#include "third_party/blink/renderer/core/dom/events/native_event_listener.h"
#include "third_party/blink/renderer/core/testing/null_execution_context.h"
#include "third_party/blink/renderer/platform/testing/task_environment.h"
#include "third_party/webrtc/api/sctp_transport_interface.h"
#include "third_party/webrtc/rtc_base/ref_counted_object.h"

namespace blink {

using testing::_;
using testing::Invoke;
using testing::Mock;
using testing::NiceMock;
using testing::Return;

class MockEventListener final : public NativeEventListener {
 public:
  MOCK_METHOD2(Invoke, void(ExecutionContext*, Event*));
};

class MockSctpTransport : public webrtc::SctpTransportInterface {
 public:
  MockSctpTransport() {
    ON_CALL(*this, Information()).WillByDefault(Return(info_));
    ON_CALL(*this, RegisterObserver(_))
        .WillByDefault(Invoke(this, &MockSctpTransport::SetObserver));
  }
  MOCK_CONST_METHOD0(dtls_transport,
                     webrtc::scoped_refptr<webrtc::DtlsTransportInterface>());
  MOCK_CONST_METHOD0(Information, webrtc::SctpTransportInformation());
  MOCK_METHOD1(RegisterObserver, void(webrtc::SctpTransportObserverInterface*));
  MOCK_METHOD0(UnregisterObserver, void());

  void SetObserver(webrtc::SctpTransportObserverInterface* observer) {
    observer_ = observer;
  }

  void SendClose() {
    if (observer_) {
      observer_->OnStateChange(webrtc::SctpTransportInformation(
          webrtc::SctpTransportState::kClosed));
    }
  }

 private:
  webrtc::SctpTransportInformation info_ =
      webrtc::SctpTransportInformation(webrtc::SctpTransportState::kNew);
  raw_ptr<webrtc::SctpTransportObserverInterface, DanglingUntriaged> observer_ =
      nullptr;
};

class RTCSctpTransportTest : public testing::Test {
 public:
  RTCSctpTransportTest();
  ~RTCSctpTransportTest() override;

  // Run the main thread and worker thread until both are idle.
  void RunUntilIdle();

 protected:
  test::TaskEnvironment task_environment_;
  scoped_refptr<base::TestSimpleTaskRunner> main_thread_;
  scoped_refptr<base::TestSimpleTaskRunner> worker_thread_;
  Vector<Persistent<MockEventListener>> mock_event_listeners_;
};

RTCSctpTransportTest::RTCSctpTransportTest()
    : main_thread_(new base::TestSimpleTaskRunner()),
      worker_thread_(new base::TestSimpleTaskRunner()) {}

RTCSctpTransportTest::~RTCSctpTransportTest() {
  RunUntilIdle();

  // Explicitly verify expectations of garbage collected mock objects.
  for (auto mock : mock_event_listeners_) {
    Mock::VerifyAndClear(mock);
  }
}

void RTCSctpTransportTest::RunUntilIdle() {
  while (worker_thread_->HasPendingTask() || main_thread_->HasPendingTask()) {
    worker_thread_->RunPendingTasks();
    main_thread_->RunPendingTasks();
  }
}

TEST_F(RTCSctpTransportTest, CreateFromMocks) {
  V8TestingScope scope;

  ExecutionContext* context = scope.GetExecutionContext();
  webrtc::scoped_refptr<webrtc::SctpTransportInterface> mock_native_transport(
      new webrtc::RefCountedObject<NiceMock<MockSctpTransport>>());
  RTCSctpTransport* transport = MakeGarbageCollected<RTCSctpTransport>(
      context, mock_native_transport, main_thread_, worker_thread_);
  WeakPersistent<RTCSctpTransport> garbage_collection_observer = transport;
  RunUntilIdle();
  transport = nullptr;
  // An unclosed transport should not be garbage collected, since events
  // might still trickle up.
  ASSERT_TRUE(garbage_collection_observer);
  // A closed transport should be garbage collected.
  static_cast<MockSctpTransport*>(mock_native_transport.get())->SendClose();
  RunUntilIdle();
  WebHeap::CollectAllGarbageForTesting();
  EXPECT_FALSE(garbage_collection_observer);
}

}  // namespace blink