Add metrics::Samples to facilitate easier testing

Currently, tests that verify metrics use a combination of
metrics::NumSamples and metrics::NumEvents to assert which samples
were recorded and how many times they were recorded. This means
that a comprehensive tests has n + 1 assertions for n distinct
samples.

The new metrics::Samples function returns a map of sample --> num
events which can be asserted against using gmock matchers,
achieving better coverage and better test failure messages in just
one line.

Bug: None
Change-Id: I07d4a766654cfc04e414b77b6de02927683a361f
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/125486
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Steve Anton <steveanton@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26974}
diff --git a/pc/peer_connection_rtp_unittest.cc b/pc/peer_connection_rtp_unittest.cc
index cf00138..48c14e3 100644
--- a/pc/peer_connection_rtp_unittest.cc
+++ b/pc/peer_connection_rtp_unittest.cc
@@ -64,6 +64,7 @@
 
 using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
 using ::testing::ElementsAre;
+using ::testing::Pair;
 using ::testing::UnorderedElementsAre;
 using ::testing::Values;
 
@@ -1606,11 +1607,8 @@
   EXPECT_EQ(cricket::kMsidSignalingMediaSection,
             answer->description()->msid_signaling());
   // Check that this is counted correctly
-  EXPECT_EQ(2, webrtc::metrics::NumSamples(
-                   "WebRTC.PeerConnection.SdpSemanticNegotiated"));
-  EXPECT_EQ(2, webrtc::metrics::NumEvents(
-                   "WebRTC.PeerConnection.SdpSemanticNegotiated",
-                   kSdpSemanticNegotiatedUnifiedPlan));
+  EXPECT_THAT(metrics::Samples("WebRTC.PeerConnection.SdpSemanticNegotiated"),
+              ElementsAre(Pair(kSdpSemanticNegotiatedUnifiedPlan, 2)));
 }
 
 TEST_F(PeerConnectionMsidSignalingTest, PlanBOfferToUnifiedPlanAnswer) {
@@ -1698,11 +1696,8 @@
 
   ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOffer()));
   // Note that only the callee does ReportSdpFormatReceived.
-  EXPECT_EQ(1, webrtc::metrics::NumSamples(
-                   "WebRTC.PeerConnection.SdpFormatReceived"));
-  EXPECT_EQ(
-      1, webrtc::metrics::NumEvents("WebRTC.PeerConnection.SdpFormatReceived",
-                                    kSdpFormatReceivedNoTracks));
+  EXPECT_THAT(metrics::Samples("WebRTC.PeerConnection.SdpFormatReceived"),
+              ElementsAre(Pair(kSdpFormatReceivedNoTracks, 1)));
 }
 #endif  // HAVE_SCTP
 
@@ -1714,11 +1709,8 @@
 
   ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOffer()));
   // Note that only the callee does ReportSdpFormatReceived.
-  EXPECT_EQ(1, webrtc::metrics::NumSamples(
-                   "WebRTC.PeerConnection.SdpFormatReceived"));
-  EXPECT_EQ(
-      1, webrtc::metrics::NumEvents("WebRTC.PeerConnection.SdpFormatReceived",
-                                    kSdpFormatReceivedSimple));
+  EXPECT_THAT(metrics::Samples("WebRTC.PeerConnection.SdpFormatReceived"),
+              ElementsAre(Pair(kSdpFormatReceivedSimple, 1)));
 }
 
 TEST_F(SdpFormatReceivedTest, SimplePlanBIsReportedAsSimple) {
@@ -1727,12 +1719,9 @@
   auto callee = CreatePeerConnectionWithUnifiedPlan();
 
   ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOffer()));
-
-  EXPECT_EQ(1, webrtc::metrics::NumSamples(
-                   "WebRTC.PeerConnection.SdpFormatReceived"));
-  EXPECT_EQ(
-      1, webrtc::metrics::NumEvents("WebRTC.PeerConnection.SdpFormatReceived",
-                                    kSdpFormatReceivedSimple));
+  // Note that only the callee does ReportSdpFormatReceived.
+  EXPECT_THAT(metrics::Samples("WebRTC.PeerConnection.SdpFormatReceived"),
+              ElementsAre(Pair(kSdpFormatReceivedSimple, 1)));
 }
 
 TEST_F(SdpFormatReceivedTest, ComplexUnifiedIsReportedAsComplexUnifiedPlan) {
@@ -1744,11 +1733,8 @@
 
   ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOffer()));
   // Note that only the callee does ReportSdpFormatReceived.
-  EXPECT_EQ(1, webrtc::metrics::NumSamples(
-                   "WebRTC.PeerConnection.SdpFormatReceived"));
-  EXPECT_EQ(
-      1, webrtc::metrics::NumEvents("WebRTC.PeerConnection.SdpFormatReceived",
-                                    kSdpFormatReceivedComplexUnifiedPlan));
+  EXPECT_THAT(metrics::Samples("WebRTC.PeerConnection.SdpFormatReceived"),
+              ElementsAre(Pair(kSdpFormatReceivedComplexUnifiedPlan, 1)));
 }
 
 TEST_F(SdpFormatReceivedTest, ComplexPlanBIsReportedAsComplexPlanB) {
@@ -1762,11 +1748,8 @@
   // SDP Format to be recorded.
   ASSERT_FALSE(callee->SetRemoteDescription(caller->CreateOffer()));
   // Note that only the callee does ReportSdpFormatReceived.
-  EXPECT_EQ(1, webrtc::metrics::NumSamples(
-                   "WebRTC.PeerConnection.SdpFormatReceived"));
-  EXPECT_EQ(
-      1, webrtc::metrics::NumEvents("WebRTC.PeerConnection.SdpFormatReceived",
-                                    kSdpFormatReceivedComplexPlanB));
+  EXPECT_THAT(metrics::Samples("WebRTC.PeerConnection.SdpFormatReceived"),
+              ElementsAre(Pair(kSdpFormatReceivedComplexPlanB, 1)));
 }
 
 // Sender setups in a call.