1 /*
2 * Copyright (c) 2022 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 #define private public
19 #define protected public
20 #include <vector>
21 #include "res_sched_service.h"
22 #include "res_sched_service_ability.h"
23 #include "plugin_mgr.h"
24
25 namespace OHOS {
26 namespace ResourceSchedule {
27 using namespace std;
28 using namespace testing::ext;
29 class ResSchedServiceTest : public testing::Test {
30 public:
31 static void SetUpTestCase(void);
32 static void TearDownTestCase(void);
33 void SetUp();
34 void TearDown();
35 protected:
36 std::shared_ptr<ResSchedService> resSchedService_ = nullptr;
37 std::shared_ptr<ResSchedServiceAbility> resSchedServiceAbility_ = nullptr;
38 };
39
40
SetUpTestCase(void)41 void ResSchedServiceTest::SetUpTestCase(void) {}
42
TearDownTestCase()43 void ResSchedServiceTest::TearDownTestCase() {}
44
SetUp()45 void ResSchedServiceTest::SetUp()
46 {
47 /**
48 * @tc.setup: initialize the member variable resSchedServiceAbility_
49 */
50 resSchedService_ = make_shared<ResSchedService>();
51 resSchedServiceAbility_ = make_shared<ResSchedServiceAbility>();
52 }
53
TearDown()54 void ResSchedServiceTest::TearDown()
55 {
56 /**
57 * @tc.teardown: clear resSchedServiceAbility_
58 */
59 resSchedService_ = nullptr;
60 resSchedServiceAbility_ = nullptr;
61 }
62
63 /**
64 * @tc.name: ressched service dump 001
65 * @tc.desc: Verify if ressched service dump commonds is success.
66 * @tc.type: FUNC
67 * @tc.require: issueI5WWV3
68 * @tc.author:lice
69 */
70 HWTEST_F(ResSchedServiceTest, ServiceDump001, Function | MediumTest | Level0)
71 {
72 PluginMgr::GetInstance().Init();
73 std::string result;
74 resSchedService_->DumpAllInfo(result);
75 EXPECT_TRUE(!result.empty());
76
77 result = "";
78 resSchedService_->DumpUsage(result);
79 EXPECT_TRUE(!result.empty());
80
81 int32_t wrongFd = -1;
82 std::vector<std::u16string> argsNull;
83 int res = resSchedService_->Dump(wrongFd, argsNull);
84 EXPECT_TRUE(!res);
85
86 int32_t correctFd = -1;
87 res = resSchedService_->Dump(correctFd, argsNull);
88
89 std::vector<std::u16string> argsHelp = {to_utf16("-h")};
90 res = resSchedService_->Dump(correctFd, argsHelp);
91
92 std::vector<std::u16string> argsAll = {to_utf16("-a")};
93 res = resSchedService_->Dump(correctFd, argsAll);
94
95 std::vector<std::u16string> argsError = {to_utf16("-e")};
96 res = resSchedService_->Dump(correctFd, argsError);
97
98 std::vector<std::u16string> argsPlugin = {to_utf16("-p")};
99 res = resSchedService_->Dump(correctFd, argsPlugin);
100
101 std::vector<std::u16string> argsOnePlugin = {to_utf16("-p"), to_utf16("1")};
102 res = resSchedService_->Dump(correctFd, argsOnePlugin);
103 }
104
105 /**
106 * @tc.name: Ressched service ReportData 001
107 * @tc.desc: Verify if Ressched service ReportData is success.
108 * @tc.type: FUNC
109 * @tc.require: issueI5WWV3
110 * @tc.author:lice
111 */
112 HWTEST_F(ResSchedServiceTest, Report001, Function | MediumTest | Level0)
113 {
114 nlohmann::json payload;
115 EXPECT_TRUE(resSchedService_ != nullptr);
116 resSchedService_->ReportData(0, 0, payload);
117 }
118
119 /**
120 * @tc.name: Start ResSchedServiceAbility 001
121 * @tc.desc: Verify if ResSchedServiceAbility OnStart is success.
122 * @tc.type: FUNC
123 * @tc.require: issueI5WWV3
124 * @tc.author:lice
125 */
126 HWTEST_F(ResSchedServiceTest, OnStart001, Function | MediumTest | Level0)
127 {
128 resSchedServiceAbility_->OnStart();
129 EXPECT_TRUE(resSchedServiceAbility_->service_ != nullptr);
130 }
131
132 /**
133 * @tc.name: ResSchedServiceAbility ChangeAbility 001
134 * @tc.desc: Verify if add and remove system ability is success.
135 * @tc.type: FUNC
136 * @tc.require: issueI5WWV3
137 * @tc.author:lice
138 */
139 HWTEST_F(ResSchedServiceTest, ChangeAbility001, Function | MediumTest | Level0)
140 {
141 std::string deviceId;
142 resSchedServiceAbility_->OnAddSystemAbility(-1, deviceId);
143 resSchedServiceAbility_->OnRemoveSystemAbility(-1, deviceId);
144 }
145
146 class TestResSchedServiceStub : public ResSchedServiceStub {
147 public:
TestResSchedServiceStub()148 TestResSchedServiceStub() : ResSchedServiceStub() {}
149
ReportData(uint32_t restype,int64_t value,const nlohmann::json & payload)150 void ReportData(uint32_t restype, int64_t value, const nlohmann::json& payload) override
151 {
152 }
153 };
154
155 /**
156 * @tc.name: ResSchedServicesStub ReportDataInner 001
157 * @tc.desc: Verify if resschedstub reportdatainner is success.
158 * @tc.type: FUNC
159 * @tc.require: issueI5WWV3
160 * @tc.author:lice
161 */
162 HWTEST_F(ResSchedServiceTest, ReportDataInner001, Function | MediumTest | Level0)
163 {
164 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
165 resSchedServiceStub_->Init();
166 MessageParcel reply;
167 MessageParcel emptyData;
168 EXPECT_TRUE(resSchedServiceStub_->ReportDataInner(emptyData, reply));
169
170 MessageParcel reportData;
171 reportData.WriteInterfaceToken(ResSchedServiceStub::GetDescriptor());
172 reportData.WriteUint32(1);
173 reportData.WriteInt64(1);
174 reportData.WriteString("{ { \" uid \" : \" 1 \" } }");
175 EXPECT_TRUE(!resSchedServiceStub_->ReportDataInner(reportData, reply));
176 }
177
178 /**
179 * @tc.name: ResSchedServicesStub StringToJson 001
180 * @tc.desc: Verify if resschedstub StringToJson is success.
181 * @tc.type: FUNC
182 * @tc.require: issueI5WWV3
183 * @tc.author:lice
184 */
185 HWTEST_F(ResSchedServiceTest, StringToJson001, Function | MediumTest | Level0)
186 {
187 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
188 nlohmann::json res = resSchedServiceStub_->StringToJsonObj("");
189 EXPECT_TRUE(!res.dump().empty());
190 }
191
192 /**
193 * @tc.name: ResSchedServicesStub RemoteRequest 001
194 * @tc.desc: Verify if resschedstub RemoteRequest is success.
195 * @tc.type: FUNC
196 * @tc.require: issueI5WWV3
197 * @tc.author:lice
198 */
199 HWTEST_F(ResSchedServiceTest, RemoteRequest001, Function | MediumTest | Level0)
200 {
201 auto resSchedServiceStub_ = make_shared<TestResSchedServiceStub>();
202 MessageOption option;
203 MessageParcel reply;
204 int32_t res = resSchedServiceStub_->OnRemoteRequest(1, reply, reply, option);
205
206 res = resSchedServiceStub_->OnRemoteRequest(0, reply, reply, option);
207 EXPECT_TRUE(res);
208 }
209 #undef private
210 #undef protected
211 } // namespace ResourceSchedule
212 } // namespace OHOS
213