flutter/packages/flutter_tools/test/general.shard/android/android_install_test.dart
Alex Li 9fd5bddc65
🐛 [tool] Installs the APK regardless of version (#160432)
Resolves https://github.com/flutter/flutter/issues/160402

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2024-12-30 02:26:27 +00:00

342 lines
11 KiB
Dart

// 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';
import 'package:flutter_tools/src/android/application_package.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:test/fake.dart';
import '../../src/common.dart';
import '../../src/fake_process_manager.dart';
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(
command: <String>['adb', '-s', '1234', 'install', '-t', '-r', '--user', '10', 'app-debug.apk'],
);
const FakeCommand kStoreShaCommand = FakeCommand(
command: <String>[
'adb',
'-s',
'1234',
'shell',
'echo',
'-n',
'',
'>',
'/data/local/tmp/sky.app.sha1',
],
);
void main() {
late FileSystem fileSystem;
late BufferLogger logger;
setUp(() {
fileSystem = MemoryFileSystem.test();
logger = BufferLogger.test();
});
AndroidDevice setUpAndroidDevice({AndroidSdk? androidSdk, ProcessManager? processManager}) {
androidSdk ??= FakeAndroidSdk();
return AndroidDevice(
'1234',
modelID: 'TestModel',
logger: logger,
platform: FakePlatform(),
androidSdk: androidSdk,
fileSystem: fileSystem,
processManager: processManager ?? FakeProcessManager.any(),
);
}
testWithoutContext('Cannot install app on API level below 16', () async {
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
kAdbVersionCommand,
kAdbStartServerCommand,
const FakeCommand(
command: <String>['adb', '-s', '1234', 'shell', 'getprop'],
stdout: '[ro.build.version.sdk]: [11]',
),
]);
final File apk = fileSystem.file('app-debug.apk')..createSync();
final AndroidApk androidApk = AndroidApk(
applicationPackage: apk,
id: 'app',
versionCode: 22,
launchActivity: 'Main',
);
final AndroidDevice androidDevice = setUpAndroidDevice(processManager: processManager);
expect(await androidDevice.installApp(androidApk), false);
expect(processManager, hasNoRemainingExpectations);
});
testWithoutContext('Cannot install app if APK file is missing', () async {
final File apk = fileSystem.file('app-debug.apk');
final AndroidApk androidApk = AndroidApk(
applicationPackage: apk,
id: 'app',
versionCode: 22,
launchActivity: 'Main',
);
final AndroidDevice androidDevice = setUpAndroidDevice();
expect(await androidDevice.installApp(androidApk), false);
});
testWithoutContext('Can install app on API level 16 or greater', () async {
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
kAdbVersionCommand,
kAdbStartServerCommand,
const FakeCommand(
command: <String>['adb', '-s', '1234', 'shell', 'getprop'],
stdout: '[ro.build.version.sdk]: [16]',
),
kInstallCommand,
kStoreShaCommand,
]);
final File apk = fileSystem.file('app-debug.apk')..createSync();
final AndroidApk androidApk = AndroidApk(
applicationPackage: apk,
id: 'app',
versionCode: 22,
launchActivity: 'Main',
);
final AndroidDevice androidDevice = setUpAndroidDevice(processManager: processManager);
expect(await androidDevice.installApp(androidApk, userIdentifier: '10'), true);
expect(processManager, hasNoRemainingExpectations);
});
testWithoutContext('Defaults to API level 16 if adb returns a null response', () async {
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
kAdbVersionCommand,
kAdbStartServerCommand,
const FakeCommand(command: <String>['adb', '-s', '1234', 'shell', 'getprop']),
kInstallCommand,
kStoreShaCommand,
]);
final File apk = fileSystem.file('app-debug.apk')..createSync();
final AndroidApk androidApk = AndroidApk(
applicationPackage: apk,
id: 'app',
versionCode: 22,
launchActivity: 'Main',
);
final AndroidDevice androidDevice = setUpAndroidDevice(processManager: processManager);
expect(await androidDevice.installApp(androidApk, userIdentifier: '10'), true);
expect(processManager, hasNoRemainingExpectations);
});
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']),
// This command is run before the user is checked and is allowed to fail.
const FakeCommand(
command: <String>[
'adb',
'-s',
'1234',
'install',
'-t',
'-r',
'--user',
'jane',
'app-debug.apk',
],
exitCode: 1,
stderr:
'Exception occurred while executing: '
'java.lang.IllegalArgumentException: '
'Bad user number: jane',
),
]);
final File apk = fileSystem.file('app-debug.apk')..createSync();
final AndroidApk androidApk = AndroidApk(
applicationPackage: apk,
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.',
),
);
expect(processManager, hasNoRemainingExpectations);
});
testWithoutContext('Will continue 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]',
),
kInstallCommand,
const FakeCommand(
command: <String>[
'adb',
'-s',
'1234',
'shell',
'echo',
'-n',
'example_sha',
'>',
'/data/local/tmp/sky.app.sha1',
],
stdout: 'example_sha',
),
]);
final File apk = fileSystem.file('app-debug.apk')..createSync();
fileSystem.file('app-debug.apk.sha1').writeAsStringSync('example_sha');
final AndroidApk androidApk = AndroidApk(
applicationPackage: apk,
id: 'app',
versionCode: 22,
launchActivity: 'Main',
);
final AndroidDevice androidDevice = setUpAndroidDevice(processManager: processManager);
expect(await androidDevice.installApp(androidApk, userIdentifier: '10'), true);
expect(processManager, hasNoRemainingExpectations);
});
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',
'install',
'-t',
'-r',
'--user',
'10',
'app-debug.apk',
],
exitCode: 1,
stderr: '[INSTALL_FAILED_INSUFFICIENT_STORAGE]',
),
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', '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-debug.apk')..createSync();
fileSystem.file('app-debug.apk.sha1').writeAsStringSync('example_sha');
final AndroidApk androidApk = AndroidApk(
applicationPackage: apk,
id: 'app',
versionCode: 22,
launchActivity: 'Main',
);
final AndroidDevice androidDevice = setUpAndroidDevice(processManager: processManager);
expect(await androidDevice.installApp(androidApk, userIdentifier: '10'), true);
expect(processManager, hasNoRemainingExpectations);
},
);
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',
'install',
'-t',
'-r',
'--user',
'10',
'app-debug.apk',
],
exitCode: 1,
stderr: '[INSTALL_FAILED_INSUFFICIENT_STORAGE]',
),
]);
final File apk = fileSystem.file('app-debug.apk')..createSync();
final AndroidApk androidApk = AndroidApk(
applicationPackage: apk,
id: 'app',
versionCode: 22,
launchActivity: 'Main',
);
final AndroidDevice androidDevice = setUpAndroidDevice(processManager: processManager);
expect(await androidDevice.installApp(androidApk, userIdentifier: '10'), false);
expect(processManager, hasNoRemainingExpectations);
},
);
}
class FakeAndroidSdk extends Fake implements AndroidSdk {
@override
String get adbPath => 'adb';
}