-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathandroid.js
More file actions
308 lines (263 loc) · 7.67 KB
/
android.js
File metadata and controls
308 lines (263 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
'use strict';
var isObject = require('lodash.isobject');
var ezspawn = require('ezspawn');
var debug = require('debug')('androidctrl:debug');
var verbose = require('debug')('androidctrl:verbose');
var fs = require('fs');
var path = require('path');
var ini = require('ini');
var findWhere = require('lodash.findwhere');
var spawnWaitFor = require('spawn-wait-for');
var promiseRetry = require('promise-retry');
var returnUndefined = function() {
return undefined;
};
var processKeyValueGroups = function(str) {
var lines = str.split('\n');
var currentKey = {};
var results = [];
lines.forEach(function(line) {
var matches = line.match(/([\w\/]+):\s(.*)/);
var key;
var value;
if (matches === null) {
return;
}
key = matches[1];
value = matches[2];
if (typeof currentKey[key] !== 'undefined') {
results.push(currentKey);
currentKey = {};
}
currentKey[key] = value;
});
if (Object.keys(currentKey).length) {
results.push(currentKey);
}
return results;
};
var Android = {
startOrCreate: function(name, hardwareOptions) {
verbose('startOrCreate(', name, hardwareOptions, ')');
var _this = this;
if (isObject(name)) {
hardwareOptions = name;
name = undefined;
}
if (!name) {
name = 'Android511';
}
return this.hasAVD(name).then(function(hasAVD) {
if (hasAVD) {
return name;
}
// create an AVD based on first target we find
return _this.listTargets().then(function(targets) {
if (!targets.length) {
throw new Error('Could not find any targets, can not create AVD');
}
var target = targets.shift();
var targetId = String(parseInt(target.id));
return _this.createAVD(
targetId,
name,
hardwareOptions
).then(function() {
return name;
});
});
}).then(function(avdName) {
return _this.start(avdName);
});
},
start: function(avdName) {
verbose('start(' + avdName + ')');
return spawnWaitFor(
'emulator -verbose -avd "' + avdName + '"',
/emulator: control console listening on port (\d+), ADB on port \d+/
).then(function(result) {
return {
process: result.process,
id: 'emulator-' + result.matches[1]
};
});
},
waitForDevice: function(emulatorId) {
verbose('waitForDevice(' + emulatorId + ')');
return this.adb(emulatorId, 'wait-for-device').then(returnUndefined);
},
ensureReady: function(emulatorId) {
verbose('ensureReady(' + emulatorId + ')');
var _this = this;
return this.waitForDevice(emulatorId)
.then(function() {
return promiseRetry(
function(retry) {
return _this.adb(
emulatorId,
'shell getprop sys.boot_completed'
).then(function(proc) {
debug('got back device status', proc);
if (!proc.stdout.match(/1/)) {
retry('Device not ready');
}
});
},
{
retries: 100,
factor: 1
}
);
});
},
setHardwareOptions: function(avdName, hardwareOptions) {
return this.listAVDs()
.then(function(avds) {
return findWhere(avds, {Name: avdName});
})
.then(function(avd) {
if (!avd) {
throw new Error('avd ' + avdName + ' could not be found');
}
return path.join(avd.Path, 'config.ini');
})
.then(function(configFile) {
var iniFile = fs.readFileSync(configFile, 'utf8');
var config = ini.parse(iniFile);
Object.keys(hardwareOptions).forEach(function(key) {
config[key] = hardwareOptions[key];
});
fs.writeFileSync(configFile, ini.stringify(config));
return undefined;
});
},
adb: function(emulatorId, cmd) {
verbose('adb(' + emulatorId + ',' + cmd + ')');
return ezspawn('adb -s ' + emulatorId + ' ' + cmd);
},
createAVD: function(targetId, name, hardwareOptions) {
verbose('createAVD(' + targetId + ',' + name + ')');
var _this = this;
return ezspawn(
'android create avd -t ' +
targetId +
' -c 500M -d "Nexus 5" -n "' +
name + '"'
)
.then(function() {
if (!hardwareOptions) {
verbose('no hardware options specified');
return undefined;
}
return _this.setHardwareOptions(name, hardwareOptions);
});
},
isInstalled: function(emulatorId, packageName) {
verbose('isInstalled(' + emulatorId + ',' + packageName + ')');
return this.listPackages(emulatorId)
.then(function(packages) {
var isInstalled = packages.indexOf(packageName) > -1;
debug(
packageName + ' is ' +
(!isInstalled ? 'not ' : '') + 'installed'
);
return isInstalled;
});
},
hasAVD: function(avdName) {
verbose('hasAVD()');
return this.listAVDs().then(function(avds) {
return avds.filter(function(avd) {
return avd.Name.toLowerCase() === avdName.toLowerCase();
}).length > 0;
});
},
install: function(emulatorId, apkPath, reinstall) {
verbose('install(' + emulatorId + ',' + apkPath + ',' + reinstall + ')');
if (typeof reinstall === 'undefined') {
reinstall = false;
}
return this.adb(
emulatorId,
'install ' +
(reinstall ? '-r ' : '') +
apkPath
).then(function(process) {
if (process.stdout.match(/Success/)) {
return undefined;
}
if (process.stdout.match(/Failure \[INSTALL_FAILED_ALREADY_EXISTS\]/)) {
throw new Error('Already installed');
}
throw new Error('Could not parse output of adb command');
});
},
stop: function(emulatorId) {
verbose('stop(' + emulatorId + ')');
return this.adb(emulatorId, 'emu kill');
},
inputKeyEvent: function(emulatorId, key) {
verbose('inputKeyEvent(' + emulatorId + ',' + key + ')');
return this.adb(emulatorId, 'shell input keyevent ' + key);
},
powerOn: function(emulatorId) {
verbose('powerOn(' + emulatorId + ')');
return this.inputKeyEvent(emulatorId, '26');
},
unlock: function(emulatorId) {
verbose('unlock(', emulatorId, ')');
return this.inputKeyEvent(emulatorId, '82');
},
devices: function() {
verbose('devices()');
return ezspawn('adb devices').then(function(output) {
var lines = output.stdout.split('\n');
lines.shift();
return lines
.map(function(line) {
var matches = line.match(/([^\s]+)\s+([^\s]+)/);
if (matches === null) {
return null;
}
return {
name: matches[1],
status: matches[2]
};
})
.filter(function(x) {
return x !== null;
});
});
},
listAVDs: function() {
verbose('listAVDs()');
return ezspawn('android list avds').then(function(output) {
var avds = processKeyValueGroups(output.stdout);
debug('got avds', avds);
return avds;
});
},
listTargets: function() {
verbose('listTargets()');
return ezspawn('android list targets').then(function(output) {
return processKeyValueGroups(output.stdout);
});
},
listPackages: function(emulatorId) {
verbose('listPackages(' + emulatorId + ')');
return this.adb(emulatorId, 'shell pm list packages').then(function(proc) {
var lines = proc.stdout.split('\n');
return lines
.map(function(line) {
return line.split(':')[1];
})
.filter(function(pkg) {
return pkg;
})
.map(function(pkg) {
return pkg.trim();
});
});
}
};
module.exports = Android;