adb_test.dart 4.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// 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';

import 'package:test/test.dart';
import 'package:collection/collection.dart';

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

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

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

    tearDown(() {
    });

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

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

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

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

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

void expectLog(List<CommandArgs> log) {
100
  expect(FakeDevice.commandLog, log);
101 102
}

103 104 105 106 107 108 109 110 111 112 113
CommandArgs cmd({
  String command,
  List<String> arguments,
  Map<String, String> environment,
}) {
  return new CommandArgs(
    command: command,
    arguments: arguments,
    environment: environment,
  );
}
114 115 116 117

typedef dynamic ExitErrorFactory();

class CommandArgs {
118
  CommandArgs({ this.command, this.arguments, this.environment });
119 120 121

  final String command;
  final List<String> arguments;
122
  final Map<String, String> environment;
123 124

  @override
125
  String toString() => 'CommandArgs(command: $command, arguments: $arguments, environment: $environment)';
126 127 128 129 130 131

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

132
    final CommandArgs otherCmd = other;
133 134 135
    return otherCmd.command == command &&
      const ListEquality<String>().equals(otherCmd.arguments, arguments) &&
      const MapEquality<String, String>().equals(otherCmd.environment, environment);
136 137 138
  }

  @override
139
  int get hashCode => 17 * (17 * command.hashCode + _hashArguments) + _hashEnvironment;
140 141 142 143 144

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

145 146
  int get _hashEnvironment => environment != null
    ? const MapEquality<String, String>().hash(environment)
147 148 149
    : null.hashCode;
}

150 151
class FakeDevice extends AndroidDevice {
  FakeDevice({String deviceId: null}) : super(deviceId: deviceId);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

  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
175
  Future<String> shellEval(String command, List<String> arguments, { Map<String, String> environment }) async {
176 177 178
    commandLog.add(new CommandArgs(
      command: command,
      arguments: arguments,
179
      environment: environment,
180 181 182 183 184
    ));
    return output;
  }

  @override
185
  Future<Null> shellExec(String command, List<String> arguments, { Map<String, String> environment }) async {
186 187 188
    commandLog.add(new CommandArgs(
      command: command,
      arguments: arguments,
189
      environment: environment,
190
    ));
191
    final dynamic exitError = exitErrorFactory();
192 193 194 195
    if (exitError != null)
      throw exitError;
  }
}