adb_test.dart 5.21 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 The Chromium Authors. All rights reserved.
// 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 141

  @override
  bool operator==(Object other) {
    if (other.runtimeType != CommandArgs)
      return false;

142
    final CommandArgs otherCmd = other;
143 144 145
    return otherCmd.command == command &&
      const ListEquality<String>().equals(otherCmd.arguments, arguments) &&
      const MapEquality<String, String>().equals(otherCmd.environment, environment);
146 147 148
  }

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

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

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

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

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

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