• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'dart:async';
6
7import 'package:collection/collection.dart' show ListEquality, MapEquality;
8
9import 'package:flutter_devicelab/framework/adb.dart';
10
11import 'common.dart';
12
13void main() {
14  group('device', () {
15    Device device;
16
17    setUp(() {
18      FakeDevice.resetLog();
19      device = null;
20      device = FakeDevice();
21    });
22
23    tearDown(() {
24    });
25
26    group('isAwake/isAsleep', () {
27      test('reads Awake', () async {
28        FakeDevice.pretendAwake();
29        expect(await device.isAwake(), isTrue);
30        expect(await device.isAsleep(), isFalse);
31      });
32
33      test('reads Asleep', () async {
34        FakeDevice.pretendAsleep();
35        expect(await device.isAwake(), isFalse);
36        expect(await device.isAsleep(), isTrue);
37      });
38    });
39
40    group('togglePower', () {
41      test('sends power event', () async {
42        await device.togglePower();
43        expectLog(<CommandArgs>[
44          cmd(command: 'input', arguments: <String>['keyevent', '26']),
45        ]);
46      });
47    });
48
49    group('wakeUp', () {
50      test('when awake', () async {
51        FakeDevice.pretendAwake();
52        await device.wakeUp();
53        expectLog(<CommandArgs>[
54          cmd(command: 'dumpsys', arguments: <String>['power']),
55        ]);
56      });
57
58      test('when asleep', () async {
59        FakeDevice.pretendAsleep();
60        await device.wakeUp();
61        expectLog(<CommandArgs>[
62          cmd(command: 'dumpsys', arguments: <String>['power']),
63          cmd(command: 'input', arguments: <String>['keyevent', '26']),
64        ]);
65      });
66    });
67
68    group('sendToSleep', () {
69      test('when asleep', () async {
70        FakeDevice.pretendAsleep();
71        await device.sendToSleep();
72        expectLog(<CommandArgs>[
73          cmd(command: 'dumpsys', arguments: <String>['power']),
74        ]);
75      });
76
77      test('when awake', () async {
78        FakeDevice.pretendAwake();
79        await device.sendToSleep();
80        expectLog(<CommandArgs>[
81          cmd(command: 'dumpsys', arguments: <String>['power']),
82          cmd(command: 'input', arguments: <String>['keyevent', '26']),
83        ]);
84      });
85    });
86
87    group('unlock', () {
88      test('sends unlock event', () async {
89        FakeDevice.pretendAwake();
90        await device.unlock();
91        expectLog(<CommandArgs>[
92          cmd(command: 'dumpsys', arguments: <String>['power']),
93          cmd(command: 'input', arguments: <String>['keyevent', '82']),
94        ]);
95      });
96    });
97
98    group('adb', () {
99      test('tap', () async {
100        await device.tap(100, 200);
101        expectLog(<CommandArgs>[
102          cmd(command: 'input', arguments: <String>['tap', '100', '200']),
103        ]);
104      });
105    });
106  });
107}
108
109void expectLog(List<CommandArgs> log) {
110  expect(FakeDevice.commandLog, log);
111}
112
113CommandArgs cmd({
114  String command,
115  List<String> arguments,
116  Map<String, String> environment,
117}) {
118  return CommandArgs(
119    command: command,
120    arguments: arguments,
121    environment: environment,
122  );
123}
124
125typedef ExitErrorFactory = dynamic Function();
126
127class CommandArgs {
128  CommandArgs({ this.command, this.arguments, this.environment });
129
130  final String command;
131  final List<String> arguments;
132  final Map<String, String> environment;
133
134  @override
135  String toString() => 'CommandArgs(command: $command, arguments: $arguments, environment: $environment)';
136
137  @override
138  bool operator==(Object other) {
139    if (other.runtimeType != CommandArgs)
140      return false;
141
142    final CommandArgs otherCmd = other;
143    return otherCmd.command == command &&
144      const ListEquality<String>().equals(otherCmd.arguments, arguments) &&
145      const MapEquality<String, String>().equals(otherCmd.environment, environment);
146  }
147
148  @override
149  int get hashCode => 17 * (17 * command.hashCode + _hashArguments) + _hashEnvironment;
150
151  int get _hashArguments => arguments != null
152    ? const ListEquality<String>().hash(arguments)
153    : null.hashCode;
154
155  int get _hashEnvironment => environment != null
156    ? const MapEquality<String, String>().hash(environment)
157    : null.hashCode;
158}
159
160class FakeDevice extends AndroidDevice {
161  FakeDevice({String deviceId}) : super(deviceId: deviceId);
162
163  static String output = '';
164  static ExitErrorFactory exitErrorFactory = () => null;
165
166  static List<CommandArgs> commandLog = <CommandArgs>[];
167
168  static void resetLog() {
169    commandLog.clear();
170  }
171
172  static void pretendAwake() {
173    output = '''
174      mWakefulness=Awake
175    ''';
176  }
177
178  static void pretendAsleep() {
179    output = '''
180      mWakefulness=Asleep
181    ''';
182  }
183
184  @override
185  Future<String> shellEval(String command, List<String> arguments, { Map<String, String> environment }) async {
186    commandLog.add(CommandArgs(
187      command: command,
188      arguments: arguments,
189      environment: environment,
190    ));
191    return output;
192  }
193
194  @override
195  Future<void> shellExec(String command, List<String> arguments, { Map<String, String> environment }) async {
196    commandLog.add(CommandArgs(
197      command: command,
198      arguments: arguments,
199      environment: environment,
200    ));
201    final dynamic exitError = exitErrorFactory();
202    if (exitError != null)
203      throw exitError;
204  }
205}
206