xref: /aosp_15_r20/art/libarttools/system_properties_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2021 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "tools/system_properties.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "gmock/gmock.h"
20*795d594fSAndroid Build Coastguard Worker #include "gtest/gtest.h"
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker namespace art {
23*795d594fSAndroid Build Coastguard Worker namespace tools {
24*795d594fSAndroid Build Coastguard Worker namespace {
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker using ::testing::Return;
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker class MockSystemProperties : public SystemProperties {
29*795d594fSAndroid Build Coastguard Worker  public:
30*795d594fSAndroid Build Coastguard Worker   MOCK_METHOD(std::string, GetProperty, (const std::string& key), (const, override));
31*795d594fSAndroid Build Coastguard Worker };
32*795d594fSAndroid Build Coastguard Worker 
33*795d594fSAndroid Build Coastguard Worker class SystemPropertiesTest : public testing::Test {
34*795d594fSAndroid Build Coastguard Worker  protected:
35*795d594fSAndroid Build Coastguard Worker   MockSystemProperties system_properties_;
36*795d594fSAndroid Build Coastguard Worker };
37*795d594fSAndroid Build Coastguard Worker 
TEST_F(SystemPropertiesTest,Get)38*795d594fSAndroid Build Coastguard Worker TEST_F(SystemPropertiesTest, Get) {
39*795d594fSAndroid Build Coastguard Worker   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return("value_1"));
40*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(system_properties_.Get("key_1", /*default_value=*/"default"), "value_1");
41*795d594fSAndroid Build Coastguard Worker }
42*795d594fSAndroid Build Coastguard Worker 
TEST_F(SystemPropertiesTest,GetWithFallback)43*795d594fSAndroid Build Coastguard Worker TEST_F(SystemPropertiesTest, GetWithFallback) {
44*795d594fSAndroid Build Coastguard Worker   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return(""));
45*795d594fSAndroid Build Coastguard Worker   EXPECT_CALL(system_properties_, GetProperty("key_2")).WillOnce(Return("value_2"));
46*795d594fSAndroid Build Coastguard Worker   EXPECT_CALL(system_properties_, GetProperty("key_3")).WillOnce(Return("value_3"));
47*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(system_properties_.Get("key_1", "key_2", "key_3", /*default_value=*/"default"),
48*795d594fSAndroid Build Coastguard Worker             "value_2");
49*795d594fSAndroid Build Coastguard Worker }
50*795d594fSAndroid Build Coastguard Worker 
TEST_F(SystemPropertiesTest,GetDefault)51*795d594fSAndroid Build Coastguard Worker TEST_F(SystemPropertiesTest, GetDefault) {
52*795d594fSAndroid Build Coastguard Worker   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return(""));
53*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(system_properties_.Get("key_1", /*default_value=*/"default"), "default");
54*795d594fSAndroid Build Coastguard Worker }
55*795d594fSAndroid Build Coastguard Worker 
TEST_F(SystemPropertiesTest,GetOrEmpty)56*795d594fSAndroid Build Coastguard Worker TEST_F(SystemPropertiesTest, GetOrEmpty) {
57*795d594fSAndroid Build Coastguard Worker   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return("value_1"));
58*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(system_properties_.GetOrEmpty("key_1"), "value_1");
59*795d594fSAndroid Build Coastguard Worker }
60*795d594fSAndroid Build Coastguard Worker 
TEST_F(SystemPropertiesTest,GetOrEmptyWithFallback)61*795d594fSAndroid Build Coastguard Worker TEST_F(SystemPropertiesTest, GetOrEmptyWithFallback) {
62*795d594fSAndroid Build Coastguard Worker   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return(""));
63*795d594fSAndroid Build Coastguard Worker   EXPECT_CALL(system_properties_, GetProperty("key_2")).WillOnce(Return("value_2"));
64*795d594fSAndroid Build Coastguard Worker   EXPECT_CALL(system_properties_, GetProperty("key_3")).WillOnce(Return("value_3"));
65*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(system_properties_.GetOrEmpty("key_1", "key_2", "key_3"), "value_2");
66*795d594fSAndroid Build Coastguard Worker }
67*795d594fSAndroid Build Coastguard Worker 
TEST_F(SystemPropertiesTest,GetOrEmptyDefault)68*795d594fSAndroid Build Coastguard Worker TEST_F(SystemPropertiesTest, GetOrEmptyDefault) {
69*795d594fSAndroid Build Coastguard Worker   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return(""));
70*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(system_properties_.GetOrEmpty("key_1"), "");
71*795d594fSAndroid Build Coastguard Worker }
72*795d594fSAndroid Build Coastguard Worker 
TEST_F(SystemPropertiesTest,GetBoolTrue)73*795d594fSAndroid Build Coastguard Worker TEST_F(SystemPropertiesTest, GetBoolTrue) {
74*795d594fSAndroid Build Coastguard Worker   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return("true"));
75*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(system_properties_.GetBool("key_1", /*default_value=*/false), true);
76*795d594fSAndroid Build Coastguard Worker }
77*795d594fSAndroid Build Coastguard Worker 
TEST_F(SystemPropertiesTest,GetBoolFalse)78*795d594fSAndroid Build Coastguard Worker TEST_F(SystemPropertiesTest, GetBoolFalse) {
79*795d594fSAndroid Build Coastguard Worker   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return("false"));
80*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(system_properties_.GetBool("key_1", /*default_value=*/true), false);
81*795d594fSAndroid Build Coastguard Worker }
82*795d594fSAndroid Build Coastguard Worker 
TEST_F(SystemPropertiesTest,GetBoolWithFallback)83*795d594fSAndroid Build Coastguard Worker TEST_F(SystemPropertiesTest, GetBoolWithFallback) {
84*795d594fSAndroid Build Coastguard Worker   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return(""));
85*795d594fSAndroid Build Coastguard Worker   EXPECT_CALL(system_properties_, GetProperty("key_2")).WillOnce(Return("true"));
86*795d594fSAndroid Build Coastguard Worker   EXPECT_CALL(system_properties_, GetProperty("key_3")).WillOnce(Return("false"));
87*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(system_properties_.GetBool("key_1", "key_2", "key_3", /*default_value=*/false), true);
88*795d594fSAndroid Build Coastguard Worker }
89*795d594fSAndroid Build Coastguard Worker 
TEST_F(SystemPropertiesTest,GetBoolDefault)90*795d594fSAndroid Build Coastguard Worker TEST_F(SystemPropertiesTest, GetBoolDefault) {
91*795d594fSAndroid Build Coastguard Worker   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return(""));
92*795d594fSAndroid Build Coastguard Worker   EXPECT_EQ(system_properties_.GetBool("key_1", /*default_value=*/true), true);
93*795d594fSAndroid Build Coastguard Worker }
94*795d594fSAndroid Build Coastguard Worker 
95*795d594fSAndroid Build Coastguard Worker }  // namespace
96*795d594fSAndroid Build Coastguard Worker }  // namespace tools
97*795d594fSAndroid Build Coastguard Worker }  // namespace art
98