1 /* 2 * Copyright (c) 2021-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 #include "mmi_client.h" 19 #include "mmi_log.h" 20 21 #undef MMI_LOG_TAG 22 #define MMI_LOG_TAG "MMIClientTest" 23 24 namespace OHOS { 25 namespace MMI { 26 namespace { 27 using namespace testing::ext; 28 } // namespace 29 30 class MMIClientTest : public testing::Test { 31 public: SetUpTestCase(void)32 static void SetUpTestCase(void) {} TearDownTestCase(void)33 static void TearDownTestCase(void) {} 34 }; 35 36 ConnectCallback connectFun; 37 38 /** 39 * @tc.name: SetEventHandler 40 * @tc.desc: Set eventHandler 41 * @tc.type: FUNC 42 * @tc.require: 43 */ 44 HWTEST_F(MMIClientTest, SetEventHandler_001, TestSize.Level1) 45 { 46 CALL_TEST_DEBUG; 47 MMIClient mmiClient; 48 EventHandlerPtr eventHandler = std::make_shared<AppExecFwk::EventHandler>(); 49 ASSERT_NO_FATAL_FAILURE(mmiClient.SetEventHandler(eventHandler)); 50 } 51 52 /** 53 * @tc.name: MarkIsEventHandlerChanged 54 * @tc.desc: Mark if eventHandler has changed 55 * @tc.type: FUNC 56 * @tc.require: 57 */ 58 HWTEST_F(MMIClientTest, MarkIsEventHandlerChanged_001, TestSize.Level1) 59 { 60 CALL_TEST_DEBUG; 61 MMIClient mmiClient; 62 std::string threadName = "mmi_client_test"; 63 auto eventRunner = AppExecFwk::EventRunner::Create(threadName); 64 EventHandlerPtr eventHandler = std::make_shared<AppExecFwk::EventHandler>(eventRunner); 65 mmiClient.SetEventHandler(eventHandler); 66 ASSERT_NO_FATAL_FAILURE(mmiClient.SetEventHandler(eventHandler)); 67 } 68 69 /** 70 * @tc.name: MarkIsEventHandlerChanged_002 71 * @tc.desc: Mark if eventHandler has changed 72 * @tc.type: FUNC 73 * @tc.require: 74 */ 75 HWTEST_F(MMIClientTest, MarkIsEventHandlerChanged_002, TestSize.Level1) 76 { 77 CALL_TEST_DEBUG; 78 MMIClient mmiClient; 79 std::string threadName1 = "mmi_client_test_1"; 80 auto eventRunner1 = AppExecFwk::EventRunner::Create(threadName1); 81 EventHandlerPtr eventHandler1 = std::make_shared<AppExecFwk::EventHandler>(eventRunner1); 82 mmiClient.SetEventHandler(eventHandler1); 83 std::string threadName2 = "mmi_client_test_2"; 84 auto eventRunner2 = AppExecFwk::EventRunner::Create(threadName2); 85 EventHandlerPtr eventHandler2 = std::make_shared<AppExecFwk::EventHandler>(eventRunner2); 86 ASSERT_NO_FATAL_FAILURE(mmiClient.SetEventHandler(eventHandler2)); 87 } 88 89 /** 90 * @tc.name: RegisterConnectedFunction 91 * @tc.desc: Verify register connected 92 * @tc.type: FUNC 93 * @tc.require: 94 */ 95 HWTEST_F(MMIClientTest, RegisterConnectedFunction, TestSize.Level1) 96 { 97 CALL_TEST_DEBUG; 98 MMIClient mmiClient; 99 ASSERT_NO_FATAL_FAILURE(mmiClient.RegisterConnectedFunction(connectFun)); 100 } 101 102 /** 103 * @tc.name: RegisterDisconnectedFunction 104 * @tc.desc: Verify register disconnected 105 * @tc.type: FUNC 106 * @tc.require: 107 */ 108 HWTEST_F(MMIClientTest, RegisterDisconnectedFunction, TestSize.Level1) 109 { 110 CALL_TEST_DEBUG; 111 MMIClient mmiClient; 112 ASSERT_NO_FATAL_FAILURE(mmiClient.RegisterDisconnectedFunction(connectFun)); 113 } 114 115 /** 116 * @tc.name: KeyCommandHandlerTest_Start_001 117 * @tc.desc: Create a connection to server 118 * @tc.type: FUNC 119 * @tc.require: 120 */ 121 HWTEST_F(MMIClientTest, MMIClientTest_Start__001, TestSize.Level1) 122 { 123 CALL_TEST_DEBUG; 124 std::shared_ptr<MMIClient> client = std::make_shared<MMIClient>(); 125 EXPECT_TRUE(client->Start()); 126 client->Stop(); 127 } 128 129 /** 130 * @tc.name: MMIClientTest_Start_002 131 * @tc.desc: Create a connection to server 132 * @tc.type: FUNC 133 * @tc.require: 134 */ 135 HWTEST_F(MMIClientTest, MMIClientTest_Start_002, TestSize.Level1) 136 { 137 CALL_TEST_DEBUG; 138 std::shared_ptr<MMIClient> client = std::make_shared<MMIClient>(); 139 std::string threadName = "mmi_client_test"; 140 auto eventRunner = AppExecFwk::EventRunner::Create(threadName); 141 EventHandlerPtr eventHandler = std::make_shared<AppExecFwk::EventHandler>(eventRunner); 142 client->SetEventHandler(eventHandler); 143 ASSERT_TRUE(client->Start()); 144 client->Stop(); 145 } 146 147 /** 148 * @tc.name: KeyCommandHandlerTest_GetCurrentConnectedStatus_001 149 * @tc.desc: Get current connection status 150 * @tc.type: FUNC 151 * @tc.require: 152 */ 153 HWTEST_F(MMIClientTest, MMIClientTest_GetCurrentConnectedStatus__001, TestSize.Level1) 154 { 155 CALL_TEST_DEBUG; 156 std::shared_ptr<MMIClient> client = std::make_shared<MMIClient>(); 157 client->Start(); 158 EXPECT_TRUE(client->GetCurrentConnectedStatus()); 159 client->Stop(); 160 } 161 162 /** 163 * @tc.name: KeyCommandHandlerTest_GetCurrentConnectedStatus_002 164 * @tc.desc: Get current connection status 165 * @tc.type: FUNC 166 * @tc.require: 167 */ 168 HWTEST_F(MMIClientTest, MMIClientTest_GetCurrentConnectedStatus__002, TestSize.Level1) 169 { 170 CALL_TEST_DEBUG; 171 std::shared_ptr<MMIClient> client = std::make_shared<MMIClient>(); 172 EXPECT_FALSE(client->GetCurrentConnectedStatus()); 173 } 174 175 /** 176 * @tc.name: MMIClientTest_OnRecvMsg_001 177 * @tc.desc: Receive msg 178 * @tc.type: FUNC 179 * @tc.require: 180 */ 181 HWTEST_F(MMIClientTest, MMIClientTest_OnRecvMsg_001, TestSize.Level1) 182 { 183 CALL_TEST_DEBUG; 184 MMIClient mmiClient; 185 const char* buf = "test_data"; 186 int32_t size = strlen(buf); 187 ASSERT_NO_FATAL_FAILURE(mmiClient.OnRecvMsg(buf, size)); 188 } 189 190 /** 191 * @tc.name: MMIClientTest_OnRecvMsg_002 192 * @tc.desc: Receive msg 193 * @tc.type: FUNC 194 * @tc.require: 195 */ 196 HWTEST_F(MMIClientTest, MMIClientTest_OnRecvMsg_002, TestSize.Level1) 197 { 198 CALL_TEST_DEBUG; 199 MMIClient mmiClient; 200 const char* buf = "test_data"; 201 int32_t size = 0; 202 ASSERT_NO_FATAL_FAILURE(mmiClient.OnRecvMsg(buf, size)); 203 } 204 205 /** 206 * @tc.name: MMIClientTest_OnRecvMsg_003 207 * @tc.desc: Receive msg 208 * @tc.type: FUNC 209 * @tc.require: 210 */ 211 HWTEST_F(MMIClientTest, MMIClientTest_OnRecvMsg_003, TestSize.Level1) 212 { 213 CALL_TEST_DEBUG; 214 MMIClient mmiClient; 215 const char* buf = "test_data"; 216 int32_t size = MAX_PACKET_BUF_SIZE + 1; 217 ASSERT_NO_FATAL_FAILURE(mmiClient.OnRecvMsg(buf, size)); 218 } 219 220 /** 221 * @tc.name: MMIClientTest_Socket_001 222 * @tc.desc: Get socket 223 * @tc.type: FUNC 224 * @tc.require: 225 */ 226 HWTEST_F(MMIClientTest, MMIClientTest_Socket_001, TestSize.Level1) 227 { 228 CALL_TEST_DEBUG; 229 MMIClient mmiClient; 230 ASSERT_NE(mmiClient.Socket(), -1); 231 } 232 233 /** 234 * @tc.name: MMIClientTest_Stop_001 235 * @tc.desc: Stop connection 236 * @tc.type: FUNC 237 * @tc.require: 238 */ 239 HWTEST_F(MMIClientTest, MMIClientTest_Stop_001, TestSize.Level1) 240 { 241 CALL_TEST_DEBUG; 242 std::shared_ptr<MMIClient> client = std::make_shared<MMIClient>(); 243 ASSERT_TRUE(client->Start()); 244 ASSERT_NO_FATAL_FAILURE(client->Stop()); 245 } 246 247 /** 248 * @tc.name: MMIClientTest_Stop_002 249 * @tc.desc: Stop connection 250 * @tc.type: FUNC 251 * @tc.require: 252 */ 253 HWTEST_F(MMIClientTest, MMIClientTest_Stop_002, TestSize.Level1) 254 { 255 CALL_TEST_DEBUG; 256 std::shared_ptr<MMIClient> client = std::make_shared<MMIClient>(); 257 std::string threadName = "OS_mmi_EventHdr"; 258 auto eventRunner = AppExecFwk::EventRunner::Create(threadName); 259 EventHandlerPtr eventHandler = std::make_shared<AppExecFwk::EventHandler>(eventRunner); 260 client->SetEventHandler(eventHandler); 261 ASSERT_TRUE(client->Start()); 262 ASSERT_NO_FATAL_FAILURE(client->Stop()); 263 } 264 265 /** 266 * @tc.name: MMIClientTest_Stop_003 267 * @tc.desc: Stop connection 268 * @tc.type: FUNC 269 * @tc.require: 270 */ 271 HWTEST_F(MMIClientTest, MMIClientTest_Stop_003, TestSize.Level1) 272 { 273 CALL_TEST_DEBUG; 274 std::shared_ptr<MMIClient> client = std::make_shared<MMIClient>(); 275 std::string threadName = "mmi_client_test"; 276 auto eventRunner = AppExecFwk::EventRunner::Create(threadName); 277 EventHandlerPtr eventHandler = std::make_shared<AppExecFwk::EventHandler>(eventRunner); 278 client->SetEventHandler(eventHandler); 279 ASSERT_TRUE(client->Start()); 280 ASSERT_NO_FATAL_FAILURE(client->Stop()); 281 } 282 283 /** 284 * @tc.name: KeyCommandHandlerTest_Reconnect_001 285 * @tc.desc: Socket reconnection 286 * @tc.type: FUNC 287 * @tc.require: 288 */ 289 HWTEST_F(MMIClientTest, MMIClientTest_Reconnect_001, TestSize.Level1) 290 { 291 CALL_TEST_DEBUG; 292 std::shared_ptr<MMIClient> client = std::make_shared<MMIClient>(); 293 client->Start(); 294 EXPECT_FALSE(client->Reconnect()); 295 client->Stop(); 296 } 297 298 /** 299 * @tc.name: KeyCommandHandlerTest_OnDisconnect_001 300 * @tc.desc: Disconnected from server 301 * @tc.type: FUNC 302 * @tc.require: 303 */ 304 HWTEST_F(MMIClientTest, MMIClientTest_OnDisconnect_001, TestSize.Level1) 305 { 306 CALL_TEST_DEBUG; 307 std::shared_ptr<MMIClient> client = std::make_shared<MMIClient>(); 308 client->Start(); 309 client->OnDisconnect(); 310 ASSERT_NO_FATAL_FAILURE(client->OnDisconnect()); 311 client->Stop(); 312 } 313 314 /** 315 * @tc.name: MMIClientTest_StartEventRunner_001 316 * @tc.desc: Start event runner 317 * @tc.type: FUNC 318 * @tc.require: 319 */ 320 HWTEST_F(MMIClientTest, MMIClientTest_StartEventRunner_001, TestSize.Level1) 321 { 322 CALL_TEST_DEBUG; 323 std::shared_ptr<MMIClient> client = std::make_shared<MMIClient>(); 324 client->isConnected_ = true; 325 client->fd_ = 1; 326 client->eventHandler_ = nullptr; 327 bool result = client->StartEventRunner(); 328 EXPECT_TRUE(result); 329 } 330 } 331 } // namespace MMI 332