blob: 80300f0343d9bfcf6a2577095610f47a57439877 [file] [log] [blame]
Jiakai Zhange1262a62022-06-30 10:24:14 +01001/*
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 "system_properties.h"
18
19#include "gmock/gmock.h"
20#include "gtest/gtest.h"
21
22namespace art {
23namespace tools {
24namespace {
25
26using ::testing::Return;
27
28class MockSystemProperties : public SystemProperties {
29 public:
30 MOCK_METHOD(std::string, GetProperty, (const std::string& key), (const, override));
31};
32
33class SystemPropertiesTest : public testing::Test {
34 protected:
35 MockSystemProperties system_properties_;
36};
37
38TEST_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
43TEST_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
51TEST_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
56TEST_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
61TEST_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
68TEST_F(SystemPropertiesTest, GetOrEmptyDefault) {
69 EXPECT_CALL(system_properties_, GetProperty("key_1")).WillOnce(Return(""));
70 EXPECT_EQ(system_properties_.GetOrEmpty("key_1"), "");
71}
72
73TEST_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
78TEST_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
83TEST_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
90TEST_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