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 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 }