1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/reporting/metrics/metric_reporting_controller.h"
#include <memory>
#include <string>
#include "base/callback_forward.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "components/reporting/metrics/fake_reporting_settings.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace reporting {
class MetricReportingControllerTest : public ::testing::Test {
public:
void SetUp() override {
settings_ = std::make_unique<FakeReportingSettings>();
enable_count_ = 0;
disable_count_ = 0;
enable_cb_ = base::BindLambdaForTesting([this]() { ++enable_count_; });
disable_cb_ = base::BindLambdaForTesting([this]() { ++disable_count_; });
}
protected:
int enable_count_ = 0;
int disable_count_ = 0;
base::RepeatingClosure enable_cb_;
base::RepeatingClosure disable_cb_;
std::unique_ptr<FakeReportingSettings> settings_;
base::test::SingleThreadTaskEnvironment task_environment_;
};
TEST_F(MetricReportingControllerTest, InvalidPath) {
MetricReportingController controller(settings_.get(), "invalid_path",
enable_cb_, disable_cb_);
EXPECT_EQ(enable_count_, 0);
EXPECT_EQ(disable_count_, 0);
}
TEST_F(MetricReportingControllerTest, TrustedCheck) {
const std::string path = "path";
settings_->SetBoolean(path, true);
settings_->SetIsTrusted(false);
MetricReportingController controller(settings_.get(), path, enable_cb_,
disable_cb_);
EXPECT_EQ(enable_count_, 0);
EXPECT_EQ(disable_count_, 0);
settings_->SetIsTrusted(true);
EXPECT_EQ(enable_count_, 1);
EXPECT_EQ(disable_count_, 0);
}
TEST_F(MetricReportingControllerTest, DefaultEnable) {
const std::string path = "path";
settings_->SetBoolean(path, true);
MetricReportingController controller(settings_.get(), path, enable_cb_,
disable_cb_);
// Only enable_cb_ is called.
EXPECT_EQ(enable_count_, 1);
EXPECT_EQ(disable_count_, 0);
// Change to disable.
settings_->SetBoolean(path, false);
// Only disable_cb_ is called.
EXPECT_EQ(enable_count_, 1);
EXPECT_EQ(disable_count_, 1);
// Change to enable.
settings_->SetBoolean(path, true);
// Only enable_cb_ is called.
EXPECT_EQ(enable_count_, 2);
EXPECT_EQ(disable_count_, 1);
}
TEST_F(MetricReportingControllerTest, DefaultDisable) {
const std::string path = "path";
settings_->SetBoolean(path, false);
MetricReportingController controller(settings_.get(), path, enable_cb_,
disable_cb_);
// No callbacks are called.
EXPECT_EQ(enable_count_, 0);
EXPECT_EQ(disable_count_, 0);
// Change to enable.
settings_->SetBoolean(path, true);
// Only enable_cb_ is called.
EXPECT_EQ(enable_count_, 1);
EXPECT_EQ(disable_count_, 0);
// Change to disable.
settings_->SetBoolean(path, false);
// Only disable_cb_ is called.
EXPECT_EQ(enable_count_, 1);
EXPECT_EQ(disable_count_, 1);
}
} // namespace reporting
|