diff --git a/request/BUILD.gn b/request/BUILD.gn index ff090cd703fdfad90da354d53e5d1c3738253230..d38c886d36b544d5539880eb7b0ffb01460933d1 100644 --- a/request/BUILD.gn +++ b/request/BUILD.gn @@ -19,5 +19,6 @@ group("request") { "RequestTest_ets:ActsRequestETSApiTest", "newRequestAuthorityTest:ActsRequestAuthorityTest", "newRequestTest:ActsRequestNewTest", + "crossplatform/RequestTest_ets:ActsRequestETSApiCrossplatformTest" ] } diff --git a/request/crossplatform/RequestTest_ets/AppScope/app.json b/request/crossplatform/RequestTest_ets/AppScope/app.json new file mode 100644 index 0000000000000000000000000000000000000000..d4d709a011e2d36a43befb71d800aeea5e447650 --- /dev/null +++ b/request/crossplatform/RequestTest_ets/AppScope/app.json @@ -0,0 +1,16 @@ +{ + "app": { + "bundleName": "ohos.acts.request.crossplatform.request", + "vendor": "example", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:app_icon", + "label": "$string:app_name", + "minAPIVersion": 9, + "targetAPIVersion": 9, + "car": { + "apiCompatibleVersion": 9, + "singleUser": false + } + } +} diff --git a/request/crossplatform/RequestTest_ets/AppScope/resources/base/element/string.json b/request/crossplatform/RequestTest_ets/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..3811bc441fc0f128eb4d14c53ed463ffb8dc3811 --- /dev/null +++ b/request/crossplatform/RequestTest_ets/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "xtsDemo1" + } + ] +} diff --git a/request/crossplatform/RequestTest_ets/AppScope/resources/base/media/app_icon.png b/request/crossplatform/RequestTest_ets/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/request/crossplatform/RequestTest_ets/AppScope/resources/base/media/app_icon.png differ diff --git a/request/crossplatform/RequestTest_ets/BUILD.gn b/request/crossplatform/RequestTest_ets/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..16e2155d1704cf2cc8a04188db87725f1c9725e6 --- /dev/null +++ b/request/crossplatform/RequestTest_ets/BUILD.gn @@ -0,0 +1,46 @@ +# 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("//test/xts/tools/build/suite.gni") + +ohos_js_hap_suite("ActsRequestETSApiCrossplatformTest") { + hap_profile = "src/main/module.json" + deps = [ + ":windowStage_js_assets", + ":windowStage_resources", + ] + ets2abc = true + certificate_profile = "./signature/openharmony_sx.p7b" + # hap_name: HAP的名字,可选,默认为目标名 + hap_name = "ActsRequestETSApiCrossplatformTest" + subsystem_name = "request" + part_name = "request" +} +ohos_app_scope("windowStage_app_profile") { + # app_profile: HAP的AppScope中的app.json,只在Stage模型下使用 + app_profile = "AppScope/app.json" + # sources: 资源文件路径 + sources = [ "AppScope/resources" ] +} +ohos_js_assets("windowStage_js_assets") { + # source_dir: js或ets代码路径,兼容FA模型的单ability配置到ability目录 + source_dir = "src/main/ets" +} +ohos_resources("windowStage_resources") { + # sources: 资源文件路径 + sources = [ "src/main/resources" ] + # deps: 当前目标的依赖 + deps = [ ":windowStage_app_profile" ] + # hap_profile: HAP的config.json,Stage模型对应module.json + hap_profile = "src/main/module.json" +} \ No newline at end of file diff --git a/request/crossplatform/RequestTest_ets/Test.json b/request/crossplatform/RequestTest_ets/Test.json new file mode 100644 index 0000000000000000000000000000000000000000..89faee8e9f57053dd3104712913731c45b117b49 --- /dev/null +++ b/request/crossplatform/RequestTest_ets/Test.json @@ -0,0 +1,19 @@ +{ + "description": "Configuration for hjunit demo Tests", + "driver": { + "type": "OHJSUnitTest", + "test-timeout": "60000", + "shell-timeout": "60000", + "bundle-name": "ohos.acts.request.crossplatform.request", + "module-name": "crossplatform_request_test_ets" + }, + "kits": [ + { + "test-file-name": [ + "ActsRequestETSApiCrossplatformTest.hap" + ], + "type": "AppInstallKit", + "cleanup-apps": true + } + ] +} \ No newline at end of file diff --git a/request/crossplatform/RequestTest_ets/signature/openharmony_sx.p7b b/request/crossplatform/RequestTest_ets/signature/openharmony_sx.p7b new file mode 100644 index 0000000000000000000000000000000000000000..02dc5c5e7f65f4a43fe136c0753a35a1b6983cbe Binary files /dev/null and b/request/crossplatform/RequestTest_ets/signature/openharmony_sx.p7b differ diff --git a/request/crossplatform/RequestTest_ets/src/main/ets/TestAbility/TestAbility.ets b/request/crossplatform/RequestTest_ets/src/main/ets/TestAbility/TestAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..003131f5db2dd9e1284ae2930ca1051e694860c9 --- /dev/null +++ b/request/crossplatform/RequestTest_ets/src/main/ets/TestAbility/TestAbility.ets @@ -0,0 +1,64 @@ +/** + * 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 UIAbility from '@ohos.app.ability.UIAbility'; +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; +import hilog from '@ohos.hilog'; +import { Hypium } from '@ohos/hypium'; +import testsuite from '../test/List.test'; +import window from '@ohos.window'; + +export default class TestAbility extends UIAbility { + onCreate(want, launchParam) { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onCreate'); + hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? ''); + hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:'+ JSON.stringify(launchParam) ?? ''); + var abilityDelegator: any + abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() + var abilityDelegatorArguments: any + abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() + hilog.info(0x0000, 'testTag', '%{public}s', 'start run testcase!!!'); + Hypium.hypiumTest(abilityDelegator, abilityDelegatorArguments, testsuite) + } + + onDestroy() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage) { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageCreate'); + globalThis.abilityContext = this.context; + windowStage.loadContent('TestAbility/pages/Index', (err, data) => { + if (err.code) { + hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + return; + } + hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', + JSON.stringify(data) ?? ''); + }); + } + + onWindowStageDestroy() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onWindowStageDestroy'); + } + + onForeground() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onForeground'); + } + + onBackground() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility onBackground'); + } +} \ No newline at end of file diff --git a/request/crossplatform/RequestTest_ets/src/main/ets/TestAbility/pages/Index.ets b/request/crossplatform/RequestTest_ets/src/main/ets/TestAbility/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..ced6b84d3a6afac333808c2b4b108d6eff25253c --- /dev/null +++ b/request/crossplatform/RequestTest_ets/src/main/ets/TestAbility/pages/Index.ets @@ -0,0 +1,49 @@ +/** + * 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 hilog from '@ohos.hilog'; + +@Entry +@Component +struct Index { + aboutToAppear() { + hilog.info(0x0000, 'testTag', '%{public}s', 'TestAbility index aboutToAppear'); + } + @State message: string = 'Hello World' + build() { + Row() { + Column() { + Text(this.message) + .fontSize(50) + .fontWeight(FontWeight.Bold) + Button() { + Text('next page 123') + .fontSize(20) + .fontWeight(FontWeight.Bold) + }.type(ButtonType.Capsule) + .margin({ + top: 20 + }) + .backgroundColor('#0D9FFB') + .width('35%') + .height('5%') + .onClick(()=>{ + }) + } + .width('100%') + } + .height('100%') + } + } \ No newline at end of file diff --git a/request/crossplatform/RequestTest_ets/src/main/ets/TestRunner/OpenHarmonyTestRunner.ts b/request/crossplatform/RequestTest_ets/src/main/ets/TestRunner/OpenHarmonyTestRunner.ts new file mode 100644 index 0000000000000000000000000000000000000000..92a16d84e8870da219c51d9f1342c79203c1f42d --- /dev/null +++ b/request/crossplatform/RequestTest_ets/src/main/ets/TestRunner/OpenHarmonyTestRunner.ts @@ -0,0 +1,49 @@ +import hilog from '@ohos.hilog'; +import TestRunner from '@ohos.application.testRunner'; +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; + +var abilityDelegator = undefined +var abilityDelegatorArguments = undefined + +async function onAbilityCreateCallback() { + hilog.info(0x0000, 'testTag', '%{public}s', 'onAbilityCreateCallback'); +} + +async function addAbilityMonitorCallback(err: any) { + hilog.info(0x0000, 'testTag', 'addAbilityMonitorCallback : %{public}s', JSON.stringify(err) ?? ''); +} + +export default class OpenHarmonyTestRunner implements TestRunner { + constructor() { + } + + onPrepare() { + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner OnPrepare '); + } + + async onRun() { + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun run'); + abilityDelegatorArguments = AbilityDelegatorRegistry.getArguments() + abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator() + var testAbilityName = abilityDelegatorArguments.bundleName + '.TestAbility' + let lMonitor = { + abilityName: testAbilityName, + onAbilityCreate: onAbilityCreateCallback, + }; + abilityDelegator.addAbilityMonitor(lMonitor, addAbilityMonitorCallback) + var cmd = 'aa start -d 0 -a TestAbility' + ' -b ' + abilityDelegatorArguments.bundleName + var debug = abilityDelegatorArguments.parameters['-D'] + if (debug == 'true') + { + cmd += ' -D' + } + hilog.info(0x0000, 'testTag', 'cmd : %{public}s', cmd); + abilityDelegator.executeShellCommand(cmd, + (err: any, d: any) => { + hilog.info(0x0000, 'testTag', 'executeShellCommand : err : %{public}s', JSON.stringify(err) ?? ''); + hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.stdResult ?? ''); + hilog.info(0x0000, 'testTag', 'executeShellCommand : data : %{public}s', d.exitCode ?? ''); + }) + hilog.info(0x0000, 'testTag', '%{public}s', 'OpenHarmonyTestRunner onRun end'); + } +} \ No newline at end of file diff --git a/request/crossplatform/RequestTest_ets/src/main/ets/test/List.test.ets b/request/crossplatform/RequestTest_ets/src/main/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..3ecf594c9921c9c7902580e8e8e16c950a340d4e --- /dev/null +++ b/request/crossplatform/RequestTest_ets/src/main/ets/test/List.test.ets @@ -0,0 +1,22 @@ +/* + * 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 requestDownloadJSUnit from './requestDownload.test'; +import requestConstantJSUnit from './requestConstant.test'; + +export default function testsuite() { + requestDownloadJSUnit() + requestConstantJSUnit() +} \ No newline at end of file diff --git a/request/crossplatform/RequestTest_ets/src/main/ets/test/requestConstant.test.ets b/request/crossplatform/RequestTest_ets/src/main/ets/test/requestConstant.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..bb57cf2da2a0115b4587375d5b8b9c87988980f4 --- /dev/null +++ b/request/crossplatform/RequestTest_ets/src/main/ets/test/requestConstant.test.ets @@ -0,0 +1,472 @@ +/* + * 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 request from "@ohos.request"; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function requestUploadJSUnit() { + describe('requestConstantJSUnit', function () { + console.info('====>################################request upload Test start'); + + /** + * beforeAll: Prerequisites at the test suite level, which are executed before the test suite is executed. + */ + beforeAll(function () { + console.info('====>beforeAll: Prerequisites are executed.'); + }); + + /** + * beforeEach: Prerequisites at the test case level, which are executed before each test case is executed. + */ + beforeEach(function () { + console.info('====>beforeEach: Prerequisites is executed.'); + }); + + /** + * afterEach: Test case-level clearance conditions, which are executed after each test case is executed. + */ + afterEach(function () { + console.info('====>afterEach: Test case-level clearance conditions is executed.'); + }); + + /** + * afterAll: Test suite-level cleanup condition, which is executed after the test suite is executed. + */ + afterAll(function () { + console.info('====>afterAll: Test suite-level cleanup condition is executed'); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0001 + * *@tc.name subRequestConstant0001 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0001', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0001 is starting-----------------------"); + expect(typeof request.EXCEPTION_PERMISSION == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0002 + * *@tc.name subRequestConstant0002 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0002', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0002 is starting-----------------------"); + expect(typeof request.EXCEPTION_PARAMCHECK == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0003 + * *@tc.name subRequestConstant0003 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0003', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0003 is starting-----------------------"); + expect(typeof request.EXCEPTION_UNSUPPORTED == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0004 + * *@tc.name subRequestConstant0004 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0004', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0004 is starting-----------------------"); + expect(typeof request.EXCEPTION_FILEIO == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0005 + * *@tc.name subRequestConstant0005 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0005', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0005 is starting-----------------------"); + expect(typeof request.EXCEPTION_FILEPATH == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0006 + * *@tc.name subRequestConstant0006 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0006', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0006 is starting-----------------------"); + expect(typeof request.EXCEPTION_SERVICE == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0007 + * *@tc.name subRequestConstant0007 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0007', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0007 is starting-----------------------"); + expect(typeof request.EXCEPTION_OTHERS == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0008 + * *@tc.name subRequestConstant0008 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0008', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0008 is starting-----------------------"); + expect(typeof request.NETWORK_MOBILE == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0009 + * *@tc.name subRequestConstant0009 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0009', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0009 is starting-----------------------"); + expect(typeof request.NETWORK_WIFI == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0010 + * *@tc.name subRequestConstant0010 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0010', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0010 is starting-----------------------"); + expect(typeof request.ERROR_CANNOT_RESUME == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0011 + * *@tc.name subRequestConstant0011 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0011', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0011 is starting-----------------------"); + expect(typeof request.ERROR_DEVICE_NOT_FOUND == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0012 + * *@tc.name subRequestConstant0012 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0012', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0012 is starting-----------------------"); + expect(typeof request.ERROR_FILE_ALREADY_EXISTS == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0013 + * *@tc.name subRequestConstant0013 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0013', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0013 is starting-----------------------"); + expect(typeof request.ERROR_FILE_ERROR == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0014 + * *@tc.name subRequestConstant0014 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0014', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0014 is starting-----------------------"); + expect(typeof request.ERROR_HTTP_DATA_ERROR == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0015 + * *@tc.name subRequestConstant0015 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0015', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0015 is starting-----------------------"); + expect(typeof request.ERROR_INSUFFICIENT_SPACE == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0016 + * *@tc.name subRequestConstant0016 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0016', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0016 is starting-----------------------"); + expect(typeof request.ERROR_TOO_MANY_REDIRECTS == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0017 + * *@tc.name subRequestConstant0017 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0017', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0017 is starting-----------------------"); + expect(typeof request.ERROR_UNHANDLED_HTTP_CODE == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0018 + * *@tc.name subRequestConstant0018 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0018', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0018 is starting-----------------------"); + expect(typeof request.ERROR_UNKNOWN == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0019 + * *@tc.name subRequestConstant0019 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0019', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0019 is starting-----------------------"); + expect(typeof request.ERROR_OFFLINE == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0020 + * *@tc.name subRequestConstant0020 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0020', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0020 is starting-----------------------"); + expect(typeof request.ERROR_UNSUPPORTED_NETWORK_TYPE == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0021 + * *@tc.name subRequestConstant0021 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0021', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0021 is starting-----------------------"); + expect(typeof request.PAUSED_QUEUED_FOR_WIFI == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0022 + * *@tc.name subRequestConstant0022 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0022', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0022 is starting-----------------------"); + expect(typeof request.PAUSED_WAITING_FOR_NETWORK == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0023 + * *@tc.name subRequestConstant0023 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0023', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0023 is starting-----------------------"); + expect(typeof request.PAUSED_WAITING_TO_RETRY == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0024 + * *@tc.name subRequestConstant0024 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0024', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0024 is starting-----------------------"); + expect(typeof request.PAUSED_BY_USER == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0025 + * *@tc.name subRequestConstant0025 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0025', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0025 is starting-----------------------"); + expect(typeof request.PAUSED_UNKNOWN == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0026 + * *@tc.name subRequestConstant0026 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0026', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0026 is starting-----------------------"); + expect(typeof request.SESSION_SUCCESSFUL == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0027 + * *@tc.name subRequestConstant0027 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0027', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0027 is starting-----------------------"); + expect(typeof request.SESSION_RUNNING == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0028 + * *@tc.name subRequestConstant0028 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0028', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0028 is starting-----------------------"); + expect(typeof request.SESSION_PENDING == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0029 + * *@tc.name subRequestConstant0029 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0029', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0029 is starting-----------------------"); + expect(typeof request.SESSION_PAUSED == 'number').assertTrue(); + done(); + }); + + /** + * @tc.number SUB_REQUEST_CONSTANT_0030 + * *@tc.name subRequestConstant0030 + * @tc.desc Test constants of the request module. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 0 + */ + it('subRequestConstant0030', 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_CONSTANT_0030 is starting-----------------------"); + expect(typeof request.SESSION_FAILED == 'number').assertTrue(); + done(); + }); + + }) +} diff --git a/request/crossplatform/RequestTest_ets/src/main/ets/test/requestDownload.test.ets b/request/crossplatform/RequestTest_ets/src/main/ets/test/requestDownload.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..d7c7544772c901856377e9665b84faa5cd04443e --- /dev/null +++ b/request/crossplatform/RequestTest_ets/src/main/ets/test/requestDownload.test.ets @@ -0,0 +1,667 @@ +/* + * 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 request from "@ohos.request"; +import fileIo from "@ohos.file.fs"; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from "@ohos/hypium"; + +const removeFile = (filePath: string): void => { + if (fileIo.accessSync(filePath)) fileIo.unlinkSync(filePath); +}; + +export default function requestDownloadJSUnit() { + describe("requestDownloadJSUnit", function () { + console.info("====>################################request download Test start"); + + /** + * beforeAll: Prerequisites at the test suite level, which are executed before the test suite is executed. + */ + beforeAll(function () { + console.info("====>beforeAll: Prerequisites are executed."); + }); + + /** + * beforeEach: Prerequisites at the test case level, which are executed before each test case is executed. + */ + beforeEach(function () { + console.info("====>beforeEach: Prerequisites is executed."); + }); + + /** + * afterEach: Test case-level clearance conditions, which are executed after each test case is executed. + */ + afterEach(function () { + console.info("====>afterEach: Test case-level clearance conditions is executed."); + }); + + /** + * afterAll: Test suite-level cleanup condition, which is executed after the test suite is executed. + */ + afterAll(function () { + console.info("====>afterAll: Test suite-level cleanup condition is executed"); + }); + + /** + * @tc.number SUB_REQUEST_DOWNDLOAD_FILE_STAGE_API_CALLBACK_0001 + * *@tc.name subRequestDownloadFileStageApiCallback0001 + * @tc.desc Starts a download session. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 2 + */ + it("subRequestDownloadFileStageApiCallback0001", 0, async function (done) { + console.info("-----------------------SUB_REQUEST_downloadFile_STAGE_API_CALLBACK_0001 is starting-----------------------"); + let downloadFilePath = `${globalThis.abilityContext.tempDir}/SUB_REQUEST_downloadFile_STAGE_API_CALLBACK_0001.txt`; + console.debug("====>downloadFileConfig:" + `${globalThis.abilityContext.tempDir}/SUB_REQUEST_downloadFile_STAGE_API_CALLBACK_0001.txt`); + let downloadFileConfig = { + url: "https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk", + header: { + headers: "http", + }, + enableMetered: false, + enableRoaming: false, + description: "XTS download test!", + networkType: request.NETWORK_WIFI, + filePath: downloadFilePath, + title: "XTS download test!", + background: false, + }; + request.downloadFile(globalThis.abilityContext, downloadFileConfig, async (err, downloadTask) => { + console.info("====>SUB_REQUEST_downloadFile_STAGE_API_CALLBACK_0001 downloadTask: " + downloadTask); + try { + expect(downloadTask != undefined).assertEqual(true); + } catch (e) { + console.info("====>SUB_REQUEST_downloadFile_STAGE_API_CALLBACK_0001 except error: " + e); + } + await downloadTask.delete(); + removeFile(downloadFilePath); + console.info("-----------------------SUB_REQUEST_downloadFile_STAGE_API_CALLBACK_0001 end-----------------------"); + done(); + }); + }); + + /** + * @tc.number SUB_REQUEST_DOWNDLOAD_FILE_STAGE_API_PROMISE_0001 + * *@tc.name subRequestDownloadFileStageApiPromise0001 + * @tc.desc Starts a download session. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 2 + */ + it("subRequestDownloadFileStageApiPromise0001", 0, async function (done) { + console.info("-----------------------SUB_REQUEST_downloadFile_STAGE_API_PROMISE_0001 is starting-----------------------"); + let downloadFilePath = `${globalThis.abilityContext.tempDir}/SUB_REQUEST_downloadFile_STAGE_API_PROMISE_0001.txt`; + console.debug("====>downloadFileConfig:" + `${globalThis.abilityContext.tempDir}/SUB_REQUEST_downloadFile_STAGE_API_PROMISE_0001.txt`); + let downloadFileConfig = { + url: "https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk", + header: { + headers: "http", + }, + enableMetered: false, + enableRoaming: false, + description: "XTS download test!", + networkType: request.NETWORK_WIFI, + filePath: downloadFilePath, + title: "XTS download test!", + background: false, + }; + try { + request + .downloadFile(globalThis.abilityContext, downloadFileConfig) + .then(async (downloadTask) => { + console.info("====>SUB_REQUEST_downloadFile_STAGE_API_PROMISE_0001 downloadTask: " + downloadTask); + expect(downloadTask != undefined).assertEqual(true); + await downloadTask.delete(); + }) + .catch((err) => { + console.error("====>SUB_REQUEST_downloadFile_STAGE_API_PROMISE_0001 error: " + err); + }) + .finally(() => { + removeFile(downloadFilePath); + done(); + }); + } catch (err) { + console.error("====>SUB_REQUEST_downloadFile_STAGE_API_PROMISE_0001 catch error: " + err); + done(); + } + }); + + /** + * @tc.number SUB_REQUEST_DOWNDLOAD_API_DELETE_0001 + * *@tc.name subRequestDownloadApiDelete0001 + * @tc.desc Delete the download task. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 2 + */ + it("subRequestDownloadApiDelete0001", 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_delete_0001 is starting-----------------------"); + let downloadFilePath = `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_DELETE_0001.txt`; + console.debug("====>downloadFileConfig:" + `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_DELETE_0001.txt`); + let downloadFileConfig = { + url: "https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk", + header: { + headers: "http", + }, + enableMetered: false, + enableRoaming: false, + description: "XTS download test!", + networkType: request.NETWORK_WIFI, + filePath: downloadFilePath, + title: "XTS download test!", + background: false, + }; + request.downloadFile(globalThis.abilityContext, downloadFileConfig, async (err, downloadTask) => { + console.info("====>SUB_REQUEST_DOWNLOAD_API_DELETE_0001 downloadTask: " + downloadTask); + try { + expect(downloadTask != undefined).assertEqual(true); + downloadTask.delete(async (err, data) => { + try { + if (err) { + console.error("====>SUB_REQUEST_DOWNLOAD_API_DELETE_0001 Failed to delete the download task."); + expect().assertFail(); + done(); + } + console.info("====>SUB_REQUEST_DOWNLOAD_API_DELETE_0001 Download task delete success."); + expect(typeof data == "boolean").assertTrue(); + } catch (err) { + console.error("====>SUB_REQUEST_DOWNLOAD_API_DELETE_0001 delete throw error" + err); + } finally { + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + } + }); + } catch (error) { + console.error("====>SUB_REQUEST_DOWNLOAD_API_DELETE_0001 delete catch error" + error); + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + } + }); + }); + + /** + * @tc.number SUB_REQUEST_DOWNDLOAD_API_DELETE_0002 + * *@tc.name subRequestDownloadApiDelete0002 + * @tc.desc Delete the download task. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 2 + */ + it("subRequestDownloadApiDelete0002", 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DELETE_0002 is starting-----------------------"); + let downloadFilePath = `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_DELETE_0002.txt`; + console.debug("====>downloadFileConfig:" + `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_DELETE_0002.txt`); + let downloadFileConfig = { + url: "https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk", + header: { + headers: "http", + }, + enableMetered: false, + enableRoaming: false, + description: "XTS download test!", + networkType: request.NETWORK_WIFI, + filePath: downloadFilePath, + title: "XTS download test!", + background: false, + }; + request.downloadFile(globalThis.abilityContext, downloadFileConfig, async (err, downloadTask) => { + console.info("====>SUB_REQUEST_DOWNLOAD_API_DELETE_0002 downloadTask: " + downloadTask); + try { + expect(downloadTask != undefined).assertEqual(true); + downloadTask + .delete() + .then(async (data) => { + console.info("====>SUB_REQUEST_DOWNLOAD_API_DELETE_0002 Download task delete success."); + expect(data).assertEqual(true); + console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DELETE_0002 end-----------------------"); + }) + .catch(async (err) => { + console.info("====>SUB_REQUEST_DOWNLOAD_API_DELETE_0002 Failed to delete the download task."); + expect().assertFail(); + }) + .finally(async () => { + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + }); + } catch (error) { + console.error("====>SUB_REQUEST_DOWNLOAD_API_DELETE_0002 delete catch error"); + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + } + }); + }); + + /** + * @tc.number SUB_REQUEST_DOWNDLOAD_API_SUSPEND_0001 + * *@tc.name subRequestDownloadApiSuspend0001 + * @tc.desc Suspend the download task + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 2 + */ + it("subRequestDownloadApiSuspend0001", 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_SUSPEND_0001 is starting-----------------------"); + let downloadFilePath = `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_SUSPEND_0001.txt`; + console.debug("====>downloadFileConfig:" + `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_SUSPEND_0001.txt`); + let downloadFileConfig = { + url: "https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk", + header: { + headers: "http", + }, + enableMetered: false, + enableRoaming: false, + description: "XTS download test!", + networkType: request.NETWORK_WIFI, + filePath: downloadFilePath, + title: "XTS download test!", + background: false, + }; + request.downloadFile(globalThis.abilityContext, downloadFileConfig, async (err, downloadTask) => { + console.info("====>SUB_REQUEST_DOWNLOAD_API_SUSPEND_0001 downloadTask: " + downloadTask); + try { + expect(downloadTask != undefined).assertEqual(true); + downloadTask.suspend(async (err, data) => { + try { + if (err) { + console.error("====>SUB_REQUEST_DOWNLOAD_API_SUSPEND_0001 Failed to suspend the download task."); + expect().assertFail(); + done(); + } + console.info("====>SUB_REQUEST_DOWNLOAD_API_SUSPEND_0001 Download task suspend success."); + expect(data == true).assertTrue(); + } catch (err) { + console.error("====>SUB_REQUEST_DOWNLOAD_API_SUSPEND_0001 throw_error: " + JSON.stringify(err)); + } + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + }); + } catch (err) { + console.error("====>SUB_REQUEST_DOWNLOAD_API_SUSPEND_0001 suspend catch error" + JSON.stringify(err)); + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + } + }); + }); + + /** + * @tc.number SUB_REQUEST_DOWNDLOAD_API_SUSPEND_0002 + * *@tc.name subRequestDownloadApiSuspend0002 + * @tc.desc Suspend the download task + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 2 + */ + it("subRequestDownloadApiSuspend0002", 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_SUSPEND_0002 is starting-----------------------"); + let downloadFilePath = `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_SUSPEND_0002.txt`; + console.debug("====>downloadFileConfig:" + `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_SUSPEND_0002.txt`); + let downloadFileConfig = { + url: "https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk", + header: { + headers: "http", + }, + enableMetered: false, + enableRoaming: false, + description: "XTS download test!", + networkType: request.NETWORK_WIFI, + filePath: downloadFilePath, + title: "XTS download test!", + background: false, + }; + request.downloadFile(globalThis.abilityContext, downloadFileConfig, async (err, downloadTask) => { + console.info("====>SUB_REQUEST_DOWNLOAD_API_SUSPEND_0002 downloadTask: " + downloadTask); + try { + expect(downloadTask != undefined).assertEqual(true); + try { + let data = await downloadTask.suspend(); + expect(data == true).assertTrue(); + } catch (err) { + console.info("====>SUB_REQUEST_DOWNLOAD_API_SUSPEND_0002 throw_err:" + JSON.stringify(err)); + } + } catch (err) { + console.error("====>SUB_REQUEST_DOWNLOAD_API_SUSPEND_0002 suspend catch error" + JSON.stringify(err)); + } finally { + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + } + }); + }); + + /** + * @tc.number SUB_REQUEST_DOWNDLOAD_API_RESTORE_0001 + * *@tc.name subRequestDownloadApiRestore0001 + * @tc.desc Restore the download task + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 2 + */ + it("subRequestDownloadApiRestore0001", 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_RESTORE_0001 is starting-----------------------"); + let downloadFilePath = `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_RESTORE_0001.txt`; + console.debug("====>downloadFileConfig:" + `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_RESTORE_0001.txt`); + let downloadFileConfig = { + url: "https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk", + header: { + headers: "http", + }, + enableMetered: false, + enableRoaming: false, + description: "XTS download test!", + networkType: request.NETWORK_WIFI, + filePath: downloadFilePath, + title: "XTS download test!", + background: false, + }; + request.downloadFile(globalThis.abilityContext, downloadFileConfig, async (err, downloadTask) => { + console.info("====>SUB_REQUEST_DOWNLOAD_API_RESTORE_0001 downloadTask: " + downloadTask); + try { + expect(downloadTask != undefined).assertEqual(true); + downloadTask.restore(async (err, data) => { + try { + if (err) { + console.error("====>SUB_REQUEST_DOWNLOAD_API_RESTORE_0001 Failed to restore the download task."); + expect().assertFail(); + done(); + } + console.info("====>SUB_REQUEST_DOWNLOAD_API_RESTORE_0001 Download restore success."); + expect(data == true).assertTrue(); + } catch (err) { + console.info("====>SUB_REQUEST_DOWNLOAD_API_RESTORE_0001 throw_err:" + JSON.stringify(err)); + } + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + }); + } catch (err) { + console.error("====>SUB_REQUEST_DOWNLOAD_API_RESTORE_0001 restore catch error" + JSON.stringify(err)); + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + } + }); + }); + + /** + * @tc.number SUB_REQUEST_DOWNDLOAD_API_RESTORE_0002 + * *@tc.name subRequestDownloadApiRestore0002 + * @tc.desc Restore the download task + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 2 + */ + it("subRequestDownloadApiRestore0002", 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_RESTORE_0002 is starting-----------------------"); + let downloadFilePath = `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_RESTORE_0002.txt`; + console.debug("====>downloadFileConfig:" + `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_RESTORE_0002.txt`); + let downloadFileConfig = { + url: "https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk", + header: { + headers: "http", + }, + enableMetered: false, + enableRoaming: false, + description: "XTS download test!", + networkType: request.NETWORK_WIFI, + filePath: downloadFilePath, + title: "XTS download test!", + background: false, + }; + request.downloadFile(globalThis.abilityContext, downloadFileConfig, async (err, downloadTask) => { + console.info("====>SUB_REQUEST_DOWNLOAD_API_RESTORE_0002 downloadTask: " + downloadTask); + try { + expect(downloadTask != undefined).assertEqual(true); + downloadTask + .restore() + .then(async (data) => { + console.info("====>SUB_REQUEST_DOWNLOAD_API_RESTORE_0002 Download task restore."); + expect(data == true).assertTrue(); + await downloadTask.delete(); + }) + .catch(async (err) => { + console.info("====>SUB_REQUEST_DOWNLOAD_API_RESTORE_0002 Failed to restore the download task."); + }) + .finally(async () => { + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + }); + } catch (err) { + console.error("====>SUB_REQUEST_DOWNLOAD_API_RESTORE_0002 restore catch error" + JSON.stringify(err)); + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + } + }); + }); + + /** + * @tc.number SUB_REQUEST_DOWNDLOAD_API_GETTASKINFO_0001 + * *@tc.name subRequestDownloadApiGetTaskInfo0001 + * @tc.desc Get the download task info + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 2 + */ + it("subRequestDownloadApiGetTaskInfo0001", 0, async function (done) { + console.info("====>---------------------SUB_REQUEST_DOWNLOAD_API_GETTASKINFO_0001 is starting---------------------"); + let downloadFilePath = `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_GETTASKINFO_0001.txt`; + console.debug("====>downloadFileConfig:" + `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_GETTASKINFO_0001.txt`); + let downloadFileConfig = { + url: "https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk", + header: { + headers: "http", + }, + enableMetered: false, + enableRoaming: false, + description: "XTS download test!", + networkType: request.NETWORK_WIFI, + filePath: downloadFilePath, + title: "XTS download test!", + background: false, + }; + request.downloadFile(globalThis.abilityContext, downloadFileConfig, async (err, downloadTask) => { + console.info("====>SUB_REQUEST_DOWNLOAD_API_GETTASKINFO_0001 downloadTask: " + downloadTask); + try { + expect(downloadTask != undefined).assertEqual(true); + downloadTask.getTaskInfo(async (err, data) => { + try { + if (err) { + console.error("====>SUB_REQUEST_DOWNLOAD_API_GETTASKINFO_0001 Failed to getTaskInfo the download task."); + expect().assertFail(); + done(); + } + console.info("====>SUB_REQUEST_DOWNLOAD_API_GETTASKINFO_0001 Download getTaskInfo success."); + expect(typeof data == "object").assertTrue(); + } catch (err) { + console.error("====>SUB_REQUEST_DOWNLOAD_API_GETTASKINFO_0001 throw_error" + JSON.stringify(err)); + } + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + }); + } catch (err) { + console.error("====>SUB_REQUEST_DOWNLOAD_API_GETTASKINFO_0001 getTaskInfo catch error" + JSON.stringify(err)); + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + } + }); + }); + + /** + * @tc.number SUB_REQUEST_DOWNDLOAD_API_GETTASKINFO_0002 + * *@tc.name subRequestDownloadApiGetTaskInfo0002 + * @tc.desc Get the download task info + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 2 + */ + it("subRequestDownloadApiGetTaskInfo0002", 0, async function (done) { + console.info("====>-------------------SUB_REQUEST_DOWNLOAD_API_GETTASKINFO_0002 is starting----------------------"); + let downloadFilePath = `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_GETTASKINFO_0002.txt`; + console.debug("====>downloadFileConfig:" + `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_GETTASKINFO_0002.txt`); + let downloadFileConfig = { + url: "https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk", + header: { + headers: "http", + }, + enableMetered: false, + enableRoaming: false, + description: "XTS download test!", + networkType: request.NETWORK_WIFI, + filePath: downloadFilePath, + title: "XTS download test!", + background: false, + }; + request.downloadFile(globalThis.abilityContext, downloadFileConfig, async (err, downloadTask) => { + console.info("====>SUB_REQUEST_DOWNLOAD_API_GETTASKINFO_0002 downloadTask: " + downloadTask); + try { + expect(downloadTask != undefined).assertEqual(true); + downloadTask + .getTaskInfo() + .then(async (data) => { + console.info("====>SUB_REQUEST_DOWNLOAD_API_GETTASKINFO_0002 Download task getTaskInfo success."); + expect(typeof data == "object").assertTrue(); + }) + .catch(async (err) => { + console.error("====>SUB_REQUEST_DOWNLOAD_API_GETTASKINFO_0002 Failed to getTaskInfo the download task."); + }) + .finally(async () => { + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + }); + } catch (err) { + console.error("====>SUB_REQUEST_DOWNLOAD_API_GETTASKINFO_0002 getTaskInfo catch error" + JSON.stringify(err)); + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + } + }); + }); + + /** + * @tc.number SUB_REQUEST_DOWNLOAD_API_GETTASKMINETYPE_0002 + * *@tc.name subRequestDownloadApigetTaskMimeType0002 + * @tc.desc Get mimetype of the download task + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 2 + */ + it("subRequestDownloadApigetTaskMimeType0002", 0, async function (done) { + console.info("====>-------------------SUB_REQUEST_DOWNLOAD_API_GETTASKMINETYPE_0002 is starting----------------------"); + let downloadFilePath = `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_GETTASKMINETYPE_0002.txt`; + console.debug("====>downloadFileConfig:" + `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNLOAD_API_GETTASKMINETYPE_0002.txt`); + let downloadFileConfig = { + url: "https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk", + header: { + headers: "http", + }, + enableMetered: false, + enableRoaming: false, + description: "XTS download test!", + networkType: request.NETWORK_WIFI, + filePath: downloadFilePath, + title: "XTS download test!", + background: false, + }; + + request.downloadFile(globalThis.abilityContext, downloadFileConfig, async (err, downloadTask) => { + console.info("====>SUB_REQUEST_DOWNLOAD_API_GETTASKMINETYPE_0002 downloadTask: " + downloadTask); + try { + let data = await downloadTask.getTaskMimeType(); + expect(data != undefined).assertEqual(true); + } catch (err) { + console.error("====>SUB_REQUEST_DOWNLOAD_API_GETTASKMINETYPE_0002 throw_error: " + JSON.stringify(err)); + } finally { + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + } + }); + }); + + /** + * @tc.number SUB_REQUEST_DOWNDLOAD_API_TASKINFO_0001 + * *@tc.name subRequestDownloadApiTaskInfo0001 + * @tc.desc Get the download task info, and test the field of TaskInfo + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 2 + */ + it("subRequestDownloadApiTaskInfo0001", 0, async function (done) { + console.info("====>---------------------SUB_REQUEST_DOWNDLOAD_API_TASKINFO_0001 is starting---------------------"); + let downloadFilePath = `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNDLOAD_API_TASKINFO_0001.txt`; + console.debug("====>downloadFileConfig:" + `${globalThis.abilityContext.tempDir}/SUB_REQUEST_DOWNDLOAD_API_TASKINFO_0001.txt`); + let downloadFileConfig = { + url: "https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk", + header: { + headers: "http", + }, + enableMetered: false, + enableRoaming: false, + description: "XTS download test!", + networkType: request.NETWORK_WIFI, + filePath: downloadFilePath, + title: "XTS download test!", + background: false, + }; + request.downloadFile(globalThis.abilityContext, downloadFileConfig, async (err, downloadTask) => { + console.info("====>SUB_REQUEST_DOWNDLOAD_API_TASKINFO_0001 downloadTask: " + downloadTask); + try { + expect(downloadTask != undefined).assertEqual(true); + downloadTask.getTaskInfo(async (err, data) => { + try { + if (err) { + console.error("====>SUB_REQUEST_DOWNDLOAD_API_TASKINFO_0001 Failed to getTaskInfo the download task."); + expect().assertFail(); + done(); + } + console.info("====>SUB_REQUEST_DOWNDLOAD_API_TASKINFO_0001 Download getTaskInfo success."); + expect(typeof data == "object").assertTrue(); + expect(typeof data.description == "string").assertTrue(); + expect(typeof data.downloadedBytes == "number").assertTrue(); + expect(typeof data.downloadId == "number").assertTrue(); + expect(typeof data.failedReason == "number").assertTrue(); + expect(typeof data.fileName == "string").assertTrue(); + expect(typeof data.filePath == "string").assertTrue(); + expect(typeof data.pausedReason == "number").assertTrue(); + expect(typeof data.status == "number").assertTrue(); + expect(typeof data.targetURI == "string").assertTrue(); + expect(typeof data.downloadTitle == "string").assertTrue(); + expect(typeof data.downloadTotalBytes == "number").assertTrue(); + } catch (err) { + console.error("====>SUB_REQUEST_DOWNDLOAD_API_TASKINFO_0001 throw_error" + JSON.stringify(err)); + } + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + }); + } catch (err) { + console.error("====>SUB_REQUEST_DOWNDLOAD_API_TASKINFO_0001 getTaskInfo catch error" + JSON.stringify(err)); + await downloadTask.delete(); + removeFile(downloadFilePath); + done(); + } + }); + }); + }); +} diff --git a/request/crossplatform/RequestTest_ets/src/main/ets/test/requestUpload.test.ets b/request/crossplatform/RequestTest_ets/src/main/ets/test/requestUpload.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..1e6474612476c07c6f0b360a409e5b8a8a45753e --- /dev/null +++ b/request/crossplatform/RequestTest_ets/src/main/ets/test/requestUpload.test.ets @@ -0,0 +1,239 @@ +/* + * 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 request from "@ohos.request"; +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from "@ohos/hypium"; + +export default function requestUploadJSUnit() { + describe("requestUploadJSUnit", function () { + console.info("====>################################request upload Test start"); + + /** + * beforeAll: Prerequisites at the test suite level, which are executed before the test suite is executed. + */ + beforeAll(function () { + console.info("====>beforeAll: Prerequisites are executed."); + }); + + /** + * beforeEach: Prerequisites at the test case level, which are executed before each test case is executed. + */ + beforeEach(function () { + console.info("====>beforeEach: Prerequisites is executed."); + }); + + /** + * afterEach: Test case-level clearance conditions, which are executed after each test case is executed. + */ + afterEach(function () { + console.info("====>afterEach: Test case-level clearance conditions is executed."); + }); + + /** + * afterAll: Test suite-level cleanup condition, which is executed after the test suite is executed. + */ + afterAll(function () { + console.info("====>afterAll: Test suite-level cleanup condition is executed"); + }); + + let RequestData = { + name: "name", + value: "123", + }; + + let File = { + filename: "test", + name: "test", + uri: "internal://cache/test.txt", + type: "txt", + }; + + let uploadConfig = { + url: "http://127.0.0.1", + header: { + headers: "http", + }, + method: "POST", + files: [File], + data: [RequestData], + }; + + /** + * @tc.number SUB_REQUEST_UPLOAD_API_DELETE_0001 + * *@tc.name subRequestUploadApiDelete0001 + * @tc.desc upload file and delete the upload task. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 2 + */ + it("subRequestUploadApiDelete0001", 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_UPLOAD_API_DELETE_0001 is starting-----------------------"); + request.uploadFile(globalThis.abilityContext, uploadConfig, (err, uploadTask) => { + try { + console.info("====>SUB_REQUEST_UPLOAD_API_DELETE_0001 uploadTask: " + uploadTask); + expect(uploadTask != undefined).assertEqual(true); + uploadTask.delete((err, data) => { + try { + if (err) { + console.error("====>SUB_REQUEST_UPLOAD_API_DELETE_0001 Failed to delete the uploadTask task."); + expect().assertFail(); + done(); + } + console.info("====>SUB_REQUEST_UPLOAD_API_DELETE_0001 uploadTask task delete success."); + expect(typeof data == "boolean").assertTrue(); + console.info("====>-----------------------SUB_REQUEST_UPLOAD_API_DELETE_0001 end-----------------------"); + done(); + } catch (err) { + console.error("====>SUB_REQUEST_UPLOAD_API_DELETE_0001 delete error" + err); + done(); + } + }); + } catch (error) { + console.error("====>SUB_REQUEST_UPLOAD_API_DELETE_0001 delete catch error" + error); + done(); + } + }); + }); + + /** + * @tc.number SUB_REQUEST_UPLOAD_API_DELETE_0002 + * *@tc.name subRequestUploadApiDelete0002 + * @tc.desc upload file and delete the upload task (Promise). + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 2 + */ + it("subRequestUploadApiDelete0002", 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_UPLOAD_API_DELETE_0002 is starting-----------------------"); + request.uploadFile(globalThis.abilityContext, uploadConfig).then((uploadTask) => { + console.info("====>SUB_REQUEST_UPLOAD_API_DELETE_0002 uploadTask: " + uploadTask); + try { + expect(uploadTask != undefined).assertEqual(true); + uploadTask + .delete() + .then((data) => { + console.info("====>SUB_REQUEST_UPLOAD_API_DELETE_0002 delete data:" + JSON.stringify(data)); + expect(data).assertEqual(true); + done(); + }) + .catch((err) => { + console.info("====>SUB_REQUEST_UPLOAD_API_DELETE_0002 Failed to delete the uploadTask task."); + expect().assertFail(); + done(); + }); + } catch (err) { + console.error("====>SUB_REQUEST_UPLOAD_API_DELETE_0002 delete catch err"); + done(); + } + }); + }); + + /** + * @tc.number SUB_REQUEST_UPLOAD_API_EVENT_0001 + * *@tc.name subRequestUploadApiEvent0001 + * @tc.desc upload file and test event. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 2 + */ + it("subRequestUploadApiEvent0001", 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_UPLOAD_API_EVENT_0001 is starting-----------------------"); + request.uploadFile(globalThis.abilityContext, uploadConfig).then((uploadTask) => { + console.info("====>SUB_REQUEST_UPLOAD_API_EVENT_0001 uploadTask: " + uploadTask); + try { + expect(uploadTask != undefined).assertEqual(true); + uploadTask.on("progress", (uploadedSize, totalSize) => { + expect(uploadedSize != undefined).assertEqual(true); + expect(totalSize != undefined).assertEqual(true); + uploadTask.off("progress", (uploadedSize, totalSize) => { + expect(uploadedSize != undefined).assertEqual(true); + expect(totalSize != undefined).assertEqual(true); + }); + }); + done(); + } catch (err) { + console.error("====>SUB_REQUEST_UPLOAD_API_EVENT_0001 delete catch err"); + done(); + } + }); + }); + + /** + * @tc.number SUB_REQUEST_UPLOAD_API_EVENT_0002 + * *@tc.name subRequestUploadApiEvent0002 + * @tc.desc upload file and test event. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 2 + */ + it("subRequestUploadApiEvent0002", 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_UPLOAD_API_EVENT_0002 is starting-----------------------"); + request.uploadFile(globalThis.abilityContext, uploadConfig).then((uploadTask) => { + console.info("====>SUB_REQUEST_UPLOAD_API_EVENT_0002 uploadTask: " + uploadTask); + try { + expect(uploadTask != undefined).assertEqual(true); + uploadTask.on("headerReceive", (header) => { + expect(header != undefined).assertEqual(true); + uploadTask.off("headerReceive", (header) => { + expect(header != undefined).assertEqual(true); + }); + }); + done(); + } catch (err) { + console.error("====>SUB_REQUEST_UPLOAD_API_EVENT_0002 delete catch err"); + done(); + } + }); + }); + + /** + * @tc.number SUB_REQUEST_UPLOAD_API_EVENT_0003 + * *@tc.name subRequestUploadApiEvent0003 + * @tc.desc upload file and test event. + * @tc.size : MediumTest + * @tc.type : Function + * @tc.level : Level 2 + */ + it("subRequestUploadApiEvent0003", 0, async function (done) { + console.info("====>-----------------------SUB_REQUEST_UPLOAD_API_EVENT_0003 is starting-----------------------"); + request.uploadFile(globalThis.abilityContext, uploadConfig).then((uploadTask) => { + console.info("====>SUB_REQUEST_UPLOAD_API_EVENT_0003 uploadTask: " + uploadTask); + try { + expect(uploadTask != undefined).assertEqual(true); + uploadTask.on("complete", (taskStates) => { + const task = taskStates.length != 0 ? taskStates[0] : null; + if (task) { + expect(task.path != null).assertEqual(true); + expect(task.responseCode != null).assertEqual(true); + expect(task.message != null).assertEqual(true); + } else { + console.info("SUB_REQUEST_UPLOAD_API_EVENT_0003: task is null"); + expect().assertFail(); + done(); + } + + uploadTask.off("complete", (taskStates) => { + expect(taskStates != undefined).assertEqual(true); + }); + }); + done(); + } catch (err) { + console.error("====>SUB_REQUEST_UPLOAD_API_EVENT_0003 delete catch err"); + done(); + } + }); + }); + }); +} diff --git a/request/crossplatform/RequestTest_ets/src/main/module.json b/request/crossplatform/RequestTest_ets/src/main/module.json new file mode 100644 index 0000000000000000000000000000000000000000..2bb17a9cd4f3cc080530a7630e369c632b57833d --- /dev/null +++ b/request/crossplatform/RequestTest_ets/src/main/module.json @@ -0,0 +1,60 @@ +{ + "module": { + "name": "crossplatform_request_test_ets", + "type": "feature", + "description": "$string:module_test_desc", + "mainElement": "TestAbility", + "deviceTypes": [ + "phone" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:test_pages", + "abilities": [ + { + "name": "TestAbility", + "srcEntry": "./ets/TestAbility/TestAbility.ets", + "description": "$string:TestAbility_desc", + "icon": "$media:icon", + "label": "$string:TestAbility_label", + "exported": true, + "startWindowIcon": "$media:icon", + "startWindowBackground": "$color:start_window_background", + "skills": [ + { + "actions": [ + "action.system.home" + ], + "entities": [ + "entity.system.home" + ] + } + ] + } + ], + "requestPermissions": [ + { + "name":"ohos.permission.INTERNET", + "reason":"need use ohos.permission.INTERNET." + }, + {"name": "ohos.permission.WRITE_MEDIA", + "reason": "need use ohos.permission.WRITE_MEDIA", + "usedScene": { + "ability": [ + "com.acts.request.test.MainAbility" + ], + "when":"always" + } + }, + {"name": "ohos.permission.READ_MEDIA", + "reason": "need use ohos.permission.WRITE_MEDIA", + "usedScene": { + "ability": [ + "com.acts.request.test.MainAbility" + ], + "when":"always" + } + } + ] + } +} diff --git a/request/crossplatform/RequestTest_ets/src/main/resources/base/element/color.json b/request/crossplatform/RequestTest_ets/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/request/crossplatform/RequestTest_ets/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/request/crossplatform/RequestTest_ets/src/main/resources/base/element/string.json b/request/crossplatform/RequestTest_ets/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..65d8fa5a7cf54aa3943dcd0214f58d1771bc1f6c --- /dev/null +++ b/request/crossplatform/RequestTest_ets/src/main/resources/base/element/string.json @@ -0,0 +1,16 @@ +{ + "string": [ + { + "name": "module_test_desc", + "value": "test ability description" + }, + { + "name": "TestAbility_desc", + "value": "the test ability" + }, + { + "name": "TestAbility_label", + "value": "test label" + } + ] +} \ No newline at end of file diff --git a/request/crossplatform/RequestTest_ets/src/main/resources/base/media/icon.png b/request/crossplatform/RequestTest_ets/src/main/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/request/crossplatform/RequestTest_ets/src/main/resources/base/media/icon.png differ diff --git a/request/crossplatform/RequestTest_ets/src/main/resources/base/profile/test_pages.json b/request/crossplatform/RequestTest_ets/src/main/resources/base/profile/test_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..77e90731b5a38d861663029b483df3d3ac9ec74b --- /dev/null +++ b/request/crossplatform/RequestTest_ets/src/main/resources/base/profile/test_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "TestAbility/pages/Index" + ] +}