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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "mozilla/Preferences.h"
#include "mozilla/browser/NimbusFeatures.h"
using namespace mozilla;
static bool gPrefUpdate = false;
TEST(NimbusFeaturesGet, Errors)
{
ASSERT_EQ(Preferences::SetInt("nimbus.syncdatastore.foo.value", 42,
PrefValueKind::User),
NS_OK);
ASSERT_EQ(NimbusFeatures::GetInt("foo"_ns, "value"_ns, 0), 42);
ASSERT_EQ(Preferences::SetBool("nimbus.syncdatastore.foo.enabled", true,
PrefValueKind::User),
NS_OK);
ASSERT_TRUE(NimbusFeatures::GetBool("foo"_ns, "enabled"_ns, false));
ASSERT_EQ(Preferences::ClearUser("nimbus.syncdatastore.foo.value"), NS_OK);
}
TEST(NimbusFeaturesGetRollout, Errors)
{
ASSERT_EQ(Preferences::SetInt("nimbus.syncdefaultsstore.rollout.value", 7,
PrefValueKind::User),
NS_OK);
ASSERT_EQ(NimbusFeatures::GetInt("rollout"_ns, "value"_ns, 0), 7);
ASSERT_EQ(Preferences::SetBool("nimbus.syncdefaultsstore.rollout.enabled",
true, PrefValueKind::User),
NS_OK);
ASSERT_TRUE(NimbusFeatures::GetBool("rollout"_ns, "enabled"_ns, false));
}
TEST(NimbusFeaturesExperimentPriorityOverRollouts, Errors)
{
ASSERT_EQ(Preferences::SetInt("nimbus.syncdatastore.feature.value", 12,
PrefValueKind::User),
NS_OK);
ASSERT_EQ(Preferences::SetInt("nimbus.syncdefaultsstore.feature.value", 22,
PrefValueKind::User),
NS_OK);
ASSERT_EQ(NimbusFeatures::GetInt("feature"_ns, "value"_ns, 0), 12);
ASSERT_EQ(Preferences::SetBool("nimbus.syncdatastore.feature.enabled", true,
PrefValueKind::User),
NS_OK);
ASSERT_EQ(Preferences::SetBool("nimbus.syncdefaultsstore.feature.enabled",
false, PrefValueKind::User),
NS_OK);
ASSERT_TRUE(NimbusFeatures::GetBool("feature"_ns, "enabled"_ns, false));
}
TEST(NimbusFeaturesDataSourcePrecedence, Errors)
{
const auto FALLBACK_VALUE = 1;
const auto EXPERIMENT_VALUE = 2;
const auto ROLLOUT_VALUE = 3;
ASSERT_EQ(Preferences::SetInt("nimbus.testing.testInt", FALLBACK_VALUE,
PrefValueKind::User),
NS_OK);
// If there is no experiment or rollout, the fallback value should be
// returned.
ASSERT_EQ(NimbusFeatures::GetInt("testFeature"_ns, "testInt"_ns, 0),
FALLBACK_VALUE);
// Enroll in an experiment.
ASSERT_EQ(Preferences::SetInt("nimbus.syncdatastore.testFeature.testInt",
EXPERIMENT_VALUE, PrefValueKind::User),
NS_OK);
// Enroll in a rollout.
ASSERT_EQ(Preferences::SetInt("nimbus.syncdefaultsstore.testFeature.testInt",
ROLLOUT_VALUE, PrefValueKind::User),
NS_OK);
// Experiment value should take precedence.
ASSERT_EQ(NimbusFeatures::GetInt("testFeature"_ns, "testInt"_ns, 0),
EXPERIMENT_VALUE);
// After experiments it should default to rollouts.
Preferences::ClearUser("nimbus.syncdatastore.testFeature.testInt");
ASSERT_EQ(NimbusFeatures::GetInt("testFeature"_ns, "testInt"_ns, 0),
ROLLOUT_VALUE);
// Cleanup
Preferences::ClearUser("nimbus.syncdefaultsstore.testFeature.testInt");
Preferences::ClearUser("nimbus.testing.testInt");
}
static void FooValueUpdated(const char* aPref, void* aUserData) {
ASSERT_STREQ(aPref, "nimbus.syncdatastore.foo.value");
ASSERT_EQ(aUserData, reinterpret_cast<void*>(13));
ASSERT_FALSE(gPrefUpdate);
gPrefUpdate = true;
ASSERT_EQ(NimbusFeatures::GetInt("foo"_ns, "value"_ns, 0), 24);
}
static void BarRolloutValueUpdated(const char* aPref, void* aUserData) {
ASSERT_STREQ(aPref, "nimbus.syncdefaultsstore.bar.value");
ASSERT_FALSE(gPrefUpdate);
gPrefUpdate = true;
}
TEST(NimbusFeaturesGetFallback, Errors)
{
// No experiment is set and we expect to return fallback pref values
// As defined by fallbackPref browser.aboutwelcome.skipFocus
// in FeatureManifest.yaml
Preferences::SetBool("browser.aboutwelcome.skipFocus", true,
PrefValueKind::Default);
ASSERT_EQ(NimbusFeatures::GetBool("aboutwelcome"_ns, "skipFocus"_ns, false),
true);
Preferences::SetBool("browser.aboutwelcome.skipFocus", false,
PrefValueKind::User);
ASSERT_EQ(NimbusFeatures::GetBool("aboutwelcome"_ns, "skipFocus"_ns, true),
false);
Preferences::ClearUser("browser.aboutwelcome.skipFocus");
const auto FALLBACK_VALUE = 5;
const auto DEFAULT_VALUE = 42;
Preferences::SetInt("nimbus.testing.testInt", FALLBACK_VALUE,
PrefValueKind::Default);
ASSERT_EQ(
NimbusFeatures::GetInt("testFeature"_ns, "testInt"_ns, DEFAULT_VALUE),
FALLBACK_VALUE);
Preferences::ClearUser("nimbus.testing.testInt");
}
TEST(NimbusFeaturesUpdate, Errors)
{
// Verify updating foo.value calls FooValueUpdated.
ASSERT_EQ(NimbusFeatures::OnUpdate("foo"_ns, "value"_ns, FooValueUpdated,
reinterpret_cast<void*>(13)),
NS_OK);
ASSERT_EQ(
NimbusFeatures::OnUpdate("bar"_ns, "value"_ns, BarRolloutValueUpdated,
reinterpret_cast<void*>(13)),
NS_OK);
ASSERT_EQ(Preferences::SetInt("nimbus.syncdatastore.foo.value", 24,
PrefValueKind::User),
NS_OK);
ASSERT_TRUE(gPrefUpdate);
ASSERT_EQ(NimbusFeatures::GetInt("foo"_ns, "value"_ns, 0), 24);
// Verify updating foo.enabled doesn't call FooValueUpdated.
ASSERT_TRUE(NimbusFeatures::GetBool("foo"_ns, "enabled"_ns, false));
ASSERT_EQ(Preferences::SetBool("nimbus.syncdatastore.foo.enabled", false,
PrefValueKind::User),
NS_OK);
ASSERT_FALSE(NimbusFeatures::GetBool("foo"_ns, "enabled"_ns, true));
gPrefUpdate = false;
ASSERT_EQ(Preferences::SetInt("nimbus.syncdefaultsstore.bar.value", 25,
PrefValueKind::User),
NS_OK);
ASSERT_TRUE(gPrefUpdate);
gPrefUpdate = false;
// Verify OffUpdate requires a matching user data pointer to unregister.
ASSERT_EQ(NimbusFeatures::OffUpdate("foo"_ns, "value"_ns, FooValueUpdated,
reinterpret_cast<void*>(14)),
NS_ERROR_FAILURE);
// Verify updating foo.value no longer calls FooValueUpdated after it has
// been unregistered.
ASSERT_EQ(NimbusFeatures::OffUpdate("foo"_ns, "value"_ns, FooValueUpdated,
reinterpret_cast<void*>(13)),
NS_OK);
ASSERT_EQ(Preferences::SetInt("nimbus.syncdatastore.foo.value", 25,
PrefValueKind::User),
NS_OK);
ASSERT_EQ(NimbusFeatures::GetInt("foo"_ns, "value"_ns, 0), 25);
}
|