Invalidate scans when the host volume is unmounted.
[chromium-blink-merge.git] / components / rappor / rappor_service_unittest.cc
blob1c88b9bcc0443eb274f3f910223a2b7608ec1f6f
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, LoadCohort) {
20 TestRapporService rappor_service;
21 rappor_service.test_prefs()->SetInteger(prefs::kRapporCohortSeed, 1);
22 EXPECT_EQ(1, rappor_service.LoadCohortForTesting());
25 TEST(RapporServiceTest, LoadSecret) {
26 TestRapporService rappor_service;
27 std::string secret = HmacByteVectorGenerator::GenerateEntropyInput();
28 std::string secret_base64;
29 base::Base64Encode(secret, &secret_base64);
30 rappor_service.test_prefs()->SetString(prefs::kRapporSecret, secret_base64);
31 EXPECT_EQ(secret, rappor_service.LoadSecretForTesting());
34 TEST(RapporServiceTest, Update) {
35 TestRapporService rappor_service;
36 EXPECT_LT(base::TimeDelta(), rappor_service.next_rotation());
37 EXPECT_TRUE(rappor_service.test_uploader()->is_running());
39 rappor_service.Update(RECORDING_DISABLED, false);
40 EXPECT_EQ(base::TimeDelta(), rappor_service.next_rotation());
41 EXPECT_FALSE(rappor_service.test_uploader()->is_running());
43 rappor_service.Update(FINE_LEVEL, false);
44 EXPECT_LT(base::TimeDelta(), rappor_service.next_rotation());
45 EXPECT_FALSE(rappor_service.test_uploader()->is_running());
47 rappor_service.Update(COARSE_LEVEL, true);
48 EXPECT_LT(base::TimeDelta(), rappor_service.next_rotation());
49 EXPECT_TRUE(rappor_service.test_uploader()->is_running());
52 // Check that samples can be recorded and exported.
53 TEST(RapporServiceTest, RecordAndExportMetrics) {
54 TestRapporService rappor_service;
56 // Multiple samples for the same metric should only generate one report.
57 rappor_service.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, "foo");
58 rappor_service.RecordSample("MyMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, "bar");
60 RapporReports reports;
61 rappor_service.GetReports(&reports);
62 EXPECT_EQ(1, reports.report_size());
64 const RapporReports::Report& report = reports.report(0);
65 EXPECT_TRUE(report.name_hash());
66 // ETLD_PLUS_ONE_RAPPOR_TYPE has 128 bits
67 EXPECT_EQ(16u, report.bits().size());
70 // Check that the reporting level is respected.
71 TEST(RapporServiceTest, RecordingLevel) {
72 TestRapporService rappor_service;
73 rappor_service.Update(COARSE_LEVEL, false);
75 // ETLD_PLUS_ONE_RAPPOR_TYPE is a FINE_LEVEL metric
76 rappor_service.RecordSample("FineMetric", ETLD_PLUS_ONE_RAPPOR_TYPE, "foo");
78 RapporReports reports;
79 rappor_service.GetReports(&reports);
80 EXPECT_EQ(0, reports.report_size());
83 // Check that the incognito is respected.
84 TEST(RapporServiceTest, Incognito) {
85 TestRapporService rappor_service;
86 rappor_service.set_is_incognito(true);
88 rappor_service.RecordSample("MyMetric", COARSE_RAPPOR_TYPE, "foo");
90 RapporReports reports;
91 rappor_service.GetReports(&reports);
92 EXPECT_EQ(0, reports.report_size());
95 } // namespace rappor