AbilityMgrTest2.cpp 34.8 KB
Newer Older
M
mamingshuai 已提交
1
/**
C
chengxingzhen 已提交
2
 * Copyright (c) 2022 Huawei Device Co., Ltd.
M
mamingshuai 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <log.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>

#include "gtest/gtest.h"
#include "securec.h"

#include "ability.h"
#include "ability_connection.h"
#include "ability_env.h"
#include "ability_event_handler.h"
#include "ability_manager.h"
#include "ability_slice.h"
#include "abilityms_client.h"
#include "bundle_manager.h"
#include "want.h"

using namespace std;
using namespace testing::ext;
using namespace OHOS;
static int32_t g_errorCode = -1;
static sem_t g_sem;
static const int32_t WAIT_TIMEOUT = 30;
static bool g_installState = false;
static string g_testPath;

extern "C" {
void __attribute__((weak)) HOS_SystemInit(void){};
}

/* install callback */
static void TestBundleStateCallback(const uint8_t resultCode, const void *resultMessage)
{
    HILOG_DEBUG(HILOG_MODULE_APP, "TestBundleStateCallback resultCode: %d", resultCode);
    HILOG_DEBUG(HILOG_MODULE_APP, "TestBundleStateCallback resultMessage: %s", (char *) resultMessage);
    g_installState = (resultCode == 0);
    g_errorCode = resultCode;
    sem_post(&g_sem);
}

/* *
 * get current dir
 * @return  string current file path of the test suits
 */
static string GetCurDir()
{
    string filePath = "";
    char *buffer;
    if ((buffer = getcwd(NULL, 0)) == NULL) {
        perror("get file path error");
    } else {
        printf("Current Dir: %s\r\n", buffer);
        filePath = buffer;
        free(buffer);
    }
    return filePath + "/";
}

/* connectAbility callback */
static void OnAbilityConnectDone(ElementName *elementName, SvcIdentity *serviceSid, int resultCode, void *storeData)
{
    printf("OnAbilityConnectDone, serviceSid is %p \n", serviceSid);
    printf("elementName is %s, %s \n", elementName->bundleName, elementName->abilityName);
    ClearElement(elementName);
    if (serviceSid == nullptr) {
        return;
    }
    printf("ipc callback success \n");
    // push data
    IpcIo request;
    char data[IPC_IO_DATA_MAX];
    IpcIoInit(&request, data, IPC_IO_DATA_MAX, 0);
    int32_t data1 = 10;
    int32_t data2 = 6;
L
liubb_0516 已提交
92 93
    WreitInt32(&request, data1);
    WriteInt32(&request, data2);
M
mamingshuai 已提交
94 95 96
    // send and getReply
    IpcIo reply = {nullptr};
    uintptr_t ptr = 0;
L
liubb_0516 已提交
97
    SendRequest(*serviceSid, 0, &request, &reply, LITEIPC_FLAG_DEFAULT, &ptr);
L
liubb_0516 已提交
98
    ReadInt32(&reply, &g_errorCode);
M
mamingshuai 已提交
99 100 101 102 103 104 105 106 107
    if (g_errorCode != 0) {
        printf("execute add method, result is %d\n", g_errorCode);
    }
    if (ptr != 0) {
        FreeBuffer(nullptr, reinterpret_cast<void *>(ptr));
    }
    sem_post(&g_sem);
}

C
chengxingzhen 已提交
108
static void OnAbilityDisconnectDone(ElementName (void)*elementName, int resultCode, void *storeData)
M
mamingshuai 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
{
    printf("OnAbilityDisconnectDone\n");
}


static IAbilityConnection g_conn = {
    .OnAbilityConnectDone = OnAbilityConnectDone,
    .OnAbilityDisconnectDone = OnAbilityDisconnectDone
};

class AbilityMgrTest2 : public testing::Test {
protected:
    static void SetUpTestCase(void)
    {
        printf("----------test case with AbilityMgrTest2 start-------------\n");
        HOS_SystemInit();
        AbilityMsClient::GetInstance().Initialize();
        sem_init(&g_sem, 0, 0);
        bool installResult = false;
        InstallParam installParam = { .installLocation = 1, .keepData = false };
        g_testPath = GetCurDir();
#ifdef __LINUX__
        string hapPath = g_testPath + "testnative_hispark_aries_linux.hap";
#else
        string hapPath = g_testPath + "testnative_hispark_aries_liteos.hap";
#endif
        installResult = Install(hapPath.c_str(), &installParam, TestBundleStateCallback);
        struct timespec ts = {};
        clock_gettime(CLOCK_REALTIME, &ts);
        ts.tv_sec += WAIT_TIMEOUT;
        sem_timedwait(&g_sem, &ts);
        if (installResult) {
            printf("sem exit \n");
        }
    }
    static void TearDownTestCase(void)
    {
        bool uninstallResult = false;
        sem_init(&g_sem, 0, 0);
        InstallParam installParam = { .installLocation = 1, .keepData = false };
J
jiyong 已提交
149
        uninstallResult = Uninstall("com.openharmony.testnative", &installParam, TestBundleStateCallback);
M
mamingshuai 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
        sem_wait(&g_sem);
        if (uninstallResult) {
            printf("sem exit \n");
        }
        printf("----------test case with AbilityMgrTest2 end-------------\n");
    }
};

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0013
 * @tc.name      : testClearElement parameter legal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testClearElement, Function | MediumTest | Level0)
{
    printf("------start testClearElement------\n");
    ElementName element = { nullptr };
    bool setResult = SetElementAbilityName(&element, "ServiceAbility");
    if (setResult) {
        char aName[] = "ServiceAbility";
        EXPECT_STREQ(element.abilityName, aName);
        printf("abilityName is %s \n", element.abilityName);
        ClearElement(&element);
        EXPECT_STREQ(element.abilityName, nullptr);
    }
    printf("------end testClearElement------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0014
 * @tc.name      : testClearElement parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testClearElementIllegal, Function | MediumTest | Level2)
{
    printf("------start testClearElementIllegal------\n");
    ElementName element = { nullptr };
    bool setResult = SetElementAbilityName(&element, "ServiceAbility");
    if (setResult) {
        char aName[] = "ServiceAbility";
        EXPECT_STREQ(element.abilityName, aName);
        printf("abilityName is %s \n", element.abilityName);
        ClearElement(nullptr);
        EXPECT_STREQ(element.abilityName, aName);
        printf("AbilityName of element is %s \n", element.abilityName);
    }
    printf("------end testClearElementIllegal------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0015
 * @tc.name      : testSetWantElement parameter legal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testSetWantElement, Function | MediumTest | Level0)
{
    printf("------start testSetWantElement------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
    SetElementDeviceID(&element, "0001000");
J
jiyong 已提交
210
    SetElementBundleName(&element, "com.openharmony.testnative");
M
mamingshuai 已提交
211 212 213 214 215 216
    SetElementAbilityName(&element, "ServiceAbility");
    if (element.abilityName != nullptr) {
        bool setResult = SetWantElement(&want, element);
        if (setResult) {
            EXPECT_STREQ(want.element->deviceId, "0001000");
            EXPECT_STREQ(want.element->abilityName, "ServiceAbility");
J
jiyong 已提交
217
            EXPECT_STREQ(want.element->bundleName, "com.openharmony.testnative");
M
mamingshuai 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
        }
    }
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testSetWantElement------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0016
 * @tc.name      : testSetWantElement parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testSetWantElementIllegal, Function | MediumTest | Level2)
{
    printf("------start testSetWantElementIllegal------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
    bool setResult = SetWantElement(&want, element);
    if (setResult) {
        EXPECT_STREQ(want.element->deviceId, nullptr);
        EXPECT_STREQ(want.element->abilityName, nullptr);
        EXPECT_STREQ(want.element->bundleName, nullptr);
    }
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testSetWantElementIllegal------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0022
 * @tc.name      : testClearWant parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testClearWantIllegal, Function | MediumTest | Level2)
{
    printf("------start testClearWantIllegal------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
    bool setResult = SetElementAbilityName(&element, "ServiceAbility");
    if (setResult) {
        setResult = SetWantElement(&want, element);
        if (setResult) {
            char aName[] = "ServiceAbility";
            EXPECT_STREQ(want.element->abilityName, aName);
            printf("abilityName is %s \n", want.element->abilityName);
            ClearWant(nullptr);
            EXPECT_STREQ(want.element->abilityName, aName);
        }
    }
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testClearWantIllegal------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0025
 * @tc.name      : testWantToUri parameter legal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testWantToUri, Function | MediumTest | Level0)
{
    printf("------start testWantToUri------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
    SetElementDeviceID(&element, "0001000");
J
jiyong 已提交
283
    SetElementBundleName(&element, "com.openharmony.testnative");
J
jiyong 已提交
284
    SetElementAbilityName(&element, "SecondAbility");
M
mamingshuai 已提交
285 286 287 288 289
    if (element.abilityName !=nullptr) {
        bool setResult = SetWantElement(&want, element);
        if (setResult) {
            const char *uri = WantToUri(want);
            printf("uri is %s \n", uri);
J
jiyong 已提交
290
            const char *expectResult = "#Want;device=0001000;bundle=com.openharmony.testnative;ability=SecondAbility;end";
M
mamingshuai 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
            EXPECT_STREQ(uri, expectResult);
            free((void*)uri);
        }
    }
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testWantToUri------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0026
 * @tc.name      : testWantToUri parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testWantToUriIllegal, Function | MediumTest | Level2)
{
    printf("------start testWantToUriIllegal------\n");
    Want want = { nullptr };
    const char *uri = WantToUri(want);
    printf("uri is %s \n", uri);
    const char *expectResult = "#Want;device=;bundle=;ability=;end";
    EXPECT_STREQ(uri, expectResult);
    if (uri != nullptr) {
        free((void*)uri);
    }
    printf("------end testWantToUriIllegal------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0017
 * @tc.name      : testSetWantDate parameter legal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testSetWantDate, Function | MediumTest | Level0)
{
    printf("------start testSetWantDate------\n");
    Want want = { nullptr };
    SetWantData(&want, "test", 5);
    if (want.data != nullptr) {
        printf("data is %s \n", (char*)(want.data));
        printf("dataLength is %d \n", want.dataLength);
        EXPECT_STREQ((char*)(want.data), "test");
        EXPECT_TRUE(want.dataLength == 5);
    }
    ClearWant(&want);
    printf("------end testSetWantDate------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0018
 * @tc.name      : testSetWantDate parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testSetWantDateIllegal, Function | MediumTest | Level2)
{
    printf("------start testSetWantDateIllegal------\n");
    Want want = { nullptr };
    SetWantData(&want, "test", -1);
    printf("dataLength is %d \n", want.dataLength);
    EXPECT_STREQ((char*)(want.data), nullptr);
    EXPECT_TRUE(want.dataLength == 0);
    SetWantData(&want, nullptr, 0);
    printf("dataLength is %d \n", want.dataLength);
    EXPECT_STREQ((char*)(want.data), nullptr);
    EXPECT_TRUE(want.dataLength == 0);
    printf("------end testSetWantDateIllegal------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0023
 * @tc.name      : testWantParseUri parameter legal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testWantParseUri, Function | MediumTest | Level0)
{
    printf("------start testWantParseUri------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
    SetElementDeviceID(&element, "0001000");
J
jiyong 已提交
370
    SetElementBundleName(&element, "com.openharmony.testnative");
M
mamingshuai 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
    SetElementAbilityName(&element, "ServiceAbility");
    if (element.abilityName != nullptr) {
        bool setResult = SetWantElement(&want, element);
        if (setResult) {
            const char *uri = WantToUri(want);
            Want *want2 = WantParseUri(uri);
            printf("uri is %s \n", uri);
            if (uri != nullptr) {
                free((void*)uri);
            }
            EXPECT_STREQ(want2->element->deviceId, want.element->deviceId);
            EXPECT_STREQ(want2->element->abilityName, want.element->abilityName);
            EXPECT_STREQ(want2->element->bundleName, want.element->bundleName);
            free(want2);
        }
    }
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testWantParseUri------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0024
 * @tc.name      : testWantParseUri parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testWantParseUriIllegal, Function | MediumTest | Level2)
{
    printf("------start testWantParseUriIllegal------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
    SetElementBundleName(&element, nullptr);
    SetWantElement(&want, element);
    const char *uri = WantToUri(want);
    // empty
    printf("uri is %s \n", uri);
    Want *want2 = WantParseUri(uri);
    if (uri != nullptr) {
        free((void*)uri);
    }
    if (want2 != nullptr) {
        printf("want is %s \n", want2->element->bundleName);
        EXPECT_STREQ(want2->element->deviceId, "");
        EXPECT_STREQ(want2->element->abilityName, "");
        EXPECT_STREQ(want2->element->bundleName, "");
        free(want2);
    }
    // nullptr
    Want *want4 = WantParseUri(nullptr);
    printf("want4 is %p \n", want4);
    EXPECT_TRUE(want4 == nullptr);
    const char *str = "test";
    // error format
    Want *want1 = WantParseUri(str);
    printf("want is %p \n", want1);
    EXPECT_TRUE(want1 == nullptr);
    Want *want3 = WantParseUri("");
    printf("want is %p \n", want3);
    EXPECT_TRUE(want3 == nullptr);
    free(want1);
    free(want3);
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testWantParseUriIllegal------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0030
 * @tc.name      : testGetBundleNameIllegal parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testGetBundleNameIllegal, Function | MediumTest | Level1)
{
    printf("------start testGetBundleNameIllegal------\n");
    Want want;
C
chengxingzhen 已提交
446
    (void)memset_s(&want, sizeof(Want), 0, sizeof(Want));
M
mamingshuai 已提交
447
    ElementName element;
C
chengxingzhen 已提交
448
    (void)memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
J
jiyong 已提交
449
    SetElementBundleName(&element, "com.openharmony.testnative");
M
mamingshuai 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
    SetElementAbilityName(&element, "ServiceAbility");
    SetWantElement(&want, element);
    int result = StartAbility(&want);
    sleep(2);
    printf("ret is %d \n", result);
    const char * bundleName1 = GetBundleName();
    printf("result of GetBundleName is %s \n", bundleName1);
    EXPECT_STREQ(bundleName1, "");
    g_errorCode = StopAbility(&want);
    sleep(2);
    printf("------end testGetBundleNameIllegal------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0031
 * @tc.name      : testGetSrcPathIllegal parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testGetSrcPathIllegal, Function | MediumTest | Level1)
{
    printf("------start testGetSrcPathIllegal------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
J
jiyong 已提交
473
    SetElementBundleName(&element, "com.openharmony.testnative");
M
mamingshuai 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
    SetElementAbilityName(&element, "ServiceAbility");
    SetWantElement(&want, element);
    int result = StartAbility(&want);
    sleep(2);
    printf("ret is %d \n", result);
    const char * srcPath = GetSrcPath();
    printf("result of GetSrcPath is %s \n", srcPath);
    EXPECT_STREQ(srcPath, "");
    g_errorCode = StopAbility(&want);
    sleep(2);
    printf("------end testGetSrcPathIllegal------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0032
 * @tc.name      : testGetDataPath parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testGetDataPathIllegal, Function | MediumTest | Level1)
{
    printf("------start testGetDataPathIllegal------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
J
jiyong 已提交
497
    SetElementBundleName(&element, "com.openharmony.testnative");
M
mamingshuai 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
    SetElementAbilityName(&element, "ServiceAbility");
    SetWantElement(&want, element);
    int result = StartAbility(&want);
    sleep(2);
    printf("ret is %d \n", result);
    const char * dataPath = GetDataPath();
    printf("result of GetDataPath is %s \n", dataPath);
    EXPECT_STREQ(dataPath, "");
    g_errorCode = StopAbility(&want);
    sleep(2);
    printf("------end testGetDataPathIllegal------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0019
 * @tc.name      : testDump parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testDumpIllegal, Function | MediumTest | Level1)
{
    printf("------start testDump------\n");
    Want want;
C
chengxingzhen 已提交
520
    (void)memset_s(&want, sizeof(Want), 0, sizeof(Want));
M
mamingshuai 已提交
521
    ElementName element;
C
chengxingzhen 已提交
522
    (void)memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
J
jiyong 已提交
523
    SetElementBundleName(&element, "com.openharmony.testnative");
M
mamingshuai 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
    SetElementAbilityName(&element, "ServiceAbility");
    SetWantElement(&want, element);
    Ability *ability = new Ability();
    int result = ability->StartAbility(want);
    sleep(2);
    printf("ret is %d \n", result);
    EXPECT_EQ(result, 0);
    char *extra = (char*)"test";
    ability->Dump(extra);
    g_errorCode = StopAbility(&want);
    sleep(2);
    printf("------end testDump------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0033
 * @tc.name      : testStartAbility parameter legal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testStartAbility, Function | MediumTest | Level1)
{
    printf("------start testStartAbility------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
J
jiyong 已提交
548
    SetElementBundleName(&element, "com.openharmony.testnative");
M
mamingshuai 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
    SetElementAbilityName(&element, "ServiceAbility");
    SetWantElement(&want, element);
    int result = StartAbility(&want);
    sleep(2);
    printf("ret is %d \n", result);
    EXPECT_EQ(result, 0);
    g_errorCode = StopAbility(&want);
    sleep(2);
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testStartAbility------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0005
 * @tc.name      : testStartAbilityIllegal parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testStartAbilityIllegal, Function | MediumTest | Level2)
{
    printf("------start testStartAbilityIllegal------\n");
    int result = StartAbility(nullptr);
    printf("ret is %d \n", result);
    int expect = -1;
    EXPECT_EQ(result, expect);
    printf("------end testStartAbilityIllegal------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0006
 * @tc.name      : testStopAbility parameter legal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testStopAbility, Function | MediumTest | Level0)
{
    printf("------start testStopAbility------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
J
jiyong 已提交
587
    SetElementBundleName(&element, "com.openharmony.testnative");
M
mamingshuai 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
    SetElementAbilityName(&element, "ServiceAbility");
    SetWantElement(&want, element);
    g_errorCode = StartAbility(&want);
    sleep(2);
    printf("ret is %d \n", g_errorCode);
    EXPECT_EQ(g_errorCode, 0);
    g_errorCode = StopAbility(&want);
    sleep(2);
    printf("ret of stop is %d \n", g_errorCode);
    EXPECT_EQ(g_errorCode, 0);
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testStopAbility------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0007
 * @tc.name      : testStopAbilityIllegal parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testStopAbilityIllegal, Function | MediumTest | Level2)
{
    printf("------start testStopAbilityIllegal------\n");
    g_errorCode = StopAbility(nullptr);
    printf("ret of stop is %d \n", g_errorCode);
    EXPECT_EQ(g_errorCode, -1);
    printf("------end testStopAbilityIllegal------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0008
 * @tc.name      : testConnectAbility parameter legal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testConnectAbility, Function | MediumTest | Level1)
{
    printf("------start testConnectAbility------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
J
jiyong 已提交
627
    SetElementBundleName(&element, "com.openharmony.testnative");
M
mamingshuai 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
    SetElementAbilityName(&element, "ServiceAbility");
    SetWantElement(&want, element);
    sem_init(&g_sem, 0, 0);
    int result = ConnectAbility(&want, &g_conn, this);
    struct timespec ts = {};
    clock_gettime(CLOCK_REALTIME, &ts);
    ts.tv_sec += WAIT_TIMEOUT;
    sem_timedwait(&g_sem, &ts);
    printf("sem exit \n");
    printf("ret is %d \n ", result);
    EXPECT_EQ(result, 0);
    EXPECT_EQ(g_errorCode, 16);
    DisconnectAbility(&g_conn);
    sleep(1);
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testConnectAbility------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0010
 * @tc.name      : testWantMathBundle
 * @tc.desc      : [C- SOFTWARE -0100]
J
jiyong 已提交
651
 * @tc.author    : lijiashan
M
mamingshuai 已提交
652 653 654 655 656 657
 */
HWTEST_F(AbilityMgrTest2, testDisConnectAbility, Function | MediumTest | Level1)
{
    printf("------start testDisConnectAbility------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
J
jiyong 已提交
658
    SetElementBundleName(&element, "com.openharmony.testnative");
M
mamingshuai 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
    SetElementAbilityName(&element, "ServiceAbility");
    SetWantElement(&want, element);
    sem_init(&g_sem, 0, 0);
    int result = ConnectAbility(&want, &g_conn, this);
    struct timespec ts = {};
    clock_gettime(CLOCK_REALTIME, &ts);
    ts.tv_sec += WAIT_TIMEOUT;
    sem_timedwait(&g_sem, &ts);
    printf("sem exit \n");
    printf("ret of connect is %d \n ", result);
    EXPECT_EQ(g_errorCode, 16);
    if (g_errorCode == 16) {
        result = DisconnectAbility(&g_conn);
        sleep(2);
        EXPECT_EQ(result, 0);
        printf("ret of disconnect is %d \n ", result);
    }
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testDisConnectAbility------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0009
 * @tc.name      : testConnectAbilityIllegal parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testConnectAbilityIllegal, Function | MediumTest | Level1)
{
    printf("------start testConnectAbilityIllegal------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
J
jiyong 已提交
691
    SetElementBundleName(&element, "com.openharmony.testnative");
M
mamingshuai 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
    SetElementAbilityName(&element, "ServiceAbility");
    SetWantElement(&want, element);
    g_errorCode = ConnectAbility(nullptr, &g_conn, this);
    printf("ret1 is %d \n ", g_errorCode);
    EXPECT_EQ(g_errorCode, -1);
    g_errorCode = ConnectAbility(&want, nullptr, this);
    printf("ret2 is %d \n ", g_errorCode);
    EXPECT_EQ(g_errorCode, -1);
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testConnectAbilityIllegal------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0011
 * @tc.name      : testDisConnectAbilityIllegal parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testDisConnectAbilityIllegal, Function | MediumTest | Level1)
{
    printf("------start testDisConnectAbilityIllegal------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
J
jiyong 已提交
715
    SetElementBundleName(&element, "com.openharmony.testnative");
M
mamingshuai 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
    SetElementAbilityName(&element, "ServiceAbility");
    SetWantElement(&want, element);
    sem_init(&g_sem, 0, 0);
    int result = ConnectAbility(&want, &g_conn, this);
    struct timespec ts = {};
    clock_gettime(CLOCK_REALTIME, &ts);
    ts.tv_sec += WAIT_TIMEOUT;
    sem_timedwait(&g_sem, &ts);
    printf("sem exit \n");
    printf("ret is of connect is %d \n ", g_errorCode);
    EXPECT_EQ(g_errorCode, 16);
    EXPECT_EQ(result, 0);
    g_errorCode = DisconnectAbility(nullptr);
    int expect = -10;
    EXPECT_EQ(g_errorCode, expect);
    printf("ret of disconnect is %d \n ", g_errorCode);
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testDisConnectAbilityIllegal------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0012
 * @tc.name      : testTerminateAbility parameter legal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testTerminateAbility, Function | MediumTest | Level1)
{
    printf("------start testTerminateAbility------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
J
jiyong 已提交
747
    SetElementBundleName(&element, "com.openharmony.testnative");
M
mamingshuai 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
    SetElementAbilityName(&element, "ServiceAbility");
    SetWantElement(&want, element);
    int result1 = StartAbility(&want);
    sleep(2);
    printf("result1 of startAbility is %d \n", result1);
    EXPECT_EQ(result1, 0);
    Ability *ability = new Ability();
    int result2 = ability->TerminateAbility();
    sleep(2);
    printf("result2 of TerminateAbility is %d \n", result2);
    EXPECT_EQ(result2, 0);
    ClearElement(&element);
    ClearWant(&want);
    delete ability;
    printf("------end testTerminateAbility------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_WANT_0001
 * @tc.name      : test Want Match BundleInfo
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testWantMatchBundle, Function | MediumTest | Level1)
{
    printf("------start testWantMathBundle------\n");
    Want want;
C
chengxingzhen 已提交
774
    (void)memset_s(&want, sizeof(Want), 0, sizeof(Want));
M
mamingshuai 已提交
775
    ElementName element;
C
chengxingzhen 已提交
776
    (void)memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
M
mamingshuai 已提交
777
    SetElementAbilityName(&element, "ServiceAbility");
J
jiyong 已提交
778
    SetElementBundleName(&element, "com.openharmony.testnative");
M
mamingshuai 已提交
779 780 781
    SetElementDeviceID(&element, "");
    SetWantElement(&want, element);
    AbilityInfo abilityInfo;
C
chengxingzhen 已提交
782
    (void)memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo));
M
mamingshuai 已提交
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
    g_errorCode = QueryAbilityInfo(&want, &abilityInfo);
    printf("ret of query is %d \n", g_errorCode);
    EXPECT_EQ(g_errorCode, 0);
    if (g_errorCode == 0) {
        printf("abilityInfo.name is %s \n", abilityInfo.name);
    }
    int result = StartAbility(&want);
    sleep(2);
    printf("result of startAbility is %d \n", result);
    EXPECT_EQ(result, 0);
    printf("element is %s \n", want.element->bundleName);
    printf("element is %s \n", want.element->abilityName);
    StopAbility(&want);
    sleep(1);
    g_errorCode = StopAbility(&want);
    sleep(2);
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testWantMathBundle------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_WANT_0004
 * @tc.name      : test Want Not Match BundleInfo
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testWantNotMathBundle, Function | MediumTest | Level2)
{
    printf("------start testWantNotMathBundle------\n");
    Want want;
C
chengxingzhen 已提交
813
    (void)memset_s(&want, sizeof(Want), 0, sizeof(Want));
M
mamingshuai 已提交
814 815
    ElementName element;
    std::string aName = "NoThisAbility";
C
chengxingzhen 已提交
816
    (void)memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
M
mamingshuai 已提交
817
    SetElementAbilityName(&element, aName.c_str());
J
jiyong 已提交
818
    SetElementBundleName(&element, "com.openharmony.nothishap");
M
mamingshuai 已提交
819 820
    SetWantElement(&want, element);
    AbilityInfo abilityInfo;
C
chengxingzhen 已提交
821
    (void)memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo));
M
mamingshuai 已提交
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
    g_errorCode = QueryAbilityInfo(&want, &abilityInfo);
    printf("ret is %d \n", g_errorCode);
    EXPECT_TRUE(g_errorCode != 0);
    int result = StartAbility(&want);
    sleep(2);
    printf("result of startAbility is %d \n", result);
    EXPECT_TRUE(result == 0);
    printf("element is %s \n", want.element->bundleName);
    g_errorCode = StopAbility(&want);
    sleep(2);
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testWantNotMathBundle------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_WANT_0002
 * @tc.name      : testWantOnlyMathBundle
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testWantOnlyMathBundleName, Function | MediumTest | Level1)
{
    printf("------start testWantOnlyMathBundleName------\n");
    Want want;
C
chengxingzhen 已提交
846
    (void)memset_s(&want, sizeof(Want), 0, sizeof(Want));
M
mamingshuai 已提交
847 848
    ElementName element;
    std::string aName = "Ability";
C
chengxingzhen 已提交
849
    (void)memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
M
mamingshuai 已提交
850
    SetElementAbilityName(&element, aName.c_str());
J
jiyong 已提交
851
    SetElementBundleName(&element, "com.openharmony.testnative");
M
mamingshuai 已提交
852 853
    SetWantElement(&want, element);
    AbilityInfo abilityInfo;
C
chengxingzhen 已提交
854
    (void)memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo));
M
mamingshuai 已提交
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
    g_errorCode = QueryAbilityInfo(&want, &abilityInfo);
    printf("ret is %d \n", g_errorCode);
    EXPECT_TRUE(g_errorCode != 0);
    int result = StartAbility(&want);
    sleep(2);
    printf("result of startAbility is %d \n", result);
    EXPECT_TRUE(result == 0);
    printf("element is %s \n", want.element->bundleName);
    g_errorCode = StopAbility(&want);
    sleep(2);
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testWantOnlyMathBundleName------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_WANT_0003
 * @tc.name      : testWantOnlyMathAbility
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testWantOnlyMathAbility, Function | MediumTest | Level1)
{
    printf("------start testWantOnlyMathAbility------\n");
    Want want;
C
chengxingzhen 已提交
879
    (void)memset_s(&want, sizeof(Want), 0, sizeof(Want));
M
mamingshuai 已提交
880 881
    ElementName element;
    std::string aName = "ServiceAbility";
C
chengxingzhen 已提交
882
    (void)memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
M
mamingshuai 已提交
883
    SetElementAbilityName(&element, aName.c_str());
J
jiyong 已提交
884
    SetElementBundleName(&element, "com.openharmony.test");
M
mamingshuai 已提交
885 886
    SetWantElement(&want, element);
    AbilityInfo abilityInfo;
C
chengxingzhen 已提交
887
    (void)memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo));
M
mamingshuai 已提交
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
    g_errorCode = QueryAbilityInfo(&want, &abilityInfo);
    printf("ret is %d \n", g_errorCode);
    EXPECT_TRUE(g_errorCode != 0);
    int result = StartAbility(&want);
    sleep(2);
    printf("result of startAbility is %d \n", result);
    EXPECT_TRUE(result == 0);
    printf("element is %s \n", want.element->abilityName);
    g_errorCode = StopAbility(&want);
    sleep(2);
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testWantOnlyMathAbility------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_WANT_0005
 * @tc.name      : test WantData Match DataLength
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testWantDataMatchLength, Function | MediumTest | Level1)
{
    printf("------start testWantDataMatchLength------\n");
    Want want;
C
chengxingzhen 已提交
912
    (void)memset_s(&want, sizeof(Want), 0, sizeof(Want));
M
mamingshuai 已提交
913
    ElementName element;
C
chengxingzhen 已提交
914
    (void)memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
M
mamingshuai 已提交
915
    SetElementAbilityName(&element, "ServiceAbility");
J
jiyong 已提交
916
    SetElementBundleName(&element, "com.openharmony.testnative");
M
mamingshuai 已提交
917 918 919
    SetWantElement(&want, element);
    SetWantData(&want, "test", 5);
    AbilityInfo abilityInfo;
C
chengxingzhen 已提交
920
    (void)memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo));
M
mamingshuai 已提交
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
    g_errorCode = QueryAbilityInfo(&want, &abilityInfo);
    printf("ret is %d \n", g_errorCode);
    EXPECT_TRUE(g_errorCode == 0);
    int result = StartAbility(&want);
    sleep(2);
    printf("result of startAbility is %d \n", result);
    EXPECT_TRUE(result == 0);
    EXPECT_STREQ((char*)(want.data), "test");
    EXPECT_EQ(want.dataLength, 5);
    StopAbility(&want);
    sleep(1);
    printf("------end testWantDataMatchLength------\n");
}


/**
 * @tc.number    : SUB_APPEXECFWK_AMS_WANT_0006
 * @tc.name      : test WantData Not Match DataLength
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testWantDataNotMatchLength, Function | MediumTest | Level2)
{
    printf("------start testWantDataNotMatchLength------\n");
    Want want;
C
chengxingzhen 已提交
945
    (void)memset_s(&want, sizeof(Want), 0, sizeof(Want));
M
mamingshuai 已提交
946
    ElementName element;
C
chengxingzhen 已提交
947
    (void)memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
M
mamingshuai 已提交
948
    SetElementAbilityName(&element, "ServiceAbility");
J
jiyong 已提交
949
    SetElementBundleName(&element, "com.openharmony.testnative");
M
mamingshuai 已提交
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
    SetWantElement(&want, element);
    SetWantData(&want, "test", 3);
    int result = StartAbility(&want);
    sleep(2);
    printf("result of startAbility is %d \n", result);
    EXPECT_TRUE(result == 0);
    EXPECT_EQ(want.dataLength, 3);
    g_errorCode = StopAbility(&want);
    sleep(2);
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testWantDataNotMatchLength------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0040
 * @tc.name      : PostTask parameter illegal test that callback is null
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest2, testPostTask, Function | MediumTest | Level1)
{
    printf("------start testPostTask------\n");
#ifdef __LINUX__
    string hapPath = g_testPath + "testnative_hispark_aries_linux.hap";
#else
    string hapPath = g_testPath + "testnative_hispark_aries_liteos.hap";
#endif
    AbilityEventHandler eventHandler1;
    auto task = [this, hapPath]{
        sem_init(&g_sem, 0, 0);
        InstallParam installParam = { .installLocation = 1, .keepData = false };
        bool installResult = Install(hapPath.c_str(), &installParam, TestBundleStateCallback);
        sem_wait(&g_sem);
        printf("installResult is %d \n", installResult);
        EXPECT_TRUE(installResult);

        AbilityEventHandler *eventHandler2 = AbilityEventHandler::GetCurrentHandler();
        eventHandler2->PostQuit();
    };
    eventHandler1.PostTask(task);
    eventHandler1.Run();
    printf("------end testPostTask------\n");
}