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/test_stats_dictionary.h" | |
6 | |
7 #include <memory> | |
8 #include <set> | |
9 #include <vector> | |
10 | |
11 #include "base/json/json_reader.h" | |
12 #include "base/logging.h" | |
13 #include "base/macros.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "base/values.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace content { | |
19 | |
20 namespace { | |
21 | |
22 const char kTestStatsReportJson[] = | |
23 "{\n" | |
24 " \"GarbageA\": {\n" | |
25 " \"id\": \"GarbageA\",\n" | |
26 " \"timestamp\": 0.0,\n" | |
27 " \"type\": \"garbage\"\n" | |
28 " },\n" | |
29 " \"RTCTestStatsID\": {\n" | |
30 " \"id\": \"RTCTestStatsID\",\n" | |
31 " \"timestamp\": 13.37,\n" | |
32 " \"type\": \"test\",\n" | |
33 " \"boolean\": true,\n" | |
34 " \"number\": 42,\n" | |
35 " \"string\": \"text\",\n" | |
36 " \"sequenceBoolean\": [ true ],\n" | |
37 " \"sequenceNumber\": [ 42 ],\n" | |
38 " \"sequenceString\": [ \"text\" ]\n" | |
39 " },\n" | |
40 " \"GarbageB\": {\n" | |
41 " \"id\": \"GarbageB\",\n" | |
42 " \"timestamp\": 0.0,\n" | |
43 " \"type\": \"garbage\"\n" | |
44 " }\n" | |
45 "}"; | |
46 | |
47 class TestStatsDictionaryTest : public testing::Test { | |
48 public: | |
49 TestStatsDictionaryTest() { | |
50 std::unique_ptr<base::Value> value = | |
51 base::JSONReader::Read(kTestStatsReportJson); | |
52 CHECK(value); | |
53 base::DictionaryValue* dictionary; | |
54 CHECK(value->GetAsDictionary(&dictionary)); | |
55 ignore_result(value.release()); | |
56 report_ = new TestStatsReportDictionary( | |
57 std::unique_ptr<base::DictionaryValue>(dictionary)); | |
58 } | |
59 | |
60 protected: | |
61 scoped_refptr<TestStatsReportDictionary> report_; | |
62 }; | |
63 | |
64 TEST_F(TestStatsDictionaryTest, ReportGetStats) { | |
65 EXPECT_FALSE(report_->Get("InvalidID")); | |
66 EXPECT_TRUE(report_->Get("GarbageA")); | |
67 EXPECT_TRUE(report_->Get("RTCTestStatsID")); | |
68 EXPECT_TRUE(report_->Get("GarbageB")); | |
69 } | |
70 | |
71 TEST_F(TestStatsDictionaryTest, ReportForEach) { | |
72 std::set<std::string> remaining; | |
73 remaining.insert("GarbageA"); | |
74 remaining.insert("RTCTestStatsID"); | |
75 remaining.insert("GarbageB"); | |
76 report_->ForEach([&remaining](const TestStatsDictionary& stats) { | |
77 remaining.erase(stats.GetString("id")); | |
78 }); | |
79 EXPECT_TRUE(remaining.empty()); | |
80 } | |
81 | |
82 TEST_F(TestStatsDictionaryTest, ReportFilterStats) { | |
83 std::vector<TestStatsDictionary> filtered_stats = report_->Filter( | |
84 [](const TestStatsDictionary& stats) -> bool { | |
85 return false; | |
86 }); | |
87 EXPECT_EQ(filtered_stats.size(), 0u); | |
88 | |
89 filtered_stats = report_->Filter( | |
90 [](const TestStatsDictionary& stats) -> bool { | |
91 return true; | |
92 }); | |
93 EXPECT_EQ(filtered_stats.size(), 3u); | |
94 | |
95 filtered_stats = report_->Filter( | |
96 [](const TestStatsDictionary& stats) -> bool { | |
97 return stats.GetString("id") == "RTCTestStatsID"; | |
98 }); | |
99 EXPECT_EQ(filtered_stats.size(), 1u); | |
100 } | |
101 | |
102 TEST_F(TestStatsDictionaryTest, ReportGetAll) { | |
103 std::set<std::string> remaining; | |
104 remaining.insert("GarbageA"); | |
105 remaining.insert("RTCTestStatsID"); | |
106 remaining.insert("GarbageB"); | |
107 for (const TestStatsDictionary& stats : report_->GetAll()) { | |
108 remaining.erase(stats.GetString("id")); | |
109 } | |
110 EXPECT_TRUE(remaining.empty()); | |
111 } | |
112 | |
113 TEST_F(TestStatsDictionaryTest, ReportGetByType) { | |
114 std::vector<TestStatsDictionary> stats = report_->GetByType("garbage"); | |
115 EXPECT_EQ(stats.size(), 2u); | |
116 std::set<std::string> remaining; | |
117 remaining.insert("GarbageA"); | |
118 remaining.insert("GarbageB"); | |
119 report_->ForEach([&remaining](const TestStatsDictionary& stats) { | |
120 remaining.erase(stats.GetString("id")); | |
121 }); | |
122 EXPECT_TRUE(remaining.empty()); | |
123 } | |
124 | |
125 TEST_F(TestStatsDictionaryTest, StatsVerifyMembers) { | |
126 std::unique_ptr<TestStatsDictionary> stats = report_->Get("RTCTestStatsID"); | |
127 EXPECT_TRUE(stats); | |
128 | |
129 EXPECT_FALSE(stats->IsBoolean("nonexistentMember")); | |
130 EXPECT_FALSE(stats->IsNumber("nonexistentMember")); | |
131 EXPECT_FALSE(stats->IsString("nonexistentMember")); | |
132 EXPECT_FALSE(stats->IsSequenceBoolean("nonexistentMember")); | |
133 EXPECT_FALSE(stats->IsSequenceNumber("nonexistentMember")); | |
134 EXPECT_FALSE(stats->IsSequenceString("nonexistentMember")); | |
135 | |
136 ASSERT_TRUE(stats->IsBoolean("boolean")); | |
137 EXPECT_EQ(stats->GetBoolean("boolean"), true); | |
138 | |
139 ASSERT_TRUE(stats->IsNumber("number")); | |
140 EXPECT_EQ(stats->GetNumber("number"), 42.0); | |
141 | |
142 ASSERT_TRUE(stats->IsString("string")); | |
143 EXPECT_EQ(stats->GetString("string"), "text"); | |
144 | |
145 ASSERT_TRUE(stats->IsSequenceBoolean("sequenceBoolean")); | |
146 EXPECT_EQ(stats->GetSequenceBoolean("sequenceBoolean"), | |
147 std::vector<bool> { true }); | |
148 | |
149 ASSERT_TRUE(stats->IsSequenceNumber("sequenceNumber")); | |
150 EXPECT_EQ(stats->GetSequenceNumber("sequenceNumber"), | |
151 std::vector<double> { 42.0 }); | |
152 | |
153 ASSERT_TRUE(stats->IsSequenceString("sequenceString")); | |
154 EXPECT_EQ(stats->GetSequenceString("sequenceString"), | |
155 std::vector<std::string> { "text" }); | |
156 } | |
157 | |
158 TEST_F(TestStatsDictionaryTest, TestStatsDictionaryShouldKeepReportAlive) { | |
159 std::unique_ptr<TestStatsDictionary> stats = report_->Get("RTCTestStatsID"); | |
160 EXPECT_TRUE(stats); | |
161 report_ = nullptr; | |
162 EXPECT_EQ(stats->GetString("string"), "text"); | |
163 } | |
164 | |
165 } // namespace | |
166 | |
167 } // namespace content | |
OLD | NEW |