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 { PageLinkedMap } from '../../runtime/main/app/map'; 40import Page from '../../runtime/main/page'; 41 42const expect = chai.expect; 43 44function clearRefs(json) { 45 delete json.ref; 46 if (json.children) { 47 json.children.forEach(clearRefs); 48 } 49} 50 51describe('framework entry', () => { 52 fakeLog(); 53 54 const pageMap: PageLinkedMap = App.pageMap; 55 let instanceId; 56 const options = { 57 orientation: 'portrait', 58 'device-type': 'phone', 59 resolution: '3.0', 60 'aspect-ratio': 'string', 61 'device-width': '1176', 62 'device-height': '2400', 63 'round-screen': false, 64 width: '0', 65 height: '0', 66 isInit: true, 67 'dark-mode': 'false', 68 pcPreview: 'disable', 69 appInstanceId: '10002', 70 packageName: 'com.example.helloworld', 71 resourcesConfiguration: [], 72 i18n: { 73 resources: [ 74 {'strings': {'hello': 'hello', 'world': 'world'}, 75 'Files': {}}, 76 {'strings': {'hello': 'Hello', 'world': 'World'}, 77 'Files': {}} 78 ] 79 }, 80 language: 'zh_CN', 81 appCreate: true, 82 appCode: '', 83 bundleUrl: '' 84 }; 85 86 before(() => { 87 initFramework(); 88 global.callNative = (id, tasks, callbackId) => { 89 if (callbackId !== '-1') { 90 framework.callJS(id, [{ 91 method: 'callback', 92 args: [callbackId, null, true] 93 }]); 94 } 95 }; 96 }); 97 98 after(() => { 99 fakeLogRestore(); 100 framework.destroyInstance(instanceId); 101 }); 102 103 it('createInstance', () => { 104 instanceId = Date.now() + ''; 105 const code: string = ` 106 $app_define$('@app-component/index', [], 107 function($app_require$, $app_exports$, $app_module$) { 108 $app_module$.exports = { 109 data: {}, 110 } 111 $app_module$.exports.template = { 112 'type': 'div', 113 'attr': {}, 114 'children': [ 115 { 116 'type': 'text', 117 'attr': { 118 'value': 'This is the index page.' 119 }, 120 'classList': [ 121 'title' 122 ] 123 } 124 ] 125 } 126 $app_module$.exports.style = { 127 '.title': { 128 'fontSize': '50px' 129 } 130 } 131 }) 132 $app_bootstrap$('@app-component/index',undefined,undefined) 133 `; 134 const expectComponent = { 135 'index': { 136 'data': {}, 137 'template': { 138 'type': 'div', 139 'attr': {}, 140 'children': [{'type': 'text', 'attr': {'value': 'This is the index page.'}, 'classList': ['title']}] 141 }, 142 'style': {'.title': {'fontSize': '50px'}} 143 } 144 }; 145 framework.createInstance(instanceId, code, options, null); 146 expect(pageMap[instanceId].customComponentMap).eql(expectComponent); 147 }); 148 149 describe('getRoot', () => { 150 it('with an exist instanceId', () => { 151 const json = framework.getRoot(instanceId); 152 expect(json.ref).eql('_root'); 153 clearRefs(json); 154 const expectJSON = { 155 type: 'div', 156 attr: {}, 157 style: {}, 158 children: [{ 159 type: 'text', 160 attr: { 161 value: 'This is the index page.' 162 }, 163 customComponent: false, 164 style: {fontSize: '50px'} 165 }], 166 event: ['viewappear', 'viewdisappear', 'viewsizechanged'], 167 customComponent: true 168 }; 169 expect(json).eql(expectJSON); 170 }); 171 }); 172 173 describe('callJS', () => { 174 it('fireEvent with no params', () => { 175 const result = framework.callJS(undefined, undefined); 176 expect(result).to.be.an.instanceof(Error); 177 }); 178 179 it('non-exist instanceId', () => { 180 const result = framework.callJS('123', [{ 181 method: 'fireEvent', 182 args: [] 183 }]); 184 expect(result).to.be.an.instanceof(Error); 185 }); 186 187 it('non-array tasks', () => { 188 const result = framework.callJS(instanceId, { 189 // @ts-ignore 190 method: 'fireEvent', 191 args: [] 192 }); 193 expect(result).to.be.an.instanceof(Error); 194 }); 195 }); 196 197 describe('destroyInstance', () => { 198 it('with no params', () => { 199 const result = framework.destroyInstance(undefined); 200 expect(result).to.be.an.instanceof(Error); 201 }); 202 203 it('with an exist instanceId', () => { 204 const result = framework.destroyInstance(instanceId); 205 expect(result[instanceId]).to.be.undefined; 206 }); 207 208 it('non-exist instanceId', () => { 209 const result = framework.destroyInstance('123'); 210 expect(result).to.be.an.instanceof(Error); 211 }); 212 }); 213 214 describe('registerModules', () => { 215 it('with object of modules', () => { 216 clearModules(); 217 expect(allModules()).to.deep.equal({}); 218 const modules = { 219 'system.test': ['getInfo', 'getAvailableStorage', 'getCpuInfo'] 220 }; 221 framework.registerModules(modules); 222 expect(getModule('system')).to.be.an('object'); 223 clearModules(); 224 }); 225 }); 226}); 227