1 /*
2  * Copyright (C) 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 "tools/system_properties.h"
18 
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 
22 namespace art {
23 namespace tools {
24 namespace {
25 
26 using ::testing::Return;
27 
28 class MockSystemProperties : public SystemProperties {
29  public:
30   MOCK_METHOD(std::string, GetProperty, (const std::string& key), (const, override));
31 };
32 
33 class SystemPropertiesTest : public testing::Test {
34  protected:
35   MockSystemProperties system_properties_;
36 };
37 
TEST_F(SystemPropertiesTest,Get)38 TEST_F(SystemPropertiesTest, Get) {
39   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return("value_1"));
40   EXPECT_EQ(system_properties_.Get("key_1", /*default_value=*/"default"), "value_1");
41 }
42 
TEST_F(SystemPropertiesTest,GetWithFallback)43 TEST_F(SystemPropertiesTest, GetWithFallback) {
44   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return(""));
45   EXPECT_CALL(system_properties_, GetProperty("key_2")).WillOnce(Return("value_2"));
46   EXPECT_CALL(system_properties_, GetProperty("key_3")).WillOnce(Return("value_3"));
47   EXPECT_EQ(system_properties_.Get("key_1", "key_2", "key_3", /*default_value=*/"default"),
48             "value_2");
49 }
50 
TEST_F(SystemPropertiesTest,GetDefault)51 TEST_F(SystemPropertiesTest, GetDefault) {
52   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return(""));
53   EXPECT_EQ(system_properties_.Get("key_1", /*default_value=*/"default"), "default");
54 }
55 
TEST_F(SystemPropertiesTest,GetOrEmpty)56 TEST_F(SystemPropertiesTest, GetOrEmpty) {
57   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return("value_1"));
58   EXPECT_EQ(system_properties_.GetOrEmpty("key_1"), "value_1");
59 }
60 
TEST_F(SystemPropertiesTest,GetOrEmptyWithFallback)61 TEST_F(SystemPropertiesTest, GetOrEmptyWithFallback) {
62   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return(""));
63   EXPECT_CALL(system_properties_, GetProperty("key_2")).WillOnce(Return("value_2"));
64   EXPECT_CALL(system_properties_, GetProperty("key_3")).WillOnce(Return("value_3"));
65   EXPECT_EQ(system_properties_.GetOrEmpty("key_1", "key_2", "key_3"), "value_2");
66 }
67 
TEST_F(SystemPropertiesTest,GetOrEmptyDefault)68 TEST_F(SystemPropertiesTest, GetOrEmptyDefault) {
69   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return(""));
70   EXPECT_EQ(system_properties_.GetOrEmpty("key_1"), "");
71 }
72 
TEST_F(SystemPropertiesTest,GetBoolTrue)73 TEST_F(SystemPropertiesTest, GetBoolTrue) {
74   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return("true"));
75   EXPECT_EQ(system_properties_.GetBool("key_1", /*default_value=*/false), true);
76 }
77 
TEST_F(SystemPropertiesTest,GetBoolFalse)78 TEST_F(SystemPropertiesTest, GetBoolFalse) {
79   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return("false"));
80   EXPECT_EQ(system_properties_.GetBool("key_1", /*default_value=*/true), false);
81 }
82 
TEST_F(SystemPropertiesTest,GetBoolWithFallback)83 TEST_F(SystemPropertiesTest, GetBoolWithFallback) {
84   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return(""));
85   EXPECT_CALL(system_properties_, GetProperty("key_2")).WillOnce(Return("true"));
86   EXPECT_CALL(system_properties_, GetProperty("key_3")).WillOnce(Return("false"));
87   EXPECT_EQ(system_properties_.GetBool("key_1", "key_2", "key_3", /*default_value=*/false), true);
88 }
89 
TEST_F(SystemPropertiesTest,GetBoolDefault)90 TEST_F(SystemPropertiesTest, GetBoolDefault) {
91   EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return(""));
92   EXPECT_EQ(system_properties_.GetBool("key_1", /*default_value=*/true), true);
93 }
94 
95 }  // namespace
96 }  // namespace tools
97 }  // namespace art
98