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

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

Issue 2617883004: Initial UKMPageLoadMetricsObserver (Closed)
Patch Set: Class rename Created 3 years, 11 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 "base/test/test_simple_task_runner.h" 7 #include "base/test/test_simple_task_runner.h"
8 #include "base/threading/thread_task_runner_handle.h" 8 #include "base/threading/thread_task_runner_handle.h"
9 #include "components/metrics/proto/ukm/report.pb.h"
10 #include "components/metrics/proto/ukm/source.pb.h"
9 #include "components/metrics/test_metrics_service_client.h" 11 #include "components/metrics/test_metrics_service_client.h"
10 #include "components/prefs/testing_pref_service.h" 12 #include "components/prefs/testing_pref_service.h"
13 #include "components/ukm/persisted_logs_metrics_impl.h"
11 #include "components/ukm/ukm_pref_names.h" 14 #include "components/ukm/ukm_pref_names.h"
15 #include "components/ukm/ukm_source.h"
12 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/zlib/google/compression_utils.h"
13 18
14 namespace ukm { 19 namespace ukm {
15 20
16 namespace { 21 namespace {
17 22
18 class UkmServiceTest : public testing::Test { 23 class UkmServiceTest : public testing::Test {
19 public: 24 public:
20 UkmServiceTest() 25 UkmServiceTest()
21 : task_runner_(new base::TestSimpleTaskRunner), 26 : task_runner_(new base::TestSimpleTaskRunner),
22 task_runner_handle_(task_runner_) { 27 task_runner_handle_(task_runner_) {
23 UkmService::RegisterPrefs(prefs_.registry()); 28 UkmService::RegisterPrefs(prefs_.registry());
24 prefs_.ClearPref(prefs::kUkmClientId); 29 prefs_.ClearPref(prefs::kUkmClientId);
25 prefs_.ClearPref(prefs::kUkmPersistedLogs); 30 prefs_.ClearPref(prefs::kUkmPersistedLogs);
26 } 31 }
27 32
28 int GetPersistedLogCount() { 33 int GetPersistedLogCount() {
29 const base::ListValue* list_value = 34 const base::ListValue* list_value =
30 prefs_.GetList(prefs::kUkmPersistedLogs); 35 prefs_.GetList(prefs::kUkmPersistedLogs);
31 return list_value->GetSize(); 36 return list_value->GetSize();
32 } 37 }
33 38
39 Report GetPersistedReport() {
40 metrics::PersistedLogs result_persisted_logs(
41 base::MakeUnique<ukm::PersistedLogsMetricsImpl>(), &prefs_,
42 prefs::kUkmPersistedLogs,
43 3, // log count limit
44 1000, // byte limit
45 0);
46
47 result_persisted_logs.DeserializeLogs();
48 result_persisted_logs.StageLog();
49
50 std::string uncompressed_log_data;
51 EXPECT_TRUE(compression::GzipUncompress(result_persisted_logs.staged_log(),
52 &uncompressed_log_data));
53
54 Report report;
55 EXPECT_TRUE(report.ParseFromString(uncompressed_log_data));
56 return report;
57 }
58
34 protected: 59 protected:
35 TestingPrefServiceSimple prefs_; 60 TestingPrefServiceSimple prefs_;
36 metrics::TestMetricsServiceClient client_; 61 metrics::TestMetricsServiceClient client_;
37 62
38 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 63 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
39 base::ThreadTaskRunnerHandle task_runner_handle_; 64 base::ThreadTaskRunnerHandle task_runner_handle_;
40 65
41 private: 66 private:
42 DISALLOW_COPY_AND_ASSIGN(UkmServiceTest); 67 DISALLOW_COPY_AND_ASSIGN(UkmServiceTest);
43 }; 68 };
(...skipping 22 matching lines...) Expand all
66 service.EnableReporting(); 91 service.EnableReporting();
67 // Should init, generate a log, and unsuccessfully attempt an upload. 92 // Should init, generate a log, and unsuccessfully attempt an upload.
68 task_runner_->RunPendingTasks(); 93 task_runner_->RunPendingTasks();
69 // Flushes the generated log to disk and generates a new one. 94 // Flushes the generated log to disk and generates a new one.
70 service.Flush(); 95 service.Flush();
71 EXPECT_EQ(GetPersistedLogCount(), 2); 96 EXPECT_EQ(GetPersistedLogCount(), 2);
72 service.Purge(); 97 service.Purge();
73 EXPECT_EQ(GetPersistedLogCount(), 0); 98 EXPECT_EQ(GetPersistedLogCount(), 0);
74 } 99 }
75 100
101 TEST_F(UkmServiceTest, SourceSerialization) {
102 UkmService service(&prefs_, &client_);
103 EXPECT_EQ(GetPersistedLogCount(), 0);
104 service.Initialize();
105 task_runner_->RunUntilIdle();
106 service.EnableReporting();
107 // Should init, generate a log, and unsuccessfully attempt an upload.
108 task_runner_->RunPendingTasks();
109 service.Purge();
110
111 std::unique_ptr<UkmSource> source = base::WrapUnique(new UkmSource());
112 source->set_committed_url(GURL("https://google.com"));
113 base::Time test_time;
114 source->set_navigation_start(test_time);
115 source->set_first_contentful_paint(base::TimeDelta::FromMilliseconds(300));
116
117 service.RecordSource(std::move(source));
118
119 service.Flush();
120 EXPECT_EQ(GetPersistedLogCount(), 1);
121
122 Report proto_report = GetPersistedReport();
123 EXPECT_EQ(1, proto_report.sources_size());
124 const Source& proto_source = proto_report.sources(0);
125
126 EXPECT_EQ(GURL("https://google.com").spec(), proto_source.url());
127 base::Time navigation_time =
128 base::Time::UnixEpoch() +
129 base::TimeDelta::FromMilliseconds(proto_source.navigation_time_msec());
130 EXPECT_EQ(test_time, navigation_time);
131 EXPECT_EQ(300, proto_source.first_contentful_paint_msec());
132 }
133
76 } // namespace ukm 134 } // namespace ukm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698