blob: a5b347a43c4dfb577095d7a0534e6f203b92a172 [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
Ady Abraham370bae92024-02-07 23:42:50 +000031using namespace com::android::graphics::surfaceflinger;
rnlee81d32602021-07-27 13:24:07 -070032using testing::Return;
33
Ady Abrahamc589dc42023-10-26 16:20:53 -070034class TestableFlagManager : public FlagManager {
rnlee81d32602021-07-27 13:24:07 -070035public:
Ady Abrahamc589dc42023-10-26 16:20:53 -070036 TestableFlagManager() : FlagManager(ConstructorTag{}) { markBootCompleted(); }
37 ~TestableFlagManager() = default;
rnlee81d32602021-07-27 13:24:07 -070038
Ady Abrahamc589dc42023-10-26 16:20:53 -070039 MOCK_METHOD(std::optional<bool>, getBoolProperty, (const char*), (const, override));
40 MOCK_METHOD(bool, getServerConfigurableFlag, (const char*), (const, override));
41
42 void markBootIncomplete() { mBootCompleted = false; }
rnlee81d32602021-07-27 13:24:07 -070043};
44
45class FlagManagerTest : public testing::Test {
46public:
Ady Abrahamc589dc42023-10-26 16:20:53 -070047 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 }
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 }
rnlee81d32602021-07-27 13:24:07 -070057
Ady Abrahamc589dc42023-10-26 16:20:53 -070058 TestableFlagManager mFlagManager;
rnlee81d32602021-07-27 13:24:07 -070059};
60
Ady Abrahamc589dc42023-10-26 16:20:53 -070061TEST_F(FlagManagerTest, isSingleton) {
62 EXPECT_EQ(&FlagManager::getInstance(), &FlagManager::getInstance());
rnlee81d32602021-07-27 13:24:07 -070063}
64
Ady Abrahamd6d80162023-10-23 12:57:41 -070065TEST_F(FlagManagerTest, legacyCreashesIfQueriedBeforeBoot) {
Ady Abrahamc589dc42023-10-26 16:20:53 -070066 mFlagManager.markBootIncomplete();
Ady Abrahamd6d80162023-10-23 12:57:41 -070067 EXPECT_DEATH(FlagManager::getInstance().test_flag(), "");
rnlee81d32602021-07-27 13:24:07 -070068}
69
Ady Abrahamd6d80162023-10-23 12:57:41 -070070TEST_F(FlagManagerTest, legacyReturnsOverride) {
Ady Abrahamc589dc42023-10-26 16:20:53 -070071 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());
rnlee81d32602021-07-27 13:24:07 -070076}
77
Ady Abrahamd6d80162023-10-23 12:57:41 -070078TEST_F(FlagManagerTest, legacyReturnsValue) {
Ady Abrahamc589dc42023-10-26 16:20:53 -070079 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());
rnlee81d32602021-07-27 13:24:07 -070086}
87
Leon Scroggins IIId8e36f32023-11-22 14:40:36 -050088TEST_F(FlagManagerTest, returnsOverrideTrue) {
89 mFlagManager.markBootCompleted();
90
Ady Abraham370bae92024-02-07 23:42:50 +000091 SET_FLAG_FOR_TEST(flags::refresh_rate_overlay_on_external_display, false);
Leon Scroggins IIId8e36f32023-11-22 14:40:36 -050092
93 // This is stored in a static variable, so this test depends on the fact
94 // that this flag has not been read in this process.
95 EXPECT_CALL(mFlagManager, getBoolProperty).WillOnce(Return(true));
Ady Abraham370bae92024-02-07 23:42:50 +000096 EXPECT_TRUE(mFlagManager.refresh_rate_overlay_on_external_display());
Leon Scroggins IIId8e36f32023-11-22 14:40:36 -050097
98 // Further calls will not result in further calls to getBoolProperty.
Ady Abraham370bae92024-02-07 23:42:50 +000099 EXPECT_TRUE(mFlagManager.refresh_rate_overlay_on_external_display());
Leon Scroggins IIId8e36f32023-11-22 14:40:36 -0500100}
101
102TEST_F(FlagManagerTest, returnsOverrideReadonly) {
Ady Abraham370bae92024-02-07 23:42:50 +0000103 SET_FLAG_FOR_TEST(flags::add_sf_skipped_frames_to_trace, false);
Leon Scroggins IIId8e36f32023-11-22 14:40:36 -0500104
105 // This is stored in a static variable, so this test depends on the fact
106 // that this flag has not been read in this process.
107 EXPECT_CALL(mFlagManager, getBoolProperty).WillOnce(Return(true));
108 EXPECT_TRUE(mFlagManager.add_sf_skipped_frames_to_trace());
109}
110
Ady Abrahamfea5dc62024-02-15 21:29:49 +0000111// disabling this test since we need to use a unique flag for this test,
112// but we only one server flag currently. Re-enable once we have a new flag
113// and change this test to use a unique flag.
114TEST_F(FlagManagerTest, DISABLED_returnsOverrideFalse) {
Leon Scroggins IIId8e36f32023-11-22 14:40:36 -0500115 mFlagManager.markBootCompleted();
116
Ady Abraham370bae92024-02-07 23:42:50 +0000117 SET_FLAG_FOR_TEST(flags::refresh_rate_overlay_on_external_display, true);
Leon Scroggins IIId8e36f32023-11-22 14:40:36 -0500118
119 // This is stored in a static variable, so this test depends on the fact
120 // that this flag has not been read in this process.
121 EXPECT_CALL(mFlagManager, getBoolProperty).WillOnce(Return(false));
122 EXPECT_FALSE(mFlagManager.refresh_rate_overlay_on_external_display());
123}
124
125TEST_F(FlagManagerTest, ignoresOverrideInUnitTestMode) {
Ady Abrahamd6d80162023-10-23 12:57:41 -0700126 mFlagManager.setUnitTestMode();
127
Ady Abraham370bae92024-02-07 23:42:50 +0000128 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
Ady Abrahamd6d80162023-10-23 12:57:41 -0700129
Leon Scroggins IIId8e36f32023-11-22 14:40:36 -0500130 // If this has not been called in this process, it will be called.
131 // Regardless, the result is ignored.
132 EXPECT_CALL(mFlagManager, getBoolProperty).WillRepeatedly(Return(false));
133
134 EXPECT_EQ(true, mFlagManager.multithreaded_present());
Ady Abrahamd6d80162023-10-23 12:57:41 -0700135}
136
137TEST_F(FlagManagerTest, returnsValue) {
138 mFlagManager.setUnitTestMode();
139
140 EXPECT_CALL(mFlagManager, getBoolProperty).WillRepeatedly(Return(std::nullopt));
141
142 {
Ady Abraham370bae92024-02-07 23:42:50 +0000143 SET_FLAG_FOR_TEST(flags::refresh_rate_overlay_on_external_display, true);
144 EXPECT_EQ(true, mFlagManager.refresh_rate_overlay_on_external_display());
Ady Abrahamd6d80162023-10-23 12:57:41 -0700145 }
146
147 {
Ady Abraham370bae92024-02-07 23:42:50 +0000148 SET_FLAG_FOR_TEST(flags::refresh_rate_overlay_on_external_display, false);
149 EXPECT_EQ(false, mFlagManager.refresh_rate_overlay_on_external_display());
Ady Abrahamd6d80162023-10-23 12:57:41 -0700150 }
151}
152
Ady Abrahamd6d80162023-10-23 12:57:41 -0700153TEST_F(FlagManagerTest, readonlyReturnsValue) {
154 mFlagManager.setUnitTestMode();
155
156 EXPECT_CALL(mFlagManager, getBoolProperty).WillRepeatedly(Return(std::nullopt));
157
158 {
Ady Abraham370bae92024-02-07 23:42:50 +0000159 SET_FLAG_FOR_TEST(flags::misc1, true);
Ady Abrahamd6d80162023-10-23 12:57:41 -0700160 EXPECT_EQ(true, mFlagManager.misc1());
161 }
162
163 {
Ady Abraham370bae92024-02-07 23:42:50 +0000164 SET_FLAG_FOR_TEST(flags::misc1, false);
Ady Abrahamd6d80162023-10-23 12:57:41 -0700165 EXPECT_EQ(false, mFlagManager.misc1());
166 }
167}
168
rnlee81d32602021-07-27 13:24:07 -0700169} // namespace android