1 // Copyright (C) 2021 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/flags/FlagProvider.h"
16
17 #include <gtest/gtest.h>
18
19 #include "tests/statsd_test_util.h"
20
21 namespace android {
22 namespace os {
23 namespace statsd {
24
25 #ifdef __ANDROID__
26
27 using namespace std;
28
29 const string TEST_FLAG = "MyFlagTest";
30
31 struct FlagParam {
32 string flagValue;
33 string label;
34 };
35
36 class FlagProviderTest_SPlus : public testing::TestWithParam<FlagParam> {
TearDown()37 void TearDown() override {
38 FlagProvider::getInstance().resetOverrides();
39 }
40 };
41
42 INSTANTIATE_TEST_SUITE_P(DefaultValues, FlagProviderTest_SPlus,
43 testing::ValuesIn<FlagParam>({
44 // Default values.
45 {FLAG_FALSE, "DefaultFalse"},
46 {FLAG_TRUE, "DefaultTrue"},
47 }),
__anonc6ac4d0c0102(const testing::TestParamInfo<FlagProviderTest_SPlus::ParamType>& info) 48 [](const testing::TestParamInfo<FlagProviderTest_SPlus::ParamType>& info) {
49 return info.param.label;
50 });
51
TEST_P(FlagProviderTest_SPlus,TestGetFlagBoolServerFlagTrue)52 TEST_P(FlagProviderTest_SPlus, TestGetFlagBoolServerFlagTrue) {
53 FlagProvider::getInstance().overrideFuncs(&isAtLeastSFuncTrue, &getServerFlagFuncTrue);
54 EXPECT_TRUE(FlagProvider::getInstance().getFlagBool(TEST_FLAG, GetParam().flagValue));
55 }
56
TEST_P(FlagProviderTest_SPlus,TestGetFlagBoolServerFlagFalse)57 TEST_P(FlagProviderTest_SPlus, TestGetFlagBoolServerFlagFalse) {
58 FlagProvider::getInstance().overrideFuncs(&isAtLeastSFuncTrue, &getServerFlagFuncFalse);
59 EXPECT_FALSE(FlagProvider::getInstance().getFlagBool(TEST_FLAG, GetParam().flagValue));
60 }
61
TEST_P(FlagProviderTest_SPlus,TestOverrideLocalFlags)62 TEST_P(FlagProviderTest_SPlus, TestOverrideLocalFlags) {
63 FlagProvider::getInstance().overrideFuncs(&isAtLeastSFuncTrue);
64
65 FlagProvider::getInstance().overrideFlag(TEST_FLAG, FLAG_FALSE, /*isBootFlag=*/false);
66 FlagProvider::getInstance().overrideFlag(TEST_FLAG, FLAG_FALSE, /*isBootFlag=*/true);
67 EXPECT_FALSE(FlagProvider::getInstance().getFlagBool(TEST_FLAG, GetParam().flagValue));
68 EXPECT_FALSE(FlagProvider::getInstance().getBootFlagBool(TEST_FLAG, GetParam().flagValue));
69
70 FlagProvider::getInstance().overrideFlag(TEST_FLAG, FLAG_TRUE, /*isBootFlag=*/false);
71 FlagProvider::getInstance().overrideFlag(TEST_FLAG, FLAG_TRUE, /*isBootFlag=*/true);
72 EXPECT_TRUE(FlagProvider::getInstance().getFlagBool(TEST_FLAG, GetParam().flagValue));
73 EXPECT_TRUE(FlagProvider::getInstance().getBootFlagBool(TEST_FLAG, GetParam().flagValue));
74 }
75
76 class FlagProviderTest_SPlus_RealValues : public testing::TestWithParam<FlagParam> {
SetUp()77 void SetUp() override {
78 if (!isAtLeastS()) {
79 GTEST_SKIP() << "Cannot query flags from system property on R-.";
80 }
81 }
82
TearDown()83 void TearDown() override {
84 writeFlag(TEST_FLAG, FLAG_EMPTY);
85 writeBootFlag(TEST_FLAG, FLAG_EMPTY);
86 FlagProvider::getInstance().initBootFlags({TEST_FLAG});
87 }
88 };
89
90 INSTANTIATE_TEST_SUITE_P(DefaultValues, FlagProviderTest_SPlus_RealValues,
91 testing::ValuesIn<FlagParam>({
92 // Default values.
93 {FLAG_FALSE, "DefaultFalse"},
94 {FLAG_TRUE, "DefaultTrue"},
95 }),
__anonc6ac4d0c0202(const testing::TestParamInfo<FlagProviderTest_SPlus::ParamType>& info) 96 [](const testing::TestParamInfo<FlagProviderTest_SPlus::ParamType>& info) {
97 return info.param.label;
98 });
99
TEST_P(FlagProviderTest_SPlus_RealValues,TestGetFlagBoolServerFlagTrue)100 TEST_P(FlagProviderTest_SPlus_RealValues, TestGetFlagBoolServerFlagTrue) {
101 writeFlag(TEST_FLAG, FLAG_TRUE);
102 EXPECT_TRUE(FlagProvider::getInstance().getFlagBool(TEST_FLAG, GetParam().flagValue));
103 }
104
TEST_P(FlagProviderTest_SPlus_RealValues,TestGetFlagBoolServerFlagFalse)105 TEST_P(FlagProviderTest_SPlus_RealValues, TestGetFlagBoolServerFlagFalse) {
106 writeFlag(TEST_FLAG, FLAG_FALSE);
107 EXPECT_FALSE(FlagProvider::getInstance().getFlagBool(TEST_FLAG, GetParam().flagValue));
108 }
109
TEST_F(FlagProviderTest_SPlus_RealValues,TestGetFlagBoolServerFlagEmptyDefaultFalse)110 TEST_F(FlagProviderTest_SPlus_RealValues, TestGetFlagBoolServerFlagEmptyDefaultFalse) {
111 writeFlag(TEST_FLAG, FLAG_EMPTY);
112 EXPECT_FALSE(FlagProvider::getInstance().getFlagBool(TEST_FLAG, FLAG_FALSE));
113 }
114
TEST_F(FlagProviderTest_SPlus_RealValues,TestGetFlagBoolServerFlagEmptyDefaultTrue)115 TEST_F(FlagProviderTest_SPlus_RealValues, TestGetFlagBoolServerFlagEmptyDefaultTrue) {
116 writeFlag(TEST_FLAG, FLAG_EMPTY);
117 EXPECT_TRUE(FlagProvider::getInstance().getFlagBool(TEST_FLAG, FLAG_TRUE));
118 }
119
TEST_P(FlagProviderTest_SPlus_RealValues,TestGetBootFlagBoolServerFlagTrue)120 TEST_P(FlagProviderTest_SPlus_RealValues, TestGetBootFlagBoolServerFlagTrue) {
121 writeBootFlag(TEST_FLAG, FLAG_TRUE);
122 FlagProvider::getInstance().initBootFlags({TEST_FLAG});
123 EXPECT_TRUE(FlagProvider::getInstance().getBootFlagBool(TEST_FLAG, GetParam().flagValue));
124 }
125
TEST_P(FlagProviderTest_SPlus_RealValues,TestGetBootFlagBoolServerFlagFalse)126 TEST_P(FlagProviderTest_SPlus_RealValues, TestGetBootFlagBoolServerFlagFalse) {
127 writeBootFlag(TEST_FLAG, FLAG_FALSE);
128 FlagProvider::getInstance().initBootFlags({TEST_FLAG});
129 EXPECT_FALSE(FlagProvider::getInstance().getBootFlagBool(TEST_FLAG, GetParam().flagValue));
130 }
131
TEST_P(FlagProviderTest_SPlus_RealValues,TestGetBootFlagBoolServerFlagUpdated)132 TEST_P(FlagProviderTest_SPlus_RealValues, TestGetBootFlagBoolServerFlagUpdated) {
133 writeBootFlag(TEST_FLAG, FLAG_FALSE);
134 FlagProvider::getInstance().initBootFlags({TEST_FLAG});
135 writeBootFlag(TEST_FLAG, FLAG_TRUE);
136 EXPECT_FALSE(FlagProvider::getInstance().getBootFlagBool(TEST_FLAG, GetParam().flagValue));
137 }
138
TEST_F(FlagProviderTest_SPlus_RealValues,TestGetFlagBoolNoInitServerFlagEmptyDefaultFalse)139 TEST_F(FlagProviderTest_SPlus_RealValues, TestGetFlagBoolNoInitServerFlagEmptyDefaultFalse) {
140 writeBootFlag(TEST_FLAG, FLAG_EMPTY);
141 EXPECT_FALSE(FlagProvider::getInstance().getFlagBool(TEST_FLAG, FLAG_FALSE));
142 }
143
TEST_F(FlagProviderTest_SPlus_RealValues,TestGetFlagBoolNoInitServerFlagEmptyDefaultTrue)144 TEST_F(FlagProviderTest_SPlus_RealValues, TestGetFlagBoolNoInitServerFlagEmptyDefaultTrue) {
145 writeBootFlag(TEST_FLAG, FLAG_EMPTY);
146 EXPECT_TRUE(FlagProvider::getInstance().getFlagBool(TEST_FLAG, FLAG_TRUE));
147 }
148
149 class FlagProviderTest_RMinus : public testing::TestWithParam<FlagParam> {
SetUp()150 void SetUp() override {
151 writeFlag(TEST_FLAG, GetParam().flagValue);
152 FlagProvider::getInstance().overrideFuncs(&isAtLeastSFuncFalse);
153 }
154
TearDown()155 void TearDown() override {
156 FlagProvider::getInstance().resetOverrides();
157 writeFlag(TEST_FLAG, FLAG_EMPTY);
158 }
159 };
160
161 INSTANTIATE_TEST_SUITE_P(
162 ServerFlagValues, FlagProviderTest_RMinus,
163 testing::ValuesIn<FlagParam>({
164 // Server flag values.
165 {FLAG_TRUE, "ServerFlagTrue"},
166 {FLAG_FALSE, "ServerFlagFalse"},
167 {FLAG_EMPTY, "ServerFlagEmpty"},
168 }),
__anonc6ac4d0c0302(const testing::TestParamInfo<FlagProviderTest_RMinus::ParamType>& info) 169 [](const testing::TestParamInfo<FlagProviderTest_RMinus::ParamType>& info) {
170 return info.param.label;
171 });
172
TEST_P(FlagProviderTest_RMinus,TestGetFlagBoolDefaultValueFalse)173 TEST_P(FlagProviderTest_RMinus, TestGetFlagBoolDefaultValueFalse) {
174 EXPECT_FALSE(FlagProvider::getInstance().getFlagBool(TEST_FLAG, FLAG_FALSE));
175 }
176
TEST_P(FlagProviderTest_RMinus,TestGetFlagBoolDefaultValueTrue)177 TEST_P(FlagProviderTest_RMinus, TestGetFlagBoolDefaultValueTrue) {
178 EXPECT_TRUE(FlagProvider::getInstance().getFlagBool(TEST_FLAG, FLAG_TRUE));
179 }
180
181 #else
182 GTEST_LOG_(INFO) << "This test does nothing.\n";
183 #endif
184
185 } // namespace statsd
186 } // namespace os
187 } // namespace android
188