screenshot.test.js 14.0 KB
Newer Older
1 2 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 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 149 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 210 211 212 213 214 215 216 217 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 283 284 285 286 287 288 289 290 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 370 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
/*
 *  * Copyright (C) 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.
 *              */
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index'
import display from '@ohos.display'
import screenshot from '@ohos.screenshot'
import image from '@ohos.multimedia.image'
import screen from '@ohos.screen'

describe('screenshot_test', function () {
	
	beforeAll(function () {
	})
	beforeEach(function () {
	})
	afterEach(function () {
	})
	afterAll(function () {
	})

	/**
	 * @tc.number     SUB_SCREENSHOT_SAVE_JSAPI_001
	 * @tc.name       Test screenShotSave_Test_001
	 * @tc.desc       Set the screenshot area to be larger than the screen width and height
	 */
	it('screenShotSave_Test_001', 0, function (done) {
		console.log('screenshotTest screenShotSaveTest1 begin');
		display.getDefaultDisplay().then((dsp) => {
			console.log('screenshotTest screenShotSaveTest1 display.getDefaultDisplay success, dsp :' + JSON.stringify(dsp));
			expect(dsp != null).assertTrue();
			var screenshotOptions = {
				screenRect: {
					left: 0,
					top: 0,
					width: 10000,
					height: 10000,
				},
				imageSize: {
					width: 300,
					height: 300
				},
				displayId: dsp.id
			};
			screenshot.save(screenshotOptions).then((data) => {
				console.log('screenshotTest screenShotSaveTest1 screenshot.save success, data :' + JSON.stringify(data));
				expect().assertFail();
				done();
			}, (err) => {
				console.log('screenshotTest screenShotSaveTest1 screenshot.save failed, err : ' + JSON.stringify(err));
				expect(err.code == 120).assertTrue();
				done();
			})
		}, (err) => {
			console.log('screenshotTest screenShotSaveTest1 display.getDefaultDisplay failed, err : ' + JSON.stringify(err));
			expect().assertFail();
			done();
		})
	})

	/**
	 * @tc.number     SUB_SCREENSHOT_SAVE_JSAPI_002
	 * @tc.name       Test screenShotSave_Test_002
	 * @tc.desc       Set the size of the screenshot area and the generated image to be larger than the screen width and height
	 */
	it('screenShotSave_Test_002', 0, function (done) {
		console.log('screenshotTest screenShotSaveTest2 begin');
		display.getDefaultDisplay().then((dsp) => {
			console.log('screenshotTest screenShotSaveTest2 display.getDefaultDisplay success, dsp :' + JSON.stringify(dsp));
			expect(dsp != null).assertTrue();
			var screenshotOptions = {
				screenRect: {
					left: 0,
					top: 0,
					width: 10000,
					height: 10000,
				},
				imageSize: {
					width: 3000,
					height: 3000
				},
				displayId: dsp.id
			};
			screenshot.save(screenshotOptions).then((data) => {
				console.log('screenshotTest screenShotSaveTest2 screenshot.save success, data :' + JSON.stringify(data));
				expect().assertFail();
				done();
			}, (err) => {
				console.log('screenshotTest screenShotSaveTest2 screenshot.save failed, err : ' + JSON.stringify(err));
				expect(err.code == 120).assertTrue();
				done();
			})
		}, (err) => {
			console.log('screenshotTest screenShotSaveTest2 display.getDefaultDisplay failed, err : ' + JSON.stringify(err));
			expect().assertFail();
			done()
		})
	})

	/**
	 * @tc.number     SUB_SCREENSHOT_SAVE_JSAPI_003
	 * @tc.name       Test screenShotSave_Test_003
	 * @tc.desc       Set the screenshot area and the size of the generated image to be smaller than the screen width and height
	 */
	it('screenShotSave_Test_003', 0, function (done) {
		console.log('screenshotTest screenShotSaveTest3 begin');
		display.getDefaultDisplay().then((dsp) => {
			console.log('screenshotTest screenShotSaveTest3 display.getDefaultDisplay success, dsp :' + JSON.stringify(dsp));
			expect(dsp != null).assertTrue();
			var screenshotOptions = {
				screenRect: {
					left: 0,
					top: 0,
					width: 100,
					height: 100,
				},
				imageSize: {
					width: 300,
					height: 300
				},
				displayId: dsp.id
			};
			screenshot.save(screenshotOptions).then((data) => {
				console.log('screenshotTest screenShotSaveTest3 screenshot.save success, data :' + JSON.stringify(data));
				expect(data != null).assertTrue();
				done();
			}, (err) => {
				console.log('screenshotTest screenShotSaveTest3 screenshot.save failed, err : ' + JSON.stringify(err));
				expect().assertFail();
				done();
			})
		}, (err) => {
			console.log('screenshotTest screenShotSaveTest3 display.getDefaultDisplay failed, err : ' + JSON.stringify(err));
			expect().assertFail();
			done();
		})
	})

	/**
	 * @tc.number     SUB_SCREENSHOT_SAVE_JSAPI_004
	 * @tc.name       Test screenShotSave_Test_004
	 * @tc.desc       Set the size of the generated image to be smaller than the screen width and height
	 */
	it('screenShotSave_Test_004', 0, function (done) {
		console.log('screenshotTest screenShotSaveTest4 begin');
		display.getDefaultDisplay().then((dsp) => {
			console.log('screenshotTest screenShotSaveTest4 display.getDefaultDisplay success, dsp :' + JSON.stringify(dsp));
			expect(dsp != null).assertTrue();
			var screenshotOptions = {
				screenRect: {
					left: 0,
					top: 0,
					width: 100,
					height: 100,
				},
				imageSize: {
					width: 3000,
					height: 3000
				},
				displayId: dsp.id
			};
			screenshot.save(screenshotOptions).then((data) => {
				console.log('screenshotTest screenShotSaveTest4 screenshot.save success, data :' + JSON.stringify(data));
				expect(data != null).assertTrue();
				done();
			}, (err) => {
				console.log('screenshotTest screenShotSaveTest4 screenshot.save failed, err : ' + JSON.stringify(err));
				expect().assertFail();
				done();
			})
		}, (err) => {
			console.log('screenshotTest screenShotSaveTest4 display.getDefaultDisplay failed, err : ' + JSON.stringify(err));
			expect().assertFail();
			done();
		})
	})

	/**
	 * @tc.number     SUB_SCREENSHOT_SAVE_JSAPI_005
	 * @tc.name       Test screenShotSave_Test_005
	 * @tc.desc       Set the screenshot area to a negative value
	 */
	it('screenShotSave_Test_005', 0, function (done) {
		console.log('screenshotTest screenShotSaveTest5 begin');
		display.getDefaultDisplay().then((dsp) => {
			console.log('screenshotTest screenShotSaveTest5 display.getDefaultDisplay success, dsp :' + JSON.stringify(dsp));
			expect(dsp != null).assertTrue();
			var screenshotOptions = {
				screenRect: {
					left: -50,
					top: -50,
					width: -100,
					height: -100,
				},
				imageSize: {
					width: 300,
					height: 300
				},
				displayId: dsp.id
			};
			screenshot.save(screenshotOptions).then((data) => {
				console.log('screenshotTest screenShotSaveTest5 screenshot.save success, data :' + JSON.stringify(data));
				expect().assertFail();
				done();
			}, (err) => {
				console.log('screenshotTest screenShotSaveTest5 screenshot.save failed, err : ' + JSON.stringify(err));
				expect(err.code == 120).assertTrue();
				done();
			})
		}, (err) => {
			console.log('screenshotTest screenShotSaveTest5 display.getDefaultDisplay failed, err : ' + JSON.stringify(err));
			expect().assertFail();
			done();
		})
	})

	/**
	 * @tc.number     SUB_SCREENSHOT_SAVE_JSAPI_006
	 * @tc.name       Test screenShotSave_Test_006
	 * @tc.desc       Set the screenshot area to 0
	 */
	it('screenShotSave_Test_006', 0, function (done) {
		console.log('screenshotTest screenShotSaveTest6 begin');
		display.getDefaultDisplay().then((dsp) => {
			console.log('screenshotTest screenShotSaveTest6 display.getDefaultDisplay success, dsp :' + JSON.stringify(dsp));
			expect(dsp != null).assertTrue();
			var screenshotOptions = {
				screenRect: {
					left: 0,
					top: 0,
					width: 0,
					height: 0,
				},
				imageSize: {
					width: 300,
					height: 300
				},
				displayId: dsp.id
			};
			screenshot.save(screenshotOptions).then((data) => {
				console.log('screenshotTest screenShotSaveTest6 screenshot.save success, data :' + JSON.stringify(data));
				expect(data != null).assertTrue();
				done();
			}, (err) => {
				console.log('screenshotTest screenShotSaveTest6 screenshot.save failed, err : ' + JSON.stringify(err));
				expect().assertFail();
				done()
			})
		}, (err) => {
			console.log('screenshotTest screenShotSaveTest6 display.getDefaultDisplay failed, err : ' + JSON.stringify(err));
			expect().assertFail();
			done();
		})
	})

	/**
	 * @tc.number     SUB_SCREENSHOT_SAVE_JSAPI_007
	 * @tc.name       Test screenShotSave_Test_007
	 * @tc.desc       Set the generated image size to a negative value
	 */
	it('screenShotSave_Test_007', 0, function (done) {
		console.log('screenshotTest screenShotSaveTest7 begin');
		display.getDefaultDisplay().then((dsp) => {
			console.log('screenshotTest screenShotSaveTest7 display.getDefaultDisplay success, dsp :' + JSON.stringify(dsp));
			expect(dsp != null).assertTrue();
			var screenshotOptions = {
				screenRect: {
					left: 0,
					top: 0,
					width: 100,
					height: 100,
				},
				imageSize: {
					width: -300,
					height: -300
				},
				displayId: dsp.id
			};
			screenshot.save(screenshotOptions).then((data) => {
				console.log('screenshotTest screenShotSaveTest7 screenshot.save success, data :' + JSON.stringify(data));
				expect().assertFail();
				done();
			}, (err) => {
				console.log('screenshotTest screenShotSaveTest7 screenshot.save failed, err : ' + JSON.stringify(err));
				expect(err.code == 120).assertTrue();
				done();
			})
		}, (err) => {
			console.log('screenshotTest screenShotSaveTest7 display.getDefaultDisplay failed, err : ' + JSON.stringify(err));
			expect().assertFail();
			done();
		})
	})

	/**
	 * @tc.number     SUB_SCREENSHOT_SAVE_JSAPI_008
	 * @tc.name       Test screenShotSave_Test_008
	 * @tc.desc       Set the generated image size to 0
	 */
	it('screenShotSave_Test_008', 0, function (done) {
		console.log('screenshotTest screenShotSaveTest8 begin');
		display.getDefaultDisplay().then((dsp) => {
			console.log('screenshotTest screenShotSaveTest8 display.getDefaultDisplay success, dsp :' + JSON.stringify(dsp));
			expect(dsp != null).assertTrue();
			var screenshotOptions = {
				screenRect: {
					left: 0,
					top: 0,
					width: 100,
					height: 100,
				},
				imageSize: {
					width: 0,
					height: 0
				},
				displayId: dsp.id
			};
			screenshot.save(screenshotOptions).then((data) => {
				console.log('screenshotTest screenShotSaveTest8 screenshot.save success, data :' + JSON.stringify(data));
				expect(data != null).assertTrue();
				done();
			}, (err) => {
				console.log('screenshotTest screenShotSaveTest8 screenshot.save failed, err : ' + JSON.stringify(err));
				expect().assertFail();
				done();
			})
		}, (err) => {
			console.log('screenshotTest screenShotSaveTest8 display.getDefaultDisplay failed, err : ' + JSON.stringify(err));
			expect().assertFail();
			done();
		})
	})

	/**
	 * @tc.number     SUB_SCREENSHOT_SAVE_JSAPI_009
	 * @tc.name       Test screenShotSave_Test_009
	 * @tc.desc       Set the screenshot area and the size of the generated image to be equal to the screen width and height
	 */
	it('screenShotSave_Test_009', 0, function (done) {
		console.log('screenshotTest screenShotSaveTest9 begin');
		display.getDefaultDisplay().then((dsp) => {
			console.log('screenshotTest screenShotSaveTest9 display.getDefaultDisplay success, dsp :' + JSON.stringify(dsp));
			expect(dsp != null).assertTrue();
			var screenshotOptions = {
				screenRect: {
					left: 0,
					top: 0,
					width: dsp.width,
					height: dsp.height
				},
				imageSize: {
					width: dsp.width,
					height: dsp.height
				},
				displayId: dsp.id
			};
			screenshot.save(screenshotOptions).then((data) => {
				console.log('screenshotTest screenShotSaveTest9 screenshot.save success, data :' + JSON.stringify(data));
				expect(data != null).assertTrue();
				done();
			}, (err) => {
				console.log('screenshotTest screenShotSaveTest9 screenshot.save failed, err : ' + JSON.stringify(err));
				expect().assertFail();
			})
		}, (err) => {
			console.log('screenshotTest screenShotSaveTest9 display.getDefaultDisplay failed, err : ' + JSON.stringify(err));
			expect().assertFail();
			done();
		})
	})

	/**
	 * @tc.number     SUB_SCREENSHOT_SAVE_JSAPI_010
	 * @tc.name       Test screenShotSave_Test_010
	 * @tc.desc       Take a screenshot of a screen that doesn't exist
	 */
	it('screenShotSave_Test_010', 0, function (done) {
		console.log('screenshotTest screenShotSaveTest10 begin');
		var screenshotOptions = {
			screenRect: {
				left: 0,
				top: 0,
				width: 100,
				height: 100,
			},
			imageSize: {
				width: 300,
				height: 300
			},
			displayId: null
		};
		screenshot.save(screenshotOptions).then((data) => {
			console.log('screenshotTest screenShotSaveTest10 screenshot.save success, data :' + JSON.stringify(data));
			expect(data != null).assertTrue()
			done();
		}, (err) => {
			console.log('screenshotTest screenShotSaveTest10 screenshot.save failed, err : ' + JSON.stringify(err));
L
liziqiang 已提交
408
			expect().assertFail();
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
			done();
		})
	})

	/**
	 * @tc.number     SUB_SCREENSHOT_SAVE_JSAPI_011
	 * @tc.name       Test screenShotSave_Test_011
	 * @tc.desc       Test screenshot.save API function test11
	 */
	it('screenShotSave_Test_011', 0, function (done) {
		console.log('screenshotTest screenShotSaveTest11 begin');
		var screenshotOptions = {
			screenRect: {
				left: 0,
				top: 0,
				width: 100,
				height: 100,
			},
			imageSize: {
				width: 300,
				height: 300
			},
			displayId: 1000
		};
		screenshot.save(screenshotOptions).then((data) => {
			console.log('screenshotTest screenShotSaveTest11 screenshot.save success, data :' + JSON.stringify(data));
			expect().assertFail();
			done();
		}, (err) => {
			console.log('screenshotTest screenShotSaveTest11 screenshot.save failed, err : ' + JSON.stringify(err));
			expect(err.code == 120).assertTrue();
			done();
		})
	})
})