UNPKG

3.43 kBJavaScriptView Raw
1/*
2 * Copyright 2012 Amadeus s.a.s.
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
16var browserMatch = require('../util/browser-detection.js').browserMatch;
17
18/**
19 * @see Browser.prototype.parseBrowserConfig
20 * <li> Usage: </li>
21 * <li> Input: 'Chrome >=30 on Desktop Windows with flags as Chrome Canary 30' </li>
22 * <li> Match: [1] = 'Chrome', [2] = '>=30', [3] = 'Desktop Windows', [4] = 'Chrome Canary 30' </li>
23 * Parts 2, 3, 4 are optional, will be undefined if not found.
24 */
25var _browserCfgRegex = /^\s*(\S+)(?:\s+(\W*\d\S*))?(?:\s+on\s+(.*?))?(?:\s+with\s+(.*?))?(?:\s+as\s+(.*?))?\s*$/i;
26
27var buildNameFromConfig = function (config) {
28 if (config.displayAs) {
29 return config.displayAs;
30 }
31
32 var name = "";
33 if (config.browserName) {
34 name = config.browserName;
35 if (config.browserVersion != null) {
36 name += " " + config.browserVersion;
37 }
38 }
39 if (config.os) {
40 name += " (" + config.os + ")";
41 }
42 if (config.flags) {
43 name += " [" + config.flags + "]";
44 }
45 return name;
46};
47
48var Browser = function (cfgString) {
49 /**
50 * {browserName, browserVersion, os, displayAs}
51 */
52 this.config = Browser.parseBrowserConfig(cfgString);
53 /**
54 * Display name for the logs
55 */
56 this.name = buildNameFromConfig(this.config);
57 /**
58 * Contains tasks not yet dispatched.
59 */
60 this.tasksQueue = [];
61 /**
62 * Counts tasks not yet finished. Will be always >= this.tasksQueue.length
63 */
64 this.pendingTasks = 0;
65};
66
67Browser.prototype = {};
68
69Browser.prototype.addTask = function (task) {
70 this.pendingTasks++;
71 this.tasksQueue.push(task);
72};
73
74Browser.prototype.hasTasks = function () {
75 return this.tasksQueue.length > 0;
76};
77
78Browser.prototype.takeTask = function () {
79 return this.tasksQueue.shift();
80};
81
82Browser.prototype.matches = function (slave) {
83 return browserMatch(this.config, slave.browserInfo);
84};
85
86Browser.prototype.onTaskFinished = function () {
87 this.pendingTasks--;
88};
89
90Browser.prototype.getJsonInfo = function () {
91 return {
92 name: this.name,
93 remainingTasks: this.pendingTasks,
94 runningTasks: this.pendingTasks - this.tasksQueue.length
95 };
96};
97
98/**
99 * @static
100 */
101Browser.parseBrowserConfig = function (cfgString) {
102 if (cfgString == null || cfgString === "") {
103 return {}; // default, unrestricted browser
104 }
105
106 var match = _browserCfgRegex.exec(cfgString);
107 if (match === null) {
108 return {
109 browserName: "unparsable browser"
110 };
111 } else {
112 // Some of the entries can be undefined, it's fine.
113 // Note that garbage input may also produce valid match.
114 return {
115 browserName: match[1],
116 browserVersion: match[2],
117 os: match[3],
118 flags: match[4],
119 displayAs: match[5]
120 };
121 }
122};
123
124module.exports = Browser;