adb_test.dart 6.6 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

7
import 'package:collection/collection.dart' show ListEquality, MapEquality;
8 9

import 'package:flutter_devicelab/framework/adb.dart';
10
import 'package:meta/meta.dart';
11

12 13
import 'common.dart';

14
void main() {
15 16
  group('device', () {
    Device device;
17 18

    setUp(() {
19 20
      FakeDevice.resetLog();
      device = null;
21
      device = FakeDevice();
22 23 24 25 26 27 28
    });

    tearDown(() {
    });

    group('isAwake/isAsleep', () {
      test('reads Awake', () async {
29
        FakeDevice.pretendAwake();
30 31 32 33 34
        expect(await device.isAwake(), isTrue);
        expect(await device.isAsleep(), isFalse);
      });

      test('reads Asleep', () async {
35
        FakeDevice.pretendAsleep();
36 37 38 39 40 41 42 43 44
        expect(await device.isAwake(), isFalse);
        expect(await device.isAsleep(), isTrue);
      });
    });

    group('togglePower', () {
      test('sends power event', () async {
        await device.togglePower();
        expectLog(<CommandArgs>[
45
          cmd(command: 'getprop', arguments: <String>['ro.bootimage.build.fingerprint', ';', 'getprop', 'ro.build.version.release', ';', 'getprop', 'ro.build.version.sdk'], environment: null),
46 47 48 49 50 51 52
          cmd(command: 'input', arguments: <String>['keyevent', '26']),
        ]);
      });
    });

    group('wakeUp', () {
      test('when awake', () async {
53
        FakeDevice.pretendAwake();
54 55
        await device.wakeUp();
        expectLog(<CommandArgs>[
56
          cmd(command: 'getprop', arguments: <String>['ro.bootimage.build.fingerprint', ';', 'getprop', 'ro.build.version.release', ';', 'getprop', 'ro.build.version.sdk'], environment: null),
57 58 59 60 61
          cmd(command: 'dumpsys', arguments: <String>['power']),
        ]);
      });

      test('when asleep', () async {
62
        FakeDevice.pretendAsleep();
63 64
        await device.wakeUp();
        expectLog(<CommandArgs>[
65
          cmd(command: 'getprop', arguments: <String>['ro.bootimage.build.fingerprint', ';', 'getprop', 'ro.build.version.release', ';', 'getprop', 'ro.build.version.sdk'], environment: null),
66 67 68 69 70 71 72 73
          cmd(command: 'dumpsys', arguments: <String>['power']),
          cmd(command: 'input', arguments: <String>['keyevent', '26']),
        ]);
      });
    });

    group('sendToSleep', () {
      test('when asleep', () async {
74
        FakeDevice.pretendAsleep();
75 76
        await device.sendToSleep();
        expectLog(<CommandArgs>[
77
          cmd(command: 'getprop', arguments: <String>['ro.bootimage.build.fingerprint', ';', 'getprop', 'ro.build.version.release', ';', 'getprop', 'ro.build.version.sdk'], environment: null),
78 79 80 81 82
          cmd(command: 'dumpsys', arguments: <String>['power']),
        ]);
      });

      test('when awake', () async {
83
        FakeDevice.pretendAwake();
84 85
        await device.sendToSleep();
        expectLog(<CommandArgs>[
86
          cmd(command: 'getprop', arguments: <String>['ro.bootimage.build.fingerprint', ';', 'getprop', 'ro.build.version.release', ';', 'getprop', 'ro.build.version.sdk'], environment: null),
87 88 89 90 91 92 93 94
          cmd(command: 'dumpsys', arguments: <String>['power']),
          cmd(command: 'input', arguments: <String>['keyevent', '26']),
        ]);
      });
    });

    group('unlock', () {
      test('sends unlock event', () async {
95
        FakeDevice.pretendAwake();
96 97
        await device.unlock();
        expectLog(<CommandArgs>[
98
          cmd(command: 'getprop', arguments: <String>['ro.bootimage.build.fingerprint', ';', 'getprop', 'ro.build.version.release', ';', 'getprop', 'ro.build.version.sdk'], environment: null),
99 100 101 102 103
          cmd(command: 'dumpsys', arguments: <String>['power']),
          cmd(command: 'input', arguments: <String>['keyevent', '82']),
        ]);
      });
    });
104 105 106 107 108

    group('adb', () {
      test('tap', () async {
        await device.tap(100, 200);
        expectLog(<CommandArgs>[
109
          cmd(command: 'getprop', arguments: <String>['ro.bootimage.build.fingerprint', ';', 'getprop', 'ro.build.version.release', ';', 'getprop', 'ro.build.version.sdk'], environment: null),
110 111 112 113
          cmd(command: 'input', arguments: <String>['tap', '100', '200']),
        ]);
      });
    });
114 115 116 117
  });
}

void expectLog(List<CommandArgs> log) {
118
  expect(FakeDevice.commandLog, log);
119 120
}

121 122 123 124 125
CommandArgs cmd({
  String command,
  List<String> arguments,
  Map<String, String> environment,
}) {
126
  return CommandArgs(
127 128 129 130 131
    command: command,
    arguments: arguments,
    environment: environment,
  );
}
132

133
typedef ExitErrorFactory = dynamic Function();
134

135
@immutable
136
class CommandArgs {
137
  const CommandArgs({ this.command, this.arguments, this.environment });
138 139 140

  final String command;
  final List<String> arguments;
141
  final Map<String, String> environment;
142 143

  @override
144
  String toString() => 'CommandArgs(command: $command, arguments: $arguments, environment: $environment)';
145 146 147 148 149

  @override
  bool operator==(Object other) {
    if (other.runtimeType != CommandArgs)
      return false;
150 151 152 153
    return other is CommandArgs
        && other.command == command
        && const ListEquality<String>().equals(other.arguments, arguments)
        && const MapEquality<String, String>().equals(other.environment, environment);
154 155 156
  }

  @override
157
  int get hashCode => 17 * (17 * command.hashCode + _hashArguments) + _hashEnvironment;
158 159 160 161 162

  int get _hashArguments => arguments != null
    ? const ListEquality<String>().hash(arguments)
    : null.hashCode;

163 164
  int get _hashEnvironment => environment != null
    ? const MapEquality<String, String>().hash(environment)
165 166 167
    : null.hashCode;
}

168
class FakeDevice extends AndroidDevice {
169
  FakeDevice({String deviceId}) : super(deviceId: deviceId);
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

  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
193
  Future<String> shellEval(String command, List<String> arguments, { Map<String, String> environment, bool silent = false }) async {
194
    commandLog.add(CommandArgs(
195 196
      command: command,
      arguments: arguments,
197
      environment: environment,
198 199 200 201 202
    ));
    return output;
  }

  @override
203
  Future<void> shellExec(String command, List<String> arguments, { Map<String, String> environment, bool silent = false }) async {
204
    commandLog.add(CommandArgs(
205 206
      command: command,
      arguments: arguments,
207
      environment: environment,
208
    ));
209
    final dynamic exitError = exitErrorFactory();
210 211 212 213
    if (exitError != null)
      throw exitError;
  }
}