1/*
2 * Copyright (C) 2024 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 { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'deccjsunit/index';
17import pasteboard from '@ohos.pasteboard';
18import UDC from '@ohos.data.unifiedDataChannel';
19import UTD from '@ohos.data.uniformTypeDescriptor'
20import image from '@ohos.multimedia.image';
21
22const KEY_TEST_ELEMENT = 'TestKey';
23const VALUE_TEST_ELEMENT = 'TestValue';
24const TEST_BUNDLE_NAME = 'MyBundleName';
25const TEST_ABILITY_NAME = 'MyAbilityName';
26
27let U8_ARRAY = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
28
29var data = new UDC.UnifiedData();
30
31describe('PasteBoardUdmfDelayJSTest', function () {
32    beforeAll(async function () {
33        console.info('beforeAll');
34    });
35
36    afterAll(async function () {
37        console.info('afterAll');
38    });
39
40    beforeEach(async function () {
41        console.info('beforeEach');
42        const systemPasteboard = pasteboard.getSystemPasteboard();
43        await systemPasteboard.clearData();
44        data = new UDC.UnifiedData();
45    });
46
47    afterEach(async function () {
48        console.info('afterEach');
49    });
50
51    function getTextData(dataType) {
52        let text = new UDC.Text();
53        text.details = {
54            Key: 'text' + KEY_TEST_ELEMENT,
55            Value: 'text' + VALUE_TEST_ELEMENT,
56        };
57        data.addRecord(text);
58        return data;
59    }
60
61    function getPlainTextData(dataType) {
62        let plainText = new UDC.PlainText();
63        plainText.details = {
64            Key: 'plainText' + KEY_TEST_ELEMENT,
65            Value: 'plainText' + VALUE_TEST_ELEMENT,
66        };
67        plainText.textContent = 'textContent';
68        plainText.abstract = 'abstract';
69        data.addRecord(plainText);
70        return data;
71    }
72
73    function getHyperlinkData(dataType) {
74        let link = new UDC.Hyperlink();
75        link.details = {
76            Key: 'hyperLink' + KEY_TEST_ELEMENT,
77            Value: 'hyperLink' + VALUE_TEST_ELEMENT,
78        };
79        link.url = 'url';
80        link.description = 'description';
81        data.addRecord(link);
82        return data;
83    }
84
85    function getHtmlData(dataType) {
86        let html = new UDC.HTML();
87        html.details = {
88            Key: 'html' + KEY_TEST_ELEMENT,
89            Value: 'html' + VALUE_TEST_ELEMENT,
90        };
91        html.htmlContent = 'htmlContent';
92        html.plainContent = 'plainContent';
93        data.addRecord(html);
94        return data;
95    }
96
97    function getFileData(dataType) {
98        let file = new UDC.File();
99        file.details = {
100            Key: 'file' + KEY_TEST_ELEMENT,
101            Value: 'file' + VALUE_TEST_ELEMENT,
102        };
103        file.uri = 'uri';
104        data.addRecord(file);
105        return data;
106    }
107
108    function getFolderData(dataType) {
109        let folder = new UDC.Folder();
110        folder.details = {
111            Key: 'folder' + KEY_TEST_ELEMENT,
112            Value: 'folder' + VALUE_TEST_ELEMENT,
113        };
114        folder.uri = 'folderUri';
115        data.addRecord(folder);
116        return data;
117    }
118
119    function getImageData(dataType) {
120        let image = new UDC.Image();
121        image.details = {
122            Key: 'image' + KEY_TEST_ELEMENT,
123            Value: 'image' + VALUE_TEST_ELEMENT,
124        };
125        image.imageUri = 'imageUri';
126        data.addRecord(image);
127        return data;
128    }
129
130    function getVideoData(dataType) {
131        let video = new UDC.Video();
132        video.details = {
133            Key: 'video' + KEY_TEST_ELEMENT,
134            Value: 'video' + VALUE_TEST_ELEMENT,
135        };
136        video.videoUri = 'videoUri';
137        data.addRecord(video);
138        return data;
139    }
140
141    function getAudioData(dataType) {
142        let audio = new UDC.Audio();
143        audio.details = {
144            Key: 'audio' + KEY_TEST_ELEMENT,
145            Value: 'audio' + VALUE_TEST_ELEMENT,
146        };
147        audio.audioUri = 'audioUri';
148        data.addRecord(audio);
149        return data;
150    }
151
152    function getSystemDefinedAppItemDataData(dataType) {
153        let appItem = new UDC.SystemDefinedAppItem();
154        appItem.appId = 'MyAppId';
155        appItem.appName = 'MyAppName';
156        appItem.abilityName = TEST_ABILITY_NAME;
157        appItem.bundleName = TEST_BUNDLE_NAME;
158        appItem.appIconId = 'MyAppIconId';
159        appItem.appLabelId = 'MyAppLabelId';
160        appItem.details = {
161            appItemKey1: 1,
162            appItemKey2: 'appItem' + VALUE_TEST_ELEMENT,
163            appItemKey3: U8_ARRAY,
164        };
165        data.addRecord(appItem);
166        return data;
167    }
168
169    function getApplicationDefinedRecordData(dataType) {
170        let applicationDefinedRecord = new UDC.ApplicationDefinedRecord();
171        applicationDefinedRecord.applicationDefinedType = 'applicationDefinedType';
172        applicationDefinedRecord.rawData = U8_ARRAY;
173        data.addRecord(applicationDefinedRecord);
174        return data;
175    }
176
177    function getWantData(dataType) {
178        let object = {
179            bundleName: 'bundleName',
180            abilityName: 'abilityName'
181        }
182        let wantRecord = new UDC.UnifiedRecord(UTD.UniformDataType.OPENHARMONY_WANT, object);
183        data.addRecord(wantRecord);
184        return data;
185    }
186
187    function getPixelMapData(dataType) {
188        const buffer = new ArrayBuffer(128);
189        const opt = {
190            size: { height: 5, width: 5 },
191            pixelFormat: 3,
192            editable: true,
193            alphaType: 1,
194            scaleMode: 1,
195        };
196        const pixelMap = image.createPixelMapSync(buffer, opt);
197        let pixelMapRecord = new UDC.UnifiedRecord(UTD.UniformDataType.OPENHARMONY_PIXEL_MAP, pixelMap);
198        data.addRecord(pixelMapRecord);
199        return data;
200    }
201
202    /**
203     * @tc.name TextTest001
204     * @tc.desc Test Unified Record of Text
205     * @tc.type FUNC
206     */
207    it('TextTest001', 0, async function (done) {
208        let properties = new UDC.UnifiedDataProperties();
209        properties.getDelayData = getTextData;
210        data.properties = properties;
211        const systemPasteboard = pasteboard.getSystemPasteboard();
212        await systemPasteboard.setUnifiedData(data);
213        const flag = await systemPasteboard.hasPasteData();
214        expect(flag).assertEqual(true);
215        let unifiedData = await systemPasteboard.getUnifiedData();
216        let records = unifiedData.getRecords();
217        expect(records.length).assertEqual(1);
218        expect(records[0].details.Key).assertEqual('text' + KEY_TEST_ELEMENT);
219        expect(records[0].details.Value).assertEqual('text' + VALUE_TEST_ELEMENT);
220        let pasteData = await systemPasteboard.getData();
221        expect(pasteData.getRecordCount()).assertEqual(1);
222        expect(pasteData.getPrimaryMimeType()).assertEqual('general.text');
223        done();
224        console.info('TextTest001 end');
225    });
226
227    /**
228     * @tc.name PlainTextTest001
229     * @tc.desc Test Unified Record of Plain Text
230     * @tc.type FUNC
231     */
232    it('PlainTextTest001', 0, async function (done) {
233        console.info('PlainTextTest001 begin');
234        let properties = new UDC.UnifiedDataProperties();
235        properties.getDelayData = getPlainTextData;
236        data.properties = properties;
237        const systemPasteboard = pasteboard.getSystemPasteboard();
238        await systemPasteboard.setUnifiedData(data);
239        const flag = await systemPasteboard.hasPasteData();
240        expect(flag).assertEqual(true);
241        let unifiedData = await systemPasteboard.getUnifiedData();
242        let records = unifiedData.getRecords();
243        expect(records.length).assertEqual(1);
244        expect(records[0].details.Key).assertEqual('plainText' + KEY_TEST_ELEMENT);
245        expect(records[0].details.Value).assertEqual('plainText' + VALUE_TEST_ELEMENT);
246        expect(records[0].textContent).assertEqual('textContent');
247        expect(records[0].abstract).assertEqual('abstract');
248        let pasteData = await systemPasteboard.getData();
249        expect(pasteData.getRecordCount()).assertEqual(1);
250        expect(pasteData.getPrimaryMimeType()).assertEqual(pasteboard.MIMETYPE_TEXT_PLAIN);
251        const primaryText = pasteData.getPrimaryText();
252        expect(primaryText).assertEqual('textContent');
253        done();
254        console.info('PlainTextTest001 end');
255    });
256
257    /**
258     * @tc.name HyperlinkTest001
259     * @tc.desc Test Unified Record of Hyper Link
260     * @tc.type FUNC
261     */
262    it('HyperlinkTest001', 0, async function (done) {
263        console.info('HyperlinkTest001 begin');
264        let properties = new UDC.UnifiedDataProperties();
265        properties.getDelayData = getHyperlinkData;
266        data.properties = properties;
267        const systemPasteboard = pasteboard.getSystemPasteboard();
268        await systemPasteboard.setUnifiedData(data);
269        const flag = await systemPasteboard.hasPasteData();
270        expect(flag).assertEqual(true);
271        let unifiedData = await systemPasteboard.getUnifiedData();
272        let records = unifiedData.getRecords();
273        expect(records.length).assertEqual(1);
274        expect(records[0].details.Key).assertEqual('hyperLink' + KEY_TEST_ELEMENT);
275        expect(records[0].details.Value).assertEqual('hyperLink' + VALUE_TEST_ELEMENT);
276        expect(records[0].url).assertEqual('url');
277        expect(records[0].description).assertEqual('description');
278        let pasteData = await systemPasteboard.getData();
279        expect(pasteData.getRecordCount()).assertEqual(1);
280        expect(pasteData.getPrimaryMimeType()).assertEqual(pasteboard.MIMETYPE_TEXT_PLAIN);
281        const primaryText = pasteData.getPrimaryText();
282        expect(primaryText).assertEqual('url');
283        done();
284        console.info('HyperlinkTest001 end');
285    });
286
287    /**
288     * @tc.name HtmlTest001
289     * @tc.desc Test Unified Record of Html
290     * @tc.type FUNC
291     */
292    it('HtmlTest001', 0, async function (done) {
293        console.info('HtmlTest001 begin');
294        let properties = new UDC.UnifiedDataProperties();
295        properties.getDelayData = getHtmlData;
296        data.properties = properties;
297        const systemPasteboard = pasteboard.getSystemPasteboard();
298        await systemPasteboard.setUnifiedData(data);
299        const flag = await systemPasteboard.hasPasteData();
300        expect(flag).assertEqual(true);
301        let unifiedData = await systemPasteboard.getUnifiedData();
302        let records = unifiedData.getRecords();
303        expect(records.length).assertEqual(1);
304        expect(records[0].details.Key).assertEqual('html' + KEY_TEST_ELEMENT);
305        expect(records[0].details.Value).assertEqual('html' + VALUE_TEST_ELEMENT);
306        expect(records[0].htmlContent).assertEqual('htmlContent');
307        expect(records[0].plainContent).assertEqual('plainContent');
308        let pasteData = await systemPasteboard.getData();
309        expect(pasteData.getRecordCount()).assertEqual(1);
310        expect(pasteData.getPrimaryMimeType()).assertEqual(pasteboard.MIMETYPE_TEXT_HTML);
311        const primaryHtml = pasteData.getPrimaryHtml();
312        expect(primaryHtml).assertEqual('htmlContent');
313        done();
314        console.info('HtmlTest001 end');
315    });
316
317    /**
318     * @tc.name FileTest001
319     * @tc.desc Test Unified Record of File
320     * @tc.type FUNC
321     */
322    it('FileTest001', 0, async function (done) {
323        console.info('FileTest001 begin');
324        let properties = new UDC.UnifiedDataProperties();
325        properties.getDelayData = getFileData;
326        data.properties = properties;
327        const systemPasteboard = pasteboard.getSystemPasteboard();
328        await systemPasteboard.setUnifiedData(data);
329        const flag = await systemPasteboard.hasPasteData();
330        expect(flag).assertEqual(true);
331        let unifiedData = await systemPasteboard.getUnifiedData();
332        let records = unifiedData.getRecords();
333        expect(records.length).assertEqual(1);
334        expect(records[0].details.Key).assertEqual('file' + KEY_TEST_ELEMENT);
335        expect(records[0].details.Value).assertEqual('file' + VALUE_TEST_ELEMENT);
336        expect(records[0].uri).assertEqual('uri');
337        done();
338        let pasteData = await systemPasteboard.getData();
339        expect(pasteData.getRecordCount()).assertEqual(1);
340        expect(pasteData.hasMimeType(pasteboard.MIMETYPE_TEXT_URI)).assertEqual(true);
341        const primaryUri = pasteData.getPrimaryUri();
342        expect(primaryUri).assertEqual('uri');
343        done();
344        console.info('FileTest001 end');
345    });
346
347    /**
348     * @tc.name FolderTest001
349     * @tc.desc Test Unified Record of Folder
350     * @tc.type FUNC
351     */
352    it('FolderTest001', 0, async function (done) {
353        console.info('FolderTest001 begin');
354        let properties = new UDC.UnifiedDataProperties();
355        properties.getDelayData = getFolderData;
356        data.properties = properties;
357        const systemPasteboard = pasteboard.getSystemPasteboard();
358        await systemPasteboard.setUnifiedData(data);
359        const flag = await systemPasteboard.hasPasteData();
360        expect(flag).assertEqual(true);
361        let unifiedData = await systemPasteboard.getUnifiedData();
362        let records = unifiedData.getRecords();
363        expect(records.length).assertEqual(1);
364        expect(records[0].details.Key).assertEqual('folder' + KEY_TEST_ELEMENT);
365        expect(records[0].details.Value).assertEqual('folder' + VALUE_TEST_ELEMENT);
366        expect(records[0].uri).assertEqual('folderUri');
367        let pasteData = await systemPasteboard.getData();
368        expect(pasteData.getRecordCount()).assertEqual(1);
369        expect(pasteData.hasMimeType(pasteboard.MIMETYPE_TEXT_URI)).assertEqual(true);
370        const primaryUri = pasteData.getPrimaryUri();
371        expect(primaryUri).assertEqual('folderUri');
372        done();
373        console.info('FolderTest001 end');
374    });
375
376    /**
377     * @tc.name ImageTest001
378     * @tc.desc Test Unified Record of Image
379     * @tc.type FUNC
380     */
381    it('ImageTest001', 0, async function (done) {
382        console.info('ImageTest001 begin');
383        let properties = new UDC.UnifiedDataProperties();
384        properties.getDelayData = getImageData;
385        data.properties = properties;
386        const systemPasteboard = pasteboard.getSystemPasteboard();
387        await systemPasteboard.setUnifiedData(data);
388        const flag = await systemPasteboard.hasPasteData();
389        expect(flag).assertEqual(true);
390        let unifiedData = await systemPasteboard.getUnifiedData();
391        let records = unifiedData.getRecords();
392        expect(records.length).assertEqual(1);
393        expect(records[0].details.Key).assertEqual('image' + KEY_TEST_ELEMENT);
394        expect(records[0].details.Value).assertEqual('image' + VALUE_TEST_ELEMENT);
395        expect(records[0].imageUri).assertEqual('imageUri');
396        let pasteData = await systemPasteboard.getData();
397        expect(pasteData.getRecordCount()).assertEqual(1);
398        expect(pasteData.hasMimeType(pasteboard.MIMETYPE_TEXT_URI)).assertEqual(true);
399        const primaryUri = pasteData.getPrimaryUri();
400        expect(primaryUri).assertEqual('imageUri');
401        done();
402        console.info('ImageTest001 end');
403    });
404
405    /**
406     * @tc.name VideoTest001
407     * @tc.desc Test Unified Record of Video
408     * @tc.type FUNC
409     */
410    it('VideoTest001', 0, async function (done) {
411        console.info('VideoTest001 begin');
412        let properties = new UDC.UnifiedDataProperties();
413        properties.getDelayData = getVideoData;
414        data.properties = properties;
415        const systemPasteboard = pasteboard.getSystemPasteboard();
416        await systemPasteboard.setUnifiedData(data);
417        const flag = await systemPasteboard.hasPasteData();
418        expect(flag).assertEqual(true);
419        let unifiedData = await systemPasteboard.getUnifiedData();
420        let records = unifiedData.getRecords();
421        expect(records.length).assertEqual(1);
422        expect(records[0].details.Key).assertEqual('video' + KEY_TEST_ELEMENT);
423        expect(records[0].details.Value).assertEqual('video' + VALUE_TEST_ELEMENT);
424        expect(records[0].videoUri).assertEqual('videoUri');
425        let pasteData = await systemPasteboard.getData();
426        expect(pasteData.getRecordCount()).assertEqual(1);
427        expect(pasteData.hasMimeType(pasteboard.MIMETYPE_TEXT_URI)).assertEqual(true);
428        const primaryUri = pasteData.getPrimaryUri();
429        expect(primaryUri).assertEqual('videoUri');
430        done();
431        console.info('VideoTest001 end');
432    });
433
434    /**
435     * @tc.name AudioTest001
436     * @tc.desc Test Unified Record of Audio
437     * @tc.type FUNC
438     */
439    it('AudioTest001', 0, async function (done) {
440        console.info('AudioTest001 begin');
441        let properties = new UDC.UnifiedDataProperties();
442        properties.getDelayData = getAudioData;
443        data.properties = properties;
444        const systemPasteboard = pasteboard.getSystemPasteboard();
445        await systemPasteboard.setUnifiedData(data);
446        const flag = await systemPasteboard.hasPasteData();
447        expect(flag).assertEqual(true);
448        let unifiedData = await systemPasteboard.getUnifiedData();
449        let records = unifiedData.getRecords();
450        expect(records.length).assertEqual(1);
451        expect(records[0].details.Key).assertEqual('audio' + KEY_TEST_ELEMENT);
452        expect(records[0].details.Value).assertEqual('audio' + VALUE_TEST_ELEMENT);
453        expect(records[0].audioUri).assertEqual('audioUri');
454        let pasteData = await systemPasteboard.getData();
455        expect(pasteData.getRecordCount()).assertEqual(1);
456        expect(pasteData.hasMimeType(pasteboard.MIMETYPE_TEXT_URI)).assertEqual(true);
457        const primaryUri = pasteData.getPrimaryUri();
458        expect(primaryUri).assertEqual('audioUri');
459        done();
460        console.info('AudioTest001 end');
461    });
462
463    /**
464     * @tc.name SystemDefinedAppItemTest001
465     * @tc.desc Test Unified Record of SystemDefinedAppItem
466     * @tc.type FUNC
467     */
468    it('SystemDefinedAppItemTest001', 0, async function (done) {
469        console.info('SystemDefinedAppItemTest001 begin');
470        let properties = new UDC.UnifiedDataProperties();
471        properties.getDelayData = getSystemDefinedAppItemDataData;
472        data.properties = properties;
473        const systemPasteboard = pasteboard.getSystemPasteboard();
474        await systemPasteboard.setUnifiedData(data);
475        const flag = await systemPasteboard.hasPasteData();
476        expect(flag).assertEqual(true);
477        let unifiedData = await systemPasteboard.getUnifiedData();
478        let records = unifiedData.getRecords();
479        expect(records.length).assertEqual(1);
480        let pasteData = await systemPasteboard.getData();
481        expect(pasteData.getRecordCount()).assertEqual(1);
482        expect(pasteData.getPrimaryMimeType()).assertEqual('openharmony.app-item');
483        done();
484        console.info('SystemDefinedAppItemTest001 end');
485    });
486
487    /**
488     * @tc.name ApplicationDefinedRecordTest001
489     * @tc.desc Test Unified Record of ApplicationDefinedRecord
490     * @tc.type FUNC
491     */
492    it('ApplicationDefinedRecordTest001', 0, async function (done) {
493        console.info('ApplicationDefinedRecordTest001 begin');
494        let properties = new UDC.UnifiedDataProperties();
495        properties.getDelayData = getApplicationDefinedRecordData;
496        data.properties = properties;
497        const systemPasteboard = pasteboard.getSystemPasteboard();
498        await systemPasteboard.setUnifiedData(data);
499        const flag = await systemPasteboard.hasPasteData();
500        expect(flag).assertEqual(true);
501        let unifiedData = await systemPasteboard.getUnifiedData();
502        let records = unifiedData.getRecords();
503        expect(records.length).assertEqual(1);
504        expect(records[0].applicationDefinedType).assertEqual('applicationDefinedType');
505        for (let i = 0; i < U8_ARRAY.length; i++) {
506            expect(records[0].rawData[i]).assertEqual(U8_ARRAY[i]);
507        }
508        let pasteData = await systemPasteboard.getData();
509        expect(pasteData.getRecordCount()).assertEqual(1);
510        expect(pasteData.getPrimaryMimeType()).assertEqual('applicationDefinedType');
511        done();
512        console.info('ApplicationDefinedRecordTest001 end');
513    });
514
515    /**
516     * @tc.name WantTest001
517     * @tc.desc Test Unified Record of Want
518     * @tc.type FUNC
519     */
520    it('WantTest001', 0, async function (done) {
521        console.info('WantTest001 begin');
522        let properties = new UDC.UnifiedDataProperties();
523        properties.getDelayData = getWantData;
524        data.properties = properties;
525        const systemPasteboard = pasteboard.getSystemPasteboard();
526        await systemPasteboard.setUnifiedData(data);
527        const flag = await systemPasteboard.hasPasteData();
528        expect(flag).assertEqual(true);
529        let unifiedData = await systemPasteboard.getUnifiedData();
530        let records = unifiedData.getRecords();
531        expect(records.length).assertEqual(1);
532        let pasteData = await systemPasteboard.getData();
533        expect(pasteData.getRecordCount()).assertEqual(1);
534        expect(pasteData.getPrimaryMimeType()).assertEqual('text/want');
535        done();
536        console.info('WantTest001 end');
537    });
538
539    /**
540     * @tc.name PixelMapTest001
541     * @tc.desc Test Unified Record of PixelMap
542     * @tc.type FUNC
543     */
544    it('PixelMapTest001', 0, async function (done) {
545        console.info('PixelMapTest001 begin');
546        let properties = new UDC.UnifiedDataProperties();
547        properties.getDelayData = getPixelMapData;
548        data.properties = properties;
549        const systemPasteboard = pasteboard.getSystemPasteboard();
550        await systemPasteboard.setUnifiedData(data);
551        const flag = await systemPasteboard.hasPasteData();
552        expect(flag).assertEqual(true);
553        let unifiedData = await systemPasteboard.getUnifiedData();
554        let records = unifiedData.getRecords();
555        expect(records.length).assertEqual(1);
556        let pasteData = await systemPasteboard.getData();
557        expect(pasteData.getRecordCount()).assertEqual(1);
558        expect(pasteData.getPrimaryMimeType()).assertEqual('pixelMap');
559        done();
560        console.info('PixelMapTest001 end');
561    });
562});