1 /*
2 * Copyright (c) 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 "test/pc/e2e/stats_poller.h"
12
13 #include "api/stats/rtc_stats_collector_callback.h"
14 #include "test/gmock.h"
15 #include "test/gtest.h"
16
17 namespace webrtc {
18 namespace webrtc_pc_e2e {
19 namespace {
20
21 using ::testing::Eq;
22
23 class TestStatsProvider : public StatsProvider {
24 public:
25 ~TestStatsProvider() override = default;
26
GetStats(RTCStatsCollectorCallback * callback)27 void GetStats(RTCStatsCollectorCallback* callback) override {
28 stats_collections_count_++;
29 }
30
stats_collections_count() const31 int stats_collections_count() const { return stats_collections_count_; }
32
33 private:
34 int stats_collections_count_ = 0;
35 };
36
37 class MockStatsObserver : public StatsObserverInterface {
38 public:
39 ~MockStatsObserver() override = default;
40
41 MOCK_METHOD(void,
42 OnStatsReports,
43 (absl::string_view pc_label,
44 const rtc::scoped_refptr<const RTCStatsReport>& report));
45 };
46
TEST(StatsPollerTest,UnregisterParticipantAddedInCtor)47 TEST(StatsPollerTest, UnregisterParticipantAddedInCtor) {
48 TestStatsProvider alice;
49 TestStatsProvider bob;
50
51 MockStatsObserver stats_observer;
52
53 StatsPoller poller(/*observers=*/{&stats_observer},
54 /*peers_to_observe=*/{{"alice", &alice}, {"bob", &bob}});
55 poller.PollStatsAndNotifyObservers();
56
57 EXPECT_THAT(alice.stats_collections_count(), Eq(1));
58 EXPECT_THAT(bob.stats_collections_count(), Eq(1));
59
60 poller.UnregisterParticipantInCall("bob");
61 poller.PollStatsAndNotifyObservers();
62
63 EXPECT_THAT(alice.stats_collections_count(), Eq(2));
64 EXPECT_THAT(bob.stats_collections_count(), Eq(1));
65 }
66
TEST(StatsPollerTest,UnregisterParticipantRegisteredInCall)67 TEST(StatsPollerTest, UnregisterParticipantRegisteredInCall) {
68 TestStatsProvider alice;
69 TestStatsProvider bob;
70
71 MockStatsObserver stats_observer;
72
73 StatsPoller poller(/*observers=*/{&stats_observer},
74 /*peers_to_observe=*/{{"alice", &alice}});
75 poller.RegisterParticipantInCall("bob", &bob);
76 poller.PollStatsAndNotifyObservers();
77
78 EXPECT_THAT(alice.stats_collections_count(), Eq(1));
79 EXPECT_THAT(bob.stats_collections_count(), Eq(1));
80
81 poller.UnregisterParticipantInCall("bob");
82 poller.PollStatsAndNotifyObservers();
83
84 EXPECT_THAT(alice.stats_collections_count(), Eq(2));
85 EXPECT_THAT(bob.stats_collections_count(), Eq(1));
86 }
87
88 } // namespace
89 } // namespace webrtc_pc_e2e
90 } // namespace webrtc
91