1 /*
2  * Copyright (c) 2022-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 
16 #include <gtest/gtest.h>
17 #define private public
18 #include "application_info.h"
19 #include "common_event_data.h"
20 #include "common_event_manager.h"
21 #include "common_event_support.h"
22 #include "res_sched_log.h"
23 #ifdef RESSCHED_COMMUNICATION_NETMANAGER_BASE_ENABLE
24 #include "net_supplier_info.h"
25 #endif
26 #include "want.h"
27 
28 #include "event_controller.h"
29 #include "res_type.h"
30 
31 namespace OHOS {
32 namespace ResourceSchedule {
33 class EventControllerTest : public testing::Test {
34 public:
SetUpTestCase()35     static void SetUpTestCase() {}
TearDownTestCase()36     static void TearDownTestCase() {}
AssertResType(uint32_t & actual,uint32_t & expect)37     static void AssertResType(uint32_t &actual, uint32_t &expect)
38     {
39         EXPECT_TRUE(actual == expect) << "Dispatch resType should be " + std::to_string(expect)
40             + ", actually is " + std::to_string(actual);
41     }
AssertValue(int64_t & actual,int64_t & expect)42     static void AssertValue(int64_t &actual, int64_t &expect)
43     {
44         EXPECT_TRUE(actual == expect) << "Dispatch value should be " + std::to_string(expect)
45             + ", actually is " + std::to_string(actual);
46     }
SetUp()47     void SetUp() {}
TearDown()48     void TearDown() {}
49 };
50 
51 /**
52  * @tc.name: connectivityChange_00100
53  * @tc.desc: test dispatching ResType::RES_TYPE_WIFI_CONNECT_STATE_CHANGE
54  *           when recieve COMMON_EVENT_CONNECTIVITY_CHANGE.
55  * @tc.type: FUNC
56  * @tc.require: SR000H0H3C AR000HORSU
57  */
58 HWTEST_F(EventControllerTest, connectivityChange_00100, testing::ext::TestSize.Level1)
59 {
60 #ifdef RESSCHED_COMMUNICATION_NETMANAGER_BASE_ENABLE
61     AAFwk::Want want;
62     EventFwk::CommonEventData data;
63     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
64     want.SetParam("NetType", 1);
65     data.SetWant(want);
66     data.SetCode(NetManagerStandard::NetConnState::NET_CONN_STATE_CONNECTING);
67 
68     EventController::GetInstance().OnReceiveEvent(data);
69     uint32_t expectResType = ResType::RES_TYPE_WIFI_CONNECT_STATE_CHANGE;
70     int64_t expectValue = 2;
71     EventControllerTest::AssertResType(EventController::GetInstance().resType_, expectResType);
72     EventControllerTest::AssertValue(EventController::GetInstance().value_, expectValue);
73 #endif
74 }
75 
76 /**
77  * @tc.name: connectivityChange_00101
78  * @tc.desc: test dispatching ResType::RES_TYPE_WIFI_CONNECT_STATE_CHANGE
79  *           when receive COMMON_EVENT_CONNECTIVITY_CHANGE and code is NET_CONN_STATE_CONNECTED.
80  * @tc.type: FUNC
81  * @tc.require: SR000H0H3C AR000HORSU
82  */
83 HWTEST_F(EventControllerTest, connectivityChange_00101, testing::ext::TestSize.Level1)
84 {
85 #ifdef RESSCHED_COMMUNICATION_NETMANAGER_BASE_ENABLE
86     AAFwk::Want want;
87     EventFwk::CommonEventData data;
88     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
89     want.SetParam("NetType", 1);
90     data.SetWant(want);
91     data.SetCode(NetManagerStandard::NetConnState::NET_CONN_STATE_CONNECTED);
92 
93     EventController::GetInstance().OnReceiveEvent(data);
94     uint32_t expectResType = ResType::RES_TYPE_WIFI_CONNECT_STATE_CHANGE;
95     int64_t expectValue = 3;
96     EventControllerTest::AssertResType(EventController::GetInstance().resType_, expectResType);
97     EventControllerTest::AssertValue(EventController::GetInstance().value_, expectValue);
98 #endif
99 }
100 
101 /**
102  * @tc.name: connectivityChange_00102
103  * @tc.desc: test dispatching ResType::RES_TYPE_APP_INSTALL_UNINSTALL when recieve COMMON_EVENT_PACKAGE_ADDED.
104  * @tc.type: FUNC
105  * @tc.require: SR000H0H3C AR000HORSU
106  */
107 HWTEST_F(EventControllerTest, connectivityChange_00102, testing::ext::TestSize.Level1)
108 {
109     AAFwk::Want want;
110     EventFwk::CommonEventData data;
111     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
112     data.SetWant(want);
113 
114     EventController::GetInstance().OnReceiveEvent(data);
115     uint32_t expectResType = ResType::RES_TYPE_APP_INSTALL_UNINSTALL;
116     int64_t expectValue = 1;
117     EventControllerTest::AssertResType(EventController::GetInstance().resType_, expectResType);
118     EventControllerTest::AssertValue(EventController::GetInstance().value_, expectValue);
119 }
120 
121 /**
122  * @tc.name: connectivityChange_00103
123  * @tc.desc: test dispatching ResType::RES_TYPE_APP_INSTALL_UNINSTALL when recieve COMMON_EVENT_PACKAGE_REMOVED.
124  * @tc.type: FUNC
125  * @tc.require: SR000H0H3C AR000HORSU
126  */
127 HWTEST_F(EventControllerTest, connectivityChange_00103, testing::ext::TestSize.Level1)
128 {
129     AAFwk::Want want;
130     EventFwk::CommonEventData data;
131     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
132     data.SetWant(want);
133 
134     EventController::GetInstance().OnReceiveEvent(data);
135     uint32_t expectResType = ResType::RES_TYPE_APP_INSTALL_UNINSTALL;
136     int64_t expectValue = 0;
137     EventControllerTest::AssertResType(EventController::GetInstance().resType_, expectResType);
138     EventControllerTest::AssertValue(EventController::GetInstance().value_, expectValue);
139 }
140 
141 /**
142  * @tc.name: connectivityChange_00104
143  * @tc.desc: test dispatching ResType::RES_TYPE_SCREEN_STATUS when recieve COMMON_EVENT_SCREEN_ON.
144  * @tc.type: FUNC
145  * @tc.require: SR000H0H3C AR000HORSU
146  */
147 HWTEST_F(EventControllerTest, connectivityChange_00104, testing::ext::TestSize.Level1)
148 {
149     AAFwk::Want want;
150     EventFwk::CommonEventData data;
151     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
152     data.SetWant(want);
153 
154     EventController::GetInstance().OnReceiveEvent(data);
155     uint32_t expectResType = ResType::RES_TYPE_SCREEN_STATUS;
156     int64_t expectValue = 1;
157     EventControllerTest::AssertResType(EventController::GetInstance().resType_, expectResType);
158     EventControllerTest::AssertValue(EventController::GetInstance().value_, expectValue);
159 }
160 
161 /**
162  * @tc.name: connectivityChange_00105
163  * @tc.desc: test dispatching ResType::RES_TYPE_SCREEN_STATUS when recieve COMMON_EVENT_SCREEN_OFF.
164  * @tc.type: FUNC
165  * @tc.require: SR000H0H3C AR000HORSU
166  */
167 HWTEST_F(EventControllerTest, connectivityChange_00105, testing::ext::TestSize.Level1)
168 {
169     AAFwk::Want want;
170     EventFwk::CommonEventData data;
171     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
172     data.SetWant(want);
173 
174     EventController::GetInstance().OnReceiveEvent(data);
175     uint32_t expectResType = ResType::RES_TYPE_SCREEN_STATUS;
176     int64_t expectValue = 0;
177     EventControllerTest::AssertResType(EventController::GetInstance().resType_, expectResType);
178     EventControllerTest::AssertValue(EventController::GetInstance().value_, expectValue);
179 }
180 
181 /**
182  * @tc.name: connectivityChange_00106
183  * @tc.desc: test the interface OnAddSystemAbility
184  * @tc.type: FUNC
185  * @tc.require: issueI8VZVN
186  */
187 HWTEST_F(EventControllerTest, connectivityChange_00106, testing::ext::TestSize.Level1)
188 {
189     EventController::GetInstance().Init();
190     SUCCEED();
191     int32_t systemAbilityId = 0;
192     std::string deviceId;
193     EventController::GetInstance().sysAbilityListener_->OnAddSystemAbility(systemAbilityId, deviceId);
194     EXPECT_NE(EventController::GetInstance().sysAbilityListener_->subscriber_, nullptr);
195 }
196 
197 /**
198  * @tc.name: connectivityChange_00107
199  * @tc.desc: test the interface
200  * @tc.type: FUNC
201  * @tc.require: issueI8VZVN
202  */
203 HWTEST_F(EventControllerTest, connectivityChange_00107, testing::ext::TestSize.Level1)
204 {
205     int32_t userId = 0;
206     std::string bundleName = "test";
207     int32_t result = EventController::GetInstance().GetUid(userId, bundleName);
208     EXPECT_EQ(result, -1);
209     EventController::GetInstance().Stop();
210 }
211 
212 /**
213  * @tc.name: connectivityChange_00108
214  * @tc.desc: test the interface Init
215  * @tc.type: FUNC
216  * @tc.require: issueI8VZVN
217  */
218 HWTEST_F(EventControllerTest, connectivityChange_00108, testing::ext::TestSize.Level1)
219 {
220     EventController::GetInstance().Init();
221     EXPECT_NE(EventController::GetInstance().sysAbilityListener_, nullptr);
222 }
223 
224 /**
225  * @tc.name: connectivityChange_00110
226  * @tc.desc: test the interface
227  * @tc.type: FUNC
228  * @tc.require: issueI8VZVN
229  */
230 HWTEST_F(EventControllerTest, connectivityChange_00110, testing::ext::TestSize.Level1)
231 {
232     EventFwk::CommonEventData data;
233     nlohmann::json payload;
234     EventFwk::Want want = data.GetWant();
235     std::string action = EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED;
236     bool b1 = EventController::GetInstance().HandlePkgCommonEvent(action, want, payload);
237     EXPECT_EQ(b1, true);
238 
239     std::string action1 = EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED;
240     bool b2 = EventController::GetInstance().HandlePkgCommonEvent(action1, want, payload);
241     EXPECT_EQ(b2, true);
242 
243     std::string action2 = EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED;
244     bool b3 = EventController::GetInstance().HandlePkgCommonEvent(action2, want, payload);
245     EXPECT_EQ(b3, true);
246 
247     std::string action3 = EventFwk::CommonEventSupport::COMMON_EVENT_BUNDLE_REMOVED;
248     bool b4 = EventController::GetInstance().HandlePkgCommonEvent(action3, want, payload);
249     EXPECT_EQ(b4, true);
250 }
251 
252 /**
253  * @tc.name: bootCompleted_001
254  * @tc.desc: test the boot bootCompleted_001
255  * @tc.type: FUNC
256  * @tc.require: issuesI9IR2I
257  */
258 HWTEST_F(EventControllerTest, bootCompleted_001, testing::ext::TestSize.Level1)
259 {
260     AAFwk::Want want;
261     EventFwk::CommonEventData data;
262     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_BOOT_COMPLETED);
263     data.SetWant(want);
264     EventController::GetInstance().OnReceiveEvent(data);
265     EXPECT_NE(want.GetAction(), "");
266 }
267 }
268 }