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, afterAll, it, expect} from 'deccjsunit/index'
17import fileuri from '@ohos.file.fileuri'
18import deviceInfo from '@ohos.deviceInfo'
19
20describe('FileShareJSTest', function () {
21    beforeAll(async function () {
22        console.info('beforeAll');
23    })
24
25    afterAll(async function () {
26        console.info('afterAll');
27    })
28
29    /**
30     * @tc.name:      is_remote_uri_test_001
31     * @tc.desc:      it is a remote URI test
32     * @tc.type:      FUNC test
33     * @tc.require:   issueI#I9010V
34     */
35    it('is_remote_uri_test_001', 0, async function () {
36        console.info(`is_remote_uri_test_001 start`);
37        try {
38            let uri = "file://com.demo.a/data/storage/el2/distributedfiles/.remote_share/data/haps/entry/files/1.txt";
39            uri += "?networkid=64799ecdf70788e396f454ff4a6e6ae4b09e20227c39c21f6e67a2aacbcef7b9";
40            let fileUriObject = new fileuri.FileUri(uri);
41            let ret = fileUriObject.isRemoteUri();
42            expect(ret).assertTrue();
43        } catch (error) {
44            console.error(`IsRemoteUri Judge failed because: ${JSON.stringify(error)}`);
45            expect(false).assertTrue();
46        }
47        console.info(`is_remote_uri_test_001 end`);
48    })
49
50    /**
51     * @tc.name:      is_remote_uri_test_002
52     * @tc.desc:      it is not a remote URI test
53     * @tc.type:      FUNC test
54     * @tc.require:   issueI#I9010V
55     */
56    it('is_remote_uri_test_002', 0, async function () {
57        console.info(`is_remote_uri_test_002 start`);
58        try {
59            let uri = "file://com.demo.a/data/storage/el2/distributedfiles/.remote_share/data/haps/entry/files/2.txt";
60            let fileUriObject = new fileuri.FileUri(uri);
61            let ret = fileUriObject.isRemoteUri();
62            console.log(`IsRemoteUri Judge result: ${JSON.stringify(ret)}`);
63            expect(ret == false).assertTrue();
64        } catch (error) {
65            console.error(`IsRemoteUri Judge failed because: ${JSON.stringify(error)}`);
66            expect(false).assertTrue();
67        }
68        console.info(`is_remote_uri_test_002 end`);
69    })
70
71    /**
72     * @tc.name:      get_path_from_uri_test_001
73     * @tc.desc:      get path from uri with same app test
74     * @tc.type:      FUNC test
75     * @tc.require:   issueI#I9010V
76     */
77    it('get_path_from_uri_test_001', 0, async function () {
78        console.info(`get_path_from_uri_test_001 start`);
79        try {
80            let uri = "file://ohos.file.fileuri.test/data/storage/el2/base/files/getpathtest001.txt";
81            let resultPath = "/data/storage/el2/base/files/getpathtest001.txt";
82            let fileUriObject = new fileuri.FileUri(uri);
83            let realPath = fileUriObject.path;
84            console.info(`getPathFromUri success: ${JSON.stringify(realPath)}`);
85            expect(resultPath == realPath).assertTrue();
86        } catch (error) {
87            console.error(`getPathFromUri failed:${JSON.stringify(error)}`);
88            expect(false).assertTrue();
89        }
90        console.info(`get_path_from_uri_test_001 end`);
91    })
92
93    /**
94     * @tc.name:      get_path_from_uri_test_002
95     * @tc.desc:      get path from uri with different app test
96     * @tc.type:      FUNC test
97     * @tc.require:   issueI#I9010V
98     */
99    it('get_path_from_uri_test_002', 0, async function () {
100        console.info(`get_path_from_uri_test_002 start`);
101        try {
102            let uri = "file://com.example.filesharea/data/storage/el2/base/files/getpathtest002.txt";
103            let resultPath = "/data/storage/el2/share/r/com.example.filesharea/data/storage/el2/base/files/getpathtest002.txt";
104            let fileUriObject = new fileuri.FileUri(uri);
105            let realPath = fileUriObject.path;
106            console.info(`getPathFromUri success: ${JSON.stringify(realPath)}`);
107            expect(resultPath == realPath).assertTrue();
108        } catch (error) {
109            console.error(`getPathFromUri failed:${JSON.stringify(error)}`);
110            expect(false).assertTrue();
111        }
112        console.info(`get_path_from_uri_test_002 end`);
113    })
114
115    /**
116     * @tc.name:      get_path_from_uri_test_003
117     * @tc.desc:      get path from uri with different app test
118     * @tc.type:      FUNC test
119     * @tc.require:   issueI#I9010V
120     */
121    it('get_path_from_uri_test_003', 0, async function () {
122        console.info(`get_path_from_uri_test_003 start`);
123        try {
124            let uri = "file://docs/storage/Users/currentUser/Documents/getpathtest003.txt";
125            if (deviceInfo.deviceType != '2in1') {
126                let resultPath = "/data/storage/el2/share/r/docs/storage/Users/currentUser/Documents/getpathtest003.txt";
127                let fileUriObject = new fileuri.FileUri(uri);
128                let realPath = fileUriObject.path;
129                console.info(`getPathFromUri success: ${JSON.stringify(realPath)}`);
130                expect(resultPath == realPath).assertTrue();
131            } else {
132                let resultPath = "/storage/Users/currentUser/Documents/getpathtest003.txt";
133                let fileUriObject = new fileuri.FileUri(uri);
134                let realPath = fileUriObject.path;
135                console.info(`getPathFromUri success: ${JSON.stringify(realPath)}`);
136                expect(resultPath == realPath).assertTrue();
137            }
138        } catch (error) {
139            console.error(`getPathFromUri failed:${JSON.stringify(error)}`);
140            expect(false).assertTrue();
141        }
142        console.info(`get_path_from_uri_test_003 end`);
143    })
144});