adb_test.dart 5.19 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 10

import 'package:flutter_devicelab/framework/adb.dart';

11 12
import 'common.dart';

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

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

    tearDown(() {
    });

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

      test('reads Asleep', () async {
34
        FakeDevice.pretendAsleep();
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
        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 {
51
        FakeDevice.pretendAwake();
52 53 54 55 56 57 58
        await device.wakeUp();
        expectLog(<CommandArgs>[
          cmd(command: 'dumpsys', arguments: <String>['power']),
        ]);
      });

      test('when asleep', () async {
59
        FakeDevice.pretendAsleep();
60 61 62 63 64 65 66 67 68 69
        await device.wakeUp();
        expectLog(<CommandArgs>[
          cmd(command: 'dumpsys', arguments: <String>['power']),
          cmd(command: 'input', arguments: <String>['keyevent', '26']),
        ]);
      });
    });

    group('sendToSleep', () {
      test('when asleep', () async {
70
        FakeDevice.pretendAsleep();
71 72 73 74 75 76 77
        await device.sendToSleep();
        expectLog(<CommandArgs>[
          cmd(command: 'dumpsys', arguments: <String>['power']),
        ]);
      });

      test('when awake', () async {
78
        FakeDevice.pretendAwake();
79 80 81 82 83 84 85 86 87 88
        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 {
89
        FakeDevice.pretendAwake();
90 91 92 93 94 95 96
        await device.unlock();
        expectLog(<CommandArgs>[
          cmd(command: 'dumpsys', arguments: <String>['power']),
          cmd(command: 'input', arguments: <String>['keyevent', '82']),
        ]);
      });
    });
97 98 99 100 101 102 103 104 105

    group('adb', () {
      test('tap', () async {
        await device.tap(100, 200);
        expectLog(<CommandArgs>[
          cmd(command: 'input', arguments: <String>['tap', '100', '200']),
        ]);
      });
    });
106 107 108 109
  });
}

void expectLog(List<CommandArgs> log) {
110
  expect(FakeDevice.commandLog, log);
111 112
}

113 114 115 116 117
CommandArgs cmd({
  String command,
  List<String> arguments,
  Map<String, String> environment,
}) {
118
  return CommandArgs(
119 120 121 122 123
    command: command,
    arguments: arguments,
    environment: environment,
  );
}
124

125
typedef ExitErrorFactory = dynamic Function();
126 127

class CommandArgs {
128
  CommandArgs({ this.command, this.arguments, this.environment });
129 130 131

  final String command;
  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 138 139 140

  @override
  bool operator==(Object other) {
    if (other.runtimeType != CommandArgs)
      return false;
141 142 143 144
    return other is CommandArgs
        && other.command == command
        && const ListEquality<String>().equals(other.arguments, arguments)
        && const MapEquality<String, String>().equals(other.environment, environment);
145 146 147
  }

  @override
148
  int get hashCode => 17 * (17 * command.hashCode + _hashArguments) + _hashEnvironment;
149 150 151 152 153

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

154 155
  int get _hashEnvironment => environment != null
    ? const MapEquality<String, String>().hash(environment)
156 157 158
    : null.hashCode;
}

159
class FakeDevice extends AndroidDevice {
160
  FakeDevice({String deviceId}) : super(deviceId: deviceId);
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

  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
184
  Future<String> shellEval(String command, List<String> arguments, { Map<String, String> environment }) async {
185
    commandLog.add(CommandArgs(
186 187
      command: command,
      arguments: arguments,
188
      environment: environment,
189 190 191 192 193
    ));
    return output;
  }

  @override
194
  Future<void> shellExec(String command, List<String> arguments, { Map<String, String> environment }) async {
195
    commandLog.add(CommandArgs(
196 197
      command: command,
      arguments: arguments,
198
      environment: environment,
199
    ));
200
    final dynamic exitError = exitErrorFactory();
201 202 203 204
    if (exitError != null)
      throw exitError;
  }
}