1 /* 2 * Copyright 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 #pragma once 18 19 #include <common/FlagManager.h> 20 21 // indirection to resolve __LINE__ in SET_FLAG_FOR_TEST, it's used to create a unique TestFlagSetter 22 // setter var name everytime so multiple flags can be set in a test 23 #define CONCAT_INNER(a, b) a##b 24 #define CONCAT(a, b) CONCAT_INNER(a, b) 25 #define SET_FLAG_FOR_TEST(name, value) \ 26 TestFlagSetter CONCAT(_testFlag_, __LINE__) { \ 27 (name), (name), (value) \ 28 } 29 30 namespace android { 31 class TestFlagSetter { 32 public: TestFlagSetter(bool (* getter)(),void ((* setter)(bool)),bool flagValue)33 TestFlagSetter(bool (*getter)(), void((*setter)(bool)), bool flagValue) { 34 FlagManager::getMutableInstance().setUnitTestMode(); 35 36 const bool initialValue = getter(); 37 setter(flagValue); 38 mResetFlagValue = [=] { setter(initialValue); }; 39 } 40 ~TestFlagSetter()41 ~TestFlagSetter() { mResetFlagValue(); } 42 43 private: 44 std::function<void()> mResetFlagValue; 45 }; 46 47 } // namespace android 48