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

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

Issue 2653693004: UKM Sync Observer (Closed)
Patch Set: Fix PageLoadMetricsObserverTest Created 3 years, 10 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
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/test/test_simple_task_runner.h" 10 #include "base/test/test_simple_task_runner.h"
(...skipping 23 matching lines...) Expand all
34 prefs_.ClearPref(prefs::kUkmPersistedLogs); 34 prefs_.ClearPref(prefs::kUkmPersistedLogs);
35 } 35 }
36 36
37 int GetPersistedLogCount() { 37 int GetPersistedLogCount() {
38 const base::ListValue* list_value = 38 const base::ListValue* list_value =
39 prefs_.GetList(prefs::kUkmPersistedLogs); 39 prefs_.GetList(prefs::kUkmPersistedLogs);
40 return list_value->GetSize(); 40 return list_value->GetSize();
41 } 41 }
42 42
43 Report GetPersistedReport() { 43 Report GetPersistedReport() {
44 EXPECT_GE(GetPersistedLogCount(), 1);
44 metrics::PersistedLogs result_persisted_logs( 45 metrics::PersistedLogs result_persisted_logs(
45 base::MakeUnique<ukm::PersistedLogsMetricsImpl>(), &prefs_, 46 base::MakeUnique<ukm::PersistedLogsMetricsImpl>(), &prefs_,
46 prefs::kUkmPersistedLogs, 47 prefs::kUkmPersistedLogs,
47 3, // log count limit 48 3, // log count limit
48 1000, // byte limit 49 1000, // byte limit
49 0); 50 0);
50 51
51 result_persisted_logs.DeserializeLogs(); 52 result_persisted_logs.DeserializeLogs();
52 result_persisted_logs.StageLog(); 53 result_persisted_logs.StageLog();
53 54
54 std::string uncompressed_log_data; 55 std::string uncompressed_log_data;
55 EXPECT_TRUE(compression::GzipUncompress(result_persisted_logs.staged_log(), 56 EXPECT_TRUE(compression::GzipUncompress(result_persisted_logs.staged_log(),
56 &uncompressed_log_data)); 57 &uncompressed_log_data));
57 58
58 Report report; 59 Report report;
59 EXPECT_TRUE(report.ParseFromString(uncompressed_log_data)); 60 EXPECT_TRUE(report.ParseFromString(uncompressed_log_data));
60 return report; 61 return report;
61 } 62 }
62 63
64 std::unique_ptr<UkmSource> MakeSource(std::string url, int paint_msec) {
65 auto source = base::MakeUnique<UkmSource>();
66 source->set_committed_url(GURL(url));
67 source->set_first_contentful_paint(
68 base::TimeDelta::FromMilliseconds(paint_msec));
69 return source;
70 }
71
63 protected: 72 protected:
64 TestingPrefServiceSimple prefs_; 73 TestingPrefServiceSimple prefs_;
65 metrics::TestMetricsServiceClient client_; 74 metrics::TestMetricsServiceClient client_;
66 75
67 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 76 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
68 base::ThreadTaskRunnerHandle task_runner_handle_; 77 base::ThreadTaskRunnerHandle task_runner_handle_;
69 78
70 private: 79 private:
71 DISALLOW_COPY_AND_ASSIGN(UkmServiceTest); 80 DISALLOW_COPY_AND_ASSIGN(UkmServiceTest);
72 }; 81 };
73 82
74 } // namespace 83 } // namespace
75 84
76 TEST_F(UkmServiceTest, EnableDisableSchedule) { 85 TEST_F(UkmServiceTest, EnableDisableSchedule) {
77 UkmService service(&prefs_, &client_); 86 UkmService service(&prefs_, &client_);
78 EXPECT_FALSE(task_runner_->HasPendingTask()); 87 EXPECT_FALSE(task_runner_->HasPendingTask());
79 service.Initialize(); 88 service.Initialize();
80 EXPECT_TRUE(task_runner_->HasPendingTask()); 89 EXPECT_TRUE(task_runner_->HasPendingTask());
81 // Allow initialization to complete. 90 // Allow initialization to complete.
82 task_runner_->RunUntilIdle(); 91 task_runner_->RunUntilIdle();
92 service.EnableRecording();
83 service.EnableReporting(); 93 service.EnableReporting();
84 EXPECT_TRUE(task_runner_->HasPendingTask()); 94 EXPECT_TRUE(task_runner_->HasPendingTask());
85 service.DisableReporting(); 95 service.DisableReporting();
86 task_runner_->RunPendingTasks(); 96 task_runner_->RunPendingTasks();
87 EXPECT_FALSE(task_runner_->HasPendingTask()); 97 EXPECT_FALSE(task_runner_->HasPendingTask());
88 } 98 }
89 99
90 TEST_F(UkmServiceTest, PersistAndPurge) { 100 TEST_F(UkmServiceTest, PersistAndPurge) {
91 UkmService service(&prefs_, &client_); 101 UkmService service(&prefs_, &client_);
92 EXPECT_EQ(GetPersistedLogCount(), 0); 102 EXPECT_EQ(GetPersistedLogCount(), 0);
93 service.Initialize(); 103 service.Initialize();
94 task_runner_->RunUntilIdle(); 104 task_runner_->RunUntilIdle();
105 service.EnableRecording();
95 service.EnableReporting(); 106 service.EnableReporting();
107 service.RecordSource(MakeSource("https://google.com", 300));
96 // Should init, generate a log, and start an upload. 108 // Should init, generate a log, and start an upload.
97 task_runner_->RunPendingTasks(); 109 task_runner_->RunPendingTasks();
98 EXPECT_TRUE(client_.uploader()->is_uploading()); 110 EXPECT_TRUE(client_.uploader()->is_uploading());
99 // Flushes the generated log to disk and generates a new one. 111 // Flushes the generated log to disk and generates a new one.
112 service.RecordSource(MakeSource("https://google.com", 300));
100 service.Flush(); 113 service.Flush();
101 EXPECT_EQ(GetPersistedLogCount(), 2); 114 EXPECT_EQ(GetPersistedLogCount(), 2);
102 service.Purge(); 115 service.Purge();
103 EXPECT_EQ(GetPersistedLogCount(), 0); 116 EXPECT_EQ(GetPersistedLogCount(), 0);
104 } 117 }
105 118
106 TEST_F(UkmServiceTest, SourceSerialization) { 119 TEST_F(UkmServiceTest, SourceSerialization) {
107 UkmService service(&prefs_, &client_); 120 UkmService service(&prefs_, &client_);
108 EXPECT_EQ(GetPersistedLogCount(), 0); 121 EXPECT_EQ(GetPersistedLogCount(), 0);
109 service.Initialize(); 122 service.Initialize();
110 task_runner_->RunUntilIdle(); 123 task_runner_->RunUntilIdle();
124 service.EnableRecording();
111 service.EnableReporting(); 125 service.EnableReporting();
112 126
113 std::unique_ptr<UkmSource> source = base::WrapUnique(new UkmSource()); 127 service.RecordSource(MakeSource("https://google.com", 300));
114 source->set_committed_url(GURL("https://google.com"));
115 source->set_first_contentful_paint(base::TimeDelta::FromMilliseconds(300));
116
117 service.RecordSource(std::move(source));
118 128
119 service.Flush(); 129 service.Flush();
120 EXPECT_EQ(GetPersistedLogCount(), 1); 130 EXPECT_EQ(GetPersistedLogCount(), 1);
121 131
122 Report proto_report = GetPersistedReport(); 132 Report proto_report = GetPersistedReport();
123 EXPECT_EQ(1, proto_report.sources_size()); 133 EXPECT_EQ(1, proto_report.sources_size());
124 const Source& proto_source = proto_report.sources(0); 134 const Source& proto_source = proto_report.sources(0);
125 135
126 EXPECT_EQ(GURL("https://google.com").spec(), proto_source.url()); 136 EXPECT_EQ(GURL("https://google.com").spec(), proto_source.url());
127 EXPECT_EQ(300, proto_source.first_contentful_paint_msec()); 137 EXPECT_EQ(300, proto_source.first_contentful_paint_msec());
128 } 138 }
129 139
130 TEST_F(UkmServiceTest, MetricsProviderTest) { 140 TEST_F(UkmServiceTest, MetricsProviderTest) {
131 UkmService service(&prefs_, &client_); 141 UkmService service(&prefs_, &client_);
132 142
133 metrics::TestMetricsProvider* provider = new metrics::TestMetricsProvider(); 143 metrics::TestMetricsProvider* provider = new metrics::TestMetricsProvider();
134 service.RegisterMetricsProvider( 144 service.RegisterMetricsProvider(
135 std::unique_ptr<metrics::MetricsProvider>(provider)); 145 std::unique_ptr<metrics::MetricsProvider>(provider));
136 146
137 service.Initialize(); 147 service.Initialize();
138 148
139 // Providers have not supplied system profile information yet. 149 // Providers have not supplied system profile information yet.
140 EXPECT_FALSE(provider->provide_system_profile_metrics_called()); 150 EXPECT_FALSE(provider->provide_system_profile_metrics_called());
141 151
142 task_runner_->RunUntilIdle(); 152 task_runner_->RunUntilIdle();
153 service.EnableRecording();
143 service.EnableReporting(); 154 service.EnableReporting();
144 155
145 service.RecordSource(base::WrapUnique(new UkmSource())); 156 service.RecordSource(MakeSource("https://google.com", 300));
146 service.Flush(); 157 service.Flush();
147 EXPECT_EQ(GetPersistedLogCount(), 1); 158 EXPECT_EQ(GetPersistedLogCount(), 1);
148 159
149 Report proto_report = GetPersistedReport(); 160 Report proto_report = GetPersistedReport();
150 EXPECT_EQ(1, proto_report.sources_size()); 161 EXPECT_EQ(1, proto_report.sources_size());
151 162
152 // Providers have now supplied system profile information. 163 // Providers have now supplied system profile information.
153 EXPECT_TRUE(provider->provide_system_profile_metrics_called()); 164 EXPECT_TRUE(provider->provide_system_profile_metrics_called());
154 } 165 }
155 166
156 } // namespace ukm 167 } // namespace ukm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698