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