android_install_test.dart 10.9 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 The Flutter 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 'package:file/memory.dart';
import 'package:flutter_tools/src/android/android_device.dart';
import 'package:flutter_tools/src/android/android_sdk.dart';
8
import 'package:flutter_tools/src/android/application_package.dart';
9
import 'package:flutter_tools/src/base/file_system.dart';
10 11
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
12
import 'package:test/fake.dart';
13 14

import '../../src/common.dart';
15
import '../../src/fake_process_manager.dart';
16 17 18 19 20 21 22 23 24

const FakeCommand kAdbVersionCommand = FakeCommand(
  command: <String>['adb', 'version'],
  stdout: 'Android Debug Bridge version 1.0.39',
);
const FakeCommand kAdbStartServerCommand = FakeCommand(
  command: <String>['adb', 'start-server']
);
const FakeCommand kInstallCommand = FakeCommand(
25 26 27 28 29 30 31 32 33
  command: <String>[
    'adb',
    '-s',
    '1234',
    'install',
    '-t',
    '-r',
    '--user',
    '10',
34
    'app.apk',
35
  ],
36 37 38 39 40 41
);
const FakeCommand kStoreShaCommand = FakeCommand(
  command: <String>['adb', '-s', '1234', 'shell', 'echo', '-n', '', '>', '/data/local/tmp/sky.app.sha1']
);

void main() {
42 43
  late FileSystem fileSystem;
  late BufferLogger logger;
44 45 46 47 48 49 50

  setUp(() {
    fileSystem = MemoryFileSystem.test();
    logger = BufferLogger.test();
  });

  AndroidDevice setUpAndroidDevice({
51 52
    AndroidSdk? androidSdk,
    ProcessManager? processManager,
53
  }) {
54
    androidSdk ??= FakeAndroidSdk();
55
    return AndroidDevice('1234',
56
      modelID: 'TestModel',
57
      logger: logger,
58
      platform: FakePlatform(),
59
      androidSdk: androidSdk,
60
      fileSystem: fileSystem,
61 62 63 64
      processManager: processManager ?? FakeProcessManager.any(),
    );
  }

65 66
  testWithoutContext('Cannot install app on API level below 16', () async {
    final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
67 68 69 70 71 72
      kAdbVersionCommand,
      kAdbStartServerCommand,
      const FakeCommand(
        command: <String>['adb', '-s', '1234', 'shell', 'getprop'],
        stdout: '[ro.build.version.sdk]: [11]',
      ),
73 74
    ]);
    final File apk = fileSystem.file('app.apk')..createSync();
75
    final AndroidApk androidApk = AndroidApk(
76
      applicationPackage: apk,
77 78 79 80
      id: 'app',
      versionCode: 22,
      launchActivity: 'Main',
    );
81 82 83
    final AndroidDevice androidDevice = setUpAndroidDevice(
      processManager: processManager,
    );
84 85

    expect(await androidDevice.installApp(androidApk), false);
86
    expect(processManager, hasNoRemainingExpectations);
87 88
  });

89 90
  testWithoutContext('Cannot install app if APK file is missing', () async {
    final File apk = fileSystem.file('app.apk');
91
    final AndroidApk androidApk = AndroidApk(
92
      applicationPackage: apk,
93 94 95 96
      id: 'app',
      versionCode: 22,
      launchActivity: 'Main',
    );
97 98
    final AndroidDevice androidDevice = setUpAndroidDevice(
    );
99

100 101 102 103 104
    expect(await androidDevice.installApp(androidApk), false);
  });

  testWithoutContext('Can install app on API level 16 or greater', () async {
    final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
105 106 107 108 109 110
      kAdbVersionCommand,
      kAdbStartServerCommand,
      const FakeCommand(
        command: <String>['adb', '-s', '1234', 'shell', 'getprop'],
        stdout: '[ro.build.version.sdk]: [16]',
      ),
111 112 113 114
      const FakeCommand(
        command: <String>['adb', '-s', '1234', 'shell', 'pm', 'list', 'packages', '--user', '10', 'app'],
        stdout: '\n'
      ),
115 116
      kInstallCommand,
      kStoreShaCommand,
117 118
    ]);
    final File apk = fileSystem.file('app.apk')..createSync();
119
    final AndroidApk androidApk = AndroidApk(
120
      applicationPackage: apk,
121 122 123 124
      id: 'app',
      versionCode: 22,
      launchActivity: 'Main',
    );
125 126 127
    final AndroidDevice androidDevice = setUpAndroidDevice(
      processManager: processManager,
    );
128

129
    expect(await androidDevice.installApp(androidApk, userIdentifier: '10'), true);
130
    expect(processManager, hasNoRemainingExpectations);
131 132 133 134
  });

  testWithoutContext('Defaults to API level 16 if adb returns a null response', () async {
    final FakeProcessManager processManager =  FakeProcessManager.list(<FakeCommand>[
135 136 137 138 139
      kAdbVersionCommand,
      kAdbStartServerCommand,
      const FakeCommand(
        command: <String>['adb', '-s', '1234', 'shell', 'getprop'],
      ),
140 141 142 143
      const FakeCommand(
        command: <String>['adb', '-s', '1234', 'shell', 'pm', 'list', 'packages', '--user', '10', 'app'],
        stdout: '\n'
      ),
144 145
      kInstallCommand,
      kStoreShaCommand,
146 147 148
    ]);
    final File apk = fileSystem.file('app.apk')..createSync();
    final AndroidApk androidApk = AndroidApk(
149
      applicationPackage: apk,
150 151 152 153 154 155 156 157
      id: 'app',
      versionCode: 22,
      launchActivity: 'Main',
    );
    final AndroidDevice androidDevice = setUpAndroidDevice(
      processManager: processManager,
    );

158
    expect(await androidDevice.installApp(androidApk, userIdentifier: '10'), true);
159
    expect(processManager, hasNoRemainingExpectations);
160 161
  });

162 163 164 165 166 167 168
  testWithoutContext('displays error if user not found', () async {
    final FakeProcessManager processManager =  FakeProcessManager.list(<FakeCommand>[
      kAdbVersionCommand,
      kAdbStartServerCommand,
      const FakeCommand(
        command: <String>['adb', '-s', '1234', 'shell', 'getprop'],
      ),
169 170 171 172 173 174
      // This command is run before the user is checked and is allowed to fail.
      const FakeCommand(
        command: <String>['adb', '-s', '1234', 'shell', 'pm', 'list', 'packages', '--user', 'jane', 'app'],
        stderr: 'Blah blah',
        exitCode: 1,
      ),
175 176 177 178 179 180 181 182 183 184
      const FakeCommand(
        command: <String>[
          'adb',
          '-s',
          '1234',
          'install',
          '-t',
          '-r',
          '--user',
          'jane',
185
          'app.apk',
186 187 188 189 190 191 192
        ],
        exitCode: 1,
        stderr: 'Exception occurred while executing: java.lang.IllegalArgumentException: Bad user number: jane',
      ),
    ]);
    final File apk = fileSystem.file('app.apk')..createSync();
    final AndroidApk androidApk = AndroidApk(
193
      applicationPackage: apk,
194 195 196 197 198 199 200 201 202 203
      id: 'app',
      versionCode: 22,
      launchActivity: 'Main',
    );
    final AndroidDevice androidDevice = setUpAndroidDevice(
      processManager: processManager,
    );

    expect(await androidDevice.installApp(androidApk, userIdentifier: 'jane'), false);
    expect(logger.errorText, contains('Error: User "jane" not found. Run "adb shell pm list users" to see list of available identifiers.'));
204
    expect(processManager, hasNoRemainingExpectations);
205
  });
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

  testWithoutContext('Will skip install if the correct version is up to date', () async {
    final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
      kAdbVersionCommand,
      kAdbStartServerCommand,
      const FakeCommand(
        command: <String>['adb', '-s', '1234', 'shell', 'getprop'],
        stdout: '[ro.build.version.sdk]: [16]',
      ),
      const FakeCommand(
        command: <String>['adb', '-s', '1234', 'shell', 'pm', 'list', 'packages', '--user', '10', 'app'],
        stdout: 'package:app\n'
      ),
      const FakeCommand(
        command: <String>['adb', '-s', '1234', 'shell', 'cat', '/data/local/tmp/sky.app.sha1'],
        stdout: 'example_sha',
      ),
    ]);
    final File apk = fileSystem.file('app.apk')..createSync();
    fileSystem.file('app.apk.sha1').writeAsStringSync('example_sha');
    final AndroidApk androidApk = AndroidApk(
227
      applicationPackage: apk,
228 229 230 231 232 233 234 235 236
      id: 'app',
      versionCode: 22,
      launchActivity: 'Main',
    );
    final AndroidDevice androidDevice = setUpAndroidDevice(
      processManager: processManager,
    );

    expect(await androidDevice.installApp(androidApk, userIdentifier: '10'), true);
237
    expect(processManager, hasNoRemainingExpectations);
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
  });

  testWithoutContext('Will uninstall if the correct version is not up to date and install fails', () async {
    final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
      kAdbVersionCommand,
      kAdbStartServerCommand,
      const FakeCommand(
        command: <String>['adb', '-s', '1234', 'shell', 'getprop'],
        stdout: '[ro.build.version.sdk]: [16]',
      ),
      const FakeCommand(
          command: <String>['adb', '-s', '1234', 'shell', 'pm', 'list', 'packages', '--user', '10', 'app'],
          stdout: 'package:app\n'
      ),
      const FakeCommand(
        command: <String>['adb', '-s', '1234', 'shell', 'cat', '/data/local/tmp/sky.app.sha1'],
        stdout: 'different_example_sha',
      ),
      const FakeCommand(
        command: <String>['adb', '-s', '1234', 'install', '-t', '-r', '--user', '10', 'app.apk'],
        exitCode: 1,
        stderr: '[INSTALL_FAILED_INSUFFICIENT_STORAGE]',
      ),
      const FakeCommand(command: <String>['adb', '-s', '1234', 'uninstall', '--user', '10', 'app']),
      kInstallCommand,
      const FakeCommand(command: <String>['adb', '-s', '1234', 'shell', 'echo', '-n', 'example_sha', '>', '/data/local/tmp/sky.app.sha1']),
    ]);
    final File apk = fileSystem.file('app.apk')..createSync();
    fileSystem.file('app.apk.sha1').writeAsStringSync('example_sha');
    final AndroidApk androidApk = AndroidApk(
268
      applicationPackage: apk,
269 270 271 272 273 274 275 276 277
      id: 'app',
      versionCode: 22,
      launchActivity: 'Main',
    );
    final AndroidDevice androidDevice = setUpAndroidDevice(
      processManager: processManager,
    );

    expect(await androidDevice.installApp(androidApk, userIdentifier: '10'), true);
278
    expect(processManager, hasNoRemainingExpectations);
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  });

  testWithoutContext('Will fail to install if the apk was never installed and it fails the first time', () async {
    final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
      kAdbVersionCommand,
      kAdbStartServerCommand,
      const FakeCommand(
        command: <String>['adb', '-s', '1234', 'shell', 'getprop'],
        stdout: '[ro.build.version.sdk]: [16]',
      ),
      const FakeCommand(
          command: <String>['adb', '-s', '1234', 'shell', 'pm', 'list', 'packages', '--user', '10', 'app'],
          stdout: '\n'
      ),
      const FakeCommand(
        command: <String>['adb', '-s', '1234', 'install', '-t', '-r', '--user', '10', 'app.apk'],
        exitCode: 1,
        stderr: '[INSTALL_FAILED_INSUFFICIENT_STORAGE]',
      ),
    ]);
    final File apk = fileSystem.file('app.apk')..createSync();
    final AndroidApk androidApk = AndroidApk(
301
      applicationPackage: apk,
302 303 304 305 306 307 308 309 310
      id: 'app',
      versionCode: 22,
      launchActivity: 'Main',
    );
    final AndroidDevice androidDevice = setUpAndroidDevice(
      processManager: processManager,
    );

    expect(await androidDevice.installApp(androidApk, userIdentifier: '10'), false);
311
    expect(processManager, hasNoRemainingExpectations);
312
  });
313 314
}

315 316 317 318
class FakeAndroidSdk extends Fake implements AndroidSdk {
  @override
  String get adbPath => 'adb';
}