Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 <tuple> |
| 12 | |
| 13 | #include "api/fakemetricsobserver.h" |
| 14 | #include "api/peerconnectionproxy.h" |
| 15 | #include "media/base/fakemediaengine.h" |
| 16 | #include "pc/mediasession.h" |
| 17 | #include "pc/peerconnection.h" |
| 18 | #include "pc/peerconnectionfactory.h" |
| 19 | #include "pc/peerconnectionwrapper.h" |
| 20 | #include "pc/sdputils.h" |
| 21 | #ifdef WEBRTC_ANDROID |
| 22 | #include "pc/test/androidtestinitializer.h" |
| 23 | #endif |
| 24 | #include "pc/test/fakesctptransport.h" |
| 25 | #include "rtc_base/gunit.h" |
| 26 | #include "rtc_base/ptr_util.h" |
| 27 | #include "rtc_base/virtualsocketserver.h" |
| 28 | |
| 29 | namespace webrtc { |
| 30 | |
| 31 | using RTCConfiguration = PeerConnectionInterface::RTCConfiguration; |
| 32 | using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions; |
| 33 | using ::testing::Values; |
| 34 | |
| 35 | static constexpr int kDefaultTimeout = 10000; |
| 36 | |
| 37 | int MakeUsageFingerprint(std::set<PeerConnection::UsageEvent> events) { |
| 38 | int signature = 0; |
| 39 | for (const auto it : events) { |
| 40 | signature |= static_cast<int>(it); |
| 41 | } |
| 42 | return signature; |
| 43 | } |
| 44 | |
| 45 | class PeerConnectionFactoryForUsageHistogramTest |
| 46 | : public rtc::RefCountedObject<PeerConnectionFactory> { |
| 47 | public: |
| 48 | PeerConnectionFactoryForUsageHistogramTest() |
| 49 | : rtc::RefCountedObject<PeerConnectionFactory>( |
| 50 | rtc::Thread::Current(), |
| 51 | rtc::Thread::Current(), |
| 52 | rtc::Thread::Current(), |
| 53 | rtc::MakeUnique<cricket::FakeMediaEngine>(), |
| 54 | CreateCallFactory(), |
| 55 | nullptr) {} |
| 56 | |
| 57 | void ActionsBeforeInitializeForTesting(PeerConnectionInterface* pc) override { |
| 58 | PeerConnection* internal_pc = static_cast<PeerConnection*>(pc); |
| 59 | if (return_histogram_very_quickly_) { |
| 60 | internal_pc->ReturnHistogramVeryQuicklyForTesting(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void ReturnHistogramVeryQuickly() { return_histogram_very_quickly_ = true; } |
| 65 | |
| 66 | private: |
| 67 | bool return_histogram_very_quickly_; |
| 68 | }; |
| 69 | |
| 70 | class PeerConnectionWrapperForUsageHistogramTest |
| 71 | : public PeerConnectionWrapper { |
| 72 | public: |
| 73 | using PeerConnectionWrapper::PeerConnectionWrapper; |
| 74 | |
| 75 | PeerConnection* GetInternalPeerConnection() { |
| 76 | auto* pci = |
| 77 | static_cast<PeerConnectionProxyWithInternal<PeerConnectionInterface>*>( |
| 78 | pc()); |
| 79 | return static_cast<PeerConnection*>(pci->internal()); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | class PeerConnectionUsageHistogramTest : public ::testing::Test { |
| 84 | protected: |
| 85 | typedef std::unique_ptr<PeerConnectionWrapperForUsageHistogramTest> |
| 86 | WrapperPtr; |
| 87 | |
| 88 | PeerConnectionUsageHistogramTest() |
| 89 | : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) { |
| 90 | #ifdef WEBRTC_ANDROID |
| 91 | InitializeAndroidObjects(); |
| 92 | #endif |
| 93 | } |
| 94 | |
| 95 | WrapperPtr CreatePeerConnection() { |
| 96 | return CreatePeerConnection( |
| 97 | RTCConfiguration(), PeerConnectionFactoryInterface::Options(), false); |
| 98 | } |
| 99 | |
| 100 | WrapperPtr CreatePeerConnectionWithImmediateReport() { |
| 101 | return CreatePeerConnection( |
| 102 | RTCConfiguration(), PeerConnectionFactoryInterface::Options(), true); |
| 103 | } |
| 104 | |
| 105 | WrapperPtr CreatePeerConnection( |
| 106 | const RTCConfiguration& config, |
| 107 | const PeerConnectionFactoryInterface::Options factory_options, |
| 108 | bool immediate_report) { |
| 109 | rtc::scoped_refptr<PeerConnectionFactoryForUsageHistogramTest> pc_factory( |
| 110 | new PeerConnectionFactoryForUsageHistogramTest()); |
| 111 | pc_factory->SetOptions(factory_options); |
| 112 | RTC_CHECK(pc_factory->Initialize()); |
| 113 | if (immediate_report) { |
| 114 | pc_factory->ReturnHistogramVeryQuickly(); |
| 115 | } |
| 116 | auto observer = rtc::MakeUnique<MockPeerConnectionObserver>(); |
| 117 | auto pc = pc_factory->CreatePeerConnection(config, nullptr, nullptr, |
| 118 | observer.get()); |
| 119 | if (!pc) { |
| 120 | return nullptr; |
| 121 | } |
| 122 | |
| 123 | auto wrapper = rtc::MakeUnique<PeerConnectionWrapperForUsageHistogramTest>( |
| 124 | pc_factory, pc, std::move(observer)); |
| 125 | return wrapper; |
| 126 | } |
| 127 | |
| 128 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 129 | rtc::AutoSocketServerThread main_; |
| 130 | }; |
| 131 | |
| 132 | TEST_F(PeerConnectionUsageHistogramTest, UsageFingerprintHistogramFromTimeout) { |
| 133 | auto pc = CreatePeerConnectionWithImmediateReport(); |
| 134 | |
| 135 | // Register UMA observer before signaling begins. |
| 136 | rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer = |
| 137 | new rtc::RefCountedObject<webrtc::FakeMetricsObserver>(); |
| 138 | pc->GetInternalPeerConnection()->RegisterUMAObserver(caller_observer); |
| 139 | int expected_fingerprint = MakeUsageFingerprint({}); |
| 140 | ASSERT_TRUE_WAIT(caller_observer->ExpectOnlySingleEnumCount( |
| 141 | webrtc::kEnumCounterUsagePattern, expected_fingerprint), |
| 142 | kDefaultTimeout); |
| 143 | } |
| 144 | |
| 145 | } // namespace webrtc |