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

5
import 'package:collection/collection.dart' show ListEquality, MapEquality;
6 7

import 'package:flutter_devicelab/framework/adb.dart';
8
import 'package:meta/meta.dart';
9

10 11
import 'common.dart';

12
void main() {
13 14
  group('device', () {
    Device device;
15 16

    setUp(() {
17 18
      FakeDevice.resetLog();
      device = null;
19
      device = FakeDevice();
20 21 22 23 24
    });

    tearDown(() {
    });

25 26 27 28 29 30 31 32 33 34 35 36
    group('cpu check', () {
      test('arm64', () async {
        FakeDevice.pretendArm64();
        final AndroidDevice androidDevice = device as AndroidDevice;
        expect(await androidDevice.isArm64(), isTrue);
        expectLog(<CommandArgs>[
          cmd(command: 'getprop', arguments: <String>['ro.bootimage.build.fingerprint', ';', 'getprop', 'ro.build.version.release', ';', 'getprop', 'ro.build.version.sdk'], environment: null),
          cmd(command: 'getprop', arguments: <String>['ro.product.cpu.abi'], environment: null),
        ]);
      });
    });

37 38
    group('isAwake/isAsleep', () {
      test('reads Awake', () async {
39
        FakeDevice.pretendAwake();
40 41 42 43 44
        expect(await device.isAwake(), isTrue);
        expect(await device.isAsleep(), isFalse);
      });

      test('reads Asleep', () async {
45
        FakeDevice.pretendAsleep();
46 47 48 49 50 51 52 53 54
        expect(await device.isAwake(), isFalse);
        expect(await device.isAsleep(), isTrue);
      });
    });

    group('togglePower', () {
      test('sends power event', () async {
        await device.togglePower();
        expectLog(<CommandArgs>[
55
          cmd(command: 'getprop', arguments: <String>['ro.bootimage.build.fingerprint', ';', 'getprop', 'ro.build.version.release', ';', 'getprop', 'ro.build.version.sdk'], environment: null),
56 57 58 59 60 61 62
          cmd(command: 'input', arguments: <String>['keyevent', '26']),
        ]);
      });
    });

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

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

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

      test('when awake', () async {
93
        FakeDevice.pretendAwake();
94 95
        await device.sendToSleep();
        expectLog(<CommandArgs>[
96
          cmd(command: 'getprop', arguments: <String>['ro.bootimage.build.fingerprint', ';', 'getprop', 'ro.build.version.release', ';', 'getprop', 'ro.build.version.sdk'], environment: null),
97 98 99 100 101 102 103 104
          cmd(command: 'dumpsys', arguments: <String>['power']),
          cmd(command: 'input', arguments: <String>['keyevent', '26']),
        ]);
      });
    });

    group('unlock', () {
      test('sends unlock event', () async {
105
        FakeDevice.pretendAwake();
106 107
        await device.unlock();
        expectLog(<CommandArgs>[
108
          cmd(command: 'getprop', arguments: <String>['ro.bootimage.build.fingerprint', ';', 'getprop', 'ro.build.version.release', ';', 'getprop', 'ro.build.version.sdk'], environment: null),
109 110 111 112 113
          cmd(command: 'dumpsys', arguments: <String>['power']),
          cmd(command: 'input', arguments: <String>['keyevent', '82']),
        ]);
      });
    });
114 115 116 117 118

    group('adb', () {
      test('tap', () async {
        await device.tap(100, 200);
        expectLog(<CommandArgs>[
119
          cmd(command: 'getprop', arguments: <String>['ro.bootimage.build.fingerprint', ';', 'getprop', 'ro.build.version.release', ';', 'getprop', 'ro.build.version.sdk'], environment: null),
120 121 122 123
          cmd(command: 'input', arguments: <String>['tap', '100', '200']),
        ]);
      });
    });
124 125 126 127
  });
}

void expectLog(List<CommandArgs> log) {
128
  expect(FakeDevice.commandLog, log);
129 130
}

131 132 133 134 135
CommandArgs cmd({
  String command,
  List<String> arguments,
  Map<String, String> environment,
}) {
136
  return CommandArgs(
137 138 139 140 141
    command: command,
    arguments: arguments,
    environment: environment,
  );
}
142

143
typedef ExitErrorFactory = dynamic Function();
144

145
@immutable
146
class CommandArgs {
147
  const CommandArgs({ this.command, this.arguments, this.environment });
148 149 150

  final String command;
  final List<String> arguments;
151
  final Map<String, String> environment;
152 153

  @override
154
  String toString() => 'CommandArgs(command: $command, arguments: $arguments, environment: $environment)';
155 156 157 158 159

  @override
  bool operator==(Object other) {
    if (other.runtimeType != CommandArgs)
      return false;
160 161 162 163
    return other is CommandArgs
        && other.command == command
        && const ListEquality<String>().equals(other.arguments, arguments)
        && const MapEquality<String, String>().equals(other.environment, environment);
164 165 166
  }

  @override
167
  int get hashCode => 17 * (17 * command.hashCode + _hashArguments) + _hashEnvironment;
168 169 170 171 172

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

173 174
  int get _hashEnvironment => environment != null
    ? const MapEquality<String, String>().hash(environment)
175 176 177
    : null.hashCode;
}

178
class FakeDevice extends AndroidDevice {
179
  FakeDevice({String deviceId}) : super(deviceId: deviceId);
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

  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
    ''';
  }

202 203 204 205 206 207
  static void pretendArm64() {
    output = '''
      arm64
    ''';
  }

208
  @override
209
  Future<String> shellEval(String command, List<String> arguments, { Map<String, String> environment, bool silent = false }) async {
210
    commandLog.add(CommandArgs(
211 212
      command: command,
      arguments: arguments,
213
      environment: environment,
214 215 216 217 218
    ));
    return output;
  }

  @override
219
  Future<void> shellExec(String command, List<String> arguments, { Map<String, String> environment, bool silent = false }) async {
220
    commandLog.add(CommandArgs(
221 222
      command: command,
      arguments: arguments,
223
      environment: environment,
224
    ));
225
    final dynamic exitError = exitErrorFactory();
226 227 228 229
    if (exitError != null)
      throw exitError;
  }
}