AbilityMgrTest.cpp 33.6 KB
Newer Older
W
wenjun 已提交
1
/**
M
mamingshuai 已提交
2
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
W
wenjun 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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>
M
mamingshuai 已提交
18 19 20
#include <stdio.h>
#include <stdlib.h>
#include <string>
W
wenjun 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

#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;
M
mamingshuai 已提交
42
static string g_testPath;
W
wenjun 已提交
43 44 45 46 47 48 49 50 51 52

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);
M
mamingshuai 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    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");
W
wenjun 已提交
68
    } else {
M
mamingshuai 已提交
69 70 71
        printf("Current Dir: %s\r\n", buffer);
        filePath = buffer;
        free(buffer);
W
wenjun 已提交
72
    }
M
mamingshuai 已提交
73
    return filePath + "/";
W
wenjun 已提交
74 75
}

M
mamingshuai 已提交
76
/* connectAbility callback */
W
wenjun 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
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;
    IpcIoPushInt32(&request, data1);
    IpcIoPushInt32(&request, data2);
    // send and getReply
    IpcIo reply = {nullptr};
    uintptr_t ptr = 0;
    Transact(NULL, *serviceSid, 0, &request, &reply, LITEIPC_FLAG_DEFAULT, &ptr);
M
mamingshuai 已提交
98 99 100
    g_errorCode = IpcIoPopInt32(&reply);
    if (g_errorCode != 0) {
        printf("execute add method, result is %d\n", g_errorCode);
W
wenjun 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    }
    if (ptr != 0) {
        FreeBuffer(nullptr, reinterpret_cast<void *>(ptr));
    }
    sem_post(&g_sem);
}

static void OnAbilityDisconnectDone(ElementName *elementName, int resultCode, void *storeData)
{
    printf("OnAbilityDisconnectDone\n");
}


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

M
mamingshuai 已提交
119
class AbilityMgrTest : public testing::Test {
W
wenjun 已提交
120 121 122 123 124 125 126 127
protected:
    static void SetUpTestCase(void)
    {
        printf("----------test case with AbilityMgrTest start-------------\n");
        HOS_SystemInit();
        AbilityMsClient::GetInstance().Initialize();
        sem_init(&g_sem, 0, 0);
        bool installResult = false;
M
mamingshuai 已提交
128 129 130 131 132 133 134 135
        InstallParam installParam = { .installLocation = 1, .keepData = false };
        g_testPath = GetCurDir();
#ifdef __LINUX__
        string hapPath = g_testPath + "testnative_hispark_taurus_linux.hap";
#else
        string hapPath = g_testPath + "testnative_hispark_taurus_liteos.hap";
#endif
        installResult = Install(hapPath.c_str(), &installParam, TestBundleStateCallback);
W
wenjun 已提交
136 137 138 139 140 141 142 143 144 145 146 147
        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);
M
mamingshuai 已提交
148
        InstallParam installParam = { .installLocation = 1, .keepData = false };
149
        uninstallResult = Uninstall("com.huawei.testnative", &installParam, TestBundleStateCallback);
W
wenjun 已提交
150 151 152 153 154 155 156 157 158
        sem_wait(&g_sem);
        if (uninstallResult) {
            printf("sem exit \n");
        }
        printf("----------test case with AbilityMgrTest end-------------\n");
    }
};

/**
M
mamingshuai 已提交
159
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0013
W
wenjun 已提交
160 161 162
 * @tc.name      : testClearElement parameter legal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
163
HWTEST_F(AbilityMgrTest, testClearElement, Function | MediumTest | Level0)
W
wenjun 已提交
164 165 166
{
    printf("------start testClearElement------\n");
    ElementName element = { nullptr };
M
mamingshuai 已提交
167 168
    bool setResult = SetElementAbilityName(&element, "SecondAbility");
    if (setResult) {
W
wenjun 已提交
169 170
        char aName[] = "SecondAbility";
        EXPECT_STREQ(element.abilityName, aName);
M
mamingshuai 已提交
171
        printf("abilityName is %s \n", element.abilityName);
W
wenjun 已提交
172 173 174 175 176 177 178
        ClearElement(&element);
        EXPECT_STREQ(element.abilityName, nullptr);
    }
    printf("------end testClearElement------\n");
}

/**
M
mamingshuai 已提交
179
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0014
W
wenjun 已提交
180 181 182
 * @tc.name      : testClearElement parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
183
HWTEST_F(AbilityMgrTest, testClearElementIllegal, Function | MediumTest | Level2)
W
wenjun 已提交
184 185 186
{
    printf("------start testClearElementIllegal------\n");
    ElementName element = { nullptr };
M
mamingshuai 已提交
187 188
    bool setResult = SetElementAbilityName(&element, "SecondAbility");
    if (setResult) {
W
wenjun 已提交
189 190
        char aName[] = "SecondAbility";
        EXPECT_STREQ(element.abilityName, aName);
M
mamingshuai 已提交
191
        printf("abilityName is %s \n", element.abilityName);
W
wenjun 已提交
192 193
        ClearElement(nullptr);
        EXPECT_STREQ(element.abilityName, aName);
M
mamingshuai 已提交
194
        printf("AbilityName of element is %s \n", element.abilityName);
W
wenjun 已提交
195 196 197 198 199
    }
    printf("------end testClearElementIllegal------\n");
}

/**
M
mamingshuai 已提交
200
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0015
W
wenjun 已提交
201 202 203
 * @tc.name      : testSetWantElement parameter legal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
204
HWTEST_F(AbilityMgrTest, testSetWantElement, Function | MediumTest | Level0)
W
wenjun 已提交
205 206 207 208 209
{
    printf("------start testSetWantElement------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
    SetElementDeviceID(&element, "0001000");
210
    SetElementBundleName(&element, "com.huawei.testnative");
W
wenjun 已提交
211
    SetElementAbilityName(&element, "SecondAbility");
M
mamingshuai 已提交
212 213 214
    if (element.abilityName != nullptr) {
        bool setResult = SetWantElement(&want, element);
        if (setResult) {
W
wenjun 已提交
215 216
            EXPECT_STREQ(want.element->deviceId, "0001000");
            EXPECT_STREQ(want.element->abilityName, "SecondAbility");
217
            EXPECT_STREQ(want.element->bundleName, "com.huawei.testnative");
W
wenjun 已提交
218 219 220 221 222 223 224 225
        }
    }
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testSetWantElement------\n");
}

/**
M
mamingshuai 已提交
226
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0016
W
wenjun 已提交
227 228 229
 * @tc.name      : testSetWantElement parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
230
HWTEST_F(AbilityMgrTest, testSetWantElementIllegal, Function | MediumTest | Level2)
W
wenjun 已提交
231 232 233 234
{
    printf("------start testSetWantElementIllegal------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
M
mamingshuai 已提交
235 236
    bool setResult = SetWantElement(&want, element);
    if (setResult) {
W
wenjun 已提交
237 238 239 240 241 242 243 244 245 246
        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");
}

/**
M
mamingshuai 已提交
247
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0022
W
wenjun 已提交
248 249 250
 * @tc.name      : testClearWant parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
251
HWTEST_F(AbilityMgrTest, testClearWantIllegal, Function | MediumTest | Level2)
W
wenjun 已提交
252 253 254 255
{
    printf("------start testClearWantIllegal------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
M
mamingshuai 已提交
256 257 258 259
    bool setResult = SetElementAbilityName(&element, "SecondAbility");
    if (setResult) {
        setResult = SetWantElement(&want, element);
        if (setResult) {
W
wenjun 已提交
260 261
            char aName[] = "SecondAbility";
            EXPECT_STREQ(want.element->abilityName, aName);
M
mamingshuai 已提交
262
            printf("abilityName is %s \n", want.element->abilityName);
W
wenjun 已提交
263 264 265 266 267 268 269 270 271 272
            ClearWant(nullptr);
            EXPECT_STREQ(want.element->abilityName, aName);
        }
    }
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testClearWantIllegal------\n");
}

/**
M
mamingshuai 已提交
273
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0025
W
wenjun 已提交
274 275 276
 * @tc.name      : testWantToUri parameter legal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
277
HWTEST_F(AbilityMgrTest, testWantToUri, Function | MediumTest | Level0)
W
wenjun 已提交
278 279 280 281 282
{
    printf("------start testWantToUri------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
    SetElementDeviceID(&element, "0001000");
283
    SetElementBundleName(&element, "com.huawei.testnative");
W
wenjun 已提交
284 285
    SetElementAbilityName(&element, "SecondAbility");
    if (element.abilityName !=nullptr) {
M
mamingshuai 已提交
286 287
        bool setResult = SetWantElement(&want, element);
        if (setResult) {
W
wenjun 已提交
288 289
            const char *uri = WantToUri(want);
            printf("uri is %s \n", uri);
290
            const char *expectResult = "#Want;device=0001000;bundle=com.huawei.testnative;ability=SecondAbility;end";
W
wenjun 已提交
291 292 293 294 295 296 297 298 299 300
            EXPECT_STREQ(uri, expectResult);
            free((void*)uri);
        }
    }
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testWantToUri------\n");
}

/**
M
mamingshuai 已提交
301
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0026
W
wenjun 已提交
302 303 304
 * @tc.name      : testWantToUri parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
305
HWTEST_F(AbilityMgrTest, testWantToUriIllegal, Function | MediumTest | Level2)
W
wenjun 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319
{
    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");
}

/**
M
mamingshuai 已提交
320
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0017
W
wenjun 已提交
321 322 323
 * @tc.name      : testSetWantDate parameter legal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
324
HWTEST_F(AbilityMgrTest, testSetWantDate, Function | MediumTest | Level0)
W
wenjun 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
{
    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");
}

/**
M
mamingshuai 已提交
340
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0018
W
wenjun 已提交
341 342 343
 * @tc.name      : testSetWantDate parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
344
HWTEST_F(AbilityMgrTest, testSetWantDateIllegal, Function | MediumTest | Level2)
W
wenjun 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
{
    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");
}

/**
M
mamingshuai 已提交
360
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0023
W
wenjun 已提交
361 362 363
 * @tc.name      : testWantParseUri parameter legal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
364
HWTEST_F(AbilityMgrTest, testWantParseUri, Function | MediumTest | Level0)
W
wenjun 已提交
365 366 367 368 369
{
    printf("------start testWantParseUri------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
    SetElementDeviceID(&element, "0001000");
370
    SetElementBundleName(&element, "com.huawei.testnative");
W
wenjun 已提交
371 372
    SetElementAbilityName(&element, "SecondAbility");
    if (element.abilityName != nullptr) {
M
mamingshuai 已提交
373 374
        bool setResult = SetWantElement(&want, element);
        if (setResult) {
W
wenjun 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
            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");
}

/**
M
mamingshuai 已提交
393
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0024
W
wenjun 已提交
394 395 396
 * @tc.name      : testWantParseUri parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
397
HWTEST_F(AbilityMgrTest, testWantParseUriIllegal, Function | MediumTest | Level2)
W
wenjun 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
{
    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);
M
mamingshuai 已提交
417
    }
W
wenjun 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
    // 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");
}

/**
M
mamingshuai 已提交
438
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0030
W
wenjun 已提交
439 440 441
 * @tc.name      : testGetBundleNameIllegal parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
442
HWTEST_F(AbilityMgrTest, testGetBundleNameIllegal, Function | MediumTest | Level1)
W
wenjun 已提交
443 444 445 446 447 448
{
    printf("------start testGetBundleNameIllegal------\n");
    Want want;
    memset_s(&want, sizeof(Want), 0, sizeof(Want));
    ElementName element;
    memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
449
    SetElementBundleName(&element, "com.huawei.testnative");
M
mamingshuai 已提交
450
    SetElementAbilityName(&element, "SecondAbility");
W
wenjun 已提交
451 452 453 454 455
    SetWantElement(&want, element);
    int result = StartAbility(&want);
    sleep(2);
    printf("ret is %d \n", result);
    const char * bundleName1 = GetBundleName();
M
mamingshuai 已提交
456
    printf("result of GetBundleName is %s \n", bundleName1);
W
wenjun 已提交
457 458 459 460 461
    EXPECT_STREQ(bundleName1, "");
    printf("------end testGetBundleNameIllegal------\n");
}

/**
M
mamingshuai 已提交
462
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0031
W
wenjun 已提交
463 464 465
 * @tc.name      : testGetSrcPathIllegal parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
466
HWTEST_F(AbilityMgrTest, testGetSrcPathIllegal, Function | MediumTest | Level1)
W
wenjun 已提交
467 468 469 470
{
    printf("------start testGetSrcPathIllegal------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
471
    SetElementBundleName(&element, "com.huawei.testnative");
M
mamingshuai 已提交
472
    SetElementAbilityName(&element, "SecondAbility");
W
wenjun 已提交
473 474 475 476 477 478 479 480 481 482
    SetWantElement(&want, element);
    int result = StartAbility(&want);
    sleep(2);
    printf("ret is %d \n", result);
    const char * srcPath = GetSrcPath();
    EXPECT_STREQ(srcPath, "");
    printf("------end testGetSrcPathIllegal------\n");
}

/**
M
mamingshuai 已提交
483
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0032
W
wenjun 已提交
484 485 486
 * @tc.name      : testGetDataPath parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
487
HWTEST_F(AbilityMgrTest, testGetDataPathIllegal, Function | MediumTest | Level1)
W
wenjun 已提交
488 489 490 491
{
    printf("------start testGetDataPathIllegal------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
492
    SetElementBundleName(&element, "com.huawei.testnative");
M
mamingshuai 已提交
493
    SetElementAbilityName(&element, "SecondAbility");
W
wenjun 已提交
494 495 496 497 498
    SetWantElement(&want, element);
    int result = StartAbility(&want);
    sleep(2);
    printf("ret is %d \n", result);
    const char * dataPath = GetDataPath();
M
mamingshuai 已提交
499
    printf("result of GetDataPath is %s \n", dataPath);
W
wenjun 已提交
500 501 502 503 504
    EXPECT_STREQ(dataPath, "");
    printf("------end testGetDataPathIllegal------\n");
}

/**
M
mamingshuai 已提交
505
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0019
W
wenjun 已提交
506 507 508
 * @tc.name      : testDump parameter illegal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
509
HWTEST_F(AbilityMgrTest, testDumpIllegal, Function | MediumTest | Level1)
W
wenjun 已提交
510 511 512 513 514 515
{
    printf("------start testDump------\n");
    Want want;
    memset_s(&want, sizeof(Want), 0, sizeof(Want));
    ElementName element;
    memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
516
    SetElementBundleName(&element, "com.huawei.testnative");
M
mamingshuai 已提交
517
    SetElementAbilityName(&element, "SecondAbility");
W
wenjun 已提交
518 519 520
    SetWantElement(&want, element);
    Ability *ability = new Ability();
    int result = ability->StartAbility(want);
M
mamingshuai 已提交
521
    sleep(2);
W
wenjun 已提交
522 523 524 525 526 527 528 529
    printf("ret is %d \n", result);
    EXPECT_EQ(result, 0);
    char *extra = (char*)"test";
    ability->Dump(extra);
    printf("------end testDump------\n");
}

/**
M
mamingshuai 已提交
530 531
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0033
 * @tc.name      : testStartAbility parameter legal test
W
wenjun 已提交
532 533
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
534
HWTEST_F(AbilityMgrTest, testStartAbility, Function | MediumTest | Level1)
W
wenjun 已提交
535
{
M
mamingshuai 已提交
536
    printf("------start testStartAbility------\n");
W
wenjun 已提交
537 538
    Want want = { nullptr };
    ElementName element = { nullptr };
539
    SetElementBundleName(&element, "com.huawei.testnative");
M
mamingshuai 已提交
540
    SetElementAbilityName(&element, "MainAbility");
W
wenjun 已提交
541
    SetWantElement(&want, element);
M
mamingshuai 已提交
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
    int result = StartAbility(&want);
    sleep(2);
    printf("ret is %d \n", result);
    EXPECT_EQ(result, 0);
    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(AbilityMgrTest, testStartAbilityIllegal, Function | MediumTest | Level2)
{
    printf("------start testStartAbilityIllegal------\n");
W
wenjun 已提交
559 560 561 562 563 564 565 566
    int result = StartAbility(nullptr);
    printf("ret is %d \n", result);
    int expect = -1;
    EXPECT_EQ(result, expect);
    printf("------end testStartAbilityIllegal------\n");
}

/**
M
mamingshuai 已提交
567 568
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0006
 * @tc.name      : testStopAbility parameter legal test
W
wenjun 已提交
569 570
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
571
HWTEST_F(AbilityMgrTest, testStopAbility, Function | MediumTest | Level0)
W
wenjun 已提交
572
{
M
mamingshuai 已提交
573
    printf("------start testStopAbility------\n");
W
wenjun 已提交
574 575
    Want want = { nullptr };
    ElementName element = { nullptr };
576
    SetElementBundleName(&element, "com.huawei.testnative");
W
wenjun 已提交
577 578 579
    SetElementAbilityName(&element, "ServiceAbility");
    SetWantElement(&want, element);
    g_errorCode = StartAbility(&want);
M
mamingshuai 已提交
580
    sleep(2);
W
wenjun 已提交
581 582
    printf("ret is %d \n", g_errorCode);
    EXPECT_EQ(g_errorCode, 0);
M
mamingshuai 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
    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(AbilityMgrTest, testStopAbilityIllegal, Function | MediumTest | Level2)
{
    printf("------start testStopAbilityIllegal------\n");
W
wenjun 已提交
600 601 602 603 604 605 606
    g_errorCode = StopAbility(nullptr);
    printf("ret of stop is %d \n", g_errorCode);
    EXPECT_EQ(g_errorCode, -1);
    printf("------end testStopAbilityIllegal------\n");
}

/**
M
mamingshuai 已提交
607 608
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0008
 * @tc.name      : testConnectAbility parameter legal test
W
wenjun 已提交
609 610
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
611
HWTEST_F(AbilityMgrTest, testConnectAbility, Function | MediumTest | Level1)
W
wenjun 已提交
612
{
M
mamingshuai 已提交
613
    printf("------start testConnectAbility------\n");
W
wenjun 已提交
614 615
    Want want = { nullptr };
    ElementName element = { nullptr };
616
    SetElementBundleName(&element, "com.huawei.testnative");
W
wenjun 已提交
617 618 619
    SetElementAbilityName(&element, "ServiceAbility");
    SetWantElement(&want, element);
    sem_init(&g_sem, 0, 0);
M
mamingshuai 已提交
620
    int result = ConnectAbility(&want, &g_conn, this);
W
wenjun 已提交
621 622 623 624 625
    struct timespec ts = {};
    clock_gettime(CLOCK_REALTIME, &ts);
    ts.tv_sec += WAIT_TIMEOUT;
    sem_timedwait(&g_sem, &ts);
    printf("sem exit \n");
M
mamingshuai 已提交
626 627 628 629
    printf("ret is %d \n ", result);
    EXPECT_EQ(result, 0);
    DisconnectAbility(&g_conn);
    sleep(1);
W
wenjun 已提交
630 631
    ClearElement(&element);
    ClearWant(&want);
M
mamingshuai 已提交
632
    printf("------end testConnectAbility------\n");
W
wenjun 已提交
633 634 635
}

/**
M
mamingshuai 已提交
636 637 638 639 640 641 642 643 644 645
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0010
 * @tc.name      : testWantMathBundle
 * @tc.desc      : [C- SOFTWARE -0100]
 * @tc.author    : lijiashan 00523117
 */
HWTEST_F(AbilityMgrTest, testDisConnectAbility, Function | MediumTest | Level1)
{
    printf("------start testDisConnectAbility------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
646
    SetElementBundleName(&element, "com.huawei.testnative");
M
mamingshuai 已提交
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
    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);
    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
W
wenjun 已提交
671 672
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
673
HWTEST_F(AbilityMgrTest, testConnectAbilityIllegal, Function | MediumTest | Level1)
W
wenjun 已提交
674
{
M
mamingshuai 已提交
675
    printf("------start testConnectAbilityIllegal------\n");
W
wenjun 已提交
676 677
    Want want = { nullptr };
    ElementName element = { nullptr };
678
    SetElementBundleName(&element, "com.huawei.testnative");
W
wenjun 已提交
679 680 681 682 683 684 685 686 687 688
    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);
M
mamingshuai 已提交
689
    printf("------end testConnectAbilityIllegal------\n");
W
wenjun 已提交
690 691 692
}

/**
M
mamingshuai 已提交
693 694
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0011
 * @tc.name      : testDisConnectAbilityIllegal parameter illegal test
W
wenjun 已提交
695 696
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
697
HWTEST_F(AbilityMgrTest, testDisConnectAbilityIllegal, Function | MediumTest | Level1)
W
wenjun 已提交
698
{
M
mamingshuai 已提交
699
    printf("------start testDisConnectAbilityIllegal------\n");
W
wenjun 已提交
700 701
    Want want = { nullptr };
    ElementName element = { nullptr };
702
    SetElementBundleName(&element, "com.huawei.testnative");
W
wenjun 已提交
703 704 705
    SetElementAbilityName(&element, "ServiceAbility");
    SetWantElement(&want, element);
    sem_init(&g_sem, 0, 0);
M
mamingshuai 已提交
706
    int result = ConnectAbility(&want, &g_conn, this);
W
wenjun 已提交
707 708 709 710 711 712
    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);
M
mamingshuai 已提交
713
    EXPECT_EQ(result, 0);
W
wenjun 已提交
714 715 716 717 718 719
    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);
M
mamingshuai 已提交
720
    printf("------end testDisConnectAbilityIllegal------\n");
W
wenjun 已提交
721 722 723
}

/**
M
mamingshuai 已提交
724
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0012
W
wenjun 已提交
725 726 727
 * @tc.name      : testTerminateAbility parameter legal test
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
728
HWTEST_F(AbilityMgrTest, testTerminateAbility, Function | MediumTest | Level1)
W
wenjun 已提交
729 730 731 732
{
    printf("------start testTerminateAbility------\n");
    Want want = { nullptr };
    ElementName element = { nullptr };
733
    SetElementBundleName(&element, "com.huawei.testnative");
W
wenjun 已提交
734 735 736
    SetElementAbilityName(&element, "SecondAbility");
    SetWantElement(&want, element);
    int result1 = StartAbility(&want);
M
mamingshuai 已提交
737
    sleep(2);
W
wenjun 已提交
738 739 740 741
    printf("result1 of startAbility is %d \n", result1);
    EXPECT_EQ(result1, 0);
    Ability *ability = new Ability();
    int result2 = ability->TerminateAbility();
M
mamingshuai 已提交
742
    sleep(2);
W
wenjun 已提交
743 744 745 746 747 748 749 750 751
    printf("result2 of TerminateAbility is %d \n", result2);
    EXPECT_EQ(result2, 0);
    ClearElement(&element);
    ClearWant(&want);
    delete ability;
    printf("------end testTerminateAbility------\n");
}

/**
M
mamingshuai 已提交
752 753 754 755 756 757 758 759 760 761 762 763
 * @tc.number    : SUB_APPEXECFWK_AMS_WANT_0001
 * @tc.name      : test Want Match BundleInfo
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest, testWantMatchBundle, Function | MediumTest | Level1)
{
    printf("------start testWantMathBundle------\n");
    Want want;
    memset_s(&want, sizeof(Want), 0, sizeof(Want));
    ElementName element;
    memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
    SetElementAbilityName(&element, "MainAbility");
764
    SetElementBundleName(&element, "com.huawei.testnative");
M
mamingshuai 已提交
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
    SetElementDeviceID(&element, "");
    SetWantElement(&want, element);
    AbilityInfo abilityInfo;
    memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo));
    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);
    printf("------end testWantMathBundle------\n");
}

/**
 * @tc.number    : SUB_APPEXECFWK_AMS_WANT_0004
W
wenjun 已提交
788 789 790
 * @tc.name      : test Want Not Match BundleInfo
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
791
HWTEST_F(AbilityMgrTest, testWantNotMathBundle, Function | MediumTest | Level2)
W
wenjun 已提交
792 793 794 795 796
{
    printf("------start testWantNotMathBundle------\n");
    Want want;
    memset_s(&want, sizeof(Want), 0, sizeof(Want));
    ElementName element;
M
mamingshuai 已提交
797
    std::string aName = "NoThisAbility";
W
wenjun 已提交
798
    memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
M
mamingshuai 已提交
799
    SetElementAbilityName(&element, aName.c_str());
800
    SetElementBundleName(&element, "com.huawei.nothishap");
W
wenjun 已提交
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
    SetWantElement(&want, element);
    AbilityInfo abilityInfo;
    memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo));
    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);
    printf("------end testWantNotMathBundle------\n");
}

/**
M
mamingshuai 已提交
816
 * @tc.number    : SUB_APPEXECFWK_AMS_WANT_0002
W
wenjun 已提交
817 818 819
 * @tc.name      : testWantOnlyMathBundle
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
820
HWTEST_F(AbilityMgrTest, testWantOnlyMathBundleName, Function | MediumTest | Level1)
W
wenjun 已提交
821 822 823 824 825
{
    printf("------start testWantOnlyMathBundleName------\n");
    Want want;
    memset_s(&want, sizeof(Want), 0, sizeof(Want));
    ElementName element;
M
mamingshuai 已提交
826
    std::string aName = "Ability";
W
wenjun 已提交
827
    memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
M
mamingshuai 已提交
828
    SetElementAbilityName(&element, aName.c_str());
829
    SetElementBundleName(&element, "com.huawei.testnative");
W
wenjun 已提交
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
    SetWantElement(&want, element);
    AbilityInfo abilityInfo;
    memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo));
    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);
    printf("------end testWantOnlyMathBundleName------\n");
}

/**
M
mamingshuai 已提交
845
 * @tc.number    : SUB_APPEXECFWK_AMS_WANT_0003
W
wenjun 已提交
846 847 848
 * @tc.name      : testWantOnlyMathAbility
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
849
HWTEST_F(AbilityMgrTest, testWantOnlyMathAbility, Function | MediumTest | Level1)
W
wenjun 已提交
850 851 852 853 854
{
    printf("------start testWantOnlyMathAbility------\n");
    Want want;
    memset_s(&want, sizeof(Want), 0, sizeof(Want));
    ElementName element;
M
mamingshuai 已提交
855
    std::string aName = "MainAbility";
W
wenjun 已提交
856
    memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
M
mamingshuai 已提交
857
    SetElementAbilityName(&element, aName.c_str());
858
    SetElementBundleName(&element, "com.huawei.test");
W
wenjun 已提交
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
    SetWantElement(&want, element);
    AbilityInfo abilityInfo;
    memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo));
    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);
    printf("------end testWantOnlyMathAbility------\n");
}

/**
M
mamingshuai 已提交
874 875 876 877 878 879 880 881 882 883 884 885
 * @tc.number    : SUB_APPEXECFWK_AMS_WANT_0005
 * @tc.name      : test WantData Match DataLength
 * @tc.desc      : [C- SOFTWARE -0200]
 */
HWTEST_F(AbilityMgrTest, testWantDataMatchLength, Function | MediumTest | Level1)
{
    printf("------start testWantDataMatchLength------\n");
    Want want;
    memset_s(&want, sizeof(Want), 0, sizeof(Want));
    ElementName element;
    memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
    SetElementAbilityName(&element, "MainAbility");
886
    SetElementBundleName(&element, "com.huawei.testnative");
M
mamingshuai 已提交
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
    SetWantElement(&want, element);
    SetWantData(&want, "test", 5);
    AbilityInfo abilityInfo;
    memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo));
    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
W
wenjun 已提交
908 909 910
 * @tc.name      : test WantData Not Match DataLength
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
911
HWTEST_F(AbilityMgrTest, testWantDataNotMatchLength, Function | MediumTest | Level2)
W
wenjun 已提交
912 913 914 915 916 917 918
{
    printf("------start testWantDataNotMatchLength------\n");
    Want want;
    memset_s(&want, sizeof(Want), 0, sizeof(Want));
    ElementName element;
    memset_s(&element, sizeof(ElementName), 0, sizeof(ElementName));
    SetElementAbilityName(&element, "SecondAbility");
919
    SetElementBundleName(&element, "com.huawei.testnative");
W
wenjun 已提交
920 921 922 923 924 925 926 927 928 929 930
    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);
    printf("------end testWantDataNotMatchLength------\n");
}

/**
M
mamingshuai 已提交
931
 * @tc.number    : SUB_APPEXECFWK_AMS_API_0040
W
wenjun 已提交
932 933 934
 * @tc.name      : PostTask parameter illegal test that callback is null
 * @tc.desc      : [C- SOFTWARE -0200]
 */
M
mamingshuai 已提交
935
HWTEST_F(AbilityMgrTest, testPostTask, Function | MediumTest | Level1)
W
wenjun 已提交
936 937
{
    printf("------start testPostTask------\n");
M
mamingshuai 已提交
938 939 940 941 942
#ifdef __LINUX__
    string hapPath = g_testPath + "testnative_hispark_taurus_linux.hap";
#else
    string hapPath = g_testPath + "testnative_hispark_taurus_liteos.hap";
#endif
W
wenjun 已提交
943 944 945
    AbilityEventHandler eventHandler1;
    auto task = [this, hapPath]{
        sem_init(&g_sem, 0, 0);
M
mamingshuai 已提交
946 947
        InstallParam installParam = { .installLocation = 1, .keepData = false };
        bool installResult = Install(hapPath.c_str(), &installParam, TestBundleStateCallback);
W
wenjun 已提交
948 949 950 951 952 953 954 955 956 957 958
        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");
}