diff --git a/interfaces/test/unittest/BUILD.gn b/interfaces/test/unittest/BUILD.gn index 341f3526966017a1d054cc69688d3e481a9ae9d4..3afc2bb84ea2b5911b54ec76494ec4388d988b27 100644 --- a/interfaces/test/unittest/BUILD.gn +++ b/interfaces/test/unittest/BUILD.gn @@ -17,5 +17,6 @@ group("unittest") { "class_file:class_file_test", "filemgmt_libn_test:filemgmt_libn_test", "remote_uri:remote_uri_test", + "environment:EnvironmentJSTest" ] } diff --git a/interfaces/test/unittest/environment/BUILD.gn b/interfaces/test/unittest/environment/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0211fb528c70e14220eb7dcf88febdb1a93cf59f --- /dev/null +++ b/interfaces/test/unittest/environment/BUILD.gn @@ -0,0 +1,21 @@ +# Copyright (C) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import("//build/test.gni") + +ohos_js_unittest("EnvironmentJSTest") { + module_out_path = "filemanagement/file_api" + + hap_profile = "./config.json" + + certificate_profile = "openharmony_sx.p7b" +} diff --git a/interfaces/test/unittest/environment/Environment.test.js b/interfaces/test/unittest/environment/Environment.test.js new file mode 100644 index 0000000000000000000000000000000000000000..c2d2e0717b5c4ce9dfb24f31e1014d917a610ace --- /dev/null +++ b/interfaces/test/unittest/environment/Environment.test.js @@ -0,0 +1,145 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the 'License'); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an 'AS IS' BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import {describe, beforeAll, afterAll, it, expect} from 'deccjsunit/index' +import environment from '@ohos.file.environment' + +const PC_STORAGE_PATH = "/storage/Users/"; +const EXTERNAL_STORAGE_PATH = "/storage/External"; +const DOWNLOAD_PATH = "/Download"; +const DESKTOP_PATH = "/Desktop"; +const DOCUMENTS_PATH = "/Documents"; +const DEVICE_TYPE = 801; + +describe('EnvironmentJSTest', function () { + beforeAll(async function () { + console.info('beforeAll'); + }) + + afterAll(async function () { + console.info('afterAll'); + }) + + /** + * @tc.name: get_user_download_dir_test_001 + * @tc.desc: get sandbox path to the public download directory test + * @tc.type: FUNC test + * @tc.require: issueI#I8KE59 + */ + it('get_user_download_dir_test_001', 0, async function () { + console.info(`getUserDownloadDirTest001 start`); + try { + let path = environment.getUserDownloadDir(); + let result = true; + if (!path.startsWith(PC_STORAGE_PATH) || !path.endsWith(DOWNLOAD_PATH)) { + result = false; + } + let userNameStr = path.substring(PC_STORAGE_PATH.length, path.length - DOWNLOAD_PATH.length); + let userName = Number(userNameStr); + if (!Number.isInteger(userName) && userName >= 0) { + result = false; + } + expect(result).assertEqual(true); + } catch (error) { + console.info(`getUserDownloadDir error ${error.code}`); + expect(error.code).assertEqual(DEVICE_TYPE); + } + console.info(`getUserDownloadDirTest001 end`); + }) + + /** + * @tc.name: get_user_desktop_dir_test_001 + * @tc.desc: get sandbox path to the public desktop directory test + * @tc.type: FUNC test + * @tc.require: issueI#I8KE59 + */ + it('get_user_desktop_dir_test_001', 0, async function () { + console.info(`getUserDesktopDirTest001 start`); + try { + let path = environment.getUserDesktopDir(); + let result = true; + if (!path.startsWith(PC_STORAGE_PATH) || !path.endsWith(DESKTOP_PATH)) { + result = false; + } + expect(result).assertEqual(true); + } catch (error) { + console.info(`getUserDesktopDir error ${error.code}`); + expect(error.code).assertEqual(DEVICE_TYPE); + } + console.info(`getUserDesktopDirTest001 end`); + }) + + /** + * @tc.name: get_user_documents_dir_test_001 + * @tc.desc: get sandbox path to the public documents directory test + * @tc.type: FUNC test + * @tc.require: issueI#I8KE59 + */ + it('get_user_documents_dir_test_001', 0, async function () { + console.info(`getUserDocumentsDirTest001 start`); + try { + let path = environment.getUserDocumentDir(); + let result = true; + if (!path.startsWith(PC_STORAGE_PATH) || !path.endsWith(DOCUMENTS_PATH)) { + result = false; + } + expect(result).assertEqual(true); + } catch (error) { + console.info(`getUserDocumentsDir error ${error.code}`); + expect(error.code).assertEqual(DEVICE_TYPE); + } + console.info(`getUserDocumentsDirTest001 end`); + }) + + /** + * @tc.name: get_user_home_dir_test_001 + * @tc.desc: get sandbox directory of current user root test + * @tc.type: FUNC test + * @tc.require: issueI#I8KE59 + */ + it('get_user_home_dir_test_001', 0, async function () { + console.info(`getUserHomeDirTest001 start`); + try { + let path = environment.getUserHomeDir(); + let result = true; + if (!path.startsWith(PC_STORAGE_PATH)) { + result = false; + } + expect(result).assertEqual(true); + } catch (error) { + console.info(`getUserHomeDir error ${error.code}`); + expect(error.code).assertEqual(DEVICE_TYPE); + } + console.info(`getUserHomeDirTest001 end`); + }) + + /** + * @tc.name: get_external_storage_dir_test_001 + * @tc.desc: get sandbox directory of the wild card root test + * @tc.type: FUNC test + * @tc.require: issueI#I8KE59 + */ + it('get_external_storage_dir_test_001', 0, async function () { + console.info(`getExternalStorageDirTest001 start`); + try { + let targetPath = EXTERNAL_STORAGE_PATH; + let path = environment.getExternalStorageDir(); + expect(path).assertEqual(targetPath); + } catch (error) { + console.info(`getExternalStorageDir error ${error.code}`); + expect(error.code).assertEqual(DEVICE_TYPE); + } + console.info(`getExternalStorageDirTest001 end`); + }) +}) \ No newline at end of file diff --git a/interfaces/test/unittest/environment/config.json b/interfaces/test/unittest/environment/config.json new file mode 100644 index 0000000000000000000000000000000000000000..6eac08210becb51f99e51b5dda25e2a9a26caddc --- /dev/null +++ b/interfaces/test/unittest/environment/config.json @@ -0,0 +1,76 @@ +{ + "app": { + "bundleName": "ohos.file.environment.test", + "vendor": "acts", + "version": { + "code": 1000000, + "name": "1.0.0" + }, + "apiVersion": { + "compatible": 8, + "target": 9 + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.file.environment.test", + "name": ".MainAbility", + "deviceType": [ + "default", + "tablet", + "2in1" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry" + }, + "reqPermissions": [ + { + "name": "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY" + }, + { + "name": "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY" + }, + { + "name": "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY" + }, + { + "name": "ohos.permission.FILE_ACCESS_MANAGER" + } + ], + "abilities": [ + { + "visible": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "name": "ohos.file.environment.test.MainAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "$string:app_name", + "type": "page", + "launchType": "standard" + } + ], + "js": [ + { + "pages": [ + "pages/index/index" + ], + "name": "default", + "window": { + "designWidth": 720, + "autoDesignWidth": false + } + } + ] + } +} diff --git a/interfaces/test/unittest/environment/openharmony_sx.p7b b/interfaces/test/unittest/environment/openharmony_sx.p7b new file mode 100644 index 0000000000000000000000000000000000000000..013b771f2201233c085ad691ebe8f89d48616961 Binary files /dev/null and b/interfaces/test/unittest/environment/openharmony_sx.p7b differ