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';
19
20var sumGetUnifiedDataTime = 0;
21var plainTextData = new UDC.UnifiedData();
22var date = new Date();
23
24const BASE_CONUT = 100;
25
26describe('PasteBoardDelayPerfJSTest', function () {
27    beforeAll(async function () {
28        console.info('beforeAll');
29    })
30
31    afterAll(async function () {
32        console.info('afterAll');
33    })
34
35    function computeAverageTime(startTime, baseCount, message) {
36        let endTime = date.getTime();
37        let averageTime = ((endTime - startTime) * 1000) / baseCount;
38        console.info(message + averageTime);
39    }
40
41    function computeAverageGetTime(baseCount, message) {
42        let averageTime = (sumGetUnifiedDataTime * 1000) / baseCount;
43        console.info(message + averageTime);
44    }
45
46    function getPlainTextData(dataType) {
47        let plainText = new UDC.PlainText();
48        plainText.details = {
49            Key: 'plainText',
50            Value: 'plainText',
51        };
52        plainText.textContent = 'textContent';
53        plainText.abstract = 'abstract';
54        plainTextData.addRecord(plainText);
55        return plainTextData;
56    }
57
58    /**
59     * @tc.name      setUnifiedData_delay_performance_test_001
60     * @tc.desc      setUnifiedData delay interface performance test
61     * @tc.type      PERFORMANCE
62     */
63    it('setUnifiedData_delay_performance_test_001', 0, async function (done) {
64        console.info('setUnifiedData_delay_performance_test_001 begin');
65        const systemPasteboard = pasteboard.getSystemPasteboard();
66        let startTime = date.getTime();
67        for (let index = 0; index < BASE_CONUT; ++index) {
68            plainTextData = new UDC.UnifiedData();
69            plainTextData.properties.getDelayData = getPlainTextData;
70            await systemPasteboard.setUnifiedData(plainTextData);
71        }
72        computeAverageTime(startTime, BASE_CONUT, "setUnifiedData_delay_performance_test_001 averageTime:");
73        done();
74        console.info('setUnifiedData_delay_performance_test_001 end');
75    })
76
77    /**
78     * @tc.name      getUnifiedData_delay_performance_test_001
79     * @tc.desc      getUnifiedData delay interface performance test
80     * @tc.type      PERFORMANCE
81     */
82    it('getUnifiedData_delay_performance_test_001', 0, async function (done) {
83        console.info('getUnifiedData_delay_performance_test_001 begin');
84        const systemPasteboard = pasteboard.getSystemPasteboard();
85        for (let index = 0; index < BASE_CONUT; ++index) {
86            plainTextData = new UDC.UnifiedData();
87            plainTextData.properties.getDelayData = getPlainTextData;
88            await systemPasteboard.setUnifiedData(plainTextData);
89            let beginGetTime = date.getTime();
90            let unifiedData = await systemPasteboard.getUnifiedData();
91            let endGetTime = date.getTime();
92            sumGetUnifiedDataTime += (endGetTime - beginGetTime);
93        }
94        computeAverageGetTime(BASE_CONUT, "getUnifiedData_delay_performance_test_001 averageTime:");
95        done();
96        console.info('getUnifiedData_delay_performance_test_001 end');
97    })
98});