1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | var browserMatch = require('../util/browser-detection.js').browserMatch;
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | var _browserCfgRegex = /^\s*(\S+)(?:\s+(\W*\d\S*))?(?:\s+on\s+(.*?))?(?:\s+with\s+(.*?))?(?:\s+as\s+(.*?))?\s*$/i;
|
26 |
|
27 | var 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 |
|
48 | var Browser = function (cfgString) {
|
49 | |
50 |
|
51 |
|
52 | this.config = Browser.parseBrowserConfig(cfgString);
|
53 | |
54 |
|
55 |
|
56 | this.name = buildNameFromConfig(this.config);
|
57 | |
58 |
|
59 |
|
60 | this.tasksQueue = [];
|
61 | |
62 |
|
63 |
|
64 | this.pendingTasks = 0;
|
65 | };
|
66 |
|
67 | Browser.prototype = {};
|
68 |
|
69 | Browser.prototype.addTask = function (task) {
|
70 | this.pendingTasks++;
|
71 | this.tasksQueue.push(task);
|
72 | };
|
73 |
|
74 | Browser.prototype.hasTasks = function () {
|
75 | return this.tasksQueue.length > 0;
|
76 | };
|
77 |
|
78 | Browser.prototype.takeTask = function () {
|
79 | return this.tasksQueue.shift();
|
80 | };
|
81 |
|
82 | Browser.prototype.matches = function (slave) {
|
83 | return browserMatch(this.config, slave.browserInfo);
|
84 | };
|
85 |
|
86 | Browser.prototype.onTaskFinished = function () {
|
87 | this.pendingTasks--;
|
88 | };
|
89 |
|
90 | Browser.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 |
|
100 |
|
101 | Browser.parseBrowserConfig = function (cfgString) {
|
102 | if (cfgString == null || cfgString === "") {
|
103 | return {};
|
104 | }
|
105 |
|
106 | var match = _browserCfgRegex.exec(cfgString);
|
107 | if (match === null) {
|
108 | return {
|
109 | browserName: "unparsable browser"
|
110 | };
|
111 | } else {
|
112 |
|
113 |
|
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 |
|
124 | module.exports = Browser;
|