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