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 resultSharePath = "/data/storage/el2/share/r/com.example.filesharea/data/storage/el2/base/files/getpathtest002.txt";
104            let resultPath = "/storage/Users/currentUser/appdata/el2/base/com.example.filesharea/files/getpathtest002.txt";
105            let fileUriObject = new fileuri.FileUri(uri);
106            let realPath = fileUriObject.path;
107            console.info(`getPathFromUri success: ${JSON.stringify(realPath)}`);
108            expect(resultSharePath == realPath || realPath == resultPath).assertTrue();
109        } catch (error) {
110            console.error(`getPathFromUri failed:${JSON.stringify(error)}`);
111            expect(false).assertTrue();
112        }
113        console.info(`get_path_from_uri_test_002 end`);
114    })
115
116    /**
117     * @tc.name:      get_path_from_uri_test_003
118     * @tc.desc:      get path from uri with different app test
119     * @tc.type:      FUNC test
120     * @tc.require:   issueI#I9010V
121     */
122    it('get_path_from_uri_test_003', 0, async function () {
123        console.info(`get_path_from_uri_test_003 start`);
124        try {
125            let uri = "file://com.example.demo/data/storage/el2/base/files/test002.txt";
126            let resultSharePath = "/data/storage/el2/share/r/com.example.demo/data/storage/el2/base/files/test002.txt";
127            let resultPath = "/storage/Users/currentUser/appdata/el2/base/com.example.demo/files/test002.txt";
128            let fileUriObject = new fileuri.FileUri(uri);
129            let realPath = fileUriObject.path;
130            console.info(`getPathFromUri success: ${JSON.stringify(realPath)}`);
131            expect(resultSharePath == realPath || realPath == resultPath).assertTrue();
132        } catch (error) {
133            console.error(`getPathFromUri failed:${JSON.stringify(error)}`);
134            expect(false).assertTrue();
135        }
136        console.info(`get_path_from_uri_test_003 end`);
137    })
138});