1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20import chai from 'chai'; 21import { 22 before, 23 after, 24 describe, 25 it 26} from 'mocha'; 27import { 28 fakeLog, 29 fakeLogRestore 30} from '../fakeLog'; 31import { initFramework } from '../../runtime/preparation/init'; 32import framework from '../../runtime/preparation/methods'; 33import { 34 getModule, 35 clearModules, 36 allModules 37} from '../../runtime/main/page/register'; 38import { App } from '../../runtime/main/app/App'; 39import Page from '../../runtime/main/page'; 40 41const expect = chai.expect; 42 43function clearRefs(json) { 44 delete json.ref; 45 if (json.children) { 46 json.children.forEach(clearRefs); 47 } 48} 49 50describe('framework entry', () => { 51 fakeLog(); 52 53 const pageMap: Map<string, Page> = App.pageMap; 54 let instanceId; 55 const options = { 56 orientation: 'portrait', 57 deviceType: 'phone', 58 resolution: '3.0', 59 aspectRatio: 'string', 60 deviceWidth: '1176', 61 deviceHeight: '2400', 62 roundScreen: false, 63 width: '0', 64 height: '0', 65 isInit: true, 66 darkMode: 'false', 67 pcPreview: 'disable', 68 appInstanceId: '10002', 69 packageName: 'com.example.helloworld', 70 resourcesConfiguration: [], 71 i18n: { 72 resources: [ 73 {'strings': {'hello': 'hello', 'world': 'world'}, 74 'Files': {}}, 75 {'strings': {'hello': 'Hello', 'world': 'World'}, 76 'Files': {}} 77 ] 78 }, 79 language: 'zh_CN', 80 appCreate: true, 81 appCode: '', 82 bundleUrl: '' 83 }; 84 85 before(() => { 86 initFramework(); 87 global.callNative = (id, tasks, callbackId) => { 88 if (callbackId !== '-1') { 89 framework.callJS(id, [{ 90 method: 'callback', 91 args: [callbackId, null, true] 92 }]); 93 } 94 }; 95 }); 96 97 after(() => { 98 fakeLogRestore(); 99 framework.destroyInstance(instanceId); 100 }); 101 102 it('createInstance', () => { 103 instanceId = Date.now() + ''; 104 const code: string = ` 105 $app_define$('@app-component/index', [], 106 function($app_require$, $app_exports$, $app_module$) { 107 $app_module$.exports = { 108 data: {}, 109 } 110 $app_module$.exports.template = { 111 'type': 'div', 112 'attr': {}, 113 'children': [ 114 { 115 'type': 'text', 116 'attr': { 117 'value': 'This is the index page.' 118 }, 119 'classList': [ 120 'title' 121 ] 122 } 123 ] 124 } 125 $app_module$.exports.style = { 126 '.title': { 127 'fontSize': '50px' 128 } 129 } 130 }) 131 $app_bootstrap$('@app-component/index',undefined,undefined) 132 `; 133 const expectComponent = { 134 'index': { 135 'data': {}, 136 'template': { 137 'type': 'div', 138 'attr': {}, 139 'children': [{'type': 'text', 'attr': {'value': 'This is the index page.'}, 'classList': ['title']}] 140 }, 141 'style': {'.title': {'fontSize': '50px'}} 142 } 143 }; 144 framework.createInstance(instanceId, code, options, null); 145 expect(pageMap.get(instanceId).customComponentMap).eql(expectComponent); 146 }); 147 148 describe('getRoot', () => { 149 it('with an exist instanceId', () => { 150 const json = framework.getRoot(instanceId); 151 expect(json.ref).eql('_root'); 152 clearRefs(json); 153 const expectJSON = { 154 type: 'div', 155 attr: {}, 156 style: {}, 157 children: [{ 158 type: 'text', 159 attr: { 160 value: 'This is the index page.' 161 }, 162 customComponent: false, 163 style: {fontSize: '50px'} 164 }], 165 event: ['viewappear', 'viewdisappear', 'viewsizechanged'], 166 customComponent: true 167 }; 168 expect(json).eql(expectJSON); 169 }); 170 }); 171 172 describe('callJS', () => { 173 it('fireEvent with no params', () => { 174 const result = framework.callJS(undefined, undefined); 175 expect(result).to.be.an.instanceof(Error); 176 }); 177 178 it('non-exist instanceId', () => { 179 const result = framework.callJS('123', [{ 180 method: 'fireEvent', 181 args: [] 182 }]); 183 expect(result).to.be.an.instanceof(Error); 184 }); 185 186 it('non-array tasks', () => { 187 const result = framework.callJS(instanceId, { 188 // @ts-ignore 189 method: 'fireEvent', 190 args: [] 191 }); 192 expect(result).to.be.an.instanceof(Error); 193 }); 194 }); 195 196 describe('destroyInstance', () => { 197 it('with no params', () => { 198 const result = framework.destroyInstance(undefined); 199 expect(result).to.be.an.instanceof(Error); 200 }); 201 202 it('with an exist instanceId', () => { 203 const result = framework.destroyInstance(instanceId); 204 expect(result[instanceId]).to.be.undefined; 205 }); 206 207 it('non-exist instanceId', () => { 208 const result = framework.destroyInstance('123'); 209 expect(result).to.be.an.instanceof(Error); 210 }); 211 }); 212 213 describe('registerModules', () => { 214 it('with object of modules', () => { 215 clearModules(); 216 expect(allModules()).to.deep.equal({}); 217 const modules = { 218 'system.test': ['getInfo', 'getAvailableStorage', 'getCpuInfo'] 219 }; 220 framework.registerModules(modules); 221 expect(getModule('system')).to.be.an('object'); 222 clearModules(); 223 }); 224 }); 225}); 226