rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 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 | |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "FlagManagerTest" |
| 19 | |
| 20 | #include "FlagManager.h" |
| 21 | |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 22 | #include <gmock/gmock.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | #include <log/log.h> |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | using testing::Return; |
| 29 | |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame^] | 30 | class TestableFlagManager : public FlagManager { |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 31 | public: |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame^] | 32 | TestableFlagManager() : FlagManager(ConstructorTag{}) { markBootCompleted(); } |
| 33 | ~TestableFlagManager() = default; |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 34 | |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame^] | 35 | MOCK_METHOD(std::optional<bool>, getBoolProperty, (const char*), (const, override)); |
| 36 | MOCK_METHOD(bool, getServerConfigurableFlag, (const char*), (const, override)); |
| 37 | |
| 38 | void markBootIncomplete() { mBootCompleted = false; } |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | class FlagManagerTest : public testing::Test { |
| 42 | public: |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame^] | 43 | FlagManagerTest() { |
| 44 | const ::testing::TestInfo* const test_info = |
| 45 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 46 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 47 | } |
| 48 | ~FlagManagerTest() override { |
| 49 | const ::testing::TestInfo* const test_info = |
| 50 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 51 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 52 | } |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 53 | |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame^] | 54 | TestableFlagManager mFlagManager; |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame^] | 57 | TEST_F(FlagManagerTest, isSingleton) { |
| 58 | EXPECT_EQ(&FlagManager::getInstance(), &FlagManager::getInstance()); |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame^] | 61 | TEST_F(FlagManagerTest, creashesIfQueriedBeforeBoot) { |
| 62 | mFlagManager.markBootIncomplete(); |
| 63 | EXPECT_DEATH(FlagManager::getInstance().use_adpf_cpu_hint(), ""); |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame^] | 66 | TEST_F(FlagManagerTest, returnsOverride) { |
| 67 | EXPECT_CALL(mFlagManager, getBoolProperty).WillOnce(Return(true)); |
| 68 | EXPECT_EQ(true, mFlagManager.test_flag()); |
| 69 | |
| 70 | EXPECT_CALL(mFlagManager, getBoolProperty).WillOnce(Return(false)); |
| 71 | EXPECT_EQ(false, mFlagManager.test_flag()); |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Ady Abraham | c589dc4 | 2023-10-26 16:20:53 -0700 | [diff] [blame^] | 74 | TEST_F(FlagManagerTest, returnsValue) { |
| 75 | EXPECT_CALL(mFlagManager, getBoolProperty).WillRepeatedly(Return(std::nullopt)); |
| 76 | |
| 77 | EXPECT_CALL(mFlagManager, getServerConfigurableFlag).WillOnce(Return(true)); |
| 78 | EXPECT_EQ(true, mFlagManager.test_flag()); |
| 79 | |
| 80 | EXPECT_CALL(mFlagManager, getServerConfigurableFlag).WillOnce(Return(false)); |
| 81 | EXPECT_EQ(false, mFlagManager.test_flag()); |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 82 | } |
| 83 | |
rnlee | 81d3260 | 2021-07-27 13:24:07 -0700 | [diff] [blame] | 84 | } // namespace android |