• 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 */
15import { SuiteService, SpecService, ExpectService, ReportService } from './service'
16import { ConfigService } from './module/config/configService'
17import { SpecEvent, TaskEvent, SuiteEvent } from './event'
18
19/**
20 * core service for execute testcase.
21 */
22class Core {
23  static getInstance () {
24    if (!this.instance) {
25      this.instance = new Core()
26    }
27    return this.instance
28  }
29
30  constructor () {
31    this.instance = null
32    this.services = {
33      suite: {},
34      spec: {},
35      config: {},
36      expect: {},
37      log: {},
38      report: {}
39
40    }
41    this.events = {
42      suite: {},
43      spec: {},
44      task: {}
45    }
46  }
47
48  addService (name, service) {
49    let serviceObj = {}
50    if (!this.services[name]) {
51      this.services[name] = serviceObj
52    } else {
53      serviceObj = this.services[name]
54    }
55    serviceObj[service.id] = service
56  }
57
58  getDefaultService (name) {
59    return this.services[name].default
60  }
61
62  getServices (name) {
63    return this.services[name]
64  }
65
66  registerEvent (serviceName, event) {
67    let eventObj = {}
68    if (!this.events[serviceName]) {
69      this.events[serviceName] = eventObj
70    } else {
71      eventObj = this.events[serviceName]
72    }
73    eventObj[event.id] = event
74  }
75
76  unRegisterEvent (serviceName, eventID) {
77    const eventObj = this.events[serviceName]
78    if (eventObj) {
79      delete eventObj[eventID]
80    }
81  }
82
83  subscribeEvent (serviceName, serviceObj) {
84    const eventObj = this.events[serviceName]
85    if (eventObj) {
86      for (const attr in eventObj) {
87        eventObj[attr]['subscribeEvent'](serviceObj)
88      }
89    }
90  }
91
92  fireEvents (serviceName, eventName) {
93    const eventObj = this.events[serviceName]
94    if (!eventObj) {
95      return
96    }
97    for (const attr in eventObj) {
98      eventObj[attr][eventName]()
99    }
100  }
101
102  addToGlobal (apis) {
103    if (typeof globalThis !== 'undefined') {
104      for (let api in apis) {
105        globalThis[api] = apis[api]
106      }
107    }
108    for (const api in apis) {
109      this[api] = apis[api]
110    }
111  }
112
113  init () {
114    this.addService('suite', new SuiteService({ id: 'default' }))
115    this.addService('spec', new SpecService({ id: 'default' }))
116    this.addService('expect', new ExpectService({ id: 'default' }))
117    this.addService('report', new ReportService({ id: 'default' }))
118    this.addService('config', new ConfigService({ id: 'default' }))
119    this.registerEvent('task', new TaskEvent({ id: 'default', coreContext: this }))
120    this.registerEvent('suite', new SuiteEvent({ id: 'default', coreContext: this }))
121    this.registerEvent('spec', new SpecEvent({ id: 'default', coreContext: this }))
122    this.subscribeEvent('spec', this.getDefaultService('report'))
123    this.subscribeEvent('suite', this.getDefaultService('report'))
124    this.subscribeEvent('task', this.getDefaultService('report'))
125    const context = this
126    for (const key in this.services) {
127      const serviceObj = this.services[key]
128      for (const serviceID in serviceObj) {
129        const service = serviceObj[serviceID]
130        service.init(context)
131
132        if (typeof service.apis !== 'function') {
133          continue
134        }
135        const apis = service.apis()
136        if (apis) {
137          this.addToGlobal(apis)
138        }
139      }
140    }
141  }
142
143  execute () {
144    const suiteService = this.getDefaultService('suite')
145    suiteService.execute()
146  }
147}
148
149export default Core
150