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, it, expect, TestType, Level, Size } from '@ohos/hypium'; 17import { Driver, ON } from '@ohos.UiTest'; 18import display from '@ohos.display' 19import file from '@system.file'; 20 21async function asyncTest() { 22 return 1; 23} 24 25class TestClass { 26 async asyncFunction() { 27 return 2; 28 } 29} 30 31export default function softwareJsTest() { 32 describe('SoftwareJsTest', function () { 33 34 /* 35 * @tc.number: STD-SOFTWARE-0200 36 * @tc.name: testJsAsyncAwait0100 37 * @tc.desc: 【STD-SOFTWARE-0200】为了方便应用的书写,JS 运行时必须支持 ES2017 中的async/await 特性 38 * @tc.size: MediumTest 39 * @tc.type: Function 40 * @tc.level: Level 0 41 */ 42 it('testJsAsyncAwait0100', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 43 let res1 = await asyncTest() 44 expect(res1).assertEqual(1) 45 let testClass = new TestClass() 46 let res2 = await testClass.asyncFunction() 47 expect(res2).assertEqual(2) 48 done() 49 }) 50 51 /* 52 * @tc.number: G-SOFTWARE-0301 53 * @tc.name: testJsES50100 54 * @tc.desc: 【G-SOFTWARE-0301】JS 运行时支持 ES5、ES6 语法规范 55 * @tc.size: MediumTest 56 * @tc.type: Function 57 * @tc.level: Level 0 58 */ 59 it('testJsES50100', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 60 // ES5 61 var array = [1, 2, 3, 4, 5, 6] 62 expect(Array.isArray(array)).assertTrue() 63 var arraymap = array.map(function (value) { 64 return value -= 1 65 }) 66 arraymap.forEach(function (index, value) { 67 expect(value).assertEqual(index) 68 }) 69 var arrayfilter = array.filter(function (item) { 70 return item < 4 71 }) 72 expect(arrayfilter.length).assertEqual(3) 73 var sum = array.reduce(function (sum, value) { 74 return sum + value; 75 }, 0) 76 expect(sum).assertEqual(21) 77 var res1 = array.every(function (value) { 78 return value < 4 79 }) 80 expect(res1).assertFalse() 81 var res2 = array.some(function (value) { 82 return value < 4 83 }) 84 expect(res2).assertTrue() 85 expect(array.indexOf(2)).assertEqual(1) 86 expect(array.lastIndexOf(5)).assertEqual(4) 87 var jsonObj = { 88 "str": "str", "num": 0 89 } 90 var jsonStr = JSON.stringify(jsonObj) 91 expect(jsonStr).assertEqual('{"str":"str","num":0}') 92 var jsonObj2 = JSON.parse(jsonStr) 93 var str = " test " 94 expect(str.trim()).assertEqual("test") 95 var obj = { 96 a: 1, b: 2, c: 3 97 } 98 expect(Object.keys(obj).toString()).assertEqual("a,b,c") 99 expect(Object.values(obj).toString()).assertEqual("1,2,3") 100 Object.defineProperty(obj, "c", { 101 writable: false, 102 value: 4, 103 }) 104 expect(obj.c).assertEqual(4) 105 done() 106 }) 107 108 /* 109 * @tc.number: G-SOFTWARE-0301 110 * @tc.name: testJsES60100 111 * @tc.desc: 【G-SOFTWARE-0301】JS 运行时支持 ES5、ES6 语法规范 112 * @tc.size: MediumTest 113 * @tc.type: Function 114 * @tc.level: Level 0 115 */ 116 it('testJsES60100', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 117 const num1 = 1 118 let num2 = 1 119 expect(num1).assertEqual(num2) 120 const sumFunc = (num1, num2) => num1 + num2 121 console.log("sssss %d", sumFunc(num1, num2)) 122 expect(sumFunc(num1, num2)).assertEqual(2) 123 expect(`num1 = ${num1}`).assertEqual("num1 = 1") 124 var obj = { 125 a: 1, b: 2, c: 3 126 } 127 var { a, b, c } = obj 128 expect(a).assertEqual(1) 129 var array1 = [1, 2, 3] 130 var array2 = [4, 5, 6] 131 var array3 = [...array1, ...array2] 132 expect(array3.length).assertEqual(6) 133 var i = 1 134 for (const num of array1) { 135 expect(num).assertEqual(i) 136 i++ 137 } 138 expect(array1.includes(1)).assertTrue() 139 var map = new Map([['a', 1], ['b', 2], ['c', 3]]) 140 expect(map.get('a')).assertEqual(1) 141 const set = new Set([1, 2, 3]) 142 expect(set.has(1)).assertTrue() 143 var obj1 = { a: 1 } 144 var obj2 = { b: 2 } 145 var obj3 = Object.assign({}, obj1, obj2) 146 expect(obj3.a).assertEqual(1) 147 expect(obj3.b).assertEqual(2) 148 done() 149 }) 150 151 /* 152 * @tc.number: G-SOFTWARE-0302 153 * @tc.name: testJsStrictMode0100 154 * @tc.desc: 【G-SOFTWARE-0302】JS 运行时环境必须运行在严格模式 155 * @tc.size: MediumTest 156 * @tc.type: Function 157 * @tc.level: Level 0 158 */ 159 it('testJsStrictMode0100', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 160 expect(this).assertUndefined() 161 done() 162 }) 163 164 /* 165 * @tc.number: G-SOFTWARE-0603 166 * @tc.name: testWeblikeAccessPath0100 167 * @tc.desc: 【G-SOFTWARE-0603】类 web 范式限制应用访问的绝对路径应统一用"/"开头、相对路径统一用"./"或"../"开头 168 * @tc.size: MediumTest 169 * @tc.type: Function 170 * @tc.level: Level 0 171 */ 172 it('testWeblikeAccessPath0100', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 173 let driver = Driver.create() 174 await driver.delayMs(2000) 175 file.readText({ 176 uri: 'internal://app/result.txt', 177 success: function (data) { 178 let result = data.text 179 console.info('read file success. result = ' + result); 180 expect(result).assertContain('AbsolutionPathSuccess') 181 expect(result).assertContain('RelativePath1Success') 182 expect(result).assertContain('RelativePath2Success') 183 expect(result.includes('RelativePath3Success')).assertFalse() 184 }, 185 fail: function (data, code) { 186 console.error('read file fail , code: ' + code + ', data: ' + data); 187 }, 188 }) 189 await driver.delayMs(2000) 190 done() 191 }) 192 193 /* 194 * @tc.number: G-SOFTWARE-0604 195 * @tc.name: testWeblikePrivateDirectoryPath0100 196 * @tc.desc: 【G-SOFTWARE-0604】类 web 范式限制应用私有目录的路径前缀应为 internal://app/, 197 * 仅本应用可见,随应用卸载删除 198 * @tc.size: MediumTest 199 * @tc.type: Function 200 * @tc.level: Level 0 201 */ 202 it('testWeblikePrivateDirectoryPath0100', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 203 let driver = Driver.create() 204 await driver.delayMs(2000) 205 let result = "" 206 file.readText({ 207 uri: 'internal://app/test.txt', 208 success: function (data) { 209 result = data.text 210 console.info('read file success. result = ' + result); 211 expect(result).assertEqual('test text') 212 }, 213 fail: function (data, code) { 214 console.error('read file fail , code: ' + code + ', data: ' + data); 215 }, 216 }) 217 await driver.delayMs(2000) 218 done() 219 }) 220 }) 221} 222