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"
20 TEST(RapporServiceTest
, Update
) {
21 TestRapporService rappor_service
;
22 EXPECT_LT(base::TimeDelta(), rappor_service
.next_rotation());
23 EXPECT_TRUE(rappor_service
.test_uploader()->is_running());
25 rappor_service
.Update(RECORDING_DISABLED
, false);
26 EXPECT_EQ(base::TimeDelta(), rappor_service
.next_rotation());
27 EXPECT_FALSE(rappor_service
.test_uploader()->is_running());
29 rappor_service
.Update(FINE_LEVEL
, false);
30 EXPECT_LT(base::TimeDelta(), rappor_service
.next_rotation());
31 EXPECT_FALSE(rappor_service
.test_uploader()->is_running());
33 rappor_service
.Update(COARSE_LEVEL
, true);
34 EXPECT_LT(base::TimeDelta(), rappor_service
.next_rotation());
35 EXPECT_TRUE(rappor_service
.test_uploader()->is_running());
38 // Check that samples can be recorded and exported.
39 TEST(RapporServiceTest
, RecordAndExportMetrics
) {
40 TestRapporService rappor_service
;
42 // Multiple samples for the same metric should only generate one report.
43 rappor_service
.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE
, "foo");
44 rappor_service
.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE
, "bar");
46 RapporReports reports
;
47 rappor_service
.GetReports(&reports
);
48 EXPECT_EQ(1, reports
.report_size());
50 const RapporReports::Report
& report
= reports
.report(0);
51 EXPECT_TRUE(report
.name_hash());
52 // ETLD_PLUS_ONE_RAPPOR_TYPE has 128 bits
53 EXPECT_EQ(16u, report
.bits().size());
56 // Check that the reporting level is respected.
57 TEST(RapporServiceTest
, RecordingLevel
) {
58 TestRapporService rappor_service
;
59 rappor_service
.Update(COARSE_LEVEL
, false);
61 // ETLD_PLUS_ONE_RAPPOR_TYPE is a FINE_LEVEL metric
62 rappor_service
.RecordSample("FineMetric", ETLD_PLUS_ONE_RAPPOR_TYPE
, "foo");
64 RapporReports reports
;
65 rappor_service
.GetReports(&reports
);
66 EXPECT_EQ(0, reports
.report_size());
69 // Check that GetRecordedSampleForMetric works as expected.
70 TEST(RapporServiceTest
, GetRecordedSampleForMetric
) {
71 TestRapporService rappor_service
;
73 // Multiple samples for the same metric; only the latest is remembered.
74 rappor_service
.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE
, "foo");
75 rappor_service
.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE
, "bar");
80 rappor_service
.GetRecordedSampleForMetric("WrongMetric", &sample
, &type
));
82 rappor_service
.GetRecordedSampleForMetric("MyMetric", &sample
, &type
));
83 EXPECT_EQ("bar", sample
);
84 EXPECT_EQ(ETLD_PLUS_ONE_RAPPOR_TYPE
, type
);
87 // Check that the incognito is respected.
88 TEST(RapporServiceTest
, Incognito
) {
89 TestRapporService rappor_service
;
90 rappor_service
.set_is_incognito(true);
92 rappor_service
.RecordSample("MyMetric", COARSE_RAPPOR_TYPE
, "foo");
94 RapporReports reports
;
95 rappor_service
.GetReports(&reports
);
96 EXPECT_EQ(0, reports
.report_size());
99 // Check that Sample objects record correctly.
100 TEST(RapporServiceTest
, RecordSample
) {
101 TestRapporService rappor_service
;
102 scoped_ptr
<Sample
> sample
= rappor_service
.CreateSample(COARSE_RAPPOR_TYPE
);
103 sample
->SetStringField("Url", "example.com");
104 sample
->SetFlagsField("Flags1", 0xbcd, 12);
105 rappor_service
.RecordSampleObj("ObjMetric", sample
.Pass());
106 uint64_t url_hash
= metrics::HashMetricName("ObjMetric.Url");
107 uint64_t flags_hash
= metrics::HashMetricName("ObjMetric.Flags1");
108 RapporReports reports
;
109 rappor_service
.GetReports(&reports
);
110 EXPECT_EQ(2, reports
.report_size());
111 size_t url_index
= reports
.report(0).name_hash() == url_hash
? 0 : 1;
112 size_t flags_index
= url_index
== 0 ? 1 : 0;
113 EXPECT_EQ(url_hash
, reports
.report(url_index
).name_hash());
114 EXPECT_EQ(1u, reports
.report(url_index
).bits().size());
115 EXPECT_EQ(flags_hash
, reports
.report(flags_index
).name_hash());
116 EXPECT_EQ(2u, reports
.report(flags_index
).bits().size());
119 } // namespace rappor