• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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 request from "@ohos.request";
17import featureAbility from '@ohos.ability.featureAbility'
18import fileio from '@ohos.fileio';
19import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from "hypium/index";
20
21export default function requestDownloadJSUnit() {
22  describe('requestDownloadTest', function () {
23    console.info('====>################################request download Test start');
24
25    /**
26     * beforeAll: Prerequisites at the test suite level, which are executed before the test suite is executed.
27     */
28    beforeAll(async function () {
29      console.info('====>beforeAll: Prerequisites are executed.');
30    });
31
32    /**
33     * beforeEach: Prerequisites at the test case level, which are executed before each test case is executed.
34     */
35    beforeEach(async function () {
36      console.info('====>beforeEach: Prerequisites is executed.');
37      await setDownLoadConfig()
38    });
39
40    /**
41     * afterEach: Test case-level clearance conditions, which are executed after each test case is executed.
42     */
43    afterEach(function () {
44      console.info('====>afterEach: Test case-level clearance conditions is executed.');
45    });
46
47    /**
48     * afterAll: Test suite-level cleanup condition, which is executed after the test suite is executed.
49     */
50    afterAll(function () {
51      console.info('====>afterAll: Test suite-level cleanup condition is executed');
52    });
53
54    let downloadConfig
55    async function setDownLoadConfig(){
56      let downloadFilePath = await featureAbility.getContext().getFilesDir()
57      downloadConfig = {
58        url: 'https://gitee.com/chenzhixue/downloadTest/releases/download/v1.0/test.apk',
59        header: {
60          headers: 'http'
61        },
62        enableMetered: false,
63        enableRoaming: false,
64        description: 'XTS download test!',
65        networkType: request.NETWORK_WIFI,
66        filePath: `${downloadFilePath}/`,
67        title: 'XTS download test!',
68        background: false
69      }
70    }
71
72    /**
73     * @tc.number    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001
74     * @tc.name    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001
75     * @tc.desc      alled when the current download session is in process.
76     * @tc.size      : MEDIUM
77     * @tc.type      : Function
78     * @tc.level     : Level 2
79     */
80    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001', 0, async function (done) {
81      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001 is starting-----------------------");
82      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001.txt';
83      let flag = false;
84      let filePath = downloadConfig.filePath;
85      request.download(downloadConfig, async (err, downloadTask)=>{
86        try{
87          console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001 downloadTask: " + JSON.stringify(downloadTask) );
88          expect(downloadTask != undefined).assertEqual(true);
89          downloadTask.on('progress', async (data1, data2) => {
90            if (data1 == data2 && flag == false){
91              flag = true;
92              try{
93                console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001 on data1 =" + data1);
94                console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001 on data2 =" + data2);
95                expect(true).assertEqual(data1 != undefined);
96                expect(true).assertEqual(data2 != undefined);
97
98                downloadTask.off('progress');
99                await downloadTask.remove();
100                fileio.unlinkSync(filePath);
101                done();
102              }catch(err){
103                console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001 throw_error: " + err);
104                downloadTask.off('progress');
105                await downloadTask.remove();
106                fileio.unlinkSync(filePath);
107                done();
108              }
109            }
110          });
111        }catch(err){
112          console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0001 error: " + err);
113          await downloadTask.remove();
114          fileio.unlinkSync(filePath);
115          done();
116        }
117      });
118    });
119
120    /**
121     * @tc.number    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002
122     * @tc.name    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002
123     * @tc.desc       Called when the current download session complete、pause or remove.
124     * @tc.size      : MEDIUM
125     * @tc.type      : Function
126     * @tc.level     : Level 2
127     */
128    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002', 0, async function (done) {
129      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002 is starting-----------------------");
130      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002.txt';
131      let flag = false;
132      let filePath = downloadConfig.filePath;
133      request.download(downloadConfig, async (err, downloadTask)=>{
134        console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002 downloadTask: " + downloadTask);
135        try{
136          expect(downloadTask != undefined).assertEqual(true);
137          downloadTask.on('complete', async () => {
138            if (flag == false){
139              flag = true;
140              downloadTask.off('complete');
141              try{
142                console.info('====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002 task completed.')
143                expect(true).assertTrue();
144              }catch(err){
145                console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002 throw_error: " + err);
146              }
147              downloadTask.off('complete');
148              await downloadTask.remove();
149              fileio.unlinkSync(filePath);
150              done();
151            }
152          });
153        }catch(err){
154          console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0002 error: " + err);
155          downloadTask.off('complete');
156          await downloadTask.remove();
157          fileio.unlinkSync(filePath);
158          done();
159        }
160      });
161    });
162
163    /**
164     * @tc.number    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003
165     * @tc.name    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003
166     * @tc.desc       Called when the current download session complete、pause or remove.
167     * @tc.size      : MEDIUM
168     * @tc.type      : Function
169     * @tc.level     : Level 2
170     */
171    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003', 0, async function (done) {
172      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003 is starting-----------------------");
173      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003.txt';
174      let flag = false;
175      let filePath = downloadConfig.filePath;
176      request.download(downloadConfig, async (err, downloadTask)=>{
177        console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003 downloadTask: " + downloadTask);
178        try{
179          expect(downloadTask != undefined).assertEqual(true);
180          downloadTask.on('pause', async () => {
181            try{
182              console.info('====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003 task pause.')
183              expect(true).assertTrue();
184            }catch(err){
185              console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003 throw_error: " + err);
186            }
187            downloadTask.off('pause');
188            await downloadTask.remove();
189            fileio.unlinkSync(filePath);
190            done();
191          });
192          downloadTask.on('progress', async (data1, data2) => {
193            if (flag == false){
194              flag = true
195              await downloadTask.pause();
196              downloadTask.off('progress');
197            }
198          });
199        }catch(err){
200          console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0003 error: " + err);
201          await downloadTask.remove();
202          fileio.unlinkSync(filePath);
203          done();
204        }
205      });
206    });
207
208    /**
209     * @tc.number    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004
210     * @tc.name    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004
211     * @tc.desc       Called when the current download session complete、pause or remove.
212     * @tc.size      : MEDIUM
213     * @tc.type      : Function
214     * @tc.level     : Level 2
215     */
216    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004', 0, async function (done) {
217      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004 is starting-----------------------");
218      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004.txt';
219      let flag = false;
220      let filePath = downloadConfig.filePath;
221      request.download(downloadConfig, async (err, downloadTask)=>{
222        console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004 downloadTask: " + downloadTask);
223        try{
224          expect(downloadTask != undefined).assertEqual(true);
225          downloadTask.on('remove', () => {
226            try{
227              console.info('====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004 remove remove')
228              expect(true).assertTrue();
229            }catch(err){
230              console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004 throw_error: " + err);
231            }
232            downloadTask.off('remove');
233            fileio.unlinkSync(filePath);
234            done();
235          });
236          downloadTask.on('progress', async (data1, data2) => {
237            if(flag == false){
238              flag = true;
239              await downloadTask.remove();
240              downloadTask.off('progress');
241            }
242          });
243        }catch(err){
244          console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0004 error: " + err);
245          await downloadTask.remove();
246          fileio.unlinkSync(filePath);
247          done();
248        }
249      });
250    });
251
252    /**
253     * @tc.number    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005
254     * @tc.name    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005
255     * @tc.desc      Called when the current download session fails.
256     * @tc.size      : MEDIUM
257     * @tc.type      : Function
258     * @tc.level     : Level 2
259     */
260    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005', 0, async function (done) {
261      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005 is starting-----------------------");
262      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005.txt';
263      downloadConfig.url = 'https://www.baidu.com/xxxxxx.jpg';
264      let flag = false;
265      let filePath = downloadConfig.filePath;
266      request.download(downloadConfig, async (err, downloadTask)=>{
267        console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005 downloadTask: " + downloadTask);
268        try{
269          expect(downloadTask != undefined).assertEqual(true);
270          downloadTask.on('fail', async (err) =>{
271            try{
272              console.info('====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005 fail fail' + err)
273              expect(true).assertTrue();
274            }catch(err){
275              console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005 throw_error: " + err);
276            }
277            downloadTask.off('fail');
278            await downloadTask.remove();
279            fileio.unlinkSync(filePath);
280            done();
281          })
282        }catch(err){
283          console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0005 error: " + err);
284          await downloadTask.remove();
285          fileio.unlinkSync(filePath);
286          done();
287        }
288      });
289    });
290
291    /**
292     * @tc.number    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0006
293     * @tc.name    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0006
294     * @tc.desc      Called when the current download session fails.
295     * @tc.size      : MEDIUM
296     * @tc.type      : Function
297     * @tc.level     : Level 2
298     */
299    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0006', 0, async function (done) {
300      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0006 is starting-----------------------");
301      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0006.txt';
302      let flag = false;
303      let filePath = downloadConfig.filePath;
304      request.download(downloadConfig, async (err, downloadTask)=>{
305        console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0006 downloadTask: " + downloadTask);
306        try{
307          expect(downloadTask != undefined).assertEqual(true);
308          downloadTask.on('complete', async () => {
309            try{
310              await request.download(downloadConfig);
311            }catch(err){
312              console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0006 second throw_error: " + err);
313              expect(err.code).assertEqual(undefined);
314              downloadTask.off('complete');
315              await downloadTask.remove();
316              fileio.unlinkSync(filePath);
317              done();
318            }
319          });
320        }catch(err){
321          console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_ON_0006 error: " + err);
322          await downloadTask.remove();
323          fileio.unlinkSync(filePath);
324          done();
325        }
326      });
327    });
328
329    /**
330     * @tc.number    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0001
331     * @tc.name    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0001
332     * @tc.desc      alled when the current download session is in process.
333     * @tc.size      : MEDIUM
334     * @tc.type      : Function
335     * @tc.level     : Level 2
336     */
337    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0001', 0, async function (done) {
338      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0001 is starting-----------------------");
339      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0001.txt';
340      let flag = false;
341      let filePath = downloadConfig.filePath;
342      request.download(downloadConfig, async (err, downloadTask)=>{
343        try{
344          console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0001 downloadTask: " + downloadTask);
345          expect(downloadTask != undefined).assertEqual(true);
346          downloadTask.on('progress', async (data1, data2) => {
347            downloadTask.off('progress');
348            await downloadTask.remove();
349            fileio.unlinkSync(filePath);
350            done();
351          });
352
353        }catch(err){
354          console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0001 error: " + err);
355          await downloadTask.remove();
356          fileio.unlinkSync(filePath);
357          done();
358        }
359      });
360    });
361
362    /**
363     * @tc.number    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0002
364     * @tc.name    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0002
365     * @tc.desc      alled when the current download session complete、pause or remove.
366     * @tc.size      : MEDIUM
367     * @tc.type      : Function
368     * @tc.level     : Level 2
369     */
370    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0002', 0, async function (done) {
371      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0002 is starting-----------------------");
372      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0002.txt';
373      let filePath = downloadConfig.filePath;
374      request.download(downloadConfig, async (err, downloadTask)=>{
375        console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0002 downloadTask: " + downloadTask);
376        try{
377          expect(downloadTask != undefined).assertEqual(true);
378          downloadTask.on('complete', () => {});
379          downloadTask.off('complete');
380          await downloadTask.remove();
381          fileio.unlinkSync(filePath);
382          done();
383        }catch(err){
384          console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0002 error: " + err);
385          await downloadTask.remove();
386          fileio.unlinkSync(filePath);
387          done();
388        }
389      });
390    });
391
392    /**
393     * @tc.number    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0003
394     * @tc.name    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0003
395     * @tc.desc      alled when the current download session complete、pause or remove.
396     * @tc.size      : MEDIUM
397     * @tc.type      : Function
398     * @tc.level     : Level 2
399     */
400    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0003', 0, async function (done) {
401      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0003 is starting-----------------------");
402      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0003.txt';
403      let filePath = downloadConfig.filePath;
404      request.download( downloadConfig, async (err, downloadTask)=>{
405        console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0003 downloadTask: " + downloadTask);
406        try{
407          expect(downloadTask != undefined).assertEqual(true);
408          downloadTask.on('pause', () => {});
409          downloadTask.off('pause');
410          await downloadTask.remove();
411          fileio.unlinkSync(filePath);
412          done();
413        }catch(err){
414          console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0003 error: " + err);
415          await downloadTask.remove();
416          fileio.unlinkSync(filePath);
417          done();
418        }
419      });
420    });
421
422    /**
423     * @tc.number    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0004
424     * @tc.name    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0004
425     * @tc.desc      alled when the current download session complete、pause or remove.
426     * @tc.size      : MEDIUM
427     * @tc.type      : Function
428     * @tc.level     : Level 2
429     */
430    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0004', 0, async function (done) {
431      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0004 is starting-----------------------");
432      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0004.txt';
433      let filePath = downloadConfig.filePath;
434      request.download( downloadConfig, async (err, downloadTask)=>{
435        console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0004 downloadTask: " + downloadTask);
436        try{
437          expect(downloadTask != undefined).assertEqual(true);
438          downloadTask.on('remove', () => {});
439          downloadTask.off('remove');
440          await downloadTask.remove();
441          fileio.unlinkSync(filePath);
442          done();
443        }catch(err){
444          console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0004 error: " + err);
445          await downloadTask.remove();
446          fileio.unlinkSync(filePath);
447          done();
448        }
449      });
450    });
451
452    /**
453     * @tc.number    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0005
454     * @tc.name    SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0005
455     * @tc.desc      Called when the current download session fails.
456     * @tc.size      : MEDIUM
457     * @tc.type      : Function
458     * @tc.level     : Level 2
459     */
460    it('SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0005', 0, async function (done) {
461      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0005 is starting-----------------------");
462      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0005.txt';
463      let filePath = downloadConfig.filePath;
464      request.download( downloadConfig, async (err, downloadTask)=>{
465        console.info("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0005 downloadTask: " + downloadTask);
466        try{
467          expect(downloadTask != undefined).assertEqual(true);
468          downloadTask.on('fail', (data) => {});
469          downloadTask.off('fail');
470          await downloadTask.remove();
471          fileio.unlinkSync(filePath);
472          done();
473        }catch(err){
474          console.error("====>SUB_REQUEST_DOWNLOAD_API_DOWNLOADTASK_OFF_0005 error: " + err);
475          await downloadTask.remove();
476          fileio.unlinkSync(filePath);
477          done();
478        }
479      });
480    });
481
482    /**
483     * @tc.number    SUB_REQUEST_DOWNLOAD_API_REMOVE_0001
484     * @tc.name    SUB_REQUEST_DOWNLOAD_API_REMOVE_0001
485     * @tc.desc      Deletes a download session and the downloaded files.
486     * @tc.size      : MEDIUM
487     * @tc.type      : Function
488     * @tc.level     : Level 2
489     */
490    it('SUB_REQUEST_DOWNLOAD_API_REMOVE_0001', 0, async function (done) {
491      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_REMOVE_0001 is starting-----------------------");
492      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_REMOVE_0001.txt';
493      let filePath = downloadConfig.filePath;
494      request.download( downloadConfig, async (err, downloadTask)=>{
495        console.info("====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0001 downloadTask: " + downloadTask);
496        try{
497          expect(downloadTask != undefined).assertEqual(true);
498          downloadTask.remove((err, data)=>{
499            try{
500              if(err) {
501                console.error('====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0001 Failed to remove the download task.');
502                expect().assertFail();
503              }
504              if (data) {
505                console.info('====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0001 removed data:' + JSON.stringify(data));
506                expect(data == true).assertTrue();
507              } else {
508                console.error('====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0001 Failed to remove the download task.');
509                expect().assertFail();
510              }
511            }catch(err){
512              console.info('====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0001 remove_throw_err:' + JSON.stringify(err))
513            }
514            fileio.unlinkSync(filePath);
515            done();
516          });
517        }catch(err){
518          console.error("====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0001 error: " + err);
519          await downloadTask.remove();
520          fileio.unlinkSync(filePath);
521          done();
522        }
523      })
524    });
525
526    /**
527     * @tc.number    SUB_REQUEST_DOWNLOAD_API_REMOVE_0002
528     * @tc.name    SUB_REQUEST_DOWNLOAD_API_REMOVE_0002
529     * @tc.desc      Deletes a download session and the downloaded files.
530     * @tc.size      : MEDIUM
531     * @tc.type      : Function
532     * @tc.level     : Level 2
533     */
534    it('SUB_REQUEST_DOWNLOAD_API_REMOVE_0002', 0, async function (done) {
535      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_REMOVE_0002 is starting-----------------------");
536      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_REMOVE_0002.txt';
537      let filePath = downloadConfig.filePath;
538      request.download( downloadConfig, async (err, downloadTask)=>{
539        console.info("====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0002 downloadTask: " + downloadTask);
540        try{
541          expect(downloadTask != undefined).assertEqual(true);
542          let data = await downloadTask.remove();
543          console.info('====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0002 removed data:' + JSON.stringify(data));
544          expect(data == true).assertTrue();
545          fileio.unlinkSync(filePath);
546          done();
547        }catch(err){
548          console.error("====>SUB_REQUEST_DOWNLOAD_API_REMOVE_0002 error: " + err);
549          await downloadTask.remove();
550          fileio.unlinkSync(filePath);
551          done();
552        }
553      })
554    });
555
556    /**
557     * @tc.number    SUB_REQUEST_DOWNLOAD_API_PAUSE_0001
558     * @tc.name    SUB_REQUEST_DOWNLOAD_API_PAUSE_0001
559     * @tc.desc      Pause a download session.
560     * @tc.size      : MEDIUM
561     * @tc.type      : Function
562     * @tc.level     : Level 2
563     */
564    it('SUB_REQUEST_DOWNLOAD_API_PAUSE_0001', 0, async function (done) {
565      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_PAUSE_0001 is starting-----------------------");
566      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_PAUSE_0001.txt';
567      let filePath = downloadConfig.filePath;
568      request.download( downloadConfig, async (err, downloadTask)=>{
569        console.info("====>SUB_REQUEST_DOWNLOAD_API_PAUSE_0001 downloadTask: " + downloadTask);
570        expect(downloadTask != undefined).assertEqual(true);
571        try{
572          downloadTask.pause(async ()=>{
573            try{
574              expect(true).assertTrue();
575              console.info('====>SUB_REQUEST_DOWNLOAD_API_PAUSE_0001 Download task pause success.');
576            }catch(err){
577              console.info('====>SUB_REQUEST_DOWNLOAD_API_PAUSE_0001 pause_throw_err:' + JSON.stringify(err))
578            }
579            await downloadTask.remove();
580            fileio.unlinkSync(filePath);
581            done();
582          })
583        }catch(err){
584          console.error("====>SUB_REQUEST_DOWNLOAD_API_PAUSE_0001 error: " + JSON.stringify(err));
585          await downloadTask.remove();
586          fileio.unlinkSync(filePath);
587          done();
588        }
589      })
590    });
591
592    /**
593     * @tc.number    SUB_REQUEST_DOWNLOAD_API_PAUSE_0002
594     * @tc.name    SUB_REQUEST_DOWNLOAD_API_PAUSE_0002
595     * @tc.desc      Pause a download session.
596     * @tc.size      : MEDIUM
597     * @tc.type      : Function
598     * @tc.level     : Level 2
599     */
600    it('SUB_REQUEST_DOWNLOAD_API_PAUSE_0002', 0, async function (done) {
601      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_PAUSE_0002 is starting-----------------------");
602      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_PAUSE_0002.txt';
603      let filePath = downloadConfig.filePath;
604      request.download( downloadConfig, async (err, downloadTask)=>{
605        console.info("====>SUB_REQUEST_DOWNLOAD_API_PAUSE_0002 downloadTask: " + downloadTask);
606        try{
607          expect(downloadTask != undefined).assertEqual(true);
608          await downloadTask.pause();
609          await downloadTask.remove()
610          fileio.unlinkSync(filePath);
611          done();
612        }catch(err){
613          console.error("====>SUB_REQUEST_DOWNLOAD_API_PAUSE_0002 error: " + JSON.stringify(err));
614          await downloadTask.remove()
615          fileio.unlinkSync(filePath);
616          done();
617        }
618      })
619    });
620
621    /**
622     * @tc.number    SUB_REQUEST_DOWNLOAD_API_REMUSE_0001
623     * @tc.name    SUB_REQUEST_DOWNLOAD_API_REMUSE_0001
624     * @tc.desc      Resume a paused download session.
625     * @tc.size      : MEDIUM
626     * @tc.type      : Function
627     * @tc.level     : Level 2
628     */
629    it('SUB_REQUEST_DOWNLOAD_API_REMUSE_0001', 0, async function (done) {
630      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_REMUSE_0001 is starting-----------------------");
631      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_REMUSE_0001.txt';
632      let filePath = downloadConfig.filePath;
633      request.download( downloadConfig, async (err, downloadTask)=>{
634        console.info("====>SUB_REQUEST_DOWNLOAD_API_REMUSE_0001 downloadTask: " + downloadTask);
635        expect(downloadTask != undefined).assertEqual(true);
636        try{
637          downloadTask.resume(async ()=>{
638            try{
639              expect(true).assertTrue();
640              console.info('====>SUB_REQUEST_DOWNLOAD_API_REMUSE_0001 Download task resume success.');
641            }catch(err){
642              console.info('====>SUB_REQUEST_DOWNLOAD_API_REMUSE_0001 pause_throw_err:' + JSON.stringify(err))
643            }
644            await downloadTask.remove();
645            fileio.unlinkSync(downloadConfig.filePath);
646            done();
647          })
648        }catch(err){
649          console.error("====>SUB_REQUEST_DOWNLOAD_API_REMUSE_0001 error: " + JSON.stringify(err));
650          await downloadTask.remove();
651          fileio.unlinkSync(downloadConfig.filePath);
652          done();
653        }
654      })
655    });
656
657    /**
658     * @tc.number    SUB_REQUEST_DOWNLOAD_API_REMUSE_0002
659     * @tc.name    SUB_REQUEST_DOWNLOAD_API_REMUSE_0002
660     * @tc.desc      Resume a paused download session.
661     * @tc.size      : MEDIUM
662     * @tc.type      : Function
663     * @tc.level     : Level 2
664     */
665    it('SUB_REQUEST_DOWNLOAD_API_REMUSE_0002', 0, async function (done) {
666      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_REMUSE_0002 is starting-----------------------");
667      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_REMUSE_0002.txt';
668      let filePath = downloadConfig.filePath;
669      request.download( downloadConfig, async (err, downloadTask)=>{
670        console.info("====>SUB_REQUEST_DOWNLOAD_API_REMUSE_0002 downloadTask: " + downloadTask);
671        try{
672          expect(downloadTask != undefined).assertEqual(true);
673          await downloadTask.resume();
674          await downloadTask.remove();
675          fileio.unlinkSync(filePath);
676          done();
677        }catch(err){
678          console.error("====>SUB_REQUEST_DOWNLOAD_API_REMUSE_0002 error: " + JSON.stringify(err));
679          await downloadTask.remove();
680          fileio.unlinkSync(filePath);
681          done();
682        }
683      })
684    });
685
686    /**
687     * @tc.number    SUB_REQUEST_DOWNLOAD_API_QUERY_0001
688     * @tc.name    SUB_REQUEST_DOWNLOAD_API_QUERY_0001
689     * @tc.desc      Queries download information of a session, which is defined in DownloadSession.DownloadInfo.
690     * @tc.size      : MEDIUM
691     * @tc.type      : Function
692     * @tc.level     : Level 2
693     */
694    it('SUB_REQUEST_DOWNLOAD_API_QUERY_0001', 0, async function (done) {
695      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_QUERY_0001 is starting-----------------------");
696      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_QUERY_0001.txt';
697      let filePath = downloadConfig.filePath;
698      request.download( downloadConfig, async (err, downloadTask)=>{
699        console.info("====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 downloadTask: " + downloadTask);
700        try{
701          expect(downloadTask != undefined).assertEqual(true);
702          downloadTask.query(async (err, downloadInfo)=>{
703            try{
704              if(err) {
705                console.error('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 Failed to query: ' + JSON.stringify(err));
706                expect().assertFail();
707              } else {
708                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: '+ downloadInfo.description);
709                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: '+ downloadInfo.downloadedBytes);
710                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: '+ downloadInfo.downloadId);
711                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: '+ downloadInfo.failedReason);
712                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: '+ downloadInfo.fileName);
713                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: '+ downloadInfo.filePath);
714                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: '+ downloadInfo.pausedReason);
715                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: '+ downloadInfo.status);
716                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: '+ downloadInfo.targetURI);
717                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: '+ downloadInfo.downloadTitle);
718                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 query info: '+ downloadInfo.downloadTotalBytes);
719                expect(true).assertTrue();
720              }
721            }catch(err){
722              console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 throw_error: " + JSON.stringify(err));
723            }
724            await downloadTask.remove();
725            fileio.unlinkSync(filePath);
726            done();
727          })
728        }catch(err){
729          console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERY_0001 error: " + JSON.stringify(err));
730          await downloadTask.remove();
731          fileio.unlinkSync(filePath);
732          done();
733        }
734      })
735    });
736
737    /**
738     * @tc.number    SUB_REQUEST_DOWNLOAD_API_QUERY_0002
739     * @tc.name    SUB_REQUEST_DOWNLOAD_API_QUERY_0002
740     * @tc.desc      Queries download information of a session, which is defined in DownloadSession.DownloadInfo.
741     * @tc.size      : MEDIUM
742     * @tc.type      : Function
743     * @tc.level     : Level 2
744     */
745    it('SUB_REQUEST_DOWNLOAD_API_QUERY_0002', 0, async function (done) {
746      console.info("====>-----------------------SUB_REQUEST_DOWNLOAD_API_QUERY_0002 is starting-----------------------");
747      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_QUERY_0002.txt';
748      let filePath = downloadConfig.filePath;
749      request.download( downloadConfig, async (err, downloadTask)=>{
750        console.info("====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 downloadTask: " + downloadTask);
751        try{
752          expect(downloadTask != undefined).assertEqual(true);
753          downloadTask.query().then(async (downloadInfo)=>{
754            try{
755              if(err) {
756                console.error('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 Failed to query: ' + JSON.stringify(err));
757                expect().assertFail();
758              } else {
759                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: '+ downloadInfo.description);
760                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: '+ downloadInfo.downloadedBytes);
761                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: '+ downloadInfo.downloadId);
762                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: '+ downloadInfo.failedReason);
763                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: '+ downloadInfo.fileName);
764                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: '+ downloadInfo.filePath);
765                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: '+ downloadInfo.pausedReason);
766                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: '+ downloadInfo.status);
767                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: '+ downloadInfo.targetURI);
768                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: '+ downloadInfo.downloadTitle);
769                console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 query info: '+ downloadInfo.downloadTotalBytes);
770                expect(true).assertTrue();
771              }
772              await downloadTask.remove();
773              fileio.unlinkSync(filePath);
774              done();
775            }catch(err){
776              console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 error: " + JSON.stringify(err));
777              await downloadTask.remove();
778              fileio.unlinkSync(filePath);
779              done();
780            }
781          }).catch(async (err)=>{
782            console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002  catch_error: " + JSON.stringify(err));
783            await downloadTask.remove();
784            fileio.unlinkSync(filePath);
785            expect().assertFail();
786            done();
787          })
788        }catch(err){
789          console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERY_0002 error: " + JSON.stringify(err));
790          await downloadTask.remove();
791          fileio.unlinkSync(downloadConfig.filePath);
792          done();
793        }
794      })
795    });
796
797    /**
798     * @tc.number    SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001
799     * @tc.name    SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001
800     * @tc.desc      Queries the MIME type of the download file.
801     * @tc.size      : MEDIUM
802     * @tc.type      : Function
803     * @tc.level     : Level 2
804     */
805    it('SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001', 0, async function (done) {
806      console.info("====>---------------------SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001 is starting---------------------");
807      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001.txt';
808      let flag = false;
809      let filePath = downloadConfig.filePath;
810      request.download( downloadConfig, async (err, downloadTask)=>{
811        console.info("====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001 downloadTask: " + downloadTask);
812        try{
813          expect(downloadTask != undefined).assertEqual(true);
814          downloadTask.on('progress', async (data1, data2)=>{
815            try {
816                if (data1 > 0 && flag == false) {
817                  flag = true;
818                  downloadTask.off('progress');
819                  downloadTask.queryMimeType(async (err, data) => {
820                    try {
821                      if (err) {
822                        console.error('====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001 return_err:' + JSON.stringify(err));
823                        fileio.unlinkSync(downloadConfig.filePath);
824                        expect().assertFail();
825                        done();
826                      }
827                      if (data) {
828                        console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001 data:' + JSON.stringify(data));
829                        expect(typeof data == "string").assertTrue();
830                        await downloadTask.remove();
831                        fileio.unlinkSync(filePath);
832                        done();
833                      } else {
834                        console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001 data_error: ");
835                        expect().assertFail();
836                      }
837                    } catch (err) {
838                      console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001 throw_error: " + JSON.stringify(err));
839                      await downloadTask.remove();
840                      fileio.unlinkSync(filePath);
841                      done();
842                    }
843                  });
844                }
845              } catch (err) {
846                console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001 error: " + JSON.stringify(err));
847                await downloadTask.remove();
848                fileio.unlinkSync(filePath);
849                done();
850              }
851          })
852        }catch(err){
853          console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0001 error: " + JSON.stringify(err));
854          await downloadTask.remove();
855          fileio.unlinkSync(filePath);
856          done();
857        }
858      })
859    });
860
861    /**
862     * @tc.number    SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002
863     * @tc.name    SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002
864     * @tc.desc      Queries the MIME type of the download file.
865     * @tc.size      : MEDIUM
866     * @tc.type      : Function
867     * @tc.level     : Level 2
868     */
869    it('SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002', 0, async function (done) {
870      console.info("====>-------------------SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002 is starting----------------------");
871      downloadConfig.filePath += 'SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002.txt';
872      let flag = false;
873      let filePath = downloadConfig.filePath;
874      request.download( downloadConfig, async (err, downloadTask)=>{
875        console.info("====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002 downloadTask: " + downloadTask);
876        try{
877          expect(downloadTask != undefined).assertEqual(true);
878          downloadTask.on('progress', async (data1, data2) => {
879            try{
880                if(data1 > 0 && flag == false){
881                  flag = true;
882                  downloadTask.off('progress');
883                  let data = await downloadTask.queryMimeType()
884                  console.info('====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002 Download task queryMimeType.');
885                  expect(typeof data == "string").assertTrue();
886                  downloadTask.off('progress');
887                  await downloadTask.remove();
888                  fileio.unlinkSync(filePath);
889                  done();
890                }
891              }catch(err){
892                console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002 throw_error: " + err);
893                await downloadTask.remove();
894                fileio.unlinkSync(filePath);
895                done();
896              }
897          })
898        }catch(err){
899          console.error("====>SUB_REQUEST_DOWNLOAD_API_QUERYMINETYPE_0002 error: " + JSON.stringify(err));
900          await downloadTask.remove();
901          fileio.unlinkSync(filePath);
902          done();
903        }
904      })
905    });
906  })
907}
908