Move top_sites_impl.{cc,h} into history component
[chromium-blink-merge.git] / components / rappor / rappor_service_unittest.cc
blobeefdb52ecb6446d498f250d3e09492d4acc72439
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/rappor/byte_vector_utils.h"
10 #include "components/rappor/proto/rappor_metric.pb.h"
11 #include "components/rappor/rappor_parameters.h"
12 #include "components/rappor/rappor_pref_names.h"
13 #include "components/rappor/test_log_uploader.h"
14 #include "components/rappor/test_rappor_service.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace rappor {
19 TEST(RapporServiceTest, Update) {
20 TestRapporService rappor_service;
21 EXPECT_LT(base::TimeDelta(), rappor_service.next_rotation());
22 EXPECT_TRUE(rappor_service.test_uploader()->is_running());
24 rappor_service.Update(RECORDING_DISABLED, false);
25 EXPECT_EQ(base::TimeDelta(), rappor_service.next_rotation());
26 EXPECT_FALSE(rappor_service.test_uploader()->is_running());
28 rappor_service.Update(FINE_LEVEL, false);
29 EXPECT_LT(base::TimeDelta(), rappor_service.next_rotation());
30 EXPECT_FALSE(rappor_service.test_uploader()->is_running());
32 rappor_service.Update(COARSE_LEVEL, true);
33 EXPECT_LT(base::TimeDelta(), rappor_service.next_rotation());
34 EXPECT_TRUE(rappor_service.test_uploader()->is_running());
37 // Check that samples can be recorded and exported.
38 TEST(RapporServiceTest, RecordAndExportMetrics) {
39 TestRapporService rappor_service;
41 // Multiple samples for the same metric should only generate one report.
42 rappor_service.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, "foo");
43 rappor_service.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, "bar");
45 RapporReports reports;
46 rappor_service.GetReports(&reports);
47 EXPECT_EQ(1, reports.report_size());
49 const RapporReports::Report& report = reports.report(0);
50 EXPECT_TRUE(report.name_hash());
51 // ETLD_PLUS_ONE_RAPPOR_TYPE has 128 bits
52 EXPECT_EQ(16u, report.bits().size());
55 // Check that the reporting level is respected.
56 TEST(RapporServiceTest, RecordingLevel) {
57 TestRapporService rappor_service;
58 rappor_service.Update(COARSE_LEVEL, false);
60 // ETLD_PLUS_ONE_RAPPOR_TYPE is a FINE_LEVEL metric
61 rappor_service.RecordSample("FineMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, "foo");
63 RapporReports reports;
64 rappor_service.GetReports(&reports);
65 EXPECT_EQ(0, reports.report_size());
68 // Check that GetRecordedSampleForMetric works as expected.
69 TEST(RapporServiceTest, GetRecordedSampleForMetric) {
70 TestRapporService rappor_service;
72 // Multiple samples for the same metric; only the latest is remembered.
73 rappor_service.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, "foo");
74 rappor_service.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, "bar");
76 std::string sample;
77 RapporType type;
78 EXPECT_FALSE(
79 rappor_service.GetRecordedSampleForMetric("WrongMetric", &sample, &type));
80 EXPECT_TRUE(
81 rappor_service.GetRecordedSampleForMetric("MyMetric", &sample, &type));
82 EXPECT_EQ("bar", sample);
83 EXPECT_EQ(ETLD_PLUS_ONE_RAPPOR_TYPE, type);
86 // Check that the incognito is respected.
87 TEST(RapporServiceTest, Incognito) {
88 TestRapporService rappor_service;
89 rappor_service.set_is_incognito(true);
91 rappor_service.RecordSample("MyMetric", COARSE_RAPPOR_TYPE, "foo");
93 RapporReports reports;
94 rappor_service.GetReports(&reports);
95 EXPECT_EQ(0, reports.report_size());
98 } // namespace rappor