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 #ifndef COMPONENTS_RAPPOR_TEST_RAPPOR_SERVICE_H_
6 #define COMPONENTS_RAPPOR_TEST_RAPPOR_SERVICE_H_
11 #include "base/prefs/testing_pref_service.h"
12 #include "components/rappor/rappor_service.h"
13 #include "components/rappor/test_log_uploader.h"
17 // This class provides a simple instance that can be instantiated by tests
18 // and examined to check that metrics were recorded. It assumes the most
19 // permissive settings so that any metric can be recorded.
20 class TestRapporService
: public RapporService
{
24 ~TestRapporService() override
;
26 // Intercepts the sample being recorded and saves it in a test structure.
27 void RecordSample(const std::string
& metric_name
,
29 const std::string
& sample
) override
;
31 // Gets the number of reports that would be uploaded by this service.
32 // This also clears the internal map of metrics as a biproduct, so if
33 // comparing numbers of reports, the comparison should be from the last time
34 // GetReportsCount() was called (not from the beginning of the test).
35 int GetReportsCount();
37 // Gets the reports proto that would be uploaded.
38 // This clears the internal map of metrics.
39 void GetReports(RapporReports
* reports
);
41 // Gets the recorded sample/type for a |metric_name|, and returns whether the
42 // recorded metric was found. Limitation: if the metric was logged more than
43 // once, this will return the latest sample that was logged.
44 bool GetRecordedSampleForMetric(const std::string
& metric_name
,
48 void set_is_incognito(bool is_incognito
) { is_incognito_
= is_incognito
; }
50 TestingPrefServiceSimple
* test_prefs() { return &test_prefs_
; }
52 TestLogUploader
* test_uploader() { return test_uploader_
; }
54 base::TimeDelta
next_rotation() { return next_rotation_
; }
57 // Cancels the next call to OnLogInterval.
58 void CancelNextLogRotation() override
;
60 // Schedules the next call to OnLogInterval.
61 void ScheduleNextLogRotation(base::TimeDelta interval
) override
;
64 // Used to keep track of recorded RAPPOR samples.
69 typedef std::map
<std::string
, RapporSample
> SamplesMap
;
72 TestingPrefServiceSimple test_prefs_
;
74 // Holds a weak ref to the uploader_ object.
75 TestLogUploader
* test_uploader_
;
77 // The last scheduled log rotation.
78 base::TimeDelta next_rotation_
;
80 // Sets this to true to mock incognito state.
83 DISALLOW_COPY_AND_ASSIGN(TestRapporService
);
88 #endif // COMPONENTS_RAPPOR_TEST_RAPPOR_SERVICE_H_