Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: components/rappor/rappor_service_unittest.cc

Issue 509203002: Create a mechanism for reporting rappor metrics from non-UMA (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/rappor/rappor_service.cc ('k') | tools/metrics/rappor/rappor.xml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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/rappor/rappor_service.h" 5 #include "components/rappor/rappor_service.h"
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/prefs/testing_pref_service.h" 8 #include "base/prefs/testing_pref_service.h"
9 #include "components/rappor/byte_vector_utils.h" 9 #include "components/rappor/byte_vector_utils.h"
10 #include "components/rappor/proto/rappor_metric.pb.h" 10 #include "components/rappor/proto/rappor_metric.pb.h"
11 #include "components/rappor/rappor_parameters.h" 11 #include "components/rappor/rappor_parameters.h"
12 #include "components/rappor/rappor_pref_names.h" 12 #include "components/rappor/rappor_pref_names.h"
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 14
15 namespace rappor { 15 namespace rappor {
16 16
17 class TestRapporService : public RapporService { 17 class TestRapporService : public RapporService {
18 public: 18 public:
19 TestRapporService() : RapporService(&prefs_) { 19 TestRapporService(ReportingLevel reporting_level) : RapporService(&prefs) {
20 RegisterPrefs(prefs_.registry()); 20 RegisterPrefs(prefs.registry());
21 prefs_.SetInteger(prefs::kRapporCohortSeed, 0); 21 Initialize(0,
22 std::string secret = HmacByteVectorGenerator::GenerateEntropyInput(); 22 HmacByteVectorGenerator::GenerateEntropyInput(),
23 std::string secret_base64; 23 reporting_level);
24 base::Base64Encode(secret, &secret_base64);
25 prefs_.SetString(prefs::kRapporSecret, secret_base64);
26 LoadCohort();
27 LoadSecret();
28 } 24 }
29 25
30 void GetReports(RapporReports* reports) { 26 void GetReports(RapporReports* reports) {
31 ExportMetrics(reports); 27 ExportMetrics(reports);
32 } 28 }
33 29
30 int32_t TestLoadCohort() {
31 return LoadCohort();
32 }
33
34 std::string TestLoadSecret() {
35 return LoadSecret();
36 }
37
34 void TestRecordSample(const std::string& metric_name, 38 void TestRecordSample(const std::string& metric_name,
35 const RapporParameters& parameters, 39 const RapporParameters& parameters,
36 const std::string& sample) { 40 const std::string& sample) {
37 RecordSampleInternal(metric_name, parameters, sample); 41 RecordSampleInternal(metric_name, parameters, sample);
38 } 42 }
39 43
40 protected: 44 TestingPrefServiceSimple prefs;
41 TestingPrefServiceSimple prefs_;
42 45
43 private: 46 private:
44 DISALLOW_COPY_AND_ASSIGN(TestRapporService); 47 DISALLOW_COPY_AND_ASSIGN(TestRapporService);
45 }; 48 };
46 49
50 TEST(RapporServiceTest, LoadCohort) {
51 TestRapporService rappor_service(REPORTING_DISABLED);
52 rappor_service.prefs.SetInteger(prefs::kRapporCohortSeed, 1);
53 EXPECT_EQ(1, rappor_service.TestLoadCohort());
54 }
55
56 TEST(RapporServiceTest, LoadSecret) {
57 TestRapporService rappor_service(REPORTING_DISABLED);
58 std::string secret = HmacByteVectorGenerator::GenerateEntropyInput();
59 std::string secret_base64;
60 base::Base64Encode(secret, &secret_base64);
61 rappor_service.prefs.SetString(prefs::kRapporSecret, secret_base64);
62 EXPECT_EQ(secret, rappor_service.TestLoadSecret());
63 }
64
65 // Check that samples can be recorded and exported.
47 TEST(RapporServiceTest, RecordAndExportMetrics) { 66 TEST(RapporServiceTest, RecordAndExportMetrics) {
48 const RapporParameters kTestRapporParameters = { 67 const RapporParameters kTestRapporParameters = {
49 1 /* Num cohorts */, 68 1 /* Num cohorts */,
50 16 /* Bloom filter size bytes */, 69 16 /* Bloom filter size bytes */,
51 4 /* Bloom filter hash count */, 70 4 /* Bloom filter hash count */,
52 PROBABILITY_75 /* Fake data probability */, 71 PROBABILITY_75 /* Fake data probability */,
53 PROBABILITY_50 /* Fake one probability */, 72 PROBABILITY_50 /* Fake one probability */,
54 PROBABILITY_75 /* One coin probability */, 73 PROBABILITY_75 /* One coin probability */,
55 PROBABILITY_50 /* Zero coin probability */}; 74 PROBABILITY_50 /* Zero coin probability */,
75 COARSE_LEVEL};
56 76
57 TestRapporService rappor_service; 77 TestRapporService rappor_service(COARSE_LEVEL);
58 78
79 // Multiple samples for the same metric should only generate one report.
59 rappor_service.TestRecordSample("MyMetric", kTestRapporParameters, "foo"); 80 rappor_service.TestRecordSample("MyMetric", kTestRapporParameters, "foo");
60 rappor_service.TestRecordSample("MyMetric", kTestRapporParameters, "bar"); 81 rappor_service.TestRecordSample("MyMetric", kTestRapporParameters, "bar");
61 82
62 RapporReports reports; 83 RapporReports reports;
63 rappor_service.GetReports(&reports); 84 rappor_service.GetReports(&reports);
64 EXPECT_EQ(1, reports.report_size()); 85 EXPECT_EQ(1, reports.report_size());
65 86
66 const RapporReports::Report& report = reports.report(0); 87 const RapporReports::Report& report = reports.report(0);
67 EXPECT_TRUE(report.name_hash()); 88 EXPECT_TRUE(report.name_hash());
68 EXPECT_EQ(16u, report.bits().size()); 89 EXPECT_EQ(16u, report.bits().size());
69 } 90 }
70 91
92 // Check that the reporting level is respected.
93 TEST(RapporServiceTest, ReportingLevel) {
94 const RapporParameters kFineRapporParameters = {
95 1 /* Num cohorts */,
96 16 /* Bloom filter size bytes */,
97 4 /* Bloom filter hash count */,
98 PROBABILITY_75 /* Fake data probability */,
99 PROBABILITY_50 /* Fake one probability */,
100 PROBABILITY_75 /* One coin probability */,
101 PROBABILITY_50 /* Zero coin probability */,
102 FINE_LEVEL};
103
104 TestRapporService rappor_service(COARSE_LEVEL);
105
106 rappor_service.TestRecordSample("FineMetric", kFineRapporParameters, "foo");
107
108 RapporReports reports;
109 rappor_service.GetReports(&reports);
110 EXPECT_EQ(0, reports.report_size());
111 }
112
71 } // namespace rappor 113 } // namespace rappor
OLDNEW
« no previous file with comments | « components/rappor/rappor_service.cc ('k') | tools/metrics/rappor/rappor.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698