Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: components/ukm/ukm_service_unittest.cc

Issue 2727013002: Add UKM API GetNewSourceID() (Closed)
Patch Set: fix comment Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/ukm/ukm_service.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/ukm/ukm_service.h" 5 #include "components/ukm/ukm_service.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/hash.h" 10 #include "base/hash.h"
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 } 93 }
94 94
95 TEST_F(UkmServiceTest, PersistAndPurge) { 95 TEST_F(UkmServiceTest, PersistAndPurge) {
96 UkmService service(&prefs_, &client_); 96 UkmService service(&prefs_, &client_);
97 EXPECT_EQ(GetPersistedLogCount(), 0); 97 EXPECT_EQ(GetPersistedLogCount(), 0);
98 service.Initialize(); 98 service.Initialize();
99 task_runner_->RunUntilIdle(); 99 task_runner_->RunUntilIdle();
100 service.EnableRecording(); 100 service.EnableRecording();
101 service.EnableReporting(); 101 service.EnableReporting();
102 102
103 int32_t id = 1; 103 int32_t id = UkmService::GetNewSourceID();
104 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); 104 service.UpdateSourceURL(id, GURL("https://google.com/foobar"));
105 // Should init, generate a log, and start an upload for source. 105 // Should init, generate a log, and start an upload for source.
106 task_runner_->RunPendingTasks(); 106 task_runner_->RunPendingTasks();
107 EXPECT_TRUE(client_.uploader()->is_uploading()); 107 EXPECT_TRUE(client_.uploader()->is_uploading());
108 // Flushes the generated log to disk and generates a new entry. 108 // Flushes the generated log to disk and generates a new entry.
109 { 109 {
110 std::unique_ptr<UkmEntryBuilder> builder = 110 std::unique_ptr<UkmEntryBuilder> builder =
111 service.GetEntryBuilder(id, "PageLoad"); 111 service.GetEntryBuilder(id, "PageLoad");
112 builder->AddMetric("FirstContentfulPaint", 300); 112 builder->AddMetric("FirstContentfulPaint", 300);
113 } 113 }
114 service.Flush(); 114 service.Flush();
115 EXPECT_EQ(GetPersistedLogCount(), 2); 115 EXPECT_EQ(GetPersistedLogCount(), 2);
116 service.Purge(); 116 service.Purge();
117 EXPECT_EQ(GetPersistedLogCount(), 0); 117 EXPECT_EQ(GetPersistedLogCount(), 0);
118 } 118 }
119 119
120 TEST_F(UkmServiceTest, SourceSerialization) { 120 TEST_F(UkmServiceTest, SourceSerialization) {
121 UkmService service(&prefs_, &client_); 121 UkmService service(&prefs_, &client_);
122 EXPECT_EQ(GetPersistedLogCount(), 0); 122 EXPECT_EQ(GetPersistedLogCount(), 0);
123 service.Initialize(); 123 service.Initialize();
124 task_runner_->RunUntilIdle(); 124 task_runner_->RunUntilIdle();
125 service.EnableRecording(); 125 service.EnableRecording();
126 service.EnableReporting(); 126 service.EnableReporting();
127 127
128 int32_t id = 1; 128 int32_t id = UkmService::GetNewSourceID();
129 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); 129 service.UpdateSourceURL(id, GURL("https://google.com/foobar"));
130 130
131 service.Flush(); 131 service.Flush();
132 EXPECT_EQ(GetPersistedLogCount(), 1); 132 EXPECT_EQ(GetPersistedLogCount(), 1);
133 133
134 Report proto_report = GetPersistedReport(); 134 Report proto_report = GetPersistedReport();
135 EXPECT_EQ(1, proto_report.sources_size()); 135 EXPECT_EQ(1, proto_report.sources_size());
136 const Source& proto_source = proto_report.sources(0); 136 const Source& proto_source = proto_report.sources(0);
137 137
138 EXPECT_EQ(id, proto_source.id()); 138 EXPECT_EQ(id, proto_source.id());
139 EXPECT_EQ(GURL("https://google.com/foobar").spec(), proto_source.url()); 139 EXPECT_EQ(GURL("https://google.com/foobar").spec(), proto_source.url());
140 } 140 }
141 141
142 TEST_F(UkmServiceTest, EntryBuilderAndSerialization) { 142 TEST_F(UkmServiceTest, EntryBuilderAndSerialization) {
143 UkmService service(&prefs_, &client_); 143 UkmService service(&prefs_, &client_);
144 EXPECT_EQ(0, GetPersistedLogCount()); 144 EXPECT_EQ(0, GetPersistedLogCount());
145 service.Initialize(); 145 service.Initialize();
146 task_runner_->RunUntilIdle(); 146 task_runner_->RunUntilIdle();
147 service.EnableRecording(); 147 service.EnableRecording();
148 service.EnableReporting(); 148 service.EnableReporting();
149 149
150 int32_t id = 1; 150 int32_t id = UkmService::GetNewSourceID();
151 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); 151 service.UpdateSourceURL(id, GURL("https://google.com/foobar"));
152 { 152 {
153 std::unique_ptr<UkmEntryBuilder> foo_builder = 153 std::unique_ptr<UkmEntryBuilder> foo_builder =
154 service.GetEntryBuilder(id, "foo"); 154 service.GetEntryBuilder(id, "foo");
155 foo_builder->AddMetric("foo_start", 0); 155 foo_builder->AddMetric("foo_start", 0);
156 foo_builder->AddMetric("foo_end", 10); 156 foo_builder->AddMetric("foo_end", 10);
157 157
158 std::unique_ptr<UkmEntryBuilder> bar_builder = 158 std::unique_ptr<UkmEntryBuilder> bar_builder =
159 service.GetEntryBuilder(id, "bar"); 159 service.GetEntryBuilder(id, "bar");
160 bar_builder->AddMetric("bar_start", 5); 160 bar_builder->AddMetric("bar_start", 5);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 } 203 }
204 204
205 TEST_F(UkmServiceTest, AddEntryOnlyWithNonEmptyMetrics) { 205 TEST_F(UkmServiceTest, AddEntryOnlyWithNonEmptyMetrics) {
206 UkmService service(&prefs_, &client_); 206 UkmService service(&prefs_, &client_);
207 EXPECT_EQ(0, GetPersistedLogCount()); 207 EXPECT_EQ(0, GetPersistedLogCount());
208 service.Initialize(); 208 service.Initialize();
209 task_runner_->RunUntilIdle(); 209 task_runner_->RunUntilIdle();
210 service.EnableRecording(); 210 service.EnableRecording();
211 service.EnableReporting(); 211 service.EnableReporting();
212 212
213 int32_t id = 1; 213 int32_t id = UkmService::GetNewSourceID();
214 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); 214 service.UpdateSourceURL(id, GURL("https://google.com/foobar"));
215 215
216 { 216 {
217 std::unique_ptr<UkmEntryBuilder> builder = 217 std::unique_ptr<UkmEntryBuilder> builder =
218 service.GetEntryBuilder(id, "PageLoad"); 218 service.GetEntryBuilder(id, "PageLoad");
219 } 219 }
220 service.Flush(); 220 service.Flush();
221 EXPECT_EQ(1, GetPersistedLogCount()); 221 EXPECT_EQ(1, GetPersistedLogCount());
222 Report proto_report = GetPersistedReport(); 222 Report proto_report = GetPersistedReport();
223 EXPECT_EQ(0, proto_report.entries_size()); 223 EXPECT_EQ(0, proto_report.entries_size());
(...skipping 18 matching lines...) Expand all
242 242
243 service.Initialize(); 243 service.Initialize();
244 244
245 // Providers have not supplied system profile information yet. 245 // Providers have not supplied system profile information yet.
246 EXPECT_FALSE(provider->provide_system_profile_metrics_called()); 246 EXPECT_FALSE(provider->provide_system_profile_metrics_called());
247 247
248 task_runner_->RunUntilIdle(); 248 task_runner_->RunUntilIdle();
249 service.EnableRecording(); 249 service.EnableRecording();
250 service.EnableReporting(); 250 service.EnableReporting();
251 251
252 int32_t id = 1; 252 int32_t id = UkmService::GetNewSourceID();
253 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); 253 service.UpdateSourceURL(id, GURL("https://google.com/foobar"));
254 { 254 {
255 std::unique_ptr<UkmEntryBuilder> builder = 255 std::unique_ptr<UkmEntryBuilder> builder =
256 service.GetEntryBuilder(id, "PageLoad"); 256 service.GetEntryBuilder(id, "PageLoad");
257 builder->AddMetric("FirstContentfulPaint", 300); 257 builder->AddMetric("FirstContentfulPaint", 300);
258 } 258 }
259 service.Flush(); 259 service.Flush();
260 EXPECT_EQ(GetPersistedLogCount(), 1); 260 EXPECT_EQ(GetPersistedLogCount(), 1);
261 261
262 Report proto_report = GetPersistedReport(); 262 Report proto_report = GetPersistedReport();
(...skipping 11 matching lines...) Expand all
274 task_runner_->RunUntilIdle(); 274 task_runner_->RunUntilIdle();
275 service.EnableRecording(); 275 service.EnableRecording();
276 service.EnableReporting(); 276 service.EnableReporting();
277 277
278 EXPECT_TRUE(task_runner_->HasPendingTask()); 278 EXPECT_TRUE(task_runner_->HasPendingTask());
279 // Neither rotation or Flush should generate logs 279 // Neither rotation or Flush should generate logs
280 task_runner_->RunPendingTasks(); 280 task_runner_->RunPendingTasks();
281 service.Flush(); 281 service.Flush();
282 EXPECT_EQ(GetPersistedLogCount(), 0); 282 EXPECT_EQ(GetPersistedLogCount(), 0);
283 283
284 int32_t id = 1; 284 int32_t id = UkmService::GetNewSourceID();
285 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); 285 service.UpdateSourceURL(id, GURL("https://google.com/foobar"));
286 // Includes a Source, so will persist. 286 // Includes a Source, so will persist.
287 service.Flush(); 287 service.Flush();
288 EXPECT_EQ(GetPersistedLogCount(), 1); 288 EXPECT_EQ(GetPersistedLogCount(), 1);
289 289
290 { 290 {
291 std::unique_ptr<UkmEntryBuilder> builder = 291 std::unique_ptr<UkmEntryBuilder> builder =
292 service.GetEntryBuilder(id, "PageLoad"); 292 service.GetEntryBuilder(id, "PageLoad");
293 builder->AddMetric("FirstPaint", 300); 293 builder->AddMetric("FirstPaint", 300);
294 } 294 }
295 // Includes an Entry, so will persist. 295 // Includes an Entry, so will persist.
296 service.Flush(); 296 service.Flush();
297 EXPECT_EQ(GetPersistedLogCount(), 2); 297 EXPECT_EQ(GetPersistedLogCount(), 2);
298 298
299 service.UpdateSourceURL(id, GURL("https://google.com/foobar")); 299 service.UpdateSourceURL(id, GURL("https://google.com/foobar"));
300 { 300 {
301 std::unique_ptr<UkmEntryBuilder> builder = 301 std::unique_ptr<UkmEntryBuilder> builder =
302 service.GetEntryBuilder(id, "PageLoad"); 302 service.GetEntryBuilder(id, "PageLoad");
303 builder->AddMetric("FirstContentfulPaint", 300); 303 builder->AddMetric("FirstContentfulPaint", 300);
304 } 304 }
305 // Includes a Source and an Entry, so will persist. 305 // Includes a Source and an Entry, so will persist.
306 service.Flush(); 306 service.Flush();
307 EXPECT_EQ(GetPersistedLogCount(), 3); 307 EXPECT_EQ(GetPersistedLogCount(), 3);
308 308
309 // Current log has no Sources. 309 // Current log has no Sources.
310 service.Flush(); 310 service.Flush();
311 EXPECT_EQ(GetPersistedLogCount(), 3); 311 EXPECT_EQ(GetPersistedLogCount(), 3);
312 } 312 }
313 313
314 TEST_F(UkmServiceTest, GetNewSourceID) {
315 int32_t id1 = UkmService::GetNewSourceID();
316 int32_t id2 = UkmService::GetNewSourceID();
317 int32_t id3 = UkmService::GetNewSourceID();
318 EXPECT_NE(id1, id2);
319 EXPECT_NE(id1, id3);
320 EXPECT_NE(id2, id3);
321 }
322
314 } // namespace ukm 323 } // namespace ukm
OLDNEW
« no previous file with comments | « components/ukm/ukm_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698