• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 requireModule(name) {
17  let mod = globalThis.requireNapi(name)
18  if (mod) {
19    return mod
20  }
21  mod = globalThis.ohosplugin
22  name.split('.').foreach(element => {
23    if (mod) {
24      mod = mod[element]
25    }
26  })
27  if (mod) {
28    return mod
29  }
30  mod = globalThis.systemplugin
31  name.split('.').foreach(element => {
32    if (mod) {
33      mod = mod[element]
34    }
35  })
36  return mod
37}
38
39async function scheduleConnectionAsync(uitest) {
40  let procInfo = requireModule('process')
41  if (procInfo == null) {
42    console.error('UiTestKit_exporter: Failed to require process napi module')
43    return
44  }
45  let registry = requireModule('application.abilityDelegatorRegistry')
46  if (registry == null) {
47    console.error('UiTestKit_exporter: Failed to require AbilityDelegator napi module')
48    return
49  }
50  let delegator = registry.getAbilityDelegator()
51  if (delegator == null) {
52    console.warn('UiTestKit_exporter: Cannot get AbilityDelegator, uitest_daemon need to be pre-started')
53    uitest.scheduleEstablishConnection('default')
54  } else {
55    let appContext = delegator.getAppContext()
56    let connToken = `${appContext.applicationInfo.name}@${procInfo.pid}@${procInfo.uid}@${appContext.area}`
57    console.info(`UiTestKit_exporter: ScheduleProbeAndEstablishConnection, token=${connToken}`)
58    uitest.scheduleEstablishConnection(connToken)
59    console.info('UiTestKit_exporter: Begin executing shell command to start server-daemon')
60    delegator.executeShellCommand(`uitest start-daemon ${connToken}`, 3).then((value) => {
61      console.info(`UiTestKit_exporter: Start server-daemon finished: ${JSON.stringify(value)}`)
62    }).catch((error) => {
63      console.error(`UiTestKit_exporter: Start server-daemon failed: ${JSON.stringify(error)}`)
64    })
65  }
66}
67
68function loadAndSetupUiTest() {
69  let uitest = globalThis.requireInternal('UiTest')
70  if (uitest.uitestSetupCalled === true) {
71    return
72  }
73  uitest.uitestSetupCalled = true
74  // check 'persist.ace.testmode.enabled' property
75  let parameter = requireModule('systemparameter')
76  if (parameter == null) {
77    console.error('UiTestKit_exporter: Failed to require systemparameter napi module!')
78    return
79  }
80  if (parameter.getSync('persist.ace.testmode.enabled', '0') !== '1') {
81    console.warn('UiTestKit_exporter: systemParameter "persist.ace.testmode.enabled" is not set!')
82  }
83  // schedule startup server_daemon and establish connection
84  scheduleConnectionAsync(uitest).catch((error) => {
85    console.error(`UiTestKit_exporter: ScheduleConnectionAsync failed: ${JSON.stringify(error)}`)
86  })
87  return uitest
88}
89
90export default loadAndSetupUiTest()
91