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 image from "@ohos.multimedia.image"; 17import fileio from "@ohos.fileio"; 18import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from "@ohos/hypium"; 19import featureAbility from "@ohos.ability.featureAbility"; 20 21export default function imageSvg() { 22 describe("imageSvg", function () { 23 const RGBA_8888 = image.PixelMapFormat.RGBA_8888; 24 let filePath; 25 let fdNumber; 26 async function getFd(fileName) { 27 let context = await featureAbility.getContext(); 28 await context.getFilesDir().then((data) => { 29 filePath = data + "/" + fileName; 30 console.info("image case filePath is " + filePath); 31 }); 32 await fileio 33 .open(filePath, 0o2, 0o777) 34 .then( 35 (data) => { 36 fdNumber = data; 37 console.info("image case open fd success " + fdNumber); 38 }, 39 (err) => { 40 console.info("image cese open fd fail" + err); 41 } 42 ) 43 .catch((err) => { 44 console.info("image case open fd err " + err); 45 }); 46 } 47 48 beforeAll(async function () { 49 console.info("beforeAll case"); 50 }); 51 52 beforeEach(function () { 53 console.info("beforeEach case"); 54 }); 55 56 afterEach(async function () { 57 console.info("afterEach case"); 58 }); 59 60 afterAll(async function () { 61 console.info("afterAll case"); 62 }); 63 64 async function packingPromise(done, testNum, pixelmap, packOpts) { 65 try { 66 const imagePackerApi = image.createImagePacker(); 67 if (imagePackerApi == undefined) { 68 console.info(`${testNum} packingPromise create image packer failed`); 69 expect(false).assertTrue(); 70 done(); 71 } else { 72 let packOptsFormat = `format:` + packOpts.format; 73 let packOptsQuality = `quality:` + packOpts.quality; 74 console.info( 75 `${testNum} packingPromise packOpts={${packOptsFormat}, ${packOptsQuality} }` 76 ); 77 let data = await imagePackerApi.packing(pixelmap, packOpts); 78 console.info(`${testNum} packing finished`); 79 if (data != undefined) { 80 console.info(`${testNum} packing success`); 81 var dataArr = new Uint8Array(data); 82 console.info(`${testNum} packing show begin(length: ${dataArr.length})`); 83 var line = 0; 84 for (var i = 0; i < dataArr.length; i++) { 85 var str = `dataArr[${i}]=`; 86 for (var j = 0; j < 20 && i < dataArr.length; j++, i++) { 87 str = str + "," + dataArr[i]; 88 } 89 console.info(`${testNum} packing ` + str); 90 i--; 91 line++; 92 } 93 console.info(`${testNum} packing show end(line: ${line} )`); 94 expect(true).assertTrue(); 95 done(); 96 } else { 97 console.info(`${testNum} packing failed`); 98 expect(false).assertTrue(); 99 done(); 100 } 101 } 102 } catch (error) { 103 console.info(`${testNum} packingPromise error: ` + error); 104 expect(false).assertTrue(); 105 done(); 106 } 107 } 108 109 async function createPixelMapPromise(done, testNum, picName, decodeOpts, packFunc, packOpts) { 110 let imageSourceApi; 111 try { 112 await getFd(picName); 113 imageSourceApi = image.createImageSource(fdNumber); 114 if (imageSourceApi == undefined) { 115 console.info(`${testNum} create imagesource failed`); 116 expect(false).assertTrue(); 117 done(); 118 } else { 119 console.info(`${testNum} create imagesource success`); 120 let pixelmap = await imageSourceApi.createPixelMap(decodeOpts); 121 if (pixelmap != undefined) { 122 console.info(`${testNum} create pixelmap success`); 123 let imageInfo = await pixelmap.getImageInfo(); 124 expect(imageInfo.size.width).assertEqual(32); 125 expect(imageInfo.size.height).assertEqual(16); 126 packFunc(done, testNum, pixelmap, packOpts); 127 } else { 128 console.info(`${testNum} create pixelmap failed`); 129 expect(false).assertTrue(); 130 done(); 131 } 132 } 133 } catch (error) { 134 console.info(`${testNum} error: ` + error); 135 expect(false).assertTrue(); 136 done(); 137 } 138 } 139 140 async function createPixelMapCallBack(done, testNum, picName, decodeOpts, packFunc, packOpts) { 141 let imageSourceApi; 142 await getFd(picName); 143 imageSourceApi = image.createImageSource(fdNumber); 144 if (imageSourceApi == undefined) { 145 console.info(`${testNum} create imagesource failed`); 146 expect(false).assertTrue(); 147 done(); 148 } else { 149 console.info(`${testNum} create imagesource success`); 150 imageSourceApi.createPixelMap(decodeOpts, async (err, pixelmap) => { 151 if (err != undefined) { 152 console.info(`${testNum} createPixelMap create pixelmap failed err: ${err}`); 153 expect(false).assertTrue(); 154 done(); 155 return; 156 } 157 console.info(`${testNum} createPixelMap success`); 158 expect(pixelmap != undefined).assertTrue(); 159 let imageInfo = await pixelmap.getImageInfo(); 160 expect(imageInfo.size.width).assertEqual(4); 161 expect(imageInfo.size.height).assertEqual(2); 162 packFunc(done, testNum, pixelmap, packOpts); 163 }); 164 } 165 } 166 167 async function createPixelMapCallBackErr(done, testNum, picName, decodeOpts) { 168 let imageSourceApi; 169 await getFd(picName); 170 imageSourceApi = image.createImageSource(fdNumber); 171 if (imageSourceApi == undefined) { 172 console.info(`${testNum} createPixelMapPromise create imagesource failed`); 173 expect(false).assertTrue(); 174 done(); 175 } else { 176 console.info(`${testNum} createPixelMapPromise create imagesource success`); 177 imageSourceApi.createPixelMap(decodeOpts, (err, pixelmap) => { 178 expect(err != undefined).assertTrue(); 179 console.info(`${testNum} success: ` + err); 180 done(); 181 }); 182 } 183 } 184 185 async function createPixelMapPromiseErr(done, testNum, picName, decodeOpts) { 186 let imageSourceApi; 187 await getFd(picName); 188 imageSourceApi = image.createImageSource(fdNumber); 189 if (imageSourceApi == undefined) { 190 console.info(`${testNum} createPixelMapPromise create imagesource failed`); 191 expect(false).assertTrue(); 192 done(); 193 } else { 194 console.info(`${testNum} createPixelMapPromise create imagesource success`); 195 try { 196 await imageSourceApi.createPixelMap(decodeOpts); 197 expect(false).assertTrue(); 198 done(); 199 } catch (error) { 200 expect(true).assertTrue(); 201 console.info(`${testNum} error: ` + error); 202 done(); 203 } 204 } 205 } 206 207 /** 208 * @tc.number : SUB_GRAPHIC_IMAGE_SVG_PROMISE_0100 209 * @tc.name : createPixelMap - promise 210 * @tc.desc : 1.create imagesource 211 * 2.set DecodeOptions 212 * 3.create PixelMap 213 * 4.packing 214 * @tc.size : MEDIUM 215 * @tc.type : Functional 216 * @tc.level : Level 0 217 */ 218 it("SUB_GRAPHIC_IMAGE_SVG_PROMISE_0100", 0, async function (done) { 219 let decodeOpts = { 220 sampleSize: 1, 221 editable: true, 222 desiredSize: { width: 32, height: 16 }, 223 rotate: 0, 224 desiredPixelFormat: RGBA_8888, 225 index: 0, 226 }; 227 let packOpts = { format: ["image/webp"], quality: 100 }; 228 createPixelMapPromise(done, "SUB_GRAPHIC_IMAGE_SVG_PROMISE_0100", "test_large.svg", decodeOpts, 229 packingPromise, packOpts); 230 }); 231 232 /** 233 * @tc.number : SUB_GRAPHIC_IMAGE_SVG_CALLBACK_0100 234 * @tc.name : createPixelMap - callback 235 * @tc.desc : 1.create imagesource 236 * 2.set DecodeOptions 237 * 3.create PixelMap 238 * 4.packing 239 * @tc.size : MEDIUM 240 * @tc.type : Functional 241 * @tc.level : Level 0 242 */ 243 it("SUB_GRAPHIC_IMAGE_SVG_CALLBACK_0100", 0, async function (done) { 244 let decodeOpts = { 245 sampleSize: 1, 246 editable: true, 247 desiredSize: { width: 4, height: 2 }, 248 rotate: 0, 249 desiredPixelFormat: RGBA_8888, 250 index: 0, 251 }; 252 let packOpts = { format: ["image/webp"], quality: 100 }; 253 createPixelMapCallBack(done, "SUB_GRAPHIC_IMAGE_SVG_CALLBACK_0100", "test_large.svg", decodeOpts, 254 packingPromise, packOpts); 255 }); 256 257 /** 258 * @tc.number : SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0100 259 * @tc.name : createPixelMap - callback-sampleSize: -1 260 * @tc.desc : 1.create imagesource 261 * 2.set DecodeOptions 262 * 3.create PixelMap 263 * @tc.size : MEDIUM 264 * @tc.type : Functional 265 * @tc.level : Level 3 266 */ 267 it("SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0100", 0, async function (done) { 268 let decodingOption = { 269 sampleSize: -1, 270 editable: true, 271 desiredSize: { width: 1, height: 2 }, 272 rotate: 0, 273 desiredPixelFormat: RGBA_8888, 274 desiredRegion: { size: { height: 1, width: 2 }, x: 0, y: 0 }, 275 index: 0, 276 }; 277 await createPixelMapCallBackErr(done, "SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0100", "test_large.svg", 278 decodingOption); 279 }); 280 281 /** 282 * @tc.number : SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0200 283 * @tc.name : createPixelMap - callback-index: -1 284 * @tc.desc : 1.create imagesource 285 * 2.set DecodeOptions 286 * 3.create PixelMap 287 * @tc.size : MEDIUM 288 * @tc.type : Functional 289 * @tc.level : Level 3 290 */ 291 it("SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0200", 0, async function (done) { 292 let decodingOption = { 293 sampleSize: 1, 294 editable: true, 295 desiredSize: { width: 1, height: 2 }, 296 rotate: 0, 297 desiredPixelFormat: RGBA_8888, 298 desiredRegion: { size: { height: 1, width: 2 }, x: 0, y: 0 }, 299 index: -1, 300 }; 301 await createPixelMapCallBackErr(done, "SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0200", "test_large.svg", 302 decodingOption); 303 }); 304 305 /** 306 * @tc.number : SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0300 307 * @tc.name : createPixelMap - callback-rotate: 500 308 * @tc.desc : 1.create imagesource 309 * 2.set DecodeOptions 310 * 3.create PixelMap 311 * @tc.size : MEDIUM 312 * @tc.type : Functional 313 * @tc.level : Level 3 314 */ 315 it("SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0300", 0, async function (done) { 316 let decodingOption = { 317 sampleSize: 1, 318 editable: true, 319 desiredSize: { width: 1, height: 2 }, 320 rotate: 500, 321 desiredPixelFormat: RGBA_8888, 322 desiredRegion: { size: { height: 1, width: 2 }, x: 0, y: 0 }, 323 index: 0, 324 }; 325 await createPixelMapCallBackErr(done, "SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0300", "test_large.svg", 326 decodingOption); 327 }); 328 329 /** 330 * @tc.number : SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0400 331 * @tc.name : createPixelMap - callback-desiredPixelFormat: 33 332 * @tc.desc : 1.create imagesource 333 * 2.set DecodeOptions 334 * 3.create PixelMap 335 * @tc.size : MEDIUM 336 * @tc.type : Functional 337 * @tc.level : Level 3 338 */ 339 it("SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0400", 0, async function (done) { 340 let decodingOption = { 341 sampleSize: 1, 342 editable: false, 343 desiredSize: { width: 1, height: 2 }, 344 rotate: 0, 345 desiredPixelFormat: 33, 346 desiredRegion: { size: { height: 1, width: 2 }, x: 0, y: 0 }, 347 index: 0, 348 }; 349 await createPixelMapCallBackErr(done, "SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0400", "test_large.svg", 350 decodingOption); 351 }); 352 353 /** 354 * @tc.number : SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0500 355 * @tc.name : createPixelMap - callback-{x: 10000, y: 0 } 356 * @tc.desc : 1.create imagesource 357 * 2.set DecodeOptions 358 * 3.create PixelMap 359 * @tc.size : MEDIUM 360 * @tc.type : Functional 361 * @tc.level : Level 3 362 */ 363 it("SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0500", 0, async function (done) { 364 let decodingOption = { 365 sampleSize: 1, 366 editable: true, 367 desiredSize: { width: 1, height: 2 }, 368 rotate: 0, 369 desiredPixelFormat: RGBA_8888, 370 desiredRegion: { size: { height: 1, width: 2 }, x: 10000, y: 0 }, 371 index: 0, 372 }; 373 await createPixelMapCallBackErr(done, "SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0500", "test_large.svg", 374 decodingOption); 375 }); 376 377 /** 378 * @tc.number : SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0600 379 * @tc.name : createPixelMap - callback-{x: 0, y: 10000 } 380 * @tc.desc : 1.create imagesource 381 * 2.set DecodeOptions 382 * 3.create PixelMap 383 * @tc.size : MEDIUM 384 * @tc.type : Functional 385 * @tc.level : Level 3 386 */ 387 it("SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0600", 0, async function (done) { 388 let decodingOption = { 389 sampleSize: 1, 390 editable: true, 391 desiredSize: { width: 1, height: 2 }, 392 rotate: 0, 393 desiredPixelFormat: RGBA_8888, 394 desiredRegion: { size: { height: 1, width: 2 }, x: 0, y: 10000 }, 395 index: 0, 396 }; 397 await createPixelMapCallBackErr(done, "SUB_GRAPHIC_IMAGE_SVG_CALLBACK_ERR_0600", "test_large.svg", 398 decodingOption); 399 }); 400 401 /** 402 * @tc.number : SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0100 403 * @tc.name : createPixelMap - promise-sampleSize: -1 404 * @tc.desc : 1.create imagesource 405 * 2.set DecodeOptions 406 * 3.create PixelMap 407 * @tc.size : MEDIUM 408 * @tc.type : Functional 409 * @tc.level : Level 3 410 */ 411 it("SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0100", 0, async function (done) { 412 let decodingOption = { 413 sampleSize: -1, 414 editable: true, 415 desiredSize: { width: 1, height: 2 }, 416 rotate: 0, 417 desiredPixelFormat: RGBA_8888, 418 desiredRegion: { size: { height: 1, width: 2 }, x: 0, y: 0 }, 419 index: 0, 420 }; 421 await createPixelMapPromiseErr(done, "SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0100", "test_large.svg", 422 decodingOption); 423 }); 424 425 /** 426 * @tc.number : SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0200 427 * @tc.name : createPixelMap - promise-index: -1 428 * @tc.desc : 1.create imagesource 429 * 2.set DecodeOptions 430 * 3.create PixelMap 431 * @tc.size : MEDIUM 432 * @tc.type : Functional 433 * @tc.level : Level 3 434 */ 435 it("SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0200", 0, async function (done) { 436 let decodingOption = { 437 sampleSize: 1, 438 editable: true, 439 desiredSize: { width: 1, height: 2 }, 440 rotate: 0, 441 desiredPixelFormat: RGBA_8888, 442 desiredRegion: { size: { height: 1, width: 2 }, x: 0, y: 0 }, 443 index: -1, 444 }; 445 await createPixelMapPromiseErr(done, "SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0200", "test_large.svg", 446 decodingOption); 447 }); 448 449 /** 450 * @tc.number : SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0300 451 * @tc.name : createPixelMap - promise-rotate: 500 452 * @tc.desc : 1.create imagesource 453 * 2.set DecodeOptions 454 * 3.create PixelMap 455 * @tc.size : MEDIUM 456 * @tc.type : Functional 457 * @tc.level : Level 3 458 */ 459 it("SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0300", 0, async function (done) { 460 let decodingOption = { 461 sampleSize: 1, 462 editable: true, 463 desiredSize: { width: 1, height: 2 }, 464 rotate: 500, 465 desiredPixelFormat: RGBA_8888, 466 desiredRegion: { size: { height: 1, width: 2 }, x: 0, y: 0 }, 467 index: 0, 468 }; 469 await createPixelMapPromiseErr(done, "SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0300", "test_large.svg", 470 decodingOption); 471 }); 472 473 /** 474 * @tc.number : SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0400 475 * @tc.name : createPixelMap - promise-desiredPixelFormat: 33 476 * @tc.desc : 1.create imagesource 477 * 2.set DecodeOptions 478 * 3.create PixelMap 479 * @tc.size : MEDIUM 480 * @tc.type : Functional 481 * @tc.level : Level 3 482 */ 483 it("SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0400", 0, async function (done) { 484 let decodingOption = { 485 sampleSize: 1, 486 editable: false, 487 desiredSize: { width: 1, height: 2 }, 488 rotate: 0, 489 desiredPixelFormat: 33, 490 desiredRegion: { size: { height: 1, width: 2 }, x: 0, y: 0 }, 491 index: 0, 492 }; 493 await createPixelMapPromiseErr(done, "SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0400", "test_large.svg", 494 decodingOption); 495 }); 496 497 /** 498 * @tc.number : SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0500 499 * @tc.name : createPixelMap - promise-{x: 10000, y: 0} 500 * @tc.desc : 1.create imagesource 501 * 2.set DecodeOptions 502 * 3.create PixelMap 503 * @tc.size : MEDIUM 504 * @tc.type : Functional 505 * @tc.level : Level 3 506 */ 507 it("SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0500", 0, async function (done) { 508 let decodingOption = { 509 sampleSize: 1, 510 editable: true, 511 desiredSize: { width: 1, height: 2 }, 512 rotate: 0, 513 desiredPixelFormat: RGBA_8888, 514 desiredRegion: { size: { height: 1, width: 2 }, x: 10000, y: 0 }, 515 index: 0, 516 }; 517 await createPixelMapPromiseErr(done, "SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0500", "test_large.svg", 518 decodingOption); 519 }); 520 521 /** 522 * @tc.number : SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0600 523 * @tc.name : createPixelMap - promise-{x: 0, y: 10000} 524 * @tc.desc : 1.create imagesource 525 * 2.set DecodeOptions 526 * 3.create PixelMap 527 * @tc.size : MEDIUM 528 * @tc.type : Functional 529 * @tc.level : Level 3 530 */ 531 it("SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0600", 0, async function (done) { 532 let decodingOption = { 533 sampleSize: 1, 534 editable: true, 535 desiredSize: { width: 1, height: 2 }, 536 rotate: 0, 537 desiredPixelFormat: RGBA_8888, 538 desiredRegion: { size: { height: 1, width: 2 }, x: 0, y: 10000 }, 539 index: 0, 540 }; 541 await createPixelMapPromiseErr(done, "SUB_GRAPHIC_IMAGE_SVG_PROMISE_ERR_0600", "test_large.svg", 542 decodingOption); 543 }); 544 }); 545}