File: local_state_reporting_settings_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (244 lines) | stat: -rw-r--r-- 9,969 bytes parent folder | download | duplicates (6)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/reporting/local_state_reporting_settings.h"

#include <memory>

#include "base/callback_list.h"
#include "base/functional/callback_helpers.h"
#include "base/test/bind.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::Eq;
using ::testing::IsNull;
using ::testing::NotNull;

namespace reporting {
namespace {

constexpr char kSettingPath[] = "TestSetting";

class LocalStateReportingSettingsTest : public ::testing::Test {
 protected:
  TestingPrefServiceSimple* local_state() { return local_state_.Get(); }

 private:
  ::content::BrowserTaskEnvironment task_environment_;
  ScopedTestingLocalState local_state_{TestingBrowserProcess::GetGlobal()};

 protected:
  LocalStateReportingSettings local_state_reporting_settings_;
};

TEST_F(LocalStateReportingSettingsTest, InvalidIntegerPrefPath) {
  local_state()->registry()->RegisterIntegerPref(kSettingPath,
                                                 /*default_value=*/0);

  // Attempt to retrieve the integer reporting setting with incorrect data type
  // and confirm it fails.
  bool out_bool_value;
  ASSERT_FALSE(local_state_reporting_settings_.GetBoolean(kSettingPath,
                                                          &out_bool_value));
  ASSERT_FALSE(local_state_reporting_settings_.GetReportingEnabled(
      kSettingPath, &out_bool_value));
  const base::Value::List* out_list_value = nullptr;
  ASSERT_FALSE(
      local_state_reporting_settings_.GetList(kSettingPath, &out_list_value));
  EXPECT_THAT(out_list_value, IsNull());
}

TEST_F(LocalStateReportingSettingsTest, InvalidBooleanPrefPath) {
  local_state()->registry()->RegisterBooleanPref(kSettingPath,
                                                 /*default_value=*/true);

  // Attempt to retrieve the boolean reporting setting with incorrect data type
  // and confirm it fails.
  int out_int_value;
  ASSERT_FALSE(
      local_state_reporting_settings_.GetInteger(kSettingPath, &out_int_value));
  const base::Value::List* out_list_value = nullptr;
  ASSERT_FALSE(
      local_state_reporting_settings_.GetList(kSettingPath, &out_list_value));
  EXPECT_THAT(out_list_value, IsNull());
}

TEST_F(LocalStateReportingSettingsTest, InvalidListPrefPath) {
  local_state()->registry()->RegisterListPref(
      kSettingPath, /*default_value=*/base::Value::List());

  // Attempt to retrieve the list reporting setting with incorrect data type and
  // confirm it fails.
  bool out_bool_value;
  ASSERT_FALSE(local_state_reporting_settings_.GetBoolean(kSettingPath,
                                                          &out_bool_value));
  int out_int_value;
  EXPECT_FALSE(
      local_state_reporting_settings_.GetInteger(kSettingPath, &out_int_value));
}

TEST_F(LocalStateReportingSettingsTest, GetUnregisteredSetting) {
  bool out_bool_value;
  EXPECT_FALSE(local_state_reporting_settings_.GetBoolean(kSettingPath,
                                                          &out_bool_value));
  int out_int_value;
  EXPECT_FALSE(
      local_state_reporting_settings_.GetInteger(kSettingPath, &out_int_value));
  const base::Value::List* out_list_value = nullptr;
  EXPECT_FALSE(
      local_state_reporting_settings_.GetList(kSettingPath, &out_list_value));
}

TEST_F(LocalStateReportingSettingsTest, GetBoolean) {
  local_state()->registry()->RegisterBooleanPref(kSettingPath,
                                                 /*default_value=*/false);
  bool out_value = true;
  ASSERT_TRUE(
      local_state_reporting_settings_.GetBoolean(kSettingPath, &out_value));
  ASSERT_FALSE(out_value);

  // Update setting value and ensure the next fetch returns the updated value.
  local_state()->SetBoolean(kSettingPath, true);
  ASSERT_TRUE(
      local_state_reporting_settings_.GetBoolean(kSettingPath, &out_value));
  EXPECT_TRUE(out_value);
}

TEST_F(LocalStateReportingSettingsTest, GetReportingEnabled_Boolean) {
  local_state()->registry()->RegisterBooleanPref(kSettingPath,
                                                 /*default_value=*/false);
  bool out_value = true;
  ASSERT_TRUE(local_state_reporting_settings_.GetReportingEnabled(kSettingPath,
                                                                  &out_value));
  EXPECT_FALSE(out_value);

  // Update setting value and ensure the next fetch returns the updated value.
  local_state()->SetBoolean(kSettingPath, true);
  ASSERT_TRUE(
      local_state_reporting_settings_.GetBoolean(kSettingPath, &out_value));
  EXPECT_TRUE(out_value);
}

TEST_F(LocalStateReportingSettingsTest, GetInteger) {
  local_state()->registry()->RegisterIntegerPref(kSettingPath,
                                                 /*default_value=*/0);
  int out_value;
  ASSERT_TRUE(
      local_state_reporting_settings_.GetInteger(kSettingPath, &out_value));
  ASSERT_THAT(out_value, Eq(0));

  // Update setting value and ensure the next fetch returns the updated value.
  static constexpr int new_value = 1;
  local_state()->SetInteger(kSettingPath, new_value);
  ASSERT_TRUE(
      local_state_reporting_settings_.GetInteger(kSettingPath, &out_value));
  EXPECT_THAT(out_value, Eq(new_value));
}

TEST_F(LocalStateReportingSettingsTest, GetList) {
  local_state()->registry()->RegisterListPref(
      kSettingPath, /*default_value=*/base::Value::List());
  const base::Value::List* out_value;
  ASSERT_TRUE(
      local_state_reporting_settings_.GetList(kSettingPath, &out_value));
  ASSERT_THAT(out_value, NotNull());
  EXPECT_TRUE(out_value->empty());

  // Update setting value and ensure the next fetch returns the updated value.
  static constexpr char kListSettingItem[] = "item";
  local_state()->SetList(kSettingPath,
                         base::Value::List().Append(kListSettingItem));
  ASSERT_TRUE(
      local_state_reporting_settings_.GetList(kSettingPath, &out_value));
  ASSERT_THAT(out_value, NotNull());
  ASSERT_THAT(out_value->size(), Eq(1uL));
  EXPECT_THAT(out_value->front().GetString(), Eq(kListSettingItem));
}

TEST_F(LocalStateReportingSettingsTest, GetReportingEnabled_List) {
  local_state()->registry()->RegisterListPref(
      kSettingPath, /*default_value=*/base::Value::List());
  bool out_value = true;
  ASSERT_TRUE(local_state_reporting_settings_.GetReportingEnabled(kSettingPath,
                                                                  &out_value));
  EXPECT_FALSE(out_value);

  // Update setting value and ensure the next fetch returns the updated value.
  static constexpr char kListSettingItem[] = "item";
  local_state()->SetList(kSettingPath,
                         base::Value::List().Append(kListSettingItem));
  ASSERT_TRUE(local_state_reporting_settings_.GetReportingEnabled(kSettingPath,
                                                                  &out_value));
  EXPECT_TRUE(out_value);
}

TEST_F(LocalStateReportingSettingsTest, ObserveBooleanSetting) {
  local_state()->registry()->RegisterBooleanPref(kSettingPath,
                                                 /*default_value=*/false);
  bool callback_called = false;
  const auto callback_subscription =
      local_state_reporting_settings_.AddSettingsObserver(
          kSettingPath, base::BindLambdaForTesting(
                            [&callback_called]() { callback_called = true; }));

  // Update setting value and ensure callback was triggered.
  local_state()->SetBoolean(kSettingPath, true);
  ASSERT_TRUE(callback_called);
}

TEST_F(LocalStateReportingSettingsTest, ObserveIntegerSetting) {
  local_state()->registry()->RegisterIntegerPref(kSettingPath,
                                                 /*default_value=*/0);
  bool callback_called = false;
  const auto callback_subscription =
      local_state_reporting_settings_.AddSettingsObserver(
          kSettingPath, base::BindLambdaForTesting(
                            [&callback_called]() { callback_called = true; }));

  // Update setting value and ensure callback was triggered.
  local_state()->SetInteger(kSettingPath, 1);
  ASSERT_TRUE(callback_called);
}

TEST_F(LocalStateReportingSettingsTest, ObserveListSetting) {
  local_state()->registry()->RegisterListPref(
      kSettingPath, /*default_value=*/base::Value::List());
  bool callback_called = false;
  const auto callback_subscription =
      local_state_reporting_settings_.AddSettingsObserver(
          kSettingPath, base::BindLambdaForTesting(
                            [&callback_called]() { callback_called = true; }));

  // Update setting value and ensure callback was triggered.
  local_state()->SetList(kSettingPath, base::Value::List().Append("item"));
  ASSERT_TRUE(callback_called);
}

TEST_F(LocalStateReportingSettingsTest, MultipleSettingObservers) {
  local_state()->registry()->RegisterBooleanPref(kSettingPath,
                                                 /*default_value=*/false);
  int callback_trigger_count = 0;
  const auto callback_subscription_1 =
      local_state_reporting_settings_.AddSettingsObserver(
          kSettingPath, base::BindLambdaForTesting([&callback_trigger_count]() {
            callback_trigger_count++;
          }));
  const auto callback_subscription_2 =
      local_state_reporting_settings_.AddSettingsObserver(
          kSettingPath, base::BindLambdaForTesting([&callback_trigger_count]() {
            callback_trigger_count++;
          }));

  // Update setting value and ensure both callbacks were triggered.
  local_state()->SetBoolean(kSettingPath, true);
  ASSERT_THAT(callback_trigger_count, Eq(2));
}

}  // namespace
}  // namespace reporting