1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <string>
18 #include <functional>
19 
20 #include <flag_checker.h>
21 #include <gtest/gtest.h>
22 
23 #include "android_test_myflags.h"
24 
25 
26 using android::test::flag::CheckFlagCondition;
27 using android::test::flag::GetFlagsNotMetRequirements;
28 
29 class CheckFlagConditionTest : public ::testing::Test {};
30 
31 
TEST_F(CheckFlagConditionTest,invalid_lagency_flag)32 TEST_F(CheckFlagConditionTest, invalid_lagency_flag) {
33   ASSERT_FALSE(
34     CheckFlagCondition(
35       true,
36       {nullptr, "flagtest, android::test::myflags"}
37     )
38   );
39 }
40 
TEST_F(CheckFlagConditionTest,lagency_flag_not_meet_condition)41 TEST_F(CheckFlagConditionTest, lagency_flag_not_meet_condition) {
42   ASSERT_FALSE(
43     CheckFlagCondition(
44       true,
45       {nullptr, "flagtest, android::test::myflags, test_flag"}
46     )
47   );
48 }
49 
TEST_F(CheckFlagConditionTest,lagency_flag_meet_true_condition)50 TEST_F(CheckFlagConditionTest, lagency_flag_meet_true_condition) {
51   ASSERT_TRUE(
52     CheckFlagCondition(
53       true,
54       {nullptr, "flagtest, android::test::myflags, test_flag_true"}
55     )
56   );
57 }
58 
TEST_F(CheckFlagConditionTest,lagency_flag_meet_false_condition)59 TEST_F(CheckFlagConditionTest, lagency_flag_meet_false_condition) {
60   ASSERT_TRUE(
61     CheckFlagCondition(
62       false,
63       {nullptr, "flagtest, android::test::myflags, test_flag_false"}
64     )
65   );
66 }
67 
TEST_F(CheckFlagConditionTest,aconfig_flag_not_meet_condition)68 TEST_F(CheckFlagConditionTest, aconfig_flag_not_meet_condition) {
69   ASSERT_FALSE(
70     CheckFlagCondition(
71       false,
72       {
73         android::test::myflags::test_flag_true,
74         "android::test::myflags, test_flag_true"
75       }
76     )
77   );
78 }
79 
TEST_F(CheckFlagConditionTest,aconfig_flag_meet_true_condition)80 TEST_F(CheckFlagConditionTest, aconfig_flag_meet_true_condition) {
81   ASSERT_TRUE(
82     CheckFlagCondition(
83       true,
84       {
85         android::test::myflags::test_flag_true,
86         "android::test::myflags, test_flag_true"
87       }
88     )
89   );
90 }
91 
TEST_F(CheckFlagConditionTest,aconfig_flag_meet_false_condition)92 TEST_F(CheckFlagConditionTest, aconfig_flag_meet_false_condition) {
93   ASSERT_TRUE(
94     CheckFlagCondition(
95       false,
96       {
97         android::test::myflags::test_flag_false,
98         "android::test::myflags, test_flag_false"
99       }
100     )
101   );
102 }
103 
104 class GetFlagsNotMetReqTest : public ::testing::Test {};
105 
TEST_F(GetFlagsNotMetReqTest,empty_flags)106 TEST_F(GetFlagsNotMetReqTest, empty_flags) {
107   ASSERT_EQ(GetFlagsNotMetRequirements({}).size(), 0);
108 }
109 
TEST_F(GetFlagsNotMetReqTest,flag_meet_condition)110 TEST_F(GetFlagsNotMetReqTest, flag_meet_condition) {
111   auto unsatisfied_flags =
112     GetFlagsNotMetRequirements({
113       {
114         true, {
115           {
116             android::test::myflags::test_flag_true,
117             "android::test::myflags, test_flag_true"
118           }
119         }
120       },
121       {
122         false, {
123           {
124             nullptr,
125             "flagtest, android::test::myflags, test_flag_false"
126           }
127         }
128       },
129     });
130   ASSERT_EQ(unsatisfied_flags.size(), 0);
131 }
132 
TEST_F(GetFlagsNotMetReqTest,flag_not_meet_condition)133 TEST_F(GetFlagsNotMetReqTest, flag_not_meet_condition) {
134   auto unsatisfied_flags =
135     GetFlagsNotMetRequirements({
136       {
137         false, {
138           {
139             android::test::myflags::test_flag_true,
140             "android::test::myflags, test_flag_true"
141           }
142         }
143       },
144       {
145         true, {
146           {
147             nullptr,
148             "flagtest, android::test::myflags, test_flag_false"
149           }
150         }
151       },
152     });
153   ASSERT_EQ(unsatisfied_flags.size(), 2);
154   ASSERT_FALSE(unsatisfied_flags[0].first);
155   ASSERT_EQ(
156     unsatisfied_flags[0].second,
157     "android::test::myflags, test_flag_true"
158   );
159   ASSERT_TRUE(unsatisfied_flags[1].first);
160   ASSERT_EQ(
161     unsatisfied_flags[1].second,
162     "flagtest, android::test::myflags, test_flag_false"
163   );
164 }
165