Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / karma / lib / launcher.js
1 var log = require('./logger').create('launcher')
2 var q = require('q')
3
4 var baseDecorator = require('./launchers/base').decoratorFactory
5 var captureTimeoutDecorator = require('./launchers/capture_timeout').decoratorFactory
6 var retryDecorator = require('./launchers/retry').decoratorFactory
7 var processDecorator = require('./launchers/process').decoratorFactory
8
9 // TODO(vojta): remove once nobody uses it
10 var baseBrowserDecoratorFactory = function (baseLauncherDecorator, captureTimeoutLauncherDecorator,
11   retryLauncherDecorator, processLauncherDecorator) {
12   return function (launcher) {
13     baseLauncherDecorator(launcher)
14     captureTimeoutLauncherDecorator(launcher)
15     retryLauncherDecorator(launcher)
16     processLauncherDecorator(launcher)
17   }
18 }
19
20 var Launcher = function (emitter, injector) {
21   var browsers = []
22   var lastStartTime
23
24   var getBrowserById = function (id) {
25     for (var i = 0; i < browsers.length; i++) {
26       if (browsers[i].id === id) {
27         return browsers[i]
28       }
29     }
30
31     return null
32   }
33
34   this.launch = function (names, hostname, port, urlRoot) {
35     var browser
36     var url = 'http://' + hostname + ':' + port + urlRoot
37
38     lastStartTime = Date.now()
39
40     names.forEach(function (name) {
41       var locals = {
42         id: ['value', Launcher.generateId()],
43         name: ['value', name],
44         baseLauncherDecorator: ['factory', baseDecorator],
45         captureTimeoutLauncherDecorator: ['factory', captureTimeoutDecorator],
46         retryLauncherDecorator: ['factory', retryDecorator],
47         processLauncherDecorator: ['factory', processDecorator],
48         baseBrowserDecorator: ['factory', baseBrowserDecoratorFactory]
49       }
50
51       // TODO(vojta): determine script from name
52       if (name.indexOf('/') !== -1) {
53         name = 'Script'
54       }
55
56       try {
57         browser = injector.createChild([locals], ['launcher:' + name]).get('launcher:' + name)
58       } catch (e) {
59         if (e.message.indexOf('No provider for "launcher:' + name + '"') !== -1) {
60           log.warn('Can not load "%s", it is not registered!\n  ' +
61             'Perhaps you are missing some plugin?', name)
62         } else {
63           log.warn('Can not load "%s"!\n  ' + e.stack, name)
64         }
65
66         return
67       }
68
69       // TODO(vojta): remove in v1.0 (BC for old launchers)
70       if (!browser.forceKill) {
71         browser.forceKill = function () {
72           var deferred = q.defer()
73           this.kill(function () {
74             deferred.resolve()
75           })
76           return deferred.promise
77         }
78
79         browser.restart = function () {
80           var self = this
81           this.kill(function () {
82             self.start(url)
83           })
84         }
85       }
86
87       log.info('Starting browser %s', browser.name)
88       browser.start(url)
89       browsers.push(browser)
90     })
91
92     return browsers
93   }
94
95   this.launch.$inject = ['config.browsers', 'config.hostname', 'config.port', 'config.urlRoot']
96
97   this.kill = function (id, callback) {
98     var browser = getBrowserById(id)
99     callback = callback || function () {}
100
101     if (!browser) {
102       process.nextTick(callback)
103       return false
104     }
105
106     browser.forceKill().then(callback)
107     return true
108   }
109
110   this.restart = function (id) {
111     var browser = getBrowserById(id)
112
113     if (!browser) {
114       return false
115     }
116
117     browser.restart()
118     return true
119   }
120
121   this.killAll = function (callback) {
122     log.debug('Disconnecting all browsers')
123
124     var remaining = 0
125     var finish = function () {
126       remaining--
127       if (!remaining && callback) {
128         callback()
129       }
130     }
131
132     if (!browsers.length) {
133       return process.nextTick(callback)
134     }
135
136     browsers.forEach(function (browser) {
137       remaining++
138       browser.forceKill().then(finish)
139     })
140   }
141
142   this.areAllCaptured = function () {
143     return !browsers.some(function (browser) {
144         return !browser.isCaptured()
145       })
146   }
147
148   this.markCaptured = function (id) {
149     browsers.forEach(function (browser) {
150       if (browser.id === id) {
151         browser.markCaptured()
152         log.debug('%s (id %s) captured in %d secs', browser.name, browser.id,
153           (Date.now() - lastStartTime) / 1000)
154       }
155     })
156   }
157
158   // register events
159   emitter.on('exit', this.killAll)
160 }
161
162 Launcher.$inject = ['emitter', 'injector']
163
164 Launcher.generateId = function () {
165   return '' + Math.floor(Math.random() * 100000000)
166 }
167
168 // PUBLISH
169 exports.Launcher = Launcher