|
| 1 | +// Copyright 2025 The Cobalt Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "starboard/android/shared/features_extension.h" |
| 16 | + |
| 17 | +#include <any> |
| 18 | +#include <iterator> |
| 19 | +#include <variant> |
| 20 | + |
| 21 | +#include "starboard/shared/starboard/feature_list.h" |
| 22 | +#include "starboard/shared/starboard/features.h" |
| 23 | +#include "starboard/system.h" |
| 24 | +#include "testing/gtest/include/gtest/gtest.h" |
| 25 | + |
| 26 | +namespace starboard::android::shared { |
| 27 | +namespace { |
| 28 | +using starboard::features::FeatureList; |
| 29 | +using starboard::features::SbFeatureParamExt; |
| 30 | + |
| 31 | +using ParamValue = std::variant<bool, int, double, std::string, int64_t>; |
| 32 | + |
| 33 | +const SbFeature FeatureTestBoolTrue = {"FeatureTestBoolTrue", true}; |
| 34 | +const SbFeature FeatureTestBoolFalse = {"FeatureTestBoolFalse", false}; |
| 35 | + |
| 36 | +const SbFeatureParam ParamTestBool = {"FeatureTestBoolTrue", |
| 37 | + "ParamBool", |
| 38 | + SbFeatureParamTypeBool, |
| 39 | + {.bool_value = true}}; |
| 40 | +const SbFeatureParam ParamTestInt = {"FeatureTestBoolTrue", |
| 41 | + "ParamInt", |
| 42 | + SbFeatureParamTypeInt, |
| 43 | + {.int_value = 10}}; |
| 44 | +const SbFeatureParam ParamTestDouble = {"FeatureTestBoolTrue", |
| 45 | + "ParamDouble", |
| 46 | + SbFeatureParamTypeDouble, |
| 47 | + {.double_value = 3.14}}; |
| 48 | +const SbFeatureParam ParamTestString = {"FeatureTestBoolTrue", |
| 49 | + "ParamString", |
| 50 | + SbFeatureParamTypeString, |
| 51 | + {.string_value = "Sunny day"}}; |
| 52 | +const SbFeatureParam ParamTestTime = {"FeatureTestBoolTrue", |
| 53 | + "ParamTime", |
| 54 | + SbFeatureParamTypeTime, |
| 55 | + {.time_value = 123456789}}; |
| 56 | + |
| 57 | +const SbFeature features[] = {FeatureTestBoolTrue, FeatureTestBoolFalse}; |
| 58 | +const SbFeatureParam params[] = {ParamTestBool, ParamTestInt, ParamTestDouble, |
| 59 | + ParamTestString, ParamTestTime}; |
| 60 | + |
| 61 | +struct ParamTestCase { |
| 62 | + std::string test_name; |
| 63 | + SbFeatureParam param_definition; |
| 64 | + ParamValue expected_value; |
| 65 | +}; |
| 66 | + |
| 67 | +} // namespace |
| 68 | + |
| 69 | +class FeatureExtensionTest : public ::testing::Test { |
| 70 | + protected: |
| 71 | + static void SetUpTestSuite() { |
| 72 | + const StarboardExtensionFeaturesApi* extension_api = |
| 73 | + static_cast<const StarboardExtensionFeaturesApi*>( |
| 74 | + SbSystemGetExtension(kStarboardExtensionFeaturesName)); |
| 75 | + |
| 76 | + extension_api->InitializeStarboardFeatures(features, std::size(features), |
| 77 | + params, std::size(params)); |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +TEST_F(FeatureExtensionTest, CanAccessFeatures) { |
| 82 | + EXPECT_TRUE(FeatureList::IsEnabled(FeatureTestBoolTrue)); |
| 83 | + EXPECT_FALSE(FeatureList::IsEnabled(FeatureTestBoolFalse)); |
| 84 | +} |
| 85 | + |
| 86 | +class ParamExtensionTest : public ::testing::Test, |
| 87 | + public ::testing::WithParamInterface<ParamTestCase> { |
| 88 | +}; |
| 89 | + |
| 90 | +TEST_P(ParamExtensionTest, CanAccessParams) { |
| 91 | + const auto& test_case = GetParam(); |
| 92 | + const auto& param_def = test_case.param_definition; |
| 93 | + |
| 94 | + // std::visit will select the right code to run based on the |
| 95 | + // type currently held in the 'expected_value' variant. |
| 96 | + std::visit( |
| 97 | + [&](const auto& expected_value) { |
| 98 | + // Get the type T from the expected_value (e.g., bool, int, ...) |
| 99 | + using T = std::decay_t<decltype(expected_value)>; |
| 100 | + |
| 101 | + // The logic is now generic and uses the deduced type T |
| 102 | + auto key = |
| 103 | + SbFeatureParamExt<T>(FeatureTestBoolTrue, param_def.param_name); |
| 104 | + auto actual_value = FeatureList::GetParam(key); |
| 105 | + |
| 106 | + EXPECT_EQ(actual_value, expected_value); |
| 107 | + }, |
| 108 | + test_case.expected_value); |
| 109 | +} |
| 110 | + |
| 111 | +INSTANTIATE_TEST_SUITE_P( |
| 112 | + ParamTests, |
| 113 | + ParamExtensionTest, |
| 114 | + ::testing::Values( |
| 115 | + ParamTestCase{"Bool", ParamTestBool, ParamTestBool.value.bool_value}, |
| 116 | + ParamTestCase{"Int", ParamTestInt, ParamTestInt.value.int_value}, |
| 117 | + ParamTestCase{"Double", ParamTestDouble, |
| 118 | + ParamTestDouble.value.double_value}, |
| 119 | + ParamTestCase{"String", ParamTestString, |
| 120 | + ParamTestString.value.string_value}, |
| 121 | + ParamTestCase{"Time", ParamTestTime, ParamTestTime.value.time_value}), |
| 122 | + // This lambda tells gtest how to name each test case |
| 123 | + [](const auto& info) { return info.param.test_name; }); |
| 124 | + |
| 125 | +} // namespace starboard::android::shared |
0 commit comments