blob: 0c820fb291c02d24a3501e0aba8fa9736b88ed74 [file] [log] [blame]
rnlee81d32602021-07-27 13:24:07 -07001/*
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
rnlee81d32602021-07-27 13:24:07 -070017#undef LOG_TAG
18#define LOG_TAG "FlagManagerTest"
19
Alec Mouri9b133ca2023-11-14 19:00:01 +000020#include <common/FlagManager.h>
Ady Abraham3db8a3c2023-11-20 17:53:47 -080021#include <common/test/FlagUtils.h>
rnlee81d32602021-07-27 13:24:07 -070022
rnlee81d32602021-07-27 13:24:07 -070023#include <gmock/gmock.h>
24#include <gtest/gtest.h>
25#include <log/log.h>
rnlee81d32602021-07-27 13:24:07 -070026
Ady Abrahamd6d80162023-10-23 12:57:41 -070027#include <com_android_graphics_surfaceflinger_flags.h>
28
rnlee81d32602021-07-27 13:24:07 -070029namespace android {
30
31using testing::Return;
32
Ady Abrahamc589dc42023-10-26 16:20:53 -070033class TestableFlagManager : public FlagManager {
rnlee81d32602021-07-27 13:24:07 -070034public:
Ady Abrahamc589dc42023-10-26 16:20:53 -070035 TestableFlagManager() : FlagManager(ConstructorTag{}) { markBootCompleted(); }
36 ~TestableFlagManager() = default;
rnlee81d32602021-07-27 13:24:07 -070037
Ady Abrahamc589dc42023-10-26 16:20:53 -070038 MOCK_METHOD(std::optional<bool>, getBoolProperty, (const char*), (const, override));
39 MOCK_METHOD(bool, getServerConfigurableFlag, (const char*), (const, override));
40
41 void markBootIncomplete() { mBootCompleted = false; }
rnlee81d32602021-07-27 13:24:07 -070042};
43
44class FlagManagerTest : public testing::Test {
45public:
Ady Abrahamc589dc42023-10-26 16:20:53 -070046 FlagManagerTest() {
47 const ::testing::TestInfo* const test_info =
48 ::testing::UnitTest::GetInstance()->current_test_info();
49 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
50 }
51 ~FlagManagerTest() override {
52 const ::testing::TestInfo* const test_info =
53 ::testing::UnitTest::GetInstance()->current_test_info();
54 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
55 }
rnlee81d32602021-07-27 13:24:07 -070056
Ady Abrahamc589dc42023-10-26 16:20:53 -070057 TestableFlagManager mFlagManager;
rnlee81d32602021-07-27 13:24:07 -070058};
59
Ady Abrahamc589dc42023-10-26 16:20:53 -070060TEST_F(FlagManagerTest, isSingleton) {
61 EXPECT_EQ(&FlagManager::getInstance(), &FlagManager::getInstance());
rnlee81d32602021-07-27 13:24:07 -070062}
63
Ady Abrahamd6d80162023-10-23 12:57:41 -070064TEST_F(FlagManagerTest, legacyCreashesIfQueriedBeforeBoot) {
Ady Abrahamc589dc42023-10-26 16:20:53 -070065 mFlagManager.markBootIncomplete();
Ady Abrahamd6d80162023-10-23 12:57:41 -070066 EXPECT_DEATH(FlagManager::getInstance().test_flag(), "");
rnlee81d32602021-07-27 13:24:07 -070067}
68
Ady Abrahamd6d80162023-10-23 12:57:41 -070069TEST_F(FlagManagerTest, legacyReturnsOverride) {
Ady Abrahamc589dc42023-10-26 16:20:53 -070070 EXPECT_CALL(mFlagManager, getBoolProperty).WillOnce(Return(true));
71 EXPECT_EQ(true, mFlagManager.test_flag());
72
73 EXPECT_CALL(mFlagManager, getBoolProperty).WillOnce(Return(false));
74 EXPECT_EQ(false, mFlagManager.test_flag());
rnlee81d32602021-07-27 13:24:07 -070075}
76
Ady Abrahamd6d80162023-10-23 12:57:41 -070077TEST_F(FlagManagerTest, legacyReturnsValue) {
Ady Abrahamc589dc42023-10-26 16:20:53 -070078 EXPECT_CALL(mFlagManager, getBoolProperty).WillRepeatedly(Return(std::nullopt));
79
80 EXPECT_CALL(mFlagManager, getServerConfigurableFlag).WillOnce(Return(true));
81 EXPECT_EQ(true, mFlagManager.test_flag());
82
83 EXPECT_CALL(mFlagManager, getServerConfigurableFlag).WillOnce(Return(false));
84 EXPECT_EQ(false, mFlagManager.test_flag());
rnlee81d32602021-07-27 13:24:07 -070085}
86
Ady Abrahamd6d80162023-10-23 12:57:41 -070087TEST_F(FlagManagerTest, creashesIfQueriedBeforeBoot) {
88 mFlagManager.markBootIncomplete();
89 EXPECT_DEATH(FlagManager::getInstance().late_boot_misc2(), "");
90}
91
Leon Scroggins IIId8e36f32023-11-22 14:40:36 -050092TEST_F(FlagManagerTest, returnsOverrideTrue) {
93 mFlagManager.markBootCompleted();
94
95 SET_FLAG_FOR_TEST(com::android::graphics::surfaceflinger::flags::late_boot_misc2, false);
96
97 // This is stored in a static variable, so this test depends on the fact
98 // that this flag has not been read in this process.
99 EXPECT_CALL(mFlagManager, getBoolProperty).WillOnce(Return(true));
100 EXPECT_TRUE(mFlagManager.late_boot_misc2());
101
102 // Further calls will not result in further calls to getBoolProperty.
103 EXPECT_TRUE(mFlagManager.late_boot_misc2());
104}
105
106TEST_F(FlagManagerTest, returnsOverrideReadonly) {
107 SET_FLAG_FOR_TEST(com::android::graphics::surfaceflinger::flags::add_sf_skipped_frames_to_trace,
108 false);
109
110 // This is stored in a static variable, so this test depends on the fact
111 // that this flag has not been read in this process.
112 EXPECT_CALL(mFlagManager, getBoolProperty).WillOnce(Return(true));
113 EXPECT_TRUE(mFlagManager.add_sf_skipped_frames_to_trace());
114}
115
116TEST_F(FlagManagerTest, returnsOverrideFalse) {
117 mFlagManager.markBootCompleted();
118
119 SET_FLAG_FOR_TEST(com::android::graphics::surfaceflinger::flags::
120 refresh_rate_overlay_on_external_display,
121 true);
122
123 // This is stored in a static variable, so this test depends on the fact
124 // that this flag has not been read in this process.
125 EXPECT_CALL(mFlagManager, getBoolProperty).WillOnce(Return(false));
126 EXPECT_FALSE(mFlagManager.refresh_rate_overlay_on_external_display());
127}
128
129TEST_F(FlagManagerTest, ignoresOverrideInUnitTestMode) {
Ady Abrahamd6d80162023-10-23 12:57:41 -0700130 mFlagManager.setUnitTestMode();
131
Leon Scroggins IIId8e36f32023-11-22 14:40:36 -0500132 SET_FLAG_FOR_TEST(com::android::graphics::surfaceflinger::flags::multithreaded_present, true);
Ady Abrahamd6d80162023-10-23 12:57:41 -0700133
Leon Scroggins IIId8e36f32023-11-22 14:40:36 -0500134 // If this has not been called in this process, it will be called.
135 // Regardless, the result is ignored.
136 EXPECT_CALL(mFlagManager, getBoolProperty).WillRepeatedly(Return(false));
137
138 EXPECT_EQ(true, mFlagManager.multithreaded_present());
Ady Abrahamd6d80162023-10-23 12:57:41 -0700139}
140
141TEST_F(FlagManagerTest, returnsValue) {
142 mFlagManager.setUnitTestMode();
143
144 EXPECT_CALL(mFlagManager, getBoolProperty).WillRepeatedly(Return(std::nullopt));
145
146 {
147 SET_FLAG_FOR_TEST(com::android::graphics::surfaceflinger::flags::late_boot_misc2, true);
148 EXPECT_EQ(true, mFlagManager.late_boot_misc2());
149 }
150
151 {
152 SET_FLAG_FOR_TEST(com::android::graphics::surfaceflinger::flags::late_boot_misc2, false);
153 EXPECT_EQ(false, mFlagManager.late_boot_misc2());
154 }
155}
156
Ady Abrahamd6d80162023-10-23 12:57:41 -0700157TEST_F(FlagManagerTest, readonlyReturnsValue) {
158 mFlagManager.setUnitTestMode();
159
160 EXPECT_CALL(mFlagManager, getBoolProperty).WillRepeatedly(Return(std::nullopt));
161
162 {
163 SET_FLAG_FOR_TEST(com::android::graphics::surfaceflinger::flags::misc1, true);
164 EXPECT_EQ(true, mFlagManager.misc1());
165 }
166
167 {
168 SET_FLAG_FOR_TEST(com::android::graphics::surfaceflinger::flags::misc1, false);
169 EXPECT_EQ(false, mFlagManager.misc1());
170 }
171}
172
173TEST_F(FlagManagerTest, dontSkipOnEarlyIsNotCached) {
174 EXPECT_CALL(mFlagManager, getBoolProperty).WillRepeatedly(Return(std::nullopt));
175
176 const auto initialValue = com::android::graphics::surfaceflinger::flags::dont_skip_on_early();
177
178 com::android::graphics::surfaceflinger::flags::dont_skip_on_early(true);
179 EXPECT_EQ(true, mFlagManager.dont_skip_on_early());
180
181 com::android::graphics::surfaceflinger::flags::dont_skip_on_early(false);
182 EXPECT_EQ(false, mFlagManager.dont_skip_on_early());
183
184 com::android::graphics::surfaceflinger::flags::dont_skip_on_early(initialValue);
185}
186
rnlee81d32602021-07-27 13:24:07 -0700187} // namespace android