1/* 2 * Copyright (C) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import hiAppEventV9 from "@ohos.hiviewdfx.hiAppEvent" 17 18import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' 19 20describe('HiAppEventJsTest', function () { 21 beforeAll(function() { 22 /* 23 * @tc.setup: setup invoked before all test cases 24 */ 25 console.info('HiAppEventJsTest beforeAll called') 26 }) 27 28 afterAll(function() { 29 /* 30 * @tc.teardown: teardown invoked after all test cases 31 */ 32 console.info('HiAppEventJsTest afterAll called') 33 }) 34 35 beforeEach(function() { 36 /* 37 * @tc.setup: setup invoked before each test case 38 */ 39 console.info('HiAppEventJsTest beforeEach called') 40 }) 41 42 afterEach(function() { 43 /* 44 * @tc.teardown: teardown invoked after each test case 45 */ 46 console.info('HiAppEventJsTest afterEach called') 47 }) 48 49 function createError2(name, type) { 50 return { code: "401", message: "Parameter error. The type of " + name + " must be " + type + "." }; 51 } 52 53 function createError4(name) { 54 return { code: "401", message: "Parameter error. The " + name + " parameter is invalid." }; 55 } 56 57 function assertErrorEqual(actualErr, expectErr) { 58 if (expectErr) { 59 expect(actualErr.code).assertEqual(expectErr.code) 60 expect(actualErr.message).assertEqual(expectErr.message) 61 } else { 62 expect(actualErr).assertNull(); 63 } 64 } 65 66 const USER_ID_NAME = 'testUserIdName'; 67 const USER_ID_VALUE = 'testUserIdValue'; 68 const USER_PROP_NAME = 'testUserPropName'; 69 const USER_PROP_VALUE = 'testUserPropValue'; 70 const INVALID_USER_ID_NAMES = [-1, '', null, undefined, '%test', `testUserIdName` + 'a'.repeat(256)]; 71 const INVALID_USER_ID_VALUES = [-1, '', null, undefined, `testUserIdValue` + 'a'.repeat(256)]; 72 const INVALID_USER_PROP_NAMES = [-1, '', null, undefined, '%test', `testUserPropName` + 'a'.repeat(256)]; 73 const INVALID_USER_PROP_VALUES = [-1, '', null, undefined, `testUserPropValue` + 'a'.repeat(1024)]; 74 const CLEAR_USER_INFO_VALUES = ['', null, undefined]; 75 76 /** 77 * @tc.number: HiAppEventUserIdsTest001_1 78 * @tc.name: HiAppEventUserIdsTest 79 * @tc.desc: Correctly set and get the user ids function. 80 * @tc.type: FUNC 81 * @tc.require: issueI8G4M9 82 */ 83 it('HiAppEventUserIdsTest001_1', 0, function () { 84 console.info('HiAppEventUserIdsTest001_1 start'); 85 86 let expectErr = createError2("userId", "string"); 87 88 try { 89 console.info(`setUserId name: ${USER_ID_NAME}, value: ${USER_ID_VALUE}`); 90 hiAppEventV9.setUserId(USER_ID_NAME, USER_ID_VALUE); 91 92 console.info(`getUserId name: ${USER_ID_NAME}`); 93 let res = hiAppEventV9.getUserId(USER_ID_NAME); 94 expect(res != "").assertTrue(); 95 } catch (err) { 96 assertErrorEqual(err, expectErr); 97 } 98 99 console.info('HiAppEventUserIdsTest001_1 end'); 100 }); 101 102 /** 103 * @tc.number: HiAppEventUserIdsTest001_2 104 * @tc.name: HiAppEventUserIdsTest 105 * @tc.desc: Correctly clear and get the user ids function. 106 * @tc.type: FUNC 107 * @tc.require: issueI8G4M9 108 */ 109 it('HiAppEventUserIdsTest001_2', 0, function () { 110 console.info('HiAppEventUserIdsTest001_2 start'); 111 112 let expectErr = createError2("userId", "string"); 113 114 try { 115 console.info(`setUserId name: ${USER_ID_NAME}, value: ${USER_ID_VALUE}`); 116 hiAppEventV9.setUserId(USER_ID_NAME, USER_ID_VALUE); 117 118 hiAppEventV9.clearData(); 119 120 console.info(`getUserId name: ${USER_ID_NAME}`); 121 let res = hiAppEventV9.getUserId(USER_ID_NAME); 122 expect(res == "").assertTrue(); 123 } catch (err) { 124 assertErrorEqual(err, expectErr); 125 } 126 127 console.info('HiAppEventUserIdsTest001_2 end'); 128 }); 129 130 /** 131 * @tc.number: HiAppEventUserIdsTest002_1 132 * @tc.name: HiAppEventUserIdsTest 133 * @tc.desc: Incorrectly set the user ids function. 134 * @tc.type: FUNC 135 * @tc.require: issueI8G4M9 136 */ 137 it('HiAppEventUserIdsTest002_1', 0, function () { 138 console.info('HiAppEventUserIdsTest002_1 start'); 139 140 for (let userIdName of INVALID_USER_ID_NAMES) { 141 let expectErr = typeof userIdName == 'string' ? createError4("name") : createError2("name", "string"); 142 try { 143 console.info(`setUserId name: ${userIdName}, value: ${USER_ID_VALUE}`); 144 hiAppEventV9.setUserId(userIdName, USER_ID_VALUE); 145 } catch (err) { 146 assertErrorEqual(err, expectErr); 147 } 148 } 149 for (let userIdVal of INVALID_USER_ID_VALUES) { 150 let expectErr = typeof userIdVal == 'string' ? createError4("value") : createError2("userId", "string"); 151 try { 152 console.info(`setUserId name: ${USER_ID_NAME}, value: ${userIdVal}`); 153 hiAppEventV9.setUserId(USER_ID_NAME, userIdVal); 154 } catch (err) { 155 assertErrorEqual(err, expectErr); 156 } 157 } 158 159 console.info('HiAppEventUserIdsTest002_1 end'); 160 }); 161 162 /** 163 * @tc.number: HiAppEventUserIdsTest003_1 164 * @tc.name: HiAppEventUserIdsTest 165 * @tc.desc: Incorrectly get the user ids function. 166 * @tc.type: FUNC 167 * @tc.require: issueI8G4M9 168 */ 169 it('HiAppEventUserIdsTest003_1', 0, function () { 170 console.info('HiAppEventUserIdsTest003_1 start'); 171 172 console.info(`setUserId name: ${USER_ID_NAME}, value: ${USER_ID_VALUE}`); 173 hiAppEventV9.setUserId(USER_ID_NAME, USER_ID_VALUE); 174 175 for (let userIdName of INVALID_USER_ID_NAMES) { 176 let expectErr = typeof userIdName == 'string' ? createError4("name") : createError2("name", "string"); 177 try { 178 console.info(`getUserId name: ${userIdName}`); 179 let res = hiAppEventV9.getUserId(userIdName); 180 expect(res == "").assertTrue(); 181 } catch (err) { 182 assertErrorEqual(err, expectErr); 183 } 184 } 185 186 console.info('HiAppEventUserIdsTest003_1 end'); 187 }); 188 189 /** 190 * @tc.number: HiAppEventUserIdsTest004_1 191 * @tc.name: HiAppEventUserIdsTest 192 * @tc.desc: Correctly replace and get the user ids function. 193 * @tc.type: FUNC 194 * @tc.require: issueI8G4M9 195 */ 196 it('HiAppEventUserIdsTest004_1', 0, function () { 197 console.info('HiAppEventUserIdsTest004_1 start'); 198 199 let expectErr = createError2("userId", "string"); 200 201 try { 202 console.info(`setUserId name: ${USER_ID_NAME}, value: ${USER_ID_VALUE}`); 203 hiAppEventV9.setUserId(USER_ID_NAME, USER_ID_VALUE); 204 console.info(`setUserId name: ${USER_ID_NAME}, value: ${'testUserIdValue1'}`); 205 hiAppEventV9.setUserId(USER_ID_NAME, "testUserIdValue1"); 206 207 console.info(`getUserId name: ${USER_ID_NAME}`); 208 let res = hiAppEventV9.getUserId(USER_ID_NAME); 209 expect(res == "testUserIdValue1").assertTrue(); 210 } catch (err) { 211 assertErrorEqual(err, expectErr); 212 } 213 214 console.info('HiAppEventUserIdsTest004_1 end'); 215 }); 216 217 /** 218 * @tc.number: HiAppEventUserIdsTest005_1 219 * @tc.name: HiAppEventUserIdsTest 220 * @tc.desc: Correctly remove and get the user ids function. 221 * @tc.type: FUNC 222 * @tc.require: issueI8G4M9 223 */ 224 it('HiAppEventUserIdsTest005_1', 0, function () { 225 console.info('HiAppEventUserIdsTest005_1 start'); 226 227 let expectErr = createError2("userId", "string"); 228 229 for (let userIdVal of CLEAR_USER_INFO_VALUES) { 230 try { 231 console.info(`setUserId name: ${USER_ID_NAME}, value: ${USER_ID_VALUE}`); 232 hiAppEventV9.setUserId(USER_ID_NAME, USER_ID_VALUE); 233 console.info(`setUserId name: ${USER_ID_NAME}, value: ${userIdVal}`); 234 hiAppEventV9.setUserId(USER_ID_NAME, userIdVal); 235 236 console.info(`getUserId name: ${USER_ID_NAME}`); 237 let res = hiAppEventV9.getUserId(USER_ID_NAME); 238 expect(res == "").assertTrue(); 239 } catch (err) { 240 assertErrorEqual(err, expectErr); 241 } 242 } 243 244 console.info('HiAppEventUserIdsTest005_1 end'); 245 }); 246 247 /** 248 * @tc.number: HiAppEventUserPropertyTest001_1 249 * @tc.name: HiAppEventUserPropertyTest 250 * @tc.desc: Correctly set and get the user properties function. 251 * @tc.type: FUNC 252 * @tc.require: issueI8G4M9 253 */ 254 it('HiAppEventUserPropertyTest001_1', 0, function () { 255 console.info('HiAppEventUserPropertyTest001_1 start'); 256 257 let expectErr = createError2("userProperties", "string"); 258 259 try { 260 console.info(`setUserProperty name: ${USER_PROP_NAME}, value: ${USER_PROP_VALUE}`); 261 hiAppEventV9.setUserProperty(USER_PROP_NAME, USER_PROP_VALUE); 262 263 console.info(`getUserProperty name: ${USER_PROP_NAME}`); 264 let res = hiAppEventV9.getUserProperty(USER_PROP_NAME); 265 expect(res != "").assertTrue(); 266 } catch (err) { 267 assertErrorEqual(err, expectErr); 268 } 269 270 console.info('HiAppEventUserPropertyTest001_1 end'); 271 }); 272 273 /** 274 * @tc.number: HiAppEventUserPropertyTest001_2 275 * @tc.name: HiAppEventUserPropertyTest 276 * @tc.desc: Correctly clear and get the user properties function. 277 * @tc.type: FUNC 278 * @tc.require: issueI8G4M9 279 */ 280 it('HiAppEventUserPropertyTest001_2', 0, function () { 281 console.info('HiAppEventUserPropertyTest001_2 start'); 282 283 let expectErr = createError2("userProperties", "string"); 284 285 try { 286 console.info(`setUserProperty name: ${USER_PROP_NAME}, value: ${USER_PROP_VALUE}`); 287 hiAppEventV9.setUserProperty(USER_PROP_NAME, USER_PROP_VALUE); 288 289 hiAppEventV9.clearData(); 290 291 console.info(`getUserProperty name: ${USER_PROP_NAME}`); 292 let res = hiAppEventV9.getUserProperty(USER_PROP_NAME); 293 expect(res == "").assertTrue(); 294 } catch (err) { 295 assertErrorEqual(err, expectErr); 296 } 297 298 console.info('HiAppEventUserPropertyTest001_2 end'); 299 }); 300 301 /** 302 * @tc.number: HiAppEventUserPropertyTest002_1 303 * @tc.name: HiAppEventUserPropertyTest 304 * @tc.desc: Incorrectly set the user properties function. 305 * @tc.type: FUNC 306 * @tc.require: issueI8G4M9 307 */ 308 it('HiAppEventUserPropertyTest002_1', 0, function () { 309 console.info('HiAppEventUserPropertyTest002_1 start'); 310 311 for (let userPropertyName of INVALID_USER_PROP_NAMES) { 312 let expectErr = typeof userPropertyName == 'string' ? createError4("name") : createError2("name", "string"); 313 try { 314 console.info(`setUserProperty name: ${userPropertyName}, value: ${USER_PROP_VALUE}`); 315 hiAppEventV9.setUserProperty(userPropertyName, USER_PROP_VALUE); 316 } catch (err) { 317 assertErrorEqual(err, expectErr); 318 } 319 } 320 for (let userPropertyVal of INVALID_USER_PROP_VALUES) { 321 let expectErr = typeof userPropertyVal == 'string' ? createError4("value") 322 : createError2("user property", "string"); 323 try { 324 console.info(`setUserProperty name: ${USER_PROP_NAME}, value: ${userPropertyVal}`); 325 hiAppEventV9.setUserProperty(USER_PROP_NAME, userPropertyVal); 326 } catch (err) { 327 assertErrorEqual(err, expectErr); 328 } 329 } 330 331 console.info('HiAppEventUserPropertyTest002_1 end'); 332 }); 333 334 /** 335 * @tc.number: HiAppEventUserPropertyTest003_1 336 * @tc.name: HiAppEventUserPropertyTest 337 * @tc.desc: Incorrectly set the user properties function. 338 * @tc.type: FUNC 339 * @tc.require: issueI8G4M9 340 */ 341 it('HiAppEventUserPropertyTest003_1', 0, function () { 342 console.info('HiAppEventUserPropertyTest003_1 start'); 343 344 console.info(`setUserProperty name: ${USER_PROP_NAME}, value: ${USER_PROP_VALUE}`); 345 hiAppEventV9.setUserProperty(USER_PROP_NAME, USER_PROP_VALUE); 346 347 for (let userPropertyName of INVALID_USER_PROP_NAMES) { 348 let expectErr = typeof userPropertyName == 'string' ? createError4("name") : createError2("name", "string"); 349 try { 350 console.info(`getUserProperty name: ${USER_PROP_NAME}`); 351 let res = hiAppEventV9.getUserProperty(userPropertyName); 352 expect(res == "").assertTrue(); 353 } catch (err) { 354 assertErrorEqual(err, expectErr); 355 } 356 } 357 358 console.info('HiAppEventUserPropertyTest003_1 end'); 359 }); 360 361 /** 362 * @tc.number: HiAppEventUserPropertyTest004_1 363 * @tc.name: HiAppEventUserPropertyTest 364 * @tc.desc: Correctly replace and get the user properties function. 365 * @tc.type: FUNC 366 * @tc.require: issueI8G4M9 367 */ 368 it('HiAppEventUserPropertyTest004_1', 0, function () { 369 console.info('HiAppEventUserPropertyTest004_1 start'); 370 371 let expectErr = createError2("userProperties", "string"); 372 373 try { 374 console.info(`setUserProperty name: ${USER_PROP_NAME}, value: ${USER_PROP_VALUE}`); 375 hiAppEventV9.setUserProperty(USER_PROP_NAME, USER_PROP_VALUE); 376 console.info(`setUserProperty name: ${USER_PROP_NAME}, value: ${'testUserProperty1'}`); 377 hiAppEventV9.setUserProperty(USER_PROP_NAME, "testUserProperty1"); 378 379 console.info(`getUserProperty name: ${USER_PROP_NAME}`); 380 let res = hiAppEventV9.getUserProperty(USER_PROP_NAME); 381 expect(res == "testUserProperty1").assertTrue(); 382 } catch (err) { 383 assertErrorEqual(err, expectErr); 384 } 385 386 console.info('HiAppEventUserPropertyTest004_1 end'); 387 }); 388 389 /** 390 * @tc.number: HiAppEventUserPropertyTest005_1 391 * @tc.name: HiAppEventUserPropertyTest 392 * @tc.desc: Correctly remove and get the user properties function. 393 * @tc.type: FUNC 394 * @tc.require: issueI8G4M9 395 */ 396 it('HiAppEventUserPropertyTest005_1', 0, function () { 397 console.info('HiAppEventUserPropertyTest005_1 start'); 398 399 let expectErr = createError2("user property", "string"); 400 401 for (let userPropertyVal of CLEAR_USER_INFO_VALUES) { 402 try { 403 console.info(`setUserProperty name: ${USER_PROP_NAME}, value: ${USER_PROP_VALUE}`); 404 hiAppEventV9.setUserProperty(USER_PROP_NAME, USER_PROP_VALUE); 405 console.info(`setUserProperty name: ${USER_PROP_NAME}, value: ${userPropertyVal}`); 406 hiAppEventV9.setUserProperty(USER_PROP_NAME, userPropertyVal); 407 408 console.info(`getUserProperty name: ${USER_PROP_NAME}`); 409 let res = hiAppEventV9.getUserProperty(USER_PROP_NAME); 410 expect(res == "").assertTrue(); 411 } catch (err) { 412 assertErrorEqual(err, expectErr); 413 } 414 } 415 416 console.info('HiAppEventUserPropertyTest005_1 end'); 417 }); 418});