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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:test/test.dart';
import 'package:collection/collection.dart';
import 'package:flutter_devicelab/framework/adb.dart';
void main() {
group('device', () {
Device device;
setUp(() {
FakeDevice.resetLog();
device = null;
device = new FakeDevice();
});
tearDown(() {
});
group('isAwake/isAsleep', () {
test('reads Awake', () async {
FakeDevice.pretendAwake();
expect(await device.isAwake(), isTrue);
expect(await device.isAsleep(), isFalse);
});
test('reads Asleep', () async {
FakeDevice.pretendAsleep();
expect(await device.isAwake(), isFalse);
expect(await device.isAsleep(), isTrue);
});
});
group('togglePower', () {
test('sends power event', () async {
await device.togglePower();
expectLog(<CommandArgs>[
cmd(command: 'input', arguments: <String>['keyevent', '26']),
]);
});
});
group('wakeUp', () {
test('when awake', () async {
FakeDevice.pretendAwake();
await device.wakeUp();
expectLog(<CommandArgs>[
cmd(command: 'dumpsys', arguments: <String>['power']),
]);
});
test('when asleep', () async {
FakeDevice.pretendAsleep();
await device.wakeUp();
expectLog(<CommandArgs>[
cmd(command: 'dumpsys', arguments: <String>['power']),
cmd(command: 'input', arguments: <String>['keyevent', '26']),
]);
});
});
group('sendToSleep', () {
test('when asleep', () async {
FakeDevice.pretendAsleep();
await device.sendToSleep();
expectLog(<CommandArgs>[
cmd(command: 'dumpsys', arguments: <String>['power']),
]);
});
test('when awake', () async {
FakeDevice.pretendAwake();
await device.sendToSleep();
expectLog(<CommandArgs>[
cmd(command: 'dumpsys', arguments: <String>['power']),
cmd(command: 'input', arguments: <String>['keyevent', '26']),
]);
});
});
group('unlock', () {
test('sends unlock event', () async {
FakeDevice.pretendAwake();
await device.unlock();
expectLog(<CommandArgs>[
cmd(command: 'dumpsys', arguments: <String>['power']),
cmd(command: 'input', arguments: <String>['keyevent', '82']),
]);
});
});
});
}
void expectLog(List<CommandArgs> log) {
expect(FakeDevice.commandLog, log);
}
CommandArgs cmd({String command, List<String> arguments, Map<String, String> env}) => new CommandArgs(
command: command,
arguments: arguments,
env: env
);
typedef dynamic ExitErrorFactory();
class CommandArgs {
CommandArgs({this.command, this.arguments, this.env});
final String command;
final List<String> arguments;
final Map<String, String> env;
@override
String toString() => 'CommandArgs(command: $command, arguments: $arguments, env: $env)';
@override
bool operator==(Object other) {
if (other.runtimeType != CommandArgs)
return false;
CommandArgs otherCmd = other;
return otherCmd.command == this.command &&
const ListEquality<String>().equals(otherCmd.arguments, this.arguments) &&
const MapEquality<String, String>().equals(otherCmd.env, this.env);
}
@override
int get hashCode => 17 * (17 * command.hashCode + _hashArguments) + _hashEnv;
int get _hashArguments => arguments != null
? const ListEquality<String>().hash(arguments)
: null.hashCode;
int get _hashEnv => env != null
? const MapEquality<String, String>().hash(env)
: null.hashCode;
}
class FakeDevice extends AndroidDevice {
FakeDevice({String deviceId: null}) : super(deviceId: deviceId);
static String output = '';
static ExitErrorFactory exitErrorFactory = () => null;
static List<CommandArgs> commandLog = <CommandArgs>[];
static void resetLog() {
commandLog.clear();
}
static void pretendAwake() {
output = '''
mWakefulness=Awake
''';
}
static void pretendAsleep() {
output = '''
mWakefulness=Asleep
''';
}
@override
Future<String> shellEval(String command, List<String> arguments, {Map<String, String> env}) async {
commandLog.add(new CommandArgs(
command: command,
arguments: arguments,
env: env
));
return output;
}
@override
Future<Null> shellExec(String command, List<String> arguments, {Map<String, String> env}) async {
commandLog.add(new CommandArgs(
command: command,
arguments: arguments,
env: env
));
dynamic exitError = exitErrorFactory();
if (exitError != null)
throw exitError;
}
}