blob: a3c5439c5efc2c5f1567fbf4f359c1312ff64cb4 [file] [log] [blame]
[email protected]064107e2014-05-02 00:59:061// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]cac267c2011-09-29 15:18:102// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]064107e2014-05-02 00:59:065#include "components/metrics/metrics_log_manager.h"
[email protected]cfee9aa52013-10-19 17:53:056
[email protected]fe58acc22012-02-29 01:29:587#include <string>
8#include <utility>
9#include <vector>
10
[email protected]7f07db62014-05-15 01:12:4511#include "base/prefs/pref_registry_simple.h"
12#include "base/prefs/testing_pref_service.h"
[email protected]bfb77b52014-06-07 01:54:0113#include "components/metrics/metrics_log.h"
[email protected]7f07db62014-05-15 01:12:4514#include "components/metrics/metrics_pref_names.h"
[email protected]bfb77b52014-06-07 01:54:0115#include "components/metrics/test_metrics_service_client.h"
[email protected]cac267c2011-09-29 15:18:1016#include "testing/gtest/include/gtest/gtest.h"
17
[email protected]064107e2014-05-02 00:59:0618namespace metrics {
19
[email protected]cac267c2011-09-29 15:18:1020namespace {
[email protected]fe58acc22012-02-29 01:29:5821
[email protected]cac267c2011-09-29 15:18:1022// Dummy serializer that just stores logs in memory.
[email protected]7f07db62014-05-15 01:12:4523class TestLogPrefService : public TestingPrefServiceSimple {
[email protected]cac267c2011-09-29 15:18:1024 public:
[email protected]7f07db62014-05-15 01:12:4525 TestLogPrefService() {
26 registry()->RegisterListPref(prefs::kMetricsInitialLogs);
27 registry()->RegisterListPref(prefs::kMetricsOngoingLogs);
[email protected]cac267c2011-09-29 15:18:1028 }
[email protected]bfb77b52014-06-07 01:54:0129
[email protected]cac267c2011-09-29 15:18:1030 // Returns the number of logs of the given type.
[email protected]bfb77b52014-06-07 01:54:0131 size_t TypeCount(MetricsLog::LogType log_type) {
[email protected]7f07db62014-05-15 01:12:4532 int list_length = 0;
[email protected]bfb77b52014-06-07 01:54:0133 if (log_type == MetricsLog::INITIAL_STABILITY_LOG)
[email protected]7f07db62014-05-15 01:12:4534 list_length = GetList(prefs::kMetricsInitialLogs)->GetSize();
35 else
36 list_length = GetList(prefs::kMetricsOngoingLogs)->GetSize();
[email protected]9706e1b2014-06-11 16:31:2437 return list_length / 2;
[email protected]cac267c2011-09-29 15:18:1038 }
[email protected]cac267c2011-09-29 15:18:1039};
[email protected]fe58acc22012-02-29 01:29:5840
41} // namespace
[email protected]cac267c2011-09-29 15:18:1042
43TEST(MetricsLogManagerTest, StandardFlow) {
[email protected]bfb77b52014-06-07 01:54:0144 TestMetricsServiceClient client;
[email protected]7f07db62014-05-15 01:12:4545 TestLogPrefService pref_service;
46 MetricsLogManager log_manager(&pref_service, 0);
[email protected]cac267c2011-09-29 15:18:1047
48 // Make sure a new manager has a clean slate.
49 EXPECT_EQ(NULL, log_manager.current_log());
50 EXPECT_FALSE(log_manager.has_staged_log());
51 EXPECT_FALSE(log_manager.has_unsent_logs());
52
53 // Check that the normal flow works.
[email protected]bfb77b52014-06-07 01:54:0154 MetricsLog* initial_log = new MetricsLog(
55 "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service);
[email protected]09dee82d2014-05-22 14:00:5356 log_manager.BeginLoggingWithLog(make_scoped_ptr(initial_log));
[email protected]cac267c2011-09-29 15:18:1057 EXPECT_EQ(initial_log, log_manager.current_log());
58 EXPECT_FALSE(log_manager.has_staged_log());
59
[email protected]29948262012-03-01 12:15:0860 log_manager.FinishCurrentLog();
[email protected]cac267c2011-09-29 15:18:1061 EXPECT_EQ(NULL, log_manager.current_log());
[email protected]29948262012-03-01 12:15:0862 EXPECT_TRUE(log_manager.has_unsent_logs());
63 EXPECT_FALSE(log_manager.has_staged_log());
[email protected]cac267c2011-09-29 15:18:1064
[email protected]bfb77b52014-06-07 01:54:0165 MetricsLog* second_log =
66 new MetricsLog("id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service);
[email protected]09dee82d2014-05-22 14:00:5367 log_manager.BeginLoggingWithLog(make_scoped_ptr(second_log));
[email protected]cac267c2011-09-29 15:18:1068 EXPECT_EQ(second_log, log_manager.current_log());
[email protected]29948262012-03-01 12:15:0869
70 log_manager.StageNextLogForUpload();
[email protected]cac267c2011-09-29 15:18:1071 EXPECT_TRUE(log_manager.has_staged_log());
[email protected]7f07db62014-05-15 01:12:4572 EXPECT_FALSE(log_manager.staged_log().empty());
[email protected]cac267c2011-09-29 15:18:1073
[email protected]5f3e1642013-05-05 03:37:3474 log_manager.DiscardStagedLog();
[email protected]cac267c2011-09-29 15:18:1075 EXPECT_EQ(second_log, log_manager.current_log());
76 EXPECT_FALSE(log_manager.has_staged_log());
[email protected]29948262012-03-01 12:15:0877 EXPECT_FALSE(log_manager.has_unsent_logs());
[email protected]cac267c2011-09-29 15:18:1078
79 EXPECT_FALSE(log_manager.has_unsent_logs());
80}
81
82TEST(MetricsLogManagerTest, AbandonedLog) {
[email protected]bfb77b52014-06-07 01:54:0183 TestMetricsServiceClient client;
[email protected]7f07db62014-05-15 01:12:4584 TestLogPrefService pref_service;
85 MetricsLogManager log_manager(&pref_service, 0);
[email protected]cac267c2011-09-29 15:18:1086
[email protected]bfb77b52014-06-07 01:54:0187 MetricsLog* dummy_log = new MetricsLog(
88 "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service);
[email protected]09dee82d2014-05-22 14:00:5389 log_manager.BeginLoggingWithLog(make_scoped_ptr(dummy_log));
[email protected]cac267c2011-09-29 15:18:1090 EXPECT_EQ(dummy_log, log_manager.current_log());
91
92 log_manager.DiscardCurrentLog();
93 EXPECT_EQ(NULL, log_manager.current_log());
94 EXPECT_FALSE(log_manager.has_staged_log());
95}
96
97TEST(MetricsLogManagerTest, InterjectedLog) {
[email protected]bfb77b52014-06-07 01:54:0198 TestMetricsServiceClient client;
[email protected]7f07db62014-05-15 01:12:4599 TestLogPrefService pref_service;
100 MetricsLogManager log_manager(&pref_service, 0);
[email protected]cac267c2011-09-29 15:18:10101
[email protected]bfb77b52014-06-07 01:54:01102 MetricsLog* ongoing_log =
103 new MetricsLog("id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service);
104 MetricsLog* temp_log = new MetricsLog(
105 "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service);
[email protected]cac267c2011-09-29 15:18:10106
[email protected]09dee82d2014-05-22 14:00:53107 log_manager.BeginLoggingWithLog(make_scoped_ptr(ongoing_log));
[email protected]cac267c2011-09-29 15:18:10108 EXPECT_EQ(ongoing_log, log_manager.current_log());
109
110 log_manager.PauseCurrentLog();
111 EXPECT_EQ(NULL, log_manager.current_log());
112
[email protected]09dee82d2014-05-22 14:00:53113 log_manager.BeginLoggingWithLog(make_scoped_ptr(temp_log));
[email protected]cac267c2011-09-29 15:18:10114 EXPECT_EQ(temp_log, log_manager.current_log());
[email protected]29948262012-03-01 12:15:08115 log_manager.FinishCurrentLog();
[email protected]cac267c2011-09-29 15:18:10116 EXPECT_EQ(NULL, log_manager.current_log());
[email protected]cac267c2011-09-29 15:18:10117
118 log_manager.ResumePausedLog();
119 EXPECT_EQ(ongoing_log, log_manager.current_log());
[email protected]cac267c2011-09-29 15:18:10120
[email protected]29948262012-03-01 12:15:08121 EXPECT_FALSE(log_manager.has_staged_log());
122 log_manager.StageNextLogForUpload();
[email protected]5f3e1642013-05-05 03:37:34123 log_manager.DiscardStagedLog();
[email protected]cac267c2011-09-29 15:18:10124 EXPECT_FALSE(log_manager.has_unsent_logs());
125}
126
[email protected]aa752c3f2012-04-27 22:31:51127TEST(MetricsLogManagerTest, InterjectedLogPreservesType) {
[email protected]bfb77b52014-06-07 01:54:01128 TestMetricsServiceClient client;
[email protected]7f07db62014-05-15 01:12:45129 TestLogPrefService pref_service;
130 MetricsLogManager log_manager(&pref_service, 0);
[email protected]80a8f312013-12-16 18:00:30131 log_manager.LoadPersistedUnsentLogs();
[email protected]aa752c3f2012-04-27 22:31:51132
[email protected]bfb77b52014-06-07 01:54:01133 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
134 "id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service)));
[email protected]aa752c3f2012-04-27 22:31:51135 log_manager.PauseCurrentLog();
[email protected]bfb77b52014-06-07 01:54:01136 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
137 "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service)));
[email protected]aa752c3f2012-04-27 22:31:51138 log_manager.FinishCurrentLog();
139 log_manager.ResumePausedLog();
140 log_manager.StageNextLogForUpload();
[email protected]5f3e1642013-05-05 03:37:34141 log_manager.DiscardStagedLog();
[email protected]aa752c3f2012-04-27 22:31:51142
143 // Verify that the remaining log (which is the original ongoing log) still
144 // has the right type.
[email protected]aa752c3f2012-04-27 22:31:51145 log_manager.FinishCurrentLog();
146 log_manager.PersistUnsentLogs();
[email protected]bfb77b52014-06-07 01:54:01147 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
148 EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]aa752c3f2012-04-27 22:31:51149}
150
[email protected]cac267c2011-09-29 15:18:10151TEST(MetricsLogManagerTest, StoreAndLoad) {
[email protected]bfb77b52014-06-07 01:54:01152 TestMetricsServiceClient client;
[email protected]7f07db62014-05-15 01:12:45153 TestLogPrefService pref_service;
[email protected]cac267c2011-09-29 15:18:10154 // Set up some in-progress logging in a scoped log manager simulating the
155 // leadup to quitting, then persist as would be done on quit.
156 {
[email protected]7f07db62014-05-15 01:12:45157 MetricsLogManager log_manager(&pref_service, 0);
[email protected]80a8f312013-12-16 18:00:30158 log_manager.LoadPersistedUnsentLogs();
159
[email protected]cac267c2011-09-29 15:18:10160 // Simulate a log having already been unsent from a previous session.
[email protected]7f07db62014-05-15 01:12:45161 {
162 std::string log("proto");
[email protected]f61623e2014-08-19 16:25:57163 PersistedLogs ongoing_logs(&pref_service, prefs::kMetricsOngoingLogs, 1,
164 1, 0);
[email protected]9706e1b2014-06-11 16:31:24165 ongoing_logs.StoreLog(log);
[email protected]7f07db62014-05-15 01:12:45166 ongoing_logs.SerializeLogs();
167 }
[email protected]bfb77b52014-06-07 01:54:01168 EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]cac267c2011-09-29 15:18:10169 EXPECT_FALSE(log_manager.has_unsent_logs());
170 log_manager.LoadPersistedUnsentLogs();
171 EXPECT_TRUE(log_manager.has_unsent_logs());
172
[email protected]bfb77b52014-06-07 01:54:01173 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
174 "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service)));
[email protected]29948262012-03-01 12:15:08175 log_manager.FinishCurrentLog();
[email protected]bfb77b52014-06-07 01:54:01176 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
177 "id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service)));
[email protected]29948262012-03-01 12:15:08178 log_manager.StageNextLogForUpload();
[email protected]29948262012-03-01 12:15:08179 log_manager.FinishCurrentLog();
[email protected]cac267c2011-09-29 15:18:10180
181 // Nothing should be written out until PersistUnsentLogs is called.
[email protected]bfb77b52014-06-07 01:54:01182 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
183 EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]cac267c2011-09-29 15:18:10184 log_manager.PersistUnsentLogs();
[email protected]bfb77b52014-06-07 01:54:01185 EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
186 EXPECT_EQ(2U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]cac267c2011-09-29 15:18:10187 }
188
189 // Now simulate the relaunch, ensure that the log manager restores
190 // everything correctly, and verify that once the are handled they are not
191 // re-persisted.
192 {
[email protected]7f07db62014-05-15 01:12:45193 MetricsLogManager log_manager(&pref_service, 0);
[email protected]cac267c2011-09-29 15:18:10194 log_manager.LoadPersistedUnsentLogs();
195 EXPECT_TRUE(log_manager.has_unsent_logs());
196
[email protected]29948262012-03-01 12:15:08197 log_manager.StageNextLogForUpload();
[email protected]5f3e1642013-05-05 03:37:34198 log_manager.DiscardStagedLog();
[email protected]cac267c2011-09-29 15:18:10199 // The initial log should be sent first; update the persisted storage to
200 // verify.
201 log_manager.PersistUnsentLogs();
[email protected]bfb77b52014-06-07 01:54:01202 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
203 EXPECT_EQ(2U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]cac267c2011-09-29 15:18:10204
205 // Handle the first ongoing log.
[email protected]29948262012-03-01 12:15:08206 log_manager.StageNextLogForUpload();
[email protected]5f3e1642013-05-05 03:37:34207 log_manager.DiscardStagedLog();
[email protected]cac267c2011-09-29 15:18:10208 EXPECT_TRUE(log_manager.has_unsent_logs());
209
210 // Handle the last log.
[email protected]29948262012-03-01 12:15:08211 log_manager.StageNextLogForUpload();
[email protected]5f3e1642013-05-05 03:37:34212 log_manager.DiscardStagedLog();
[email protected]cac267c2011-09-29 15:18:10213 EXPECT_FALSE(log_manager.has_unsent_logs());
214
215 // Nothing should have changed "on disk" since PersistUnsentLogs hasn't been
216 // called again.
[email protected]bfb77b52014-06-07 01:54:01217 EXPECT_EQ(2U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]cac267c2011-09-29 15:18:10218 // Persist, and make sure nothing is left.
219 log_manager.PersistUnsentLogs();
[email protected]bfb77b52014-06-07 01:54:01220 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
221 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]cac267c2011-09-29 15:18:10222 }
223}
224
[email protected]f829cc362012-03-10 10:09:16225TEST(MetricsLogManagerTest, StoreStagedLogTypes) {
[email protected]bfb77b52014-06-07 01:54:01226 TestMetricsServiceClient client;
227
[email protected]f829cc362012-03-10 10:09:16228 // Ensure that types are preserved when storing staged logs.
229 {
[email protected]7f07db62014-05-15 01:12:45230 TestLogPrefService pref_service;
231 MetricsLogManager log_manager(&pref_service, 0);
[email protected]80a8f312013-12-16 18:00:30232 log_manager.LoadPersistedUnsentLogs();
[email protected]f829cc362012-03-10 10:09:16233
[email protected]bfb77b52014-06-07 01:54:01234 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
235 "id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service)));
[email protected]f829cc362012-03-10 10:09:16236 log_manager.FinishCurrentLog();
237 log_manager.StageNextLogForUpload();
[email protected]f829cc362012-03-10 10:09:16238 log_manager.PersistUnsentLogs();
239
[email protected]bfb77b52014-06-07 01:54:01240 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
241 EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]f829cc362012-03-10 10:09:16242 }
243
244 {
[email protected]7f07db62014-05-15 01:12:45245 TestLogPrefService pref_service;
246 MetricsLogManager log_manager(&pref_service, 0);
[email protected]80a8f312013-12-16 18:00:30247 log_manager.LoadPersistedUnsentLogs();
[email protected]f829cc362012-03-10 10:09:16248
[email protected]bfb77b52014-06-07 01:54:01249 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
250 "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service)));
[email protected]f829cc362012-03-10 10:09:16251 log_manager.FinishCurrentLog();
252 log_manager.StageNextLogForUpload();
[email protected]f829cc362012-03-10 10:09:16253 log_manager.PersistUnsentLogs();
254
[email protected]bfb77b52014-06-07 01:54:01255 EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
256 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]f829cc362012-03-10 10:09:16257 }
258}
259
[email protected]cac267c2011-09-29 15:18:10260TEST(MetricsLogManagerTest, LargeLogDiscarding) {
[email protected]bfb77b52014-06-07 01:54:01261 TestMetricsServiceClient client;
[email protected]7f07db62014-05-15 01:12:45262 TestLogPrefService pref_service;
[email protected]cac267c2011-09-29 15:18:10263 // Set the size threshold very low, to verify that it's honored.
[email protected]7f07db62014-05-15 01:12:45264 MetricsLogManager log_manager(&pref_service, 1);
265 log_manager.LoadPersistedUnsentLogs();
[email protected]cac267c2011-09-29 15:18:10266
[email protected]bfb77b52014-06-07 01:54:01267 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
268 "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service)));
[email protected]29948262012-03-01 12:15:08269 log_manager.FinishCurrentLog();
[email protected]bfb77b52014-06-07 01:54:01270 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
271 "id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service)));
[email protected]29948262012-03-01 12:15:08272 log_manager.FinishCurrentLog();
[email protected]cac267c2011-09-29 15:18:10273
274 // Only the ongoing log should be written out, due to the threshold.
275 log_manager.PersistUnsentLogs();
[email protected]bfb77b52014-06-07 01:54:01276 EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
277 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]cac267c2011-09-29 15:18:10278}
[email protected]e7508d82012-05-03 15:59:53279
[email protected]faa8d222014-07-25 21:30:26280TEST(MetricsLogManagerTest, DiscardOrder) {
281 // Ensure that the correct log is discarded if new logs are pushed while
282 // a log is staged.
[email protected]bfb77b52014-06-07 01:54:01283 TestMetricsServiceClient client;
[email protected]e7508d82012-05-03 15:59:53284 {
[email protected]7f07db62014-05-15 01:12:45285 TestLogPrefService pref_service;
286 MetricsLogManager log_manager(&pref_service, 0);
[email protected]80a8f312013-12-16 18:00:30287 log_manager.LoadPersistedUnsentLogs();
288
[email protected]bfb77b52014-06-07 01:54:01289 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
290 "id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &pref_service)));
[email protected]e7508d82012-05-03 15:59:53291 log_manager.FinishCurrentLog();
[email protected]bfb77b52014-06-07 01:54:01292 log_manager.BeginLoggingWithLog(make_scoped_ptr(new MetricsLog(
293 "id", 0, MetricsLog::ONGOING_LOG, &client, &pref_service)));
[email protected]e7508d82012-05-03 15:59:53294 log_manager.StageNextLogForUpload();
[email protected]e7508d82012-05-03 15:59:53295 log_manager.FinishCurrentLog();
[email protected]faa8d222014-07-25 21:30:26296 log_manager.DiscardStagedLog();
[email protected]e7508d82012-05-03 15:59:53297
[email protected]e7508d82012-05-03 15:59:53298 log_manager.PersistUnsentLogs();
[email protected]bfb77b52014-06-07 01:54:01299 EXPECT_EQ(0U, pref_service.TypeCount(MetricsLog::INITIAL_STABILITY_LOG));
300 EXPECT_EQ(1U, pref_service.TypeCount(MetricsLog::ONGOING_LOG));
[email protected]e7508d82012-05-03 15:59:53301 }
302}
303
[email protected]064107e2014-05-02 00:59:06304} // namespace metrics