1 /*
2  * Copyright (c) 2023-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 #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 "standby_service_client.h"
25 #include "standby_service_proxy.h"
26 #include "standby_service_subscriber_stub.h"
27 #include "mock_sa_service.h"
28 using namespace testing::ext;
29 
30 namespace OHOS {
31 namespace DevStandbyMgr {
32 class StandbyServiceClientUnitTest : public testing::Test {
33 public:
SetUpTestCase()34     static void SetUpTestCase() {}
TearDownTestCase()35     static void TearDownTestCase() {}
SetUp()36     void SetUp() override {}
TearDown()37     void TearDown() override {}
38 };
39 
40 /**
41  * @tc.name: MockStandbyServiceClientUnitTest_001
42  * @tc.desc: test MockFunction.
43  * @tc.type: FUNC
44  * @tc.require:
45  */
46 HWTEST_F(StandbyServiceClientUnitTest, MockStandbyServiceClientUnitTest_001, TestSize.Level1)
47 {
48     MockSaService::MockGetSystemAbilityManager(false);
49     sptr<IStandbyServiceSubscriber> nullSubscriber = nullptr;
50     EXPECT_NE(StandbyServiceClient::GetInstance().SubscribeStandbyCallback(nullSubscriber), ERR_OK);
51     EXPECT_NE(StandbyServiceClient::GetInstance().UnsubscribeStandbyCallback(nullSubscriber), ERR_OK);
52     sptr<ResourceRequest> nullRequest = nullptr;
53     EXPECT_NE(StandbyServiceClient::GetInstance().ApplyAllowResource(nullRequest), ERR_OK);
54     EXPECT_NE(StandbyServiceClient::GetInstance().UnapplyAllowResource(nullRequest), ERR_OK);
55     std::vector<AllowInfo> allowInfoList;
56     nullRequest = nullptr;
57     EXPECT_NE(StandbyServiceClient::GetInstance().GetAllowList(AllowType::NETWORK, allowInfoList, 0), ERR_OK);
58     EXPECT_NE(StandbyServiceClient::GetInstance().GetAllowList(0, allowInfoList, 0), ERR_OK);
59     bool isStandby {false};
60     EXPECT_NE(StandbyServiceClient::GetInstance().IsDeviceInStandby(isStandby), ERR_OK);
61     uint32_t type = 1;
62     bool enable = true;
63     uint32_t interval = 300;
64     EXPECT_NE(StandbyServiceClient::GetInstance().SetNatInterval(type, enable, interval), ERR_OK);
65     StandbyServiceClient::GetInstance().ReportWorkSchedulerStatus(false, -1, "");
66     MockSaService::MockGetSystemAbilityManager(true);
67     StandbyServiceClient::GetInstance().IsDeviceInStandby(isStandby);
68     StandbyServiceClient::GetInstance().SetNatInterval(type, enable, interval);
69     StandbyServiceClient::GetInstance().ReportWorkSchedulerStatus(true, -1, "");
70 }
71 
72 /**
73  * @tc.name: MockStandbyServiceClientUnitTest_002
74  * @tc.desc: test StandbyServiceProxy.
75  * @tc.type: FUNC
76  * @tc.require:
77  */
78 HWTEST_F(StandbyServiceClientUnitTest, MockStandbyServiceClientUnitTest_002, TestSize.Level1)
79 {
80     sptr<IRemoteObject> impl {};
81     sptr<StandbyServiceProxy> proxy = new (std::nothrow) StandbyServiceProxy(impl);
82     MockSaService::MockSendRequest(true);
83     sptr<IStandbyServiceSubscriber> subscriber = new (std::nothrow) StandbyServiceSubscriberStub();
84     EXPECT_NE(proxy->SubscribeStandbyCallback(subscriber), ERR_OK);
85     EXPECT_NE(proxy->UnsubscribeStandbyCallback(subscriber), ERR_OK);
86     sptr<ResourceRequest> resouarceRequest = new (std::nothrow) ResourceRequest();
87     EXPECT_NE(proxy->ApplyAllowResource(resouarceRequest), ERR_OK);
88     EXPECT_NE(proxy->UnapplyAllowResource(resouarceRequest), ERR_OK);
89     std::vector<AllowInfo> allowInfoList;
90     EXPECT_NE(proxy->GetAllowList(0, allowInfoList, 0), ERR_OK);
91     bool isStandby {false};
92     proxy->IsDeviceInStandby(isStandby);
93     proxy->ReportWorkSchedulerStatus(false, -1, "");
94     uint32_t type = 1;
95     bool enable = true;
96     uint32_t interval = 300;
97     proxy->SetNatInterval(type, enable, interval);
98 
99     MockSaService::MockSendRequest(false);
100     EXPECT_NE(proxy->SubscribeStandbyCallback(subscriber), ERR_OK);
101     EXPECT_NE(proxy->UnsubscribeStandbyCallback(subscriber), ERR_OK);
102     EXPECT_NE(proxy->ApplyAllowResource(resouarceRequest), ERR_OK);
103     EXPECT_NE(proxy->UnapplyAllowResource(resouarceRequest), ERR_OK);
104     EXPECT_NE(proxy->GetAllowList(0, allowInfoList, 0), ERR_OK);
105     proxy->IsDeviceInStandby(isStandby);
106     proxy->ReportWorkSchedulerStatus(false, -1, "");
107     proxy->SetNatInterval(type, enable, interval);
108 }
109 }  // namespace DevStandbyMgr
110 }  // namespace OHOS
111