diff --git a/packages/flutter_tools/lib/src/android/aar.dart b/packages/flutter_tools/lib/src/android/aar.dart deleted file mode 100644 index f619f775e0f..00000000000 --- a/packages/flutter_tools/lib/src/android/aar.dart +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright 2019 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:meta/meta.dart'; - -import '../base/common.dart'; -import '../build_info.dart'; -import '../project.dart'; - -import 'android_sdk.dart'; -import 'gradle.dart'; - -/// Provides a method to build a module or plugin as AAR. -abstract class AarBuilder { - /// Builds the AAR artifacts. - Future build({ - @required FlutterProject project, - @required AndroidBuildInfo androidBuildInfo, - @required String target, - @required String outputDir, - }); -} - -/// Default implementation of [AarBuilder]. -class AarBuilderImpl extends AarBuilder { - AarBuilderImpl(); - - /// Builds the AAR and POM files for the current Flutter module or plugin. - @override - Future build({ - @required FlutterProject project, - @required AndroidBuildInfo androidBuildInfo, - @required String target, - @required String outputDir, - }) async { - if (!project.android.isUsingGradle) { - throwToolExit( - 'The build process for Android has changed, and the current project configuration\n' - 'is no longer valid. Please consult\n\n' - ' https://github.com/flutter/flutter/wiki/Upgrading-Flutter-projects-to-build-with-gradle\n\n' - 'for details on how to upgrade the project.' - ); - } - if (!project.manifest.isModule && !project.manifest.isPlugin) { - throwToolExit('AARs can only be built for plugin or module projects.'); - } - // Validate that we can find an Android SDK. - if (androidSdk == null) { - throwToolExit('No Android SDK found. Try setting the `ANDROID_SDK_ROOT` environment variable.'); - } - await buildGradleAar( - project: project, - androidBuildInfo: androidBuildInfo, - target: target, - outputDir: outputDir, - ); - androidSdk.reinitialize(); - } -} diff --git a/packages/flutter_tools/lib/src/android/android_builder.dart b/packages/flutter_tools/lib/src/android/android_builder.dart new file mode 100644 index 00000000000..fdff9800af0 --- /dev/null +++ b/packages/flutter_tools/lib/src/android/android_builder.dart @@ -0,0 +1,169 @@ +// Copyright 2019 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:meta/meta.dart'; + +import '../base/common.dart'; +import '../base/context.dart'; +import '../build_info.dart'; +import '../globals.dart'; +import '../project.dart'; + +import 'android_sdk.dart'; +import 'gradle.dart'; + +/// The builder in the current context. +AndroidBuilder get androidBuilder => context.get() ?? _AndroidBuilderImpl(); + +/// Provides the methods to build Android artifacts. +abstract class AndroidBuilder { + /// Builds an AAR artifact. + Future buildAar({ + @required FlutterProject project, + @required AndroidBuildInfo androidBuildInfo, + @required String target, + @required String outputDir, + }); + + /// Builds an APK artifact. + Future buildApk({ + @required FlutterProject project, + @required AndroidBuildInfo androidBuildInfo, + @required String target, + }); + + /// Builds an App Bundle artifact. + Future buildAab({ + @required FlutterProject project, + @required AndroidBuildInfo androidBuildInfo, + @required String target, + }); +} + +/// Default implementation of [AarBuilder]. +class _AndroidBuilderImpl extends AndroidBuilder { + _AndroidBuilderImpl(); + + /// Builds the AAR and POM files for the current Flutter module or plugin. + @override + Future buildAar({ + @required FlutterProject project, + @required AndroidBuildInfo androidBuildInfo, + @required String target, + @required String outputDir, + }) async { + if (!project.android.isUsingGradle) { + throwToolExit( + 'The build process for Android has changed, and the current project configuration ' + 'is no longer valid. Please consult\n\n' + ' https://github.com/flutter/flutter/wiki/Upgrading-Flutter-projects-to-build-with-gradle\n\n' + 'for details on how to upgrade the project.' + ); + } + if (!project.manifest.isModule && !project.manifest.isPlugin) { + throwToolExit('AARs can only be built for plugin or module projects.'); + } + // Validate that we can find an Android SDK. + if (androidSdk == null) { + throwToolExit('No Android SDK found. Try setting the `ANDROID_SDK_ROOT` environment variable.'); + } + await buildGradleAar( + project: project, + androidBuildInfo: androidBuildInfo, + target: target, + outputDir: outputDir, + ); + androidSdk.reinitialize(); + } + + /// Builds the APK. + @override + Future buildApk({ + @required FlutterProject project, + @required AndroidBuildInfo androidBuildInfo, + @required String target, + }) async { + if (!project.android.isUsingGradle) { + throwToolExit( + 'The build process for Android has changed, and the current project configuration ' + 'is no longer valid. Please consult\n\n' + ' https://github.com/flutter/flutter/wiki/Upgrading-Flutter-projects-to-build-with-gradle\n\n' + 'for details on how to upgrade the project.' + ); + } + // Validate that we can find an android sdk. + if (androidSdk == null) { + throwToolExit('No Android SDK found. Try setting the ANDROID_SDK_ROOT environment variable.'); + } + await buildGradleProject( + project: project, + androidBuildInfo: androidBuildInfo, + target: target, + isBuildingBundle: false, + ); + androidSdk.reinitialize(); + } + + /// Builds the App Bundle. + @override + Future buildAab({ + @required FlutterProject project, + @required AndroidBuildInfo androidBuildInfo, + @required String target, + }) async { + if (!project.android.isUsingGradle) { + throwToolExit( + 'The build process for Android has changed, and the current project configuration ' + 'is no longer valid. Please consult\n\n' + 'https://github.com/flutter/flutter/wiki/Upgrading-Flutter-projects-to-build-with-gradle\n\n' + 'for details on how to upgrade the project.' + ); + } + // Validate that we can find an android sdk. + if (androidSdk == null) { + throwToolExit('No Android SDK found. Try setting the ANDROID_HOME environment variable.'); + } + final List validationResult = androidSdk.validateSdkWellFormed(); + if (validationResult.isNotEmpty) { + for (String message in validationResult) { + printError(message, wrap: false); + } + throwToolExit('Try re-installing or updating your Android SDK.'); + } + return buildGradleProject( + project: project, + androidBuildInfo: androidBuildInfo, + target: target, + isBuildingBundle: true, + ); + } +} + +/// A fake implementation of [AndroidBuilder]. +@visibleForTesting +class FakeAndroidBuilder implements AndroidBuilder { + @override + Future buildAar({ + @required FlutterProject project, + @required AndroidBuildInfo androidBuildInfo, + @required String target, + @required String outputDir, + }) async {} + + @override + Future buildApk({ + @required FlutterProject project, + @required AndroidBuildInfo androidBuildInfo, + @required String target, + }) async {} + + @override + Future buildAab({ + @required FlutterProject project, + @required AndroidBuildInfo androidBuildInfo, + @required String target, + }) async {} +} diff --git a/packages/flutter_tools/lib/src/android/android_device.dart b/packages/flutter_tools/lib/src/android/android_device.dart index d250a129a11..327197e74b5 100644 --- a/packages/flutter_tools/lib/src/android/android_device.dart +++ b/packages/flutter_tools/lib/src/android/android_device.dart @@ -6,9 +6,9 @@ import 'dart:async'; import 'package:meta/meta.dart'; +import '../android/android_builder.dart'; import '../android/android_sdk.dart'; import '../android/android_workflow.dart'; -import '../android/apk.dart'; import '../application_package.dart'; import '../base/common.dart' show throwToolExit; import '../base/file_system.dart'; @@ -482,7 +482,7 @@ class AndroidDevice extends Device { if (!prebuiltApplication || androidSdk.licensesAvailable && androidSdk.latestVersion == null) { printTrace('Building APK'); final FlutterProject project = FlutterProject.current(); - await buildApk( + await androidBuilder.buildApk( project: project, target: mainPath, androidBuildInfo: AndroidBuildInfo(debuggingOptions.buildInfo, diff --git a/packages/flutter_tools/lib/src/android/apk.dart b/packages/flutter_tools/lib/src/android/apk.dart deleted file mode 100644 index 908c535f7d0..00000000000 --- a/packages/flutter_tools/lib/src/android/apk.dart +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2015 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:meta/meta.dart'; - -import '../base/common.dart'; -import '../build_info.dart'; -import '../project.dart'; - -import 'android_sdk.dart'; -import 'gradle.dart'; - -Future buildApk({ - @required FlutterProject project, - @required String target, - @required AndroidBuildInfo androidBuildInfo, -}) async { - if (!project.android.isUsingGradle) { - throwToolExit( - 'The build process for Android has changed, and the current project configuration\n' - 'is no longer valid. Please consult\n\n' - ' https://github.com/flutter/flutter/wiki/Upgrading-Flutter-projects-to-build-with-gradle\n\n' - 'for details on how to upgrade the project.' - ); - } - - // Validate that we can find an android sdk. - if (androidSdk == null) - throwToolExit('No Android SDK found. Try setting the ANDROID_SDK_ROOT environment variable.'); - - await buildGradleProject( - project: project, - androidBuildInfo: androidBuildInfo, - target: target, - isBuildingBundle: false, - ); - androidSdk.reinitialize(); -} diff --git a/packages/flutter_tools/lib/src/android/app_bundle.dart b/packages/flutter_tools/lib/src/android/app_bundle.dart deleted file mode 100644 index 4f304c1a56f..00000000000 --- a/packages/flutter_tools/lib/src/android/app_bundle.dart +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2015 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:meta/meta.dart'; - -import '../base/common.dart'; -import '../build_info.dart'; -import '../globals.dart'; -import '../project.dart'; - -import 'android_sdk.dart'; -import 'gradle.dart'; - -Future buildAppBundle({ - @required FlutterProject project, - @required String target, - @required AndroidBuildInfo androidBuildInfo, -}) async { - if (!project.android.isUsingGradle) { - throwToolExit( - 'The build process for Android has changed, and the current project configuration\n' - 'is no longer valid. Please consult\n\n' - 'https://github.com/flutter/flutter/wiki/Upgrading-Flutter-projects-to-build-with-gradle\n\n' - 'for details on how to upgrade the project.' - ); - } - - // Validate that we can find an android sdk. - if (androidSdk == null) - throwToolExit('No Android SDK found. Try setting the ANDROID_HOME environment variable.'); - - final List validationResult = androidSdk.validateSdkWellFormed(); - if (validationResult.isNotEmpty) { - for (String message in validationResult) { - printError(message, wrap: false); - } - throwToolExit('Try re-installing or updating your Android SDK.'); - } - - return buildGradleProject( - project: project, - androidBuildInfo: androidBuildInfo, - target: target, - isBuildingBundle: true, - ); -} diff --git a/packages/flutter_tools/lib/src/commands/build_aar.dart b/packages/flutter_tools/lib/src/commands/build_aar.dart index 73c415aef59..a7ef6750a37 100644 --- a/packages/flutter_tools/lib/src/commands/build_aar.dart +++ b/packages/flutter_tools/lib/src/commands/build_aar.dart @@ -4,8 +4,7 @@ import 'dart:async'; -import '../android/aar.dart'; -import '../base/context.dart'; +import '../android/android_builder.dart'; import '../base/os.dart'; import '../build_info.dart'; import '../project.dart'; @@ -13,9 +12,6 @@ import '../reporting/reporting.dart'; import '../runner/flutter_command.dart' show DevelopmentArtifact, FlutterCommandResult; import 'build.dart'; -/// The AAR builder in the current context. -AarBuilder get aarBuilder => context.get() ?? AarBuilderImpl(); - class BuildAarCommand extends BuildSubCommand { BuildAarCommand({bool verboseHelp = false}) { addBuildModeFlags(verboseHelp: verboseHelp); @@ -74,7 +70,7 @@ class BuildAarCommand extends BuildSubCommand { final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(buildInfo, targetArchs: argResults['target-platform'].map(getAndroidArchForName)); - await aarBuilder.build( + await androidBuilder.buildAar( project: _getProject(), target: '', // Not needed because this command only builds Android's code. androidBuildInfo: androidBuildInfo, diff --git a/packages/flutter_tools/lib/src/commands/build_apk.dart b/packages/flutter_tools/lib/src/commands/build_apk.dart index 437fc4c9486..95b230ce09f 100644 --- a/packages/flutter_tools/lib/src/commands/build_apk.dart +++ b/packages/flutter_tools/lib/src/commands/build_apk.dart @@ -4,11 +4,12 @@ import 'dart:async'; -import '../android/apk.dart'; +import '../android/android_builder.dart'; import '../base/terminal.dart'; import '../build_info.dart'; import '../globals.dart'; import '../project.dart'; +import '../reporting/reporting.dart'; import '../runner/flutter_command.dart' show DevelopmentArtifact, FlutterCommandResult; import 'build.dart'; @@ -39,18 +40,40 @@ class BuildApkCommand extends BuildSubCommand { @override final String name = 'apk'; - @override - Future> get requiredArtifacts async => const { - DevelopmentArtifact.universal, - DevelopmentArtifact.android, - }; - @override final String description = 'Build an Android APK file from your app.\n\n' 'This command can build debug and release versions of your application. \'debug\' builds support ' 'debugging and a quick development cycle. \'release\' builds don\'t support debugging and are ' 'suitable for deploying to app stores.'; + @override + Future> get usageValues async { + final Map usage = {}; + + usage[CustomDimensions.commandBuildApkTargetPlatform] = + (argResults['target-platform'] as List).join(','); + usage[CustomDimensions.commandBuildApkSplitPerAbi] = + argResults['split-per-abi'].toString(); + + if (argResults['release']) { + usage[CustomDimensions.commandBuildApkBuildMode] = 'release'; + } else if (argResults['debug']) { + usage[CustomDimensions.commandBuildApkBuildMode] = 'debug'; + } else if (argResults['profile']) { + usage[CustomDimensions.commandBuildApkBuildMode] = 'profile'; + } else { + // The build defaults to release. + usage[CustomDimensions.commandBuildApkBuildMode] = 'release'; + } + return usage; + } + + @override + Future> get requiredArtifacts async => const { + DevelopmentArtifact.universal, + DevelopmentArtifact.android, + }; + @override Future runCommand() async { final BuildInfo buildInfo = getBuildInfo(); @@ -76,7 +99,7 @@ class BuildApkCommand extends BuildSubCommand { '--split-per-abi', indent: 8); printStatus('Learn more on: https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split',indent: 8); } - await buildApk( + await androidBuilder.buildApk( project: FlutterProject.current(), target: targetFile, androidBuildInfo: androidBuildInfo, diff --git a/packages/flutter_tools/lib/src/commands/build_appbundle.dart b/packages/flutter_tools/lib/src/commands/build_appbundle.dart index 76c5ad1725d..7496708b0ff 100644 --- a/packages/flutter_tools/lib/src/commands/build_appbundle.dart +++ b/packages/flutter_tools/lib/src/commands/build_appbundle.dart @@ -4,9 +4,10 @@ import 'dart:async'; -import '../android/app_bundle.dart'; +import '../android/android_builder.dart'; import '../build_info.dart'; import '../project.dart'; +import '../reporting/reporting.dart'; import '../runner/flutter_command.dart' show FlutterCommandResult; import 'build.dart'; @@ -39,12 +40,32 @@ class BuildAppBundleCommand extends BuildSubCommand { 'debugging and a quick development cycle. \'release\' builds don\'t support debugging and are ' 'suitable for deploying to app stores. \n app bundle improves your app size'; + @override + Future> get usageValues async { + final Map usage = {}; + + usage[CustomDimensions.commandBuildAppBundleTargetPlatform] = + (argResults['target-platform'] as List).join(','); + + if (argResults['release']) { + usage[CustomDimensions.commandBuildAppBundleBuildMode] = 'release'; + } else if (argResults['debug']) { + usage[CustomDimensions.commandBuildAppBundleBuildMode] = 'debug'; + } else if (argResults['profile']) { + usage[CustomDimensions.commandBuildAppBundleBuildMode] = 'profile'; + } else { + // The build defaults to release. + usage[CustomDimensions.commandBuildAppBundleBuildMode] = 'release'; + } + return usage; + } + @override Future runCommand() async { final AndroidBuildInfo androidBuildInfo = AndroidBuildInfo(getBuildInfo(), targetArchs: argResults['target-platform'].map(getAndroidArchForName) ); - await buildAppBundle( + await androidBuilder.buildAab( project: FlutterProject.current(), target: targetFile, androidBuildInfo: androidBuildInfo, diff --git a/packages/flutter_tools/lib/src/reporting/usage.dart b/packages/flutter_tools/lib/src/reporting/usage.dart index de48d6526f3..0f33c975719 100644 --- a/packages/flutter_tools/lib/src/reporting/usage.dart +++ b/packages/flutter_tools/lib/src/reporting/usage.dart @@ -48,6 +48,11 @@ enum CustomDimensions { commandBuildAarProjectType, // cd35 buildEventCommand, // cd36 buildEventSettings, // cd37 + commandBuildApkTargetPlatform, // cd38 + commandBuildApkBuildMode, // cd39 + commandBuildApkSplitPerAbi, // cd40 + commandBuildAppBundleTargetPlatform, // cd41 + commandBuildAppBundleBuildMode, // cd42 } String cdKey(CustomDimensions cd) => 'cd${cd.index + 1}'; diff --git a/packages/flutter_tools/test/general.shard/commands/build_aar_test.dart b/packages/flutter_tools/test/general.shard/commands/build_aar_test.dart index 67cfb5be0de..466438d12ef 100644 --- a/packages/flutter_tools/test/general.shard/commands/build_aar_test.dart +++ b/packages/flutter_tools/test/general.shard/commands/build_aar_test.dart @@ -3,12 +3,11 @@ // found in the LICENSE file. import 'package:args/command_runner.dart'; -import 'package:flutter_tools/src/android/aar.dart'; +import 'package:flutter_tools/src/android/android_builder.dart'; import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/commands/build_aar.dart'; import 'package:flutter_tools/src/reporting/reporting.dart'; -import 'package:mockito/mockito.dart'; import '../../src/common.dart'; import '../../src/context.dart'; @@ -18,16 +17,9 @@ void main() { group('getUsage', () { Directory tempDir; - AarBuilder mockAarBuilder; setUp(() { tempDir = fs.systemTempDirectory.createTempSync('flutter_tools_packages_test.'); - mockAarBuilder = MockAarBuilder(); - when(mockAarBuilder.build( - project: anyNamed('project'), - androidBuildInfo: anyNamed('androidBuildInfo'), - target: anyNamed('target'), - outputDir: anyNamed('outputDir'))).thenAnswer((_) => Future.value()); }); tearDown(() { @@ -54,7 +46,7 @@ void main() { containsPair(CustomDimensions.commandBuildAarProjectType, 'module')); }, overrides: { - AarBuilder: () => mockAarBuilder, + AndroidBuilder: () => FakeAndroidBuilder(), }, timeout: allowForCreateFlutterProject); testUsingContext('indicate that project is a plugin', () async { @@ -66,7 +58,7 @@ void main() { containsPair(CustomDimensions.commandBuildAarProjectType, 'plugin')); }, overrides: { - AarBuilder: () => mockAarBuilder, + AndroidBuilder: () => FakeAndroidBuilder(), }, timeout: allowForCreateFlutterProject); testUsingContext('indicate the target platform', () async { @@ -79,9 +71,7 @@ void main() { containsPair(CustomDimensions.commandBuildAarTargetPlatform, 'android-arm')); }, overrides: { - AarBuilder: () => mockAarBuilder, + AndroidBuilder: () => FakeAndroidBuilder(), }, timeout: allowForCreateFlutterProject); }); } - -class MockAarBuilder extends Mock implements AarBuilder {} diff --git a/packages/flutter_tools/test/general.shard/commands/build_apk_test.dart b/packages/flutter_tools/test/general.shard/commands/build_apk_test.dart new file mode 100644 index 00000000000..b8b35726c40 --- /dev/null +++ b/packages/flutter_tools/test/general.shard/commands/build_apk_test.dart @@ -0,0 +1,96 @@ +// Copyright 2019 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 'package:args/command_runner.dart'; +import 'package:flutter_tools/src/android/android_builder.dart'; +import 'package:flutter_tools/src/base/file_system.dart'; +import 'package:flutter_tools/src/cache.dart'; +import 'package:flutter_tools/src/commands/build_apk.dart'; +import 'package:flutter_tools/src/reporting/reporting.dart'; + +import '../../src/common.dart'; +import '../../src/context.dart'; + +void main() { + Cache.disableLocking(); + + group('getUsage', () { + Directory tempDir; + + setUp(() { + tempDir = fs.systemTempDirectory.createTempSync('flutter_tools_packages_test.'); + }); + + tearDown(() { + tryToDelete(tempDir); + }); + + Future runCommandIn(String target, { List arguments }) async { + final BuildApkCommand command = BuildApkCommand(); + final CommandRunner runner = createTestCommandRunner(command); + await runner.run([ + 'apk', + ...?arguments, + fs.path.join(target, 'lib', 'main.dart'), + ]); + return command; + } + + testUsingContext('indicate the default target platforms', () async { + final String projectPath = await createProject(tempDir, + arguments: ['--no-pub', '--template=app']); + final BuildApkCommand command = await runCommandIn(projectPath); + + expect(await command.usageValues, + containsPair(CustomDimensions.commandBuildApkTargetPlatform, 'android-arm,android-arm64')); + + }, overrides: { + AndroidBuilder: () => FakeAndroidBuilder(), + }, timeout: allowForCreateFlutterProject); + + testUsingContext('split per abi', () async { + final String projectPath = await createProject(tempDir, + arguments: ['--no-pub', '--template=app']); + + final BuildApkCommand commandWithFlag = await runCommandIn(projectPath, + arguments: ['--split-per-abi']); + expect(await commandWithFlag.usageValues, + containsPair(CustomDimensions.commandBuildApkSplitPerAbi, 'true')); + + final BuildApkCommand commandWithoutFlag = await runCommandIn(projectPath); + expect(await commandWithoutFlag.usageValues, + containsPair(CustomDimensions.commandBuildApkSplitPerAbi, 'false')); + + }, overrides: { + AndroidBuilder: () => FakeAndroidBuilder(), + }, timeout: allowForCreateFlutterProject); + + testUsingContext('build type', () async { + final String projectPath = await createProject(tempDir, + arguments: ['--no-pub', '--template=app']); + + final BuildApkCommand commandDefault = await runCommandIn(projectPath); + expect(await commandDefault.usageValues, + containsPair(CustomDimensions.commandBuildApkBuildMode, 'release')); + + final BuildApkCommand commandInRelease = await runCommandIn(projectPath, + arguments: ['--release']); + expect(await commandInRelease.usageValues, + containsPair(CustomDimensions.commandBuildApkBuildMode, 'release')); + + final BuildApkCommand commandInDebug = await runCommandIn(projectPath, + arguments: ['--debug']); + expect(await commandInDebug.usageValues, + containsPair(CustomDimensions.commandBuildApkBuildMode, 'debug')); + + final BuildApkCommand commandInProfile = await runCommandIn(projectPath, + arguments: ['--profile']); + expect(await commandInProfile.usageValues, + containsPair(CustomDimensions.commandBuildApkBuildMode, 'profile')); + + }, overrides: { + AndroidBuilder: () => FakeAndroidBuilder(), + }, timeout: allowForCreateFlutterProject); + }); +} diff --git a/packages/flutter_tools/test/general.shard/commands/build_appbundle_test.dart b/packages/flutter_tools/test/general.shard/commands/build_appbundle_test.dart new file mode 100644 index 00000000000..31fb604ffc7 --- /dev/null +++ b/packages/flutter_tools/test/general.shard/commands/build_appbundle_test.dart @@ -0,0 +1,79 @@ +// Copyright 2019 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 'package:args/command_runner.dart'; +import 'package:flutter_tools/src/android/android_builder.dart'; +import 'package:flutter_tools/src/base/file_system.dart'; +import 'package:flutter_tools/src/cache.dart'; +import 'package:flutter_tools/src/commands/build_appbundle.dart'; +import 'package:flutter_tools/src/reporting/reporting.dart'; + +import '../../src/common.dart'; +import '../../src/context.dart'; + +void main() { + Cache.disableLocking(); + + group('getUsage', () { + Directory tempDir; + + setUp(() { + tempDir = fs.systemTempDirectory.createTempSync('flutter_tools_packages_test.'); + }); + + tearDown(() { + tryToDelete(tempDir); + }); + + Future runCommandIn(String target, { List arguments }) async { + final BuildAppBundleCommand command = BuildAppBundleCommand(); + final CommandRunner runner = createTestCommandRunner(command); + await runner.run([ + 'appbundle', + ...?arguments, + fs.path.join(target, 'lib', 'main.dart'), + ]); + return command; + } + + testUsingContext('indicate the default target platforms', () async { + final String projectPath = await createProject(tempDir, + arguments: ['--no-pub', '--template=app']); + final BuildAppBundleCommand command = await runCommandIn(projectPath); + + expect(await command.usageValues, + containsPair(CustomDimensions.commandBuildAppBundleTargetPlatform, 'android-arm,android-arm64')); + + }, overrides: { + AndroidBuilder: () => FakeAndroidBuilder(), + }, timeout: allowForCreateFlutterProject); + + testUsingContext('build type', () async { + final String projectPath = await createProject(tempDir, + arguments: ['--no-pub', '--template=app']); + + final BuildAppBundleCommand commandDefault = await runCommandIn(projectPath); + expect(await commandDefault.usageValues, + containsPair(CustomDimensions.commandBuildAppBundleBuildMode, 'release')); + + final BuildAppBundleCommand commandInRelease = await runCommandIn(projectPath, + arguments: ['--release']); + expect(await commandInRelease.usageValues, + containsPair(CustomDimensions.commandBuildAppBundleBuildMode, 'release')); + + final BuildAppBundleCommand commandInDebug = await runCommandIn(projectPath, + arguments: ['--debug']); + expect(await commandInDebug.usageValues, + containsPair(CustomDimensions.commandBuildAppBundleBuildMode, 'debug')); + + final BuildAppBundleCommand commandInProfile = await runCommandIn(projectPath, + arguments: ['--profile']); + expect(await commandInProfile.usageValues, + containsPair(CustomDimensions.commandBuildAppBundleBuildMode, 'profile')); + + }, overrides: { + AndroidBuilder: () => FakeAndroidBuilder(), + }, timeout: allowForCreateFlutterProject); + }); +}