ability_mgr_test.c 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * 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 <stdio.h>
#include <stdlib.h>
18 19 20 21
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
22 23 24

#include "hctest.h"
#include "ohos_types.h"
25
#include "slite/ability_manager.h"
26
#include "ability_errors.h"
27 28
#include "want.h"

29
#define DATA_EXTERN (2 * 3)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

/**
* @brief  register a test suit named AbilityMgrTestSuite
* @param  subsystem name is aafwk
* @param  module name is  abilitymgr
* @param  test suit name is AbilityMgrTestSuite
*/
LITE_TEST_SUIT(aafwk, abilitymgr, AbilityMgrTestSuite);

static BOOL AbilityMgrTestSuiteSetUp(void)
{
    printf("----------test case with AbilityMgrTest start-------------\n");
    return TRUE;
}

static BOOL AbilityMgrTestSuiteTearDown(void)
{
    printf("----------test case with AbilityMgrTest end-------------\n");
    return TRUE;
}

/**
 * @tc.number    : SUB_AAFWK_ABILITY_0001
53 54
 * @tc.name      : testClearElementLegal
 * @tc.desc      : testClearElement parameter legal test
55
 */
56
LITE_TEST_CASE(AbilityMgrTestSuite, testClearElementLegal, Function | MediumTest | Level0)
57
{
58
    printf("------start testClearElementLegal------\n");
59
    ElementName element = { 0 };
60 61
    SetElementDeviceID(&element, "0001000");
    SetElementBundleName(&element, "com.openharmony.testnative");
62 63 64
    bool ret = SetElementAbilityName(&element, "SecondAbility");
    if (ret) {
        char aName[] = "SecondAbility";
65 66
        char bName[] = "com.openharmony.testnative";
        char dID[] = "0001000";
67
        TEST_ASSERT_EQUAL_STRING(element.abilityName, aName);
68 69
        TEST_ASSERT_EQUAL_STRING(element.bundleName, bName);
        TEST_ASSERT_EQUAL_STRING(element.deviceId, dID);
70 71
        ClearElement(&element);
        TEST_ASSERT_EQUAL_STRING(element.abilityName, NULL);
72 73
        TEST_ASSERT_EQUAL_STRING(element.bundleName, NULL);
        TEST_ASSERT_EQUAL_STRING(element.deviceId, NULL);
74
    }
75
    printf("------end testClearElementLegal------\n");
76 77 78 79
}

/**
 * @tc.number    : SUB_AAFWK_ABILITY_0002
80 81
 * @tc.name      : testClearElementIllegal
 * @tc.desc      : testClearElement parameter illegal test
82 83 84 85 86
 */
LITE_TEST_CASE(AbilityMgrTestSuite, testClearElementIllegal, Function | MediumTest | Level0)
{
    printf("------start testClearElementIllegal------\n");
    ElementName element = { 0 };
87 88
    SetElementDeviceID(&element, "0001000");
    SetElementBundleName(&element, "com.openharmony.testnative");
89 90 91
    bool ret = SetElementAbilityName(&element, "SecondAbility");
    if (ret) {
        char aName[] = "SecondAbility";
92 93
        char bName[] = "com.openharmony.testnative";
        char dID[] = "0001000";
94
        TEST_ASSERT_EQUAL_STRING(element.abilityName, aName);
95 96
        TEST_ASSERT_EQUAL_STRING(element.bundleName, bName);
        TEST_ASSERT_EQUAL_STRING(element.deviceId, dID);
97 98
        ClearElement(NULL);
        TEST_ASSERT_EQUAL_STRING(element.abilityName, aName);
99 100
        TEST_ASSERT_EQUAL_STRING(element.bundleName, bName);
        TEST_ASSERT_EQUAL_STRING(element.deviceId, dID);
101 102 103 104 105 106
    }
    printf("------end testClearElementIllegal------\n");
}

/**
 * @tc.number    : SUB_AAFWK_ABILITY_0003
107 108
 * @tc.name      : testSetWantElementLegal
 * @tc.desc      : testSetWantElement parameter legal test
109
 */
110
LITE_TEST_CASE(AbilityMgrTestSuite, testSetWantElementLegal, Function | MediumTest | Level0)
111
{
112
    printf("------start testSetWantElementLegal------\n");
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    Want want = { 0 };
    ElementName element = { 0 };
    SetElementDeviceID(&element, "0001000");
    SetElementBundleName(&element, "com.openharmony.testnative");
    SetElementAbilityName(&element, "SecondAbility");
    if (element.abilityName != NULL) {
        bool ret = SetWantElement(&want, element);
        if (ret) {
            TEST_ASSERT_EQUAL_STRING(want.element->deviceId, "0001000");
            TEST_ASSERT_EQUAL_STRING(want.element->abilityName, "SecondAbility");
            TEST_ASSERT_EQUAL_STRING(want.element->bundleName, "com.openharmony.testnative");
        }
    }
    ClearElement(&element);
    ClearWant(&want);
128 129
    TEST_ASSERT_EQUAL_STRING(want.element, NULL);
    printf("------end testSetWantElementLegal------\n");
130 131 132 133
}

/**
 * @tc.number    : SUB_AAFWK_ABILITY_0004
134 135
 * @tc.name      : testSetWantElementIllegal
 * @tc.desc      : testSetWantElement parameter illegal test
136 137 138 139
 */
LITE_TEST_CASE(AbilityMgrTestSuite, testSetWantElementIllegal, Function | MediumTest | Level2)
{
    printf("------start testSetWantElementIllegal------\n");
140
    Want want = { 0 };
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    ElementName element = { 0 };
    bool ret = SetWantElement(&want, element);
    if (ret) {
        TEST_ASSERT_EQUAL_STRING(want.element->deviceId, NULL);
        TEST_ASSERT_EQUAL_STRING(want.element->abilityName, NULL);
        TEST_ASSERT_EQUAL_STRING(want.element->bundleName, NULL);
    }
    ClearElement(&element);
    TEST_ASSERT_EQUAL_STRING(element.abilityName, NULL);
    ClearWant(&want);
    TEST_ASSERT_EQUAL_STRING(want.element, NULL);
    printf("------end testSetWantElementIllegal------\n");
}

/**
 * @tc.number    : SUB_AAFWK_ABILITY_0005
157 158
 * @tc.name      : testClearWantIllegal
 * @tc.desc      : testClearWant parameter illegal test
159 160 161 162 163 164
 */
LITE_TEST_CASE(AbilityMgrTestSuite, testClearWantIllegal, Function | MediumTest | Level2)
{
    printf("------start testClearWantIllegal------\n");
    Want want = { 0 };
    ElementName element = { 0 };
165 166
    SetElementDeviceID(&element, "0001000");
    SetElementBundleName(&element, "com.openharmony.testnative");
167 168 169 170
    bool ret = SetElementAbilityName(&element, "SecondAbility");
    if (ret) {
        ret = SetWantElement(&want, element);
        if (ret) {
171 172 173
            TEST_ASSERT_EQUAL_STRING(want.element->deviceId, "0001000");
            TEST_ASSERT_EQUAL_STRING(want.element->abilityName, "SecondAbility");
            TEST_ASSERT_EQUAL_STRING(want.element->bundleName, "com.openharmony.testnative");
174
            ClearWant(NULL);
175 176 177
            TEST_ASSERT_EQUAL_STRING(want.element->deviceId, "0001000");
            TEST_ASSERT_EQUAL_STRING(want.element->abilityName, "SecondAbility");
            TEST_ASSERT_EQUAL_STRING(want.element->bundleName, "com.openharmony.testnative");
178 179 180 181 182 183 184 185 186
        }
    }
    ClearElement(&element);
    ClearWant(&want);
    printf("------end testClearWantIllegal------\n");
}

/**
 * @tc.number    : SUB_AAFWK_ABILITY_0006
187 188
 * @tc.name      : testSetWantDateLegal
 * @tc.desc      : testSetWantDate parameter legal test
189
 */
190
LITE_TEST_CASE(AbilityMgrTestSuite, testSetWantDateLegal, Function | MediumTest | Level0)
191
{
192
    printf("------start testSetWantDateLegal------\n");
193
    Want want = { 0 };
194 195
    char *data = "test";
    SetWantData(&want, (void *)data, strlen(data) + 1);
196
    if (want.data != NULL) {
197 198
        TEST_ASSERT_EQUAL_STRING((char*)(want.data), data);
        TEST_ASSERT_TRUE(want.dataLength == strlen(data) + 1);
199 200
    }
    ClearWant(&want);
201 202 203
    TEST_ASSERT_EQUAL_STRING(want.data, NULL);
    TEST_ASSERT_TRUE(want.dataLength == strlen(data) + 1);
    printf("------end testSetWantDateLegal------\n");
204 205 206 207
}

/**
 * @tc.number    : SUB_AAFWK_ABILITY_0007
208 209
 * @tc.name      : testSetWantDateIllegal
 * @tc.desc      : testSetWantDate parameter illegal test
210 211 212 213 214 215 216 217 218 219 220 221 222 223
 */
LITE_TEST_CASE(AbilityMgrTestSuite, testSetWantDateIllegal, Function | MediumTest | Level2)
{
    printf("------start testSetWantDateIllegal------\n");
    Want want = { 0 };
    SetWantData(&want, "test", -1);
    TEST_ASSERT_EQUAL_STRING((char*)(want.data), NULL);
    TEST_ASSERT_FALSE(want.dataLength);
    SetWantData(&want, NULL, 0);
    TEST_ASSERT_EQUAL_STRING((char*)(want.data), NULL);
    TEST_ASSERT_FALSE(want.dataLength);
    printf("------end testSetWantDateIllegal------\n");
}

224 225
/**
 * @tc.number    : SUB_AAFWK_ABILITY_0008
226 227
 * @tc.name      : testStartAbilityIllegal
 * @tc.desc      : testStartAbility parameter illegal test
228
 */
229
LITE_TEST_CASE(AbilityMgrTestSuite, testStartAbilityIllegal, Function | MediumTest | Level0)
230 231
{
    printf("------start testStartAbilityIllegal------\n");
232 233
    int ret = StartAbility(NULL);
    TEST_ASSERT_TRUE(ret == PARAM_CHECK_ERROR);
234 235 236 237
    printf("------end testStartAbilityIllegal------\n");
}

/**
238 239 240
 * @tc.number    : SUB_AAFWK_ABILITY_0010
 * @tc.name      : testSetIntParamIllegal
 * @tc.desc      : testSetIntParam parameter illegal test
241
 */
242
LITE_TEST_CASE(AbilityMgrTestSuite, testSetIntParamIllegal, Function | MediumTest | Level0)
243 244
{
    printf("------start testSetIntParamIllegal------\n");
245 246
    bool ret = SetIntParam(NULL, NULL, 0, 0);
    TEST_ASSERT_FALSE(ret);
247 248 249 250
    printf("------end testSetIntParamIllegal------\n");
}

/**
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
 * @tc.number    : SUB_AAFWK_ABILITY_0011
 * @tc.name      : testSetIntParamLegal
 * @tc.desc      : testSetIntParam parameter legal test
 */
LITE_TEST_CASE(AbilityMgrTestSuite, testSetIntParamLegal, Function | MediumTest | Level0)
{
    printf("------start testSetIntParamLegal------\n");
    Want want = { 0 };
    int32_t value = 1;
    char *key = "key";
    bool ret = SetIntParam(&want, key, strlen(key), value);
    TEST_ASSERT_TRUE(ret);
    TEST_ASSERT_TRUE(want.dataLength == (strlen(key) + sizeof(int) + DATA_EXTERN));
    TEST_ASSERT_TRUE(want.data != NULL);
    printf("------end testSetIntParamLegal------\n");
}

/**
 * @tc.number    : SUB_AAFWK_ABILITY_0012
 * @tc.name      : testSetStrParamIllegal
 * @tc.desc      : testSetStrParam parameter illegal test
272
 */
273
LITE_TEST_CASE(AbilityMgrTestSuite, testSetStrParamIllegal, Function | MediumTest | Level0)
274 275
{
    printf("------start testSetStrParamIllegal------\n");
276 277
    bool ret = SetStrParam(NULL, NULL, 0, NULL, 0);
    TEST_ASSERT_FALSE(ret);
278 279 280
    printf("------end testSetStrParamIllegal------\n");
}

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
/**
 * @tc.number    : SUB_AAFWK_ABILITY_0013
 * @tc.name      : testSetStrParamLegal
 * @tc.desc      : testSetStrParam parameter legal test
 */
LITE_TEST_CASE(AbilityMgrTestSuite, testSetStrParamLegal, Function | MediumTest | Level2)
{
    printf("------start testSetStrParamLegal------\n");
    Want want = { 0 };
    char *key = "key";
    char *value = "value";
    bool ret = SetStrParam(&want, key, strlen(key), value, strlen(value));
    TEST_ASSERT_TRUE(ret);
    TEST_ASSERT_TRUE(want.dataLength == (strlen(value) + strlen(key) + DATA_EXTERN));
    TEST_ASSERT_TRUE(want.data != NULL);
    printf("------end testSetStrParamLegal------\n");
}

RUN_TEST_SUITE(AbilityMgrTestSuite);