blob: 0905cd14b1e88f343817cbce67a8ee9db1c98c0a [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
17#include <cstdint>
18#undef LOG_TAG
19#define LOG_TAG "FlagManagerTest"
20
21#include "FlagManager.h"
22
23#include <android-base/properties.h>
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
26#include <log/log.h>
27#include <server_configurable_flags/get_flags.h>
28#include <optional>
29
30namespace android {
31
32using testing::Return;
33
34class MockFlagManager : public FlagManager {
35public:
36 MockFlagManager() = default;
37 ~MockFlagManager() = default;
38
39 MOCK_METHOD(std::string, getServerConfigurableFlag, (const std::string& experimentFlagName),
40 (const, override));
41};
42
43class FlagManagerTest : public testing::Test {
44public:
45 FlagManagerTest();
46 ~FlagManagerTest() override;
47 std::unique_ptr<MockFlagManager> mFlagManager;
48
49 template <typename T>
50 T getValue(const std::string& experimentFlagName, std::optional<T> systemPropertyOpt,
51 T defaultValue);
52};
53
54FlagManagerTest::FlagManagerTest() {
55 const ::testing::TestInfo* const test_info =
56 ::testing::UnitTest::GetInstance()->current_test_info();
57 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
58 mFlagManager = std::make_unique<MockFlagManager>();
59}
60
61FlagManagerTest::~FlagManagerTest() {
62 const ::testing::TestInfo* const test_info =
63 ::testing::UnitTest::GetInstance()->current_test_info();
64 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
65}
66
67template <typename T>
68T FlagManagerTest::getValue(const std::string& experimentFlagName,
69 std::optional<T> systemPropertyOpt, T defaultValue) {
70 return mFlagManager->getValue(experimentFlagName, systemPropertyOpt, defaultValue);
71}
72
73namespace {
74TEST_F(FlagManagerTest, getValue_bool_default) {
75 EXPECT_CALL(*mFlagManager, getServerConfigurableFlag).Times(1).WillOnce(Return(""));
76 const bool defaultValue = false;
77 std::optional<bool> systemPropertyValue = std::nullopt;
78 const bool result = FlagManagerTest::getValue("test_flag", systemPropertyValue, defaultValue);
79 ASSERT_EQ(result, defaultValue);
80}
81
82TEST_F(FlagManagerTest, getValue_bool_sysprop) {
83 const bool defaultValue = false;
84 std::optional<bool> systemPropertyValue = std::make_optional(true);
85 const bool result = FlagManagerTest::getValue("test_flag", systemPropertyValue, defaultValue);
86 ASSERT_EQ(result, true);
87}
88
89TEST_F(FlagManagerTest, getValue_bool_experiment) {
90 EXPECT_CALL(*mFlagManager, getServerConfigurableFlag).Times(1).WillOnce(Return("1"));
91 const bool defaultValue = false;
92 std::optional<bool> systemPropertyValue = std::nullopt;
93 const bool result = FlagManagerTest::getValue("test_flag", systemPropertyValue, defaultValue);
94 ASSERT_EQ(result, true);
95}
96
97TEST_F(FlagManagerTest, getValue_int32_default) {
98 EXPECT_CALL(*mFlagManager, getServerConfigurableFlag).Times(1).WillOnce(Return(""));
99 int32_t defaultValue = 30;
100 std::optional<int32_t> systemPropertyValue = std::nullopt;
101 int32_t result = FlagManagerTest::getValue("test_flag", systemPropertyValue, defaultValue);
102 ASSERT_EQ(result, defaultValue);
103}
104
105TEST_F(FlagManagerTest, getValue_int32_sysprop) {
106 int32_t defaultValue = 30;
107 std::optional<int32_t> systemPropertyValue = std::make_optional(10);
108 int32_t result = FlagManagerTest::getValue("test_flag", systemPropertyValue, defaultValue);
109 ASSERT_EQ(result, 10);
110}
111
112TEST_F(FlagManagerTest, getValue_int32_experiment) {
113 EXPECT_CALL(*mFlagManager, getServerConfigurableFlag).Times(1).WillOnce(Return("50"));
114 std::int32_t defaultValue = 30;
115 std::optional<std::int32_t> systemPropertyValue = std::nullopt;
116 std::int32_t result = FlagManagerTest::getValue("test_flag", systemPropertyValue, defaultValue);
117 ASSERT_EQ(result, 50);
118}
119
120TEST_F(FlagManagerTest, getValue_int64_default) {
121 EXPECT_CALL(*mFlagManager, getServerConfigurableFlag).Times(1).WillOnce(Return(""));
122 int64_t defaultValue = 30;
123 std::optional<int64_t> systemPropertyValue = std::nullopt;
124 int64_t result = getValue("flag_name", systemPropertyValue, defaultValue);
125 ASSERT_EQ(result, defaultValue);
126}
127
128TEST_F(FlagManagerTest, getValue_int64_sysprop) {
129 int64_t defaultValue = 30;
130 std::optional<int64_t> systemPropertyValue = std::make_optional(10);
131 int64_t result = getValue("flag_name", systemPropertyValue, defaultValue);
132 ASSERT_EQ(result, 10);
133}
134
135TEST_F(FlagManagerTest, getValue_int64_experiment) {
136 EXPECT_CALL(*mFlagManager, getServerConfigurableFlag).Times(1).WillOnce(Return("50"));
137 int64_t defaultValue = 30;
138 std::optional<int64_t> systemPropertyValue = std::nullopt;
139 int64_t result = getValue("flag_name", systemPropertyValue, defaultValue);
140 ASSERT_EQ(result, 50);
141}
142} // namespace
143} // namespace android