Roll src/third_party/WebKit a2aeeb7:6373c4f (svn 198318:198336)
[chromium-blink-merge.git] / components / rappor / rappor_service_unittest.cc
blob3b38c1af01a88e710cbdfc0609d116a710b75de6
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/rappor/rappor_service.h"
7 #include "base/base64.h"
8 #include "base/prefs/testing_pref_service.h"
9 #include "components/metrics/metrics_hashes.h"
10 #include "components/rappor/byte_vector_utils.h"
11 #include "components/rappor/proto/rappor_metric.pb.h"
12 #include "components/rappor/rappor_parameters.h"
13 #include "components/rappor/rappor_pref_names.h"
14 #include "components/rappor/test_log_uploader.h"
15 #include "components/rappor/test_rappor_service.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace rappor {
20 TEST(RapporServiceTest, Update) {
21 // Test rappor service initially has uploading and reporting enabled.
22 TestRapporService rappor_service;
23 EXPECT_LT(base::TimeDelta(), rappor_service.next_rotation());
24 EXPECT_TRUE(rappor_service.test_uploader()->is_running());
26 // Disabling both should stop both uploads and reports.
27 rappor_service.Update(0, false);
28 EXPECT_EQ(base::TimeDelta(), rappor_service.next_rotation());
29 EXPECT_FALSE(rappor_service.test_uploader()->is_running());
31 // Some recording, but no reporting.
32 rappor_service.Update(UMA_RAPPOR_GROUP, false);
33 // Reports generation should still be scheduled.
34 EXPECT_LT(base::TimeDelta(), rappor_service.next_rotation());
35 EXPECT_FALSE(rappor_service.test_uploader()->is_running());
37 // Some recording and reporting enabled.
38 rappor_service.Update(SAFEBROWSING_RAPPOR_GROUP, true);
39 EXPECT_LT(base::TimeDelta(), rappor_service.next_rotation());
40 EXPECT_TRUE(rappor_service.test_uploader()->is_running());
43 // Check that samples can be recorded and exported.
44 TEST(RapporServiceTest, RecordAndExportMetrics) {
45 TestRapporService rappor_service;
47 // Multiple samples for the same metric should only generate one report.
48 rappor_service.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, "foo");
49 rappor_service.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, "bar");
51 RapporReports reports;
52 rappor_service.GetReports(&reports);
53 EXPECT_EQ(1, reports.report_size());
55 const RapporReports::Report& report = reports.report(0);
56 EXPECT_TRUE(report.name_hash());
57 // ETLD_PLUS_ONE_RAPPOR_TYPE has 128 bits
58 EXPECT_EQ(16u, report.bits().size());
61 // Check that the reporting groups are respected.
62 TEST(RapporServiceTest, UmaRecordingGroup) {
63 TestRapporService rappor_service;
64 rappor_service.Update(SAFEBROWSING_RAPPOR_GROUP, false);
66 // Wrong recording group.
67 rappor_service.RecordSample("UmaMetric", UMA_RAPPOR_TYPE, "foo");
69 RapporReports reports;
70 rappor_service.GetReports(&reports);
71 EXPECT_EQ(0, reports.report_size());
74 // Check that the reporting groups are respected.
75 TEST(RapporServiceTest, SafeBrowsingRecordingGroup) {
76 TestRapporService rappor_service;
77 rappor_service.Update(UMA_RAPPOR_GROUP, false);
79 // Wrong recording group.
80 rappor_service.RecordSample("SbMetric", SAFEBROWSING_RAPPOR_TYPE, "foo");
82 RapporReports reports;
83 rappor_service.GetReports(&reports);
84 EXPECT_EQ(0, reports.report_size());
87 // Check that GetRecordedSampleForMetric works as expected.
88 TEST(RapporServiceTest, GetRecordedSampleForMetric) {
89 TestRapporService rappor_service;
91 // Multiple samples for the same metric; only the latest is remembered.
92 rappor_service.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, "foo");
93 rappor_service.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, "bar");
95 std::string sample;
96 RapporType type;
97 EXPECT_FALSE(
98 rappor_service.GetRecordedSampleForMetric("WrongMetric", &sample, &type));
99 EXPECT_TRUE(
100 rappor_service.GetRecordedSampleForMetric("MyMetric", &sample, &type));
101 EXPECT_EQ("bar", sample);
102 EXPECT_EQ(ETLD_PLUS_ONE_RAPPOR_TYPE, type);
105 // Check that the incognito is respected.
106 TEST(RapporServiceTest, Incognito) {
107 TestRapporService rappor_service;
108 rappor_service.set_is_incognito(true);
110 rappor_service.RecordSample("MyMetric", SAFEBROWSING_RAPPOR_TYPE, "foo");
112 RapporReports reports;
113 rappor_service.GetReports(&reports);
114 EXPECT_EQ(0, reports.report_size());
117 // Check that Sample objects record correctly.
118 TEST(RapporServiceTest, RecordSample) {
119 TestRapporService rappor_service;
120 scoped_ptr<Sample> sample =
121 rappor_service.CreateSample(SAFEBROWSING_RAPPOR_TYPE);
122 sample->SetStringField("Url", "example.com");
123 sample->SetFlagsField("Flags1", 0xbcd, 12);
124 rappor_service.RecordSampleObj("ObjMetric", sample.Pass());
125 uint64_t url_hash = metrics::HashMetricName("ObjMetric.Url");
126 uint64_t flags_hash = metrics::HashMetricName("ObjMetric.Flags1");
127 RapporReports reports;
128 rappor_service.GetReports(&reports);
129 EXPECT_EQ(2, reports.report_size());
130 size_t url_index = reports.report(0).name_hash() == url_hash ? 0 : 1;
131 size_t flags_index = url_index == 0 ? 1 : 0;
132 EXPECT_EQ(url_hash, reports.report(url_index).name_hash());
133 EXPECT_EQ(1u, reports.report(url_index).bits().size());
134 EXPECT_EQ(flags_hash, reports.report(flags_index).name_hash());
135 EXPECT_EQ(2u, reports.report(flags_index).bits().size());
138 } // namespace rappor