1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
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
16 #include <gtest/gtest.h>
17
18 #include <system_ability_definition.h>
19
20 #include "setting_provider.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Rosen {
27 namespace {
28 constexpr uint32_t SLEEP_TIME_US = 10000;
29 }
30 class SettingProviderTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp() override;
35 void TearDown() override;
36 };
37
SetUpTestCase()38 void SettingProviderTest::SetUpTestCase()
39 {
40 }
41
TearDownTestCase()42 void SettingProviderTest::TearDownTestCase()
43 {
44 }
45
SetUp()46 void SettingProviderTest::SetUp()
47 {
48 }
49
TearDown()50 void SettingProviderTest::TearDown()
51 {
52 usleep(SLEEP_TIME_US);
53 }
54
55 namespace {
56 /**
57 * @tc.name: GetIntValue
58 * @tc.desc: test function : GetIntValue
59 * @tc.type: FUNC
60 */
61 HWTEST_F(SettingProviderTest, GetIntValue, Function | SmallTest | Level1)
62 {
63 std::string key = "";
64 int32_t value = 0;
65 EXPECT_NE(SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).GetIntValue(key, value), ERR_OK);
66 }
67
68 /**
69 * @tc.name: GetLongValue
70 * @tc.desc: test function : GetLongValue
71 * @tc.type: FUNC
72 */
73 HWTEST_F(SettingProviderTest, GetLongValue, Function | SmallTest | Level1)
74 {
75 std::string key = "";
76 int64_t value = 0;
77 EXPECT_NE(SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).GetLongValue(key, value), ERR_OK);
78 }
79
80 /**
81 * @tc.name: GetBoolValue
82 * @tc.desc: test function : GetBoolValue
83 * @tc.type: FUNC
84 */
85 HWTEST_F(SettingProviderTest, GetBoolValue, Function | SmallTest | Level1)
86 {
87 std::string key = "";
88 bool value = false;
89 EXPECT_NE(SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).GetBoolValue(key, value), ERR_OK);
90 }
91
92 /**
93 * @tc.name: ExecRegisterCb01
94 * @tc.desc: test function : ExecRegisterCb
95 * @tc.type: FUNC
96 */
97 HWTEST_F(SettingProviderTest, ExecRegisterCb01, Function | SmallTest | Level1)
98 {
99 sptr<SettingObserver> observer = nullptr;
100 SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).ExecRegisterCb(observer);
101 EXPECT_EQ(observer, nullptr);
102 }
103
104 /**
105 * @tc.name: ExecRegisterCb02
106 * @tc.desc: test function : ExecRegisterCb
107 * @tc.type: FUNC
108 */
109 HWTEST_F(SettingProviderTest, ExecRegisterCb02, Function | SmallTest | Level1)
110 {
__anonbd6a443c0302(const std::string&) 111 SettingObserver::UpdateFunc updateFunc = [&](const std::string&) {};
112 auto observer = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).CreateObserver(
113 "settings.power.suspend_sources", updateFunc);
114 SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).ExecRegisterCb(observer);
115 EXPECT_NE(observer, nullptr);
116 }
117
118 /**
119 * @tc.name: UnregisterObserver
120 * @tc.desc: test function : UnregisterObserver
121 * @tc.type: FUNC
122 */
123 HWTEST_F(SettingProviderTest, UnregisterObserver, Function | SmallTest | Level1)
124 {
__anonbd6a443c0402(const std::string&) 125 SettingObserver::UpdateFunc updateFunc = [&](const std::string&) {};
126 auto observer = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).CreateObserver(
127 "settings.power.suspend_sources", updateFunc);
128 EXPECT_EQ(SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).UnregisterObserver(observer), ERR_OK);
129 }
130
131 /**
132 * @tc.name: PutStringValue01
133 * @tc.desc: test function : PutStringValue
134 * @tc.type: FUNC
135 */
136 HWTEST_F(SettingProviderTest, PutStringValue01, Function | SmallTest | Level1)
137 {
__anonbd6a443c0502(const std::string&) 138 SettingObserver::UpdateFunc updateFunc = [&](const std::string&) {};
139 std::string key = "key";
140 std::string value = "value";
141 bool needNotify = true;
142 auto observer = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).CreateObserver(
143 "settings.power.suspend_sources", updateFunc);
144 EXPECT_EQ(SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).PutStringValue(key, value, needNotify), ERR_OK);
145 }
146
147 /**
148 * @tc.name: PutStringValue02
149 * @tc.desc: test function : PutStringValue
150 * @tc.type: FUNC
151 */
152 HWTEST_F(SettingProviderTest, PutStringValue02, Function | SmallTest | Level1)
153 {
__anonbd6a443c0602(const std::string&) 154 SettingObserver::UpdateFunc updateFunc = [&](const std::string&) {};
155 std::string key = "key";
156 std::string value = "value";
157 bool needNotify = false;
158 auto observer = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).CreateObserver(
159 "settings.power.suspend_sources", updateFunc);
160 EXPECT_EQ(SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).PutStringValue(key, value, needNotify), ERR_OK);
161 }
162 }
163 }
164 }