OLD | NEW |
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" | 9 #include "components/metrics/proto/ukm/report.pb.h" |
10 #include "components/metrics/proto/ukm/source.pb.h" | 10 #include "components/metrics/proto/ukm/source.pb.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 prefs_.ClearPref(prefs::kUkmPersistedLogs); | 30 prefs_.ClearPref(prefs::kUkmPersistedLogs); |
31 } | 31 } |
32 | 32 |
33 int GetPersistedLogCount() { | 33 int GetPersistedLogCount() { |
34 const base::ListValue* list_value = | 34 const base::ListValue* list_value = |
35 prefs_.GetList(prefs::kUkmPersistedLogs); | 35 prefs_.GetList(prefs::kUkmPersistedLogs); |
36 return list_value->GetSize(); | 36 return list_value->GetSize(); |
37 } | 37 } |
38 | 38 |
39 Report GetPersistedReport() { | 39 Report GetPersistedReport() { |
| 40 EXPECT_GE(GetPersistedLogCount(), 1); |
40 metrics::PersistedLogs result_persisted_logs( | 41 metrics::PersistedLogs result_persisted_logs( |
41 base::MakeUnique<ukm::PersistedLogsMetricsImpl>(), &prefs_, | 42 base::MakeUnique<ukm::PersistedLogsMetricsImpl>(), &prefs_, |
42 prefs::kUkmPersistedLogs, | 43 prefs::kUkmPersistedLogs, |
43 3, // log count limit | 44 3, // log count limit |
44 1000, // byte limit | 45 1000, // byte limit |
45 0); | 46 0); |
46 | 47 |
47 result_persisted_logs.DeserializeLogs(); | 48 result_persisted_logs.DeserializeLogs(); |
48 result_persisted_logs.StageLog(); | 49 result_persisted_logs.StageLog(); |
49 | 50 |
50 std::string uncompressed_log_data; | 51 std::string uncompressed_log_data; |
51 EXPECT_TRUE(compression::GzipUncompress(result_persisted_logs.staged_log(), | 52 EXPECT_TRUE(compression::GzipUncompress(result_persisted_logs.staged_log(), |
52 &uncompressed_log_data)); | 53 &uncompressed_log_data)); |
53 | 54 |
54 Report report; | 55 Report report; |
55 EXPECT_TRUE(report.ParseFromString(uncompressed_log_data)); | 56 EXPECT_TRUE(report.ParseFromString(uncompressed_log_data)); |
56 return report; | 57 return report; |
57 } | 58 } |
58 | 59 |
| 60 std::unique_ptr<UkmSource> MakeSource(std::string url, int paint_msec) { |
| 61 auto source = base::MakeUnique<UkmSource>(); |
| 62 source->set_committed_url(GURL(url)); |
| 63 source->set_first_contentful_paint( |
| 64 base::TimeDelta::FromMilliseconds(paint_msec)); |
| 65 return source; |
| 66 } |
| 67 |
59 protected: | 68 protected: |
60 TestingPrefServiceSimple prefs_; | 69 TestingPrefServiceSimple prefs_; |
61 metrics::TestMetricsServiceClient client_; | 70 metrics::TestMetricsServiceClient client_; |
62 | 71 |
63 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 72 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
64 base::ThreadTaskRunnerHandle task_runner_handle_; | 73 base::ThreadTaskRunnerHandle task_runner_handle_; |
65 | 74 |
66 private: | 75 private: |
67 DISALLOW_COPY_AND_ASSIGN(UkmServiceTest); | 76 DISALLOW_COPY_AND_ASSIGN(UkmServiceTest); |
68 }; | 77 }; |
69 | 78 |
70 } // namespace | 79 } // namespace |
71 | 80 |
72 TEST_F(UkmServiceTest, EnableDisableSchedule) { | 81 TEST_F(UkmServiceTest, EnableDisableSchedule) { |
73 UkmService service(&prefs_, &client_); | 82 UkmService service(&prefs_, &client_); |
74 EXPECT_FALSE(task_runner_->HasPendingTask()); | 83 EXPECT_FALSE(task_runner_->HasPendingTask()); |
75 service.Initialize(); | 84 service.Initialize(); |
76 EXPECT_TRUE(task_runner_->HasPendingTask()); | 85 EXPECT_TRUE(task_runner_->HasPendingTask()); |
77 // Allow initialization to complete. | 86 // Allow initialization to complete. |
78 task_runner_->RunUntilIdle(); | 87 task_runner_->RunUntilIdle(); |
| 88 service.EnableRecording(); |
79 service.EnableReporting(); | 89 service.EnableReporting(); |
80 EXPECT_TRUE(task_runner_->HasPendingTask()); | 90 EXPECT_TRUE(task_runner_->HasPendingTask()); |
81 service.DisableReporting(); | 91 service.DisableReporting(); |
82 task_runner_->RunPendingTasks(); | 92 task_runner_->RunPendingTasks(); |
83 EXPECT_FALSE(task_runner_->HasPendingTask()); | 93 EXPECT_FALSE(task_runner_->HasPendingTask()); |
84 } | 94 } |
85 | 95 |
86 TEST_F(UkmServiceTest, PersistAndPurge) { | 96 TEST_F(UkmServiceTest, PersistAndPurge) { |
87 UkmService service(&prefs_, &client_); | 97 UkmService service(&prefs_, &client_); |
88 EXPECT_EQ(GetPersistedLogCount(), 0); | 98 EXPECT_EQ(GetPersistedLogCount(), 0); |
89 service.Initialize(); | 99 service.Initialize(); |
90 task_runner_->RunUntilIdle(); | 100 task_runner_->RunUntilIdle(); |
| 101 service.EnableRecording(); |
91 service.EnableReporting(); | 102 service.EnableReporting(); |
| 103 service.RecordSource(MakeSource("https://google.com", 300)); |
92 // Should init, generate a log, and start an upload. | 104 // Should init, generate a log, and start an upload. |
93 task_runner_->RunPendingTasks(); | 105 task_runner_->RunPendingTasks(); |
94 EXPECT_TRUE(client_.uploader()->is_uploading()); | 106 EXPECT_TRUE(client_.uploader()->is_uploading()); |
95 // Flushes the generated log to disk and generates a new one. | 107 // Flushes the generated log to disk and generates a new one. |
| 108 service.RecordSource(MakeSource("https://google.com", 300)); |
96 service.Flush(); | 109 service.Flush(); |
97 EXPECT_EQ(GetPersistedLogCount(), 2); | 110 EXPECT_EQ(GetPersistedLogCount(), 2); |
98 service.Purge(); | 111 service.Purge(); |
99 EXPECT_EQ(GetPersistedLogCount(), 0); | 112 EXPECT_EQ(GetPersistedLogCount(), 0); |
100 } | 113 } |
101 | 114 |
102 TEST_F(UkmServiceTest, SourceSerialization) { | 115 TEST_F(UkmServiceTest, SourceSerialization) { |
103 UkmService service(&prefs_, &client_); | 116 UkmService service(&prefs_, &client_); |
104 EXPECT_EQ(GetPersistedLogCount(), 0); | 117 EXPECT_EQ(GetPersistedLogCount(), 0); |
105 service.Initialize(); | 118 service.Initialize(); |
106 task_runner_->RunUntilIdle(); | 119 task_runner_->RunUntilIdle(); |
| 120 service.EnableRecording(); |
107 service.EnableReporting(); | 121 service.EnableReporting(); |
108 | 122 |
109 std::unique_ptr<UkmSource> source = base::WrapUnique(new UkmSource()); | 123 service.RecordSource(MakeSource("https://google.com", 300)); |
110 source->set_committed_url(GURL("https://google.com")); | |
111 source->set_first_contentful_paint(base::TimeDelta::FromMilliseconds(300)); | |
112 | |
113 service.RecordSource(std::move(source)); | |
114 | 124 |
115 service.Flush(); | 125 service.Flush(); |
116 EXPECT_EQ(GetPersistedLogCount(), 1); | 126 EXPECT_EQ(GetPersistedLogCount(), 1); |
117 | 127 |
118 Report proto_report = GetPersistedReport(); | 128 Report proto_report = GetPersistedReport(); |
119 EXPECT_EQ(1, proto_report.sources_size()); | 129 EXPECT_EQ(1, proto_report.sources_size()); |
120 const Source& proto_source = proto_report.sources(0); | 130 const Source& proto_source = proto_report.sources(0); |
121 | 131 |
122 EXPECT_EQ(GURL("https://google.com").spec(), proto_source.url()); | 132 EXPECT_EQ(GURL("https://google.com").spec(), proto_source.url()); |
123 EXPECT_EQ(300, proto_source.first_contentful_paint_msec()); | 133 EXPECT_EQ(300, proto_source.first_contentful_paint_msec()); |
124 } | 134 } |
125 | 135 |
126 } // namespace ukm | 136 } // namespace ukm |
OLD | NEW |