• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include <functional>
16 #include <chrono>
17 #include <thread>
18 #include <message_parcel.h>
19 
20 #include "gtest/gtest.h"
21 #include "singleton.h"
22 
23 #include "allow_type.h"
24 #include "istandby_service.h"
25 #include "resource_request.h"
26 #include "standby_ipc_interface_code.h"
27 #include "standby_service_client.h"
28 #include "standby_service_proxy.h"
29 #include "standby_service_subscriber_stub.h"
30 #include "standby_service_subscriber_proxy.h"
31 
32 using namespace testing::ext;
33 
34 namespace OHOS {
35 namespace DevStandbyMgr {
36 class StandbyServiceClientUnitTest : public testing::Test {
37 public:
SetUpTestCase()38     static void SetUpTestCase() {}
TearDownTestCase()39     static void TearDownTestCase() {}
SetUp()40     void SetUp() override {}
TearDown()41     void TearDown() override {}
42 };
43 
44 /**
45  * @tc.name: StandbyServiceClientUnitTest_001
46  * @tc.desc: test SubscribeStandbyCallback.
47  * @tc.type: FUNC
48  * @tc.require:
49  */
50 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_001, TestSize.Level1)
51 {
52     sptr<IStandbyServiceSubscriber> nullSubscriber = nullptr;
53     EXPECT_NE(StandbyServiceClient::GetInstance().SubscribeStandbyCallback(nullSubscriber), ERR_OK);
54     EXPECT_NE(StandbyServiceClient::GetInstance().SubscribeStandbyCallback(nullSubscriber), ERR_OK);
55     StandbyServiceClient::GetInstance().UnsubscribeStandbyCallback(nullSubscriber);
56     sptr<IStandbyServiceSubscriber> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
57     EXPECT_NE(StandbyServiceClient::GetInstance().SubscribeStandbyCallback(subscriber), ERR_OK);
58     StandbyServiceClient::GetInstance().SubscribeStandbyCallback(subscriber);
59     EXPECT_NE(StandbyServiceClient::GetInstance().UnsubscribeStandbyCallback(subscriber), ERR_OK);
60     EXPECT_NE(StandbyServiceClient::GetInstance().UnsubscribeStandbyCallback(subscriber), ERR_OK);
61 }
62 
63 /**
64  * @tc.name: StandbyServiceClientUnitTest_002
65  * @tc.desc: test ApplyAllowResource.
66  * @tc.type: FUNC
67  * @tc.require:
68  */
69 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_002, TestSize.Level1)
70 {
71     sptr<ResourceRequest> nullRequest = nullptr;
72     EXPECT_NE(StandbyServiceClient::GetInstance().ApplyAllowResource(nullRequest), ERR_OK);
73     EXPECT_NE(StandbyServiceClient::GetInstance().UnapplyAllowResource(nullRequest), ERR_OK);
74 
75     sptr<ResourceRequest> resouarceRequest = new (std::nothrow) ResourceRequest();
76     EXPECT_NE(StandbyServiceClient::GetInstance().ApplyAllowResource(resouarceRequest), ERR_OK);
77     EXPECT_NE(StandbyServiceClient::GetInstance().UnapplyAllowResource(resouarceRequest), ERR_OK);
78 
79     sptr<ResourceRequest> validResRequest = new (std::nothrow) ResourceRequest(AllowType::NETWORK,
80         0, "test_process", 100, "test", 1);
81     EXPECT_EQ(StandbyServiceClient::GetInstance().ApplyAllowResource(validResRequest), ERR_OK);
82     EXPECT_EQ(StandbyServiceClient::GetInstance().UnapplyAllowResource(validResRequest), ERR_OK);
83 }
84 
85 /**
86  * @tc.name: StandbyServiceClientUnitTest_003
87  * @tc.desc: test GetAllowList.
88  * @tc.type: FUNC
89  * @tc.require:
90  */
91 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_003, TestSize.Level1)
92 {
93     std::vector<AllowInfo> allowInfoList;
94     sptr<ResourceRequest> nullRequest = nullptr;
95     EXPECT_EQ(StandbyServiceClient::GetInstance().GetAllowList(AllowType::NETWORK, allowInfoList, 0), ERR_OK);
96     EXPECT_NE(StandbyServiceClient::GetInstance().GetAllowList(0, allowInfoList, 0), ERR_OK);
97     EXPECT_EQ(StandbyServiceClient::GetInstance().GetAllowList((1 << 6), allowInfoList, 0), ERR_OK);
98     allowInfoList.emplace_back(AllowInfo {});
99     StandbyServiceClient::GetInstance().GetAllowList((1 << 6), allowInfoList, 0);
100 }
101 
102 /**
103  * @tc.name: StandbyServiceClientUnitTest_004
104  * @tc.desc: test IsDeviceInStandby.
105  * @tc.type: FUNC
106  * @tc.require:
107  */
108 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_004, TestSize.Level1)
109 {
110     bool isStandby {false};
111     EXPECT_EQ(StandbyServiceClient::GetInstance().IsDeviceInStandby(isStandby), ERR_OK);
112 }
113 
114 /**
115  * @tc.name: StandbyServiceClientUnitTest_005
116  * @tc.desc: test Unmarshalling.
117  * @tc.type: FUNC
118  * @tc.require:
119  */
120 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_005, TestSize.Level1)
121 {
122     auto allowInfo = std::make_shared<AllowInfo>();
123     MessageParcel out;
124     EXPECT_TRUE(allowInfo->Marshalling(out));
125     MessageParcel data;
126     EXPECT_EQ(AllowInfo::Unmarshalling(data), nullptr);
127     EXPECT_EQ(ResourceRequest::Unmarshalling(data), nullptr);
128 }
129 
130 /**
131  * @tc.name: StandbyServiceClientUnitTest_006
132  * @tc.desc: test ResetStandbyServiceClient.
133  * @tc.type: FUNC
134  * @tc.require:
135  */
136 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_006, TestSize.Level1)
137 {
138     StandbyServiceClient::GetInstance().ResetStandbyServiceClient();
139     StandbyServiceClient::GetInstance().ResetStandbyServiceClient();
140     StandbyServiceClient::GetInstance().GetStandbyServiceProxy();
141     StandbyServiceClient::GetInstance().ResetStandbyServiceClient();
142     EXPECT_EQ(StandbyServiceClient::GetInstance().standbyServiceProxy_, nullptr);
143 }
144 
145 /**
146  * @tc.name: StandbyServiceClientUnitTest_007
147  * @tc.desc: test StandbyServiceSubscriberStub.
148  * @tc.type: FUNC
149  * @tc.require:
150  */
151 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_007, TestSize.Level1)
152 {
153     sptr<StandbyServiceSubscriberStub> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
154     MessageParcel data {};
155     MessageParcel reply {};
156     MessageOption option {};
157     subscriber->OnRemoteRequestInner(
158         (static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_ALLOW_LIST_CHANGED)), data, reply, option);
159     subscriber->OnRemoteRequestInner(
160         (static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_ALLOW_LIST_CHANGED)) + 1, data, reply, option);
161     EXPECT_NE(subscriber->HandleOnDeviceIdleMode(data), ERR_OK);
162     subscriber->HandleOnAllowListChanged(data);
163     data.WriteBool(false);
164     subscriber->HandleOnDeviceIdleMode(data);
165     data.WriteBool(false);
166     subscriber->HandleOnDeviceIdleMode(data);
167     subscriber->HandleOnAllowListChanged(data);
168     MessageParcel allowListData {};
169     subscriber->HandleOnAllowListChanged(allowListData);
170     allowListData.WriteInt32(0);
171     subscriber->HandleOnAllowListChanged(allowListData);
172     allowListData.WriteInt32(0);
173     subscriber->HandleOnAllowListChanged(allowListData);
174     allowListData.WriteString("");
175     subscriber->HandleOnAllowListChanged(allowListData);
176     allowListData.WriteUint32(0);
177     subscriber->HandleOnAllowListChanged(allowListData);
178     allowListData.WriteBool(false);
179     subscriber->HandleOnAllowListChanged(allowListData);
180 }
181 
182 /**
183  * @tc.name: StandbyServiceClientUnitTest_009
184  * @tc.desc: test ReportWorkSchedulerStatus.
185  * @tc.type: FUNC
186  * @tc.require:
187  */
188 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_009, TestSize.Level1)
189 {
190     EXPECT_EQ(StandbyServiceClient::GetInstance().ReportWorkSchedulerStatus(true, -1, ""), ERR_OK);
191     EXPECT_EQ(StandbyServiceClient::GetInstance().ReportWorkSchedulerStatus(false, -1, ""), ERR_OK);
192 }
193 
194 /**
195  * @tc.name: StandbyServiceClientUnitTest_010
196  * @tc.desc: test StandbyServiceSubscriberProxy.
197  * @tc.type: FUNC
198  * @tc.require:
199  */
200 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_010, TestSize.Level1)
201 {
202     sptr<IRemoteObject> impl {};
203     sptr<StandbyServiceSubscriberProxy> proxy = new (std::nothrow) StandbyServiceSubscriberProxy(impl);
204     sptr<StandbyServiceSubscriberProxy> nullSubscriber = nullptr;
205     proxy->OnDeviceIdleMode(false, false);
206     proxy->OnAllowListChanged(-1, "", 0, false);
207     EXPECT_NE(proxy, nullptr);
208 }
209 
210 /**
211  * @tc.name: StandbyServiceClientUnitTest_011
212  * @tc.desc: test IsStrategyEnabled.
213  * @tc.type: FUNC
214  * @tc.require:
215  */
216 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_011, TestSize.Level1)
217 {
218     std::string strategyName;
219     bool isEnabled =  false;
220     EXPECT_EQ(StandbyServiceClient::GetInstance().IsStrategyEnabled(strategyName, isEnabled), ERR_OK);
221 }
222 
223 /**
224  * @tc.name: StandbyServiceClientUnitTest_012
225  * @tc.desc: test IsStrategyEnabled.
226  * @tc.type: FUNC
227  * @tc.require:
228  */
229 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_012, TestSize.Level1)
230 {
231     DeviceStateType type = DeviceStateType::WIFI_P2P_CHANGE;
232     bool enabled = false;
233     EXPECT_EQ(StandbyServiceClient::GetInstance().ReportDeviceStateChanged(type, enabled), ERR_OK);
234 }
235 
236 /**
237  * @tc.name: StandbyServiceClientUnitTest_013
238  * @tc.desc: test IsStrategyEnabled.
239  * @tc.type: FUNC
240  * @tc.require:
241  */
242 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_013, TestSize.Level1)
243 {
244     uint32_t restrictType = 1;
245     std::vector<AllowInfo> restrictInfoList;
246     uint32_t reasonCode = ReasonCodeEnum::REASON_APP_API;
247     EXPECT_EQ(StandbyServiceClient::GetInstance().GetRestrictList(restrictType, restrictInfoList, reasonCode), ERR_OK);
248 }
249 
250 /**
251  * @tc.name: StandbyServiceClientUnitTest_014
252  * @tc.desc: test IsDeviceInStandby.
253  * @tc.type: FUNC
254  * @tc.require:
255  */
256 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_014, TestSize.Level1)
257 {
258     uint32_t type = 1;
259     bool enable = true;
260     uint32_t interval = 300;
261     EXPECT_EQ(StandbyServiceClient::GetInstance().SetNatInterval(type, enable, interval), ERR_PERMISSION_DENIED);
262     int64_t timestamp = 100;
263     EXPECT_EQ(StandbyServiceClient::GetInstance().DelayHeartBeat(timestamp), ERR_PERMISSION_DENIED);
264 }
265 
266 /**
267  * @tc.name: StandbyServiceClientUnitTest_015
268  * @tc.desc: test ReportPowerOverused.
269  * @tc.type: FUNC
270  * @tc.require:
271  */
272 HWTEST_F(StandbyServiceClientUnitTest, StandbyServiceClientUnitTest_015, TestSize.Level1)
273 {
274     std::string TEST_MODULE_NAME = "TestModule";
275     uint32_t level = static_cast<uint32_t>(PowerOverusedLevel::NORMAL);
276     EXPECT_EQ(StandbyServiceClient::GetInstance().ReportPowerOverused(TEST_MODULE_NAME, level), ERR_OK);
277 }
278 
279 }  // namespace DevStandbyMgr
280 }  // namespace OHOS
281