adb_test.dart 7.01 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 6
// @dart = 2.8

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

9
import 'package:flutter_devicelab/framework/devices.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
    });

    tearDown(() {
    });

27 28 29 30 31 32 33 34 35 36 37 38
    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),
        ]);
      });
    });

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

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

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

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

      test('when asleep', () async {
74
        FakeDevice.pretendAsleep();
75 76
        await device.wakeUp();
        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 83 84 85
          cmd(command: 'dumpsys', arguments: <String>['power']),
          cmd(command: 'input', arguments: <String>['keyevent', '26']),
        ]);
      });
    });

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

      test('when awake', () async {
95
        FakeDevice.pretendAwake();
96 97
        await device.sendToSleep();
        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 104 105 106
          cmd(command: 'dumpsys', arguments: <String>['power']),
          cmd(command: 'input', arguments: <String>['keyevent', '26']),
        ]);
      });
    });

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

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

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

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

145
typedef ExitErrorFactory = dynamic Function();
146

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

  final String command;
  final List<String> arguments;
153
  final Map<String, String> environment;
154 155

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

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

  @override
169 170 171 172 173 174 175 176
  int get hashCode {
    return Object.hash(
      command,
      Object.hashAll(arguments ?? const <String>[]),
      Object.hashAllUnordered(environment?.keys ?? const <String>[]),
      Object.hashAllUnordered(environment?.values ?? const <String>[]),
    );
  }
177 178
}

179
class FakeDevice extends AndroidDevice {
180
  FakeDevice({String deviceId}) : super(deviceId: deviceId);
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 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 226
    ));
  }
}