OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/media/webrtc/rtc_stats_dictionary.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/json/json_reader.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/values.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace content { |
| 18 |
| 19 namespace { |
| 20 |
| 21 const char kTestStatsReportJson[] = |
| 22 "{\n" |
| 23 " \"GarbageA\": {\n" |
| 24 " \"id\": \"GarbageA\",\n" |
| 25 " \"timestamp\": 0.0,\n" |
| 26 " \"type\": \"garbage\"\n" |
| 27 " },\n" |
| 28 " \"RTCTestStatsID\": {\n" |
| 29 " \"id\": \"RTCTestStatsID\",\n" |
| 30 " \"timestamp\": 13.37,\n" |
| 31 " \"type\": \"test\",\n" |
| 32 " \"boolean\": true,\n" |
| 33 " \"number\": 42,\n" |
| 34 " \"string\": \"text\",\n" |
| 35 " \"sequenceBoolean\": [ true ],\n" |
| 36 " \"sequenceNumber\": [ 42 ],\n" |
| 37 " \"sequenceString\": [ \"text\" ]\n" |
| 38 " },\n" |
| 39 " \"GarbageB\": {\n" |
| 40 " \"id\": \"GarbageB\",\n" |
| 41 " \"timestamp\": 0.0,\n" |
| 42 " \"type\": \"garbage\"\n" |
| 43 " }\n" |
| 44 "}"; |
| 45 |
| 46 class RTCStatsDictionaryTest : public testing::Test { |
| 47 public: |
| 48 RTCStatsDictionaryTest() { |
| 49 std::unique_ptr<base::Value> value = |
| 50 base::JSONReader::Read(kTestStatsReportJson); |
| 51 CHECK(value); |
| 52 base::DictionaryValue* dictionary; |
| 53 CHECK(value->GetAsDictionary(&dictionary)); |
| 54 ignore_result(value.release()); |
| 55 report_ = new RTCStatsReportDictionary( |
| 56 std::unique_ptr<base::DictionaryValue>(dictionary)); |
| 57 } |
| 58 |
| 59 protected: |
| 60 scoped_refptr<RTCStatsReportDictionary> report_; |
| 61 }; |
| 62 |
| 63 TEST_F(RTCStatsDictionaryTest, ReportGetStats) { |
| 64 EXPECT_FALSE(report_->Get("InvalidID")); |
| 65 EXPECT_TRUE(report_->Get("GarbageA")); |
| 66 EXPECT_TRUE(report_->Get("RTCTestStatsID")); |
| 67 EXPECT_TRUE(report_->Get("GarbageB")); |
| 68 } |
| 69 |
| 70 TEST_F(RTCStatsDictionaryTest, ReportFilterStats) { |
| 71 std::vector<RTCStatsDictionary> filtered_stats = report_->Filter( |
| 72 [](const RTCStatsDictionary& stats) -> bool { |
| 73 return false; |
| 74 }); |
| 75 EXPECT_EQ(filtered_stats.size(), 0u); |
| 76 |
| 77 filtered_stats = report_->Filter( |
| 78 [](const RTCStatsDictionary& stats) -> bool { |
| 79 return true; |
| 80 }); |
| 81 EXPECT_EQ(filtered_stats.size(), 3u); |
| 82 |
| 83 filtered_stats = report_->Filter( |
| 84 [](const RTCStatsDictionary& stats) -> bool { |
| 85 return stats.GetString("id") == "RTCTestStatsID"; |
| 86 }); |
| 87 EXPECT_EQ(filtered_stats.size(), 1u); |
| 88 } |
| 89 |
| 90 TEST_F(RTCStatsDictionaryTest, StatsVerifyMembers) { |
| 91 std::unique_ptr<RTCStatsDictionary> stats = report_->Get("RTCTestStatsID"); |
| 92 EXPECT_TRUE(stats); |
| 93 |
| 94 EXPECT_FALSE(stats->IsBoolean("nonexistentMember")); |
| 95 EXPECT_FALSE(stats->IsNumber("nonexistentMember")); |
| 96 EXPECT_FALSE(stats->IsString("nonexistentMember")); |
| 97 EXPECT_FALSE(stats->IsSequenceBoolean("nonexistentMember")); |
| 98 EXPECT_FALSE(stats->IsSequenceNumber("nonexistentMember")); |
| 99 EXPECT_FALSE(stats->IsSequenceString("nonexistentMember")); |
| 100 |
| 101 ASSERT_TRUE(stats->IsBoolean("boolean")); |
| 102 EXPECT_EQ(stats->GetBoolean("boolean"), true); |
| 103 |
| 104 ASSERT_TRUE(stats->IsNumber("number")); |
| 105 EXPECT_EQ(stats->GetNumber("number"), 42.0); |
| 106 |
| 107 ASSERT_TRUE(stats->IsString("string")); |
| 108 EXPECT_EQ(stats->GetString("string"), "text"); |
| 109 |
| 110 ASSERT_TRUE(stats->IsSequenceBoolean("sequenceBoolean")); |
| 111 EXPECT_EQ(stats->GetSequenceBoolean("sequenceBoolean"), |
| 112 std::vector<bool> { true }); |
| 113 |
| 114 ASSERT_TRUE(stats->IsSequenceNumber("sequenceNumber")); |
| 115 EXPECT_EQ(stats->GetSequenceNumber("sequenceNumber"), |
| 116 std::vector<double> { 42.0 }); |
| 117 |
| 118 ASSERT_TRUE(stats->IsSequenceString("sequenceString")); |
| 119 EXPECT_EQ(stats->GetSequenceString("sequenceString"), |
| 120 std::vector<std::string> { "text" }); |
| 121 } |
| 122 |
| 123 TEST_F(RTCStatsDictionaryTest, RTCStatsDictionaryShouldKeepReportAlive) { |
| 124 std::unique_ptr<RTCStatsDictionary> stats = report_->Get("RTCTestStatsID"); |
| 125 EXPECT_TRUE(stats); |
| 126 report_ = nullptr; |
| 127 EXPECT_EQ(stats->GetString("string"), "text"); |
| 128 } |
| 129 |
| 130 } // namespace |
| 131 |
| 132 } // namespace content |
OLD | NEW |