1/* 2 * Copyright (c) 2021-2022 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 16function exportUiTestLifeCycleMethods() { 17 globalThis.setupUiTestEnvironment = async function () { 18 let uitest = globalThis.requireInternal('uitest') 19 if (uitest.uitestSetupDone === true) { 20 return 21 } 22 // check 'persist.ace.testmode.enabled' property 23 let parameter = globalThis.requireNapi('systemparameter') 24 if (parameter == null) { 25 console.error('UiTestKit_exporter: Failed to require systemparameter napi module!') 26 return 27 } 28 if (parameter.getSync('persist.ace.testmode.enabled', '0') !== '1') { 29 console.warn('UiTestKit_exporter: systemParameter "persist.ace.testmode.enabled" is not set!') 30 return 31 } 32 let registry = globalThis.requireNapi('application.abilityDelegatorRegistry') 33 if (registry == null) { 34 console.error('UiTestKit_exporter: Failed to require AbilityDelegator napi module') 35 return 36 } 37 // startup server_daemon 38 let delegator = registry.getAbilityDelegator() 39 if (delegator == null) { 40 console.warn('UiTestKit_exporter: Cannot get AbilityDelegator, uitest_daemon need to be pre-started') 41 } else { 42 console.info('UiTestKit_lifecycle: Begin executing shell command to start server-daemon') 43 try { 44 let result = await delegator.executeShellCommand('uitest start-daemon 0123456789', 1) 45 console.info(`UiTestKit_lifecycle: Start server-daemon finished: ${JSON.stringify(result)}`) 46 } catch (error) { 47 console.error(`UiTestKit_lifecycle: Start server-daemon failed: ${JSON.stringify(error)}`) 48 } 49 } 50 uitest.setup('0123456789') 51 uitest.uitestSetupDone = true 52 } 53 54 globalThis.teardownUiTestEnvironment = function () { 55 if (uitest.uitestSetupDone === true) { 56 console.info('UiTestKit_lifecycle, disposal uitest') 57 uitest.teardown() 58 } 59 } 60} 61 62function requireUiTest() { 63 console.info('UiTestKit_exporter: Start running uitest_exporter') 64 exportUiTestLifeCycleMethods() 65 let module = undefined 66 try { 67 module = globalThis.requireInternal('uitest') 68 } catch (error) { 69 console.error(`UiTestKit_exporter: Run exporter failed: ${JSON.stringify(error)}`) 70 } finally { 71 console.debug('UiTestKit_exporter: UiTest module=' + module) 72 } 73 return module 74} 75 76export default requireUiTest() 77