• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 #undef LOG_TAG
18 #define LOG_TAG "FlagManagerTest"
19 
20 #include <common/FlagManager.h>
21 #include <common/test/FlagUtils.h>
22 
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25 #include <log/log.h>
26 
27 #include <com_android_graphics_surfaceflinger_flags.h>
28 
29 namespace android {
30 
31 using namespace com::android::graphics::surfaceflinger;
32 using testing::Return;
33 
34 class TestableFlagManager : public FlagManager {
35 public:
TestableFlagManager()36     TestableFlagManager() : FlagManager(ConstructorTag{}) { markBootCompleted(); }
37     ~TestableFlagManager() = default;
38 
39     MOCK_METHOD(std::optional<bool>, getBoolProperty, (const char*), (const, override));
40     MOCK_METHOD(bool, getServerConfigurableFlag, (const char*), (const, override));
41 
markBootIncomplete()42     void markBootIncomplete() { mBootCompleted = false; }
43 };
44 
45 class FlagManagerTest : public testing::Test {
46 public:
FlagManagerTest()47     FlagManagerTest() {
48         const ::testing::TestInfo* const test_info =
49                 ::testing::UnitTest::GetInstance()->current_test_info();
50         ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
51     }
~FlagManagerTest()52     ~FlagManagerTest() override {
53         const ::testing::TestInfo* const test_info =
54                 ::testing::UnitTest::GetInstance()->current_test_info();
55         ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
56     }
57 
58     TestableFlagManager mFlagManager;
59 };
60 
TEST_F(FlagManagerTest,isSingleton)61 TEST_F(FlagManagerTest, isSingleton) {
62     EXPECT_EQ(&FlagManager::getInstance(), &FlagManager::getInstance());
63 }
64 
TEST_F(FlagManagerTest,legacyCreashesIfQueriedBeforeBoot)65 TEST_F(FlagManagerTest, legacyCreashesIfQueriedBeforeBoot) {
66     mFlagManager.markBootIncomplete();
67     EXPECT_DEATH(FlagManager::getInstance().test_flag(), "");
68 }
69 
TEST_F(FlagManagerTest,legacyReturnsOverride)70 TEST_F(FlagManagerTest, legacyReturnsOverride) {
71     EXPECT_CALL(mFlagManager, getBoolProperty).WillOnce(Return(true));
72     EXPECT_EQ(true, mFlagManager.test_flag());
73 
74     EXPECT_CALL(mFlagManager, getBoolProperty).WillOnce(Return(false));
75     EXPECT_EQ(false, mFlagManager.test_flag());
76 }
77 
TEST_F(FlagManagerTest,legacyReturnsValue)78 TEST_F(FlagManagerTest, legacyReturnsValue) {
79     EXPECT_CALL(mFlagManager, getBoolProperty).WillRepeatedly(Return(std::nullopt));
80 
81     EXPECT_CALL(mFlagManager, getServerConfigurableFlag).WillOnce(Return(true));
82     EXPECT_EQ(true, mFlagManager.test_flag());
83 
84     EXPECT_CALL(mFlagManager, getServerConfigurableFlag).WillOnce(Return(false));
85     EXPECT_EQ(false, mFlagManager.test_flag());
86 }
87 
TEST_F(FlagManagerTest,crashesIfQueriedBeforeBoot)88 TEST_F(FlagManagerTest, crashesIfQueriedBeforeBoot) {
89     mFlagManager.markBootIncomplete();
90     EXPECT_DEATH(FlagManager::getInstance()
91         .refresh_rate_overlay_on_external_display(), "");
92 }
93 
TEST_F(FlagManagerTest,returnsOverrideTrue)94 TEST_F(FlagManagerTest, returnsOverrideTrue) {
95     mFlagManager.markBootCompleted();
96 
97     SET_FLAG_FOR_TEST(flags::refresh_rate_overlay_on_external_display, false);
98 
99     // This is stored in a static variable, so this test depends on the fact
100     // that this flag has not been read in this process.
101     EXPECT_CALL(mFlagManager, getBoolProperty).WillOnce(Return(true));
102     EXPECT_TRUE(mFlagManager.refresh_rate_overlay_on_external_display());
103 
104     // Further calls will not result in further calls to getBoolProperty.
105     EXPECT_TRUE(mFlagManager.refresh_rate_overlay_on_external_display());
106 }
107 
TEST_F(FlagManagerTest,returnsOverrideReadonly)108 TEST_F(FlagManagerTest, returnsOverrideReadonly) {
109     SET_FLAG_FOR_TEST(flags::add_sf_skipped_frames_to_trace, false);
110 
111     // This is stored in a static variable, so this test depends on the fact
112     // that this flag has not been read in this process.
113     EXPECT_CALL(mFlagManager, getBoolProperty).WillOnce(Return(true));
114     EXPECT_TRUE(mFlagManager.add_sf_skipped_frames_to_trace());
115 }
116 
117 // disabling this test since we need to use a unique flag for this test,
118 // but we only one server flag currently. Re-enable once we have a new flag
119 // and change this test to use a unique flag.
TEST_F(FlagManagerTest,DISABLED_returnsOverrideFalse)120 TEST_F(FlagManagerTest, DISABLED_returnsOverrideFalse) {
121     mFlagManager.markBootCompleted();
122 
123     SET_FLAG_FOR_TEST(flags::refresh_rate_overlay_on_external_display, true);
124 
125     // This is stored in a static variable, so this test depends on the fact
126     // that this flag has not been read in this process.
127     EXPECT_CALL(mFlagManager, getBoolProperty).WillOnce(Return(false));
128     EXPECT_FALSE(mFlagManager.refresh_rate_overlay_on_external_display());
129 }
130 
TEST_F(FlagManagerTest,ignoresOverrideInUnitTestMode)131 TEST_F(FlagManagerTest, ignoresOverrideInUnitTestMode) {
132     mFlagManager.setUnitTestMode();
133 
134     SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
135 
136     // If this has not been called in this process, it will be called.
137     // Regardless, the result is ignored.
138     EXPECT_CALL(mFlagManager, getBoolProperty).WillRepeatedly(Return(false));
139 
140     EXPECT_EQ(true, mFlagManager.multithreaded_present());
141 }
142 
TEST_F(FlagManagerTest,returnsValue)143 TEST_F(FlagManagerTest, returnsValue) {
144     mFlagManager.setUnitTestMode();
145 
146     EXPECT_CALL(mFlagManager, getBoolProperty).WillRepeatedly(Return(std::nullopt));
147 
148     {
149         SET_FLAG_FOR_TEST(flags::refresh_rate_overlay_on_external_display, true);
150         EXPECT_EQ(true, mFlagManager.refresh_rate_overlay_on_external_display());
151     }
152 
153     {
154         SET_FLAG_FOR_TEST(flags::refresh_rate_overlay_on_external_display, false);
155         EXPECT_EQ(false, mFlagManager.refresh_rate_overlay_on_external_display());
156     }
157 }
158 
TEST_F(FlagManagerTest,readonlyReturnsValue)159 TEST_F(FlagManagerTest, readonlyReturnsValue) {
160     mFlagManager.setUnitTestMode();
161 
162     EXPECT_CALL(mFlagManager, getBoolProperty).WillRepeatedly(Return(std::nullopt));
163 
164     {
165         SET_FLAG_FOR_TEST(flags::misc1, true);
166         EXPECT_EQ(true, mFlagManager.misc1());
167     }
168 
169     {
170         SET_FLAG_FOR_TEST(flags::misc1, false);
171         EXPECT_EQ(false, mFlagManager.misc1());
172     }
173 }
174 
175 } // namespace android
176