From 51b23bc961c792cc59e14d0f0bbd07c9b0f4a500 Mon Sep 17 00:00:00 2001 From: Elias Yishak <42216813+eliasyishak@users.noreply.github.com> Date: Wed, 12 Apr 2023 15:34:11 -0400 Subject: [PATCH] Update runner to handle logic for both analytics packages (#124606) Update runner to handle logic for both analytics packages --- packages/flutter_tools/lib/runner.dart | 59 +++++++++++---- .../general.shard/runner/runner_test.dart | 75 ++++++++++++++----- 2 files changed, 101 insertions(+), 33 deletions(-) diff --git a/packages/flutter_tools/lib/runner.dart b/packages/flutter_tools/lib/runner.dart index 893626d95e..7858632a01 100644 --- a/packages/flutter_tools/lib/runner.dart +++ b/packages/flutter_tools/lib/runner.dart @@ -22,6 +22,7 @@ import 'src/context_runner.dart'; import 'src/doctor.dart'; import 'src/globals.dart' as globals; import 'src/reporting/crash_reporting.dart'; +import 'src/reporting/first_run.dart'; import 'src/reporting/reporting.dart'; import 'src/runner/flutter_command.dart'; import 'src/runner/flutter_command_runner.dart'; @@ -62,19 +63,6 @@ Future run( StackTrace? firstStackTrace; return runZoned>(() async { try { - // Ensure that the consent message has been displayed - if (globals.analytics.shouldShowMessage) { - globals.logger.printStatus(globals.analytics.getConsentMessage); - - // Invoking this will onboard the flutter tool onto - // the package on the developer's machine and will - // allow for events to be sent to Google Analytics - // on subsequent runs of the flutter tool (ie. no events - // will be sent on the first run to allow developers to - // opt out of collection) - globals.analytics.clientShowedMessage(); - } - // Disable analytics if user passes in the `--disable-telemetry` option // `flutter --disable-telemetry` // @@ -100,6 +88,14 @@ Future run( await globals.analytics.setTelemetry(value); } + // If the user has opted out of legacy analytics, we will continue + // to opt them out of unified analytics and inform them + if (!globals.flutterUsage.enabled && globals.analytics.telemetryEnabled) { + await globals.analytics.setTelemetry(false); + globals.logger.printStatus( + 'Please note that analytics reporting was already disabled, and will continue to be disabled.\n'); + } + await runner.run(args); // Triggering [runZoned]'s error callback does not necessarily mean that @@ -275,9 +271,44 @@ Future _createLocalCrashReport(CrashDetails details) async { } Future _exit(int code, {required ShutdownHooks shutdownHooks}) async { - // Prints the welcome message if needed. + // Need to get the boolean returned from `messenger.shouldDisplayLicenseTerms()` + // before invoking the print welcome method because the print welcome method + // will set `messenger.shouldDisplayLicenseTerms()` to false + final FirstRunMessenger messenger = + FirstRunMessenger(persistentToolState: globals.persistentToolState!); + final bool legacyAnalyticsMessageShown = + messenger.shouldDisplayLicenseTerms(); + + // Prints the welcome message if needed for legacy analytics. globals.flutterUsage.printWelcome(); + // Ensure that the consent message has been displayed for unified analytics + if (globals.analytics.shouldShowMessage) { + globals.logger.printStatus(globals.analytics.getConsentMessage); + + // Because the legacy analytics may have also sent a message, + // the conditional below will print additional messaging informing + // users that the two consent messages they are receiving is not a + // bug + if (legacyAnalyticsMessageShown) { + globals.logger + .printStatus('You have received two consent messages because ' + 'the flutter tool is migrating to a new analytics system. ' + 'Disabling analytics collection will disable both the legacy ' + 'and new analytics collection systems. ' + 'You can disable analytics reporting by running either `flutter --disable-telemetry` ' + 'or `flutter config --no-analytics\n'); + } + + // Invoking this will onboard the flutter tool onto + // the package on the developer's machine and will + // allow for events to be sent to Google Analytics + // on subsequent runs of the flutter tool (ie. no events + // will be sent on the first run to allow developers to + // opt out of collection) + globals.analytics.clientShowedMessage(); + } + // Send any last analytics calls that are in progress without overly delaying // the tool's exit (we wait a maximum of 250ms). if (globals.flutterUsage.enabled) { diff --git a/packages/flutter_tools/test/general.shard/runner/runner_test.dart b/packages/flutter_tools/test/general.shard/runner/runner_test.dart index 6fde91ee59..ff4bc7c37e 100644 --- a/packages/flutter_tools/test/general.shard/runner/runner_test.dart +++ b/packages/flutter_tools/test/general.shard/runner/runner_test.dart @@ -316,28 +316,65 @@ void main() { }); }); - testUsingContext('runner disable telemetry with flag', () async { - io.setExitFunctionForTests((int exitCode) {}); + group('unified_analytics', () { + final Usage legacyAnalytics = TestUsage(); + setUp(() { + legacyAnalytics.enabled = false; + }); - expect(globals.analytics.telemetryEnabled, true); - expect(globals.analytics.shouldShowMessage, true); + testUsingContext( + 'runner disable telemetry with flag', + () async { + io.setExitFunctionForTests((int exitCode) {}); - await runner.run( - ['--disable-telemetry'], - () => [], - // This flutterVersion disables crash reporting. - flutterVersion: '[user-branch]/', - shutdownHooks: ShutdownHooks(), - ); + expect(globals.analytics.telemetryEnabled, true); + expect(globals.analytics.shouldShowMessage, true); - expect(globals.analytics.telemetryEnabled, false); - }, - overrides: { - Analytics: () => FakeAnalytics(), - FileSystem: () => MemoryFileSystem.test(), - ProcessManager: () => FakeProcessManager.any(), - }, - ); + await runner.run( + ['--disable-telemetry'], + () => [], + // This flutterVersion disables crash reporting. + flutterVersion: '[user-branch]/', + shutdownHooks: ShutdownHooks(), + ); + + expect(globals.analytics.telemetryEnabled, false); + }, + overrides: { + Analytics: () => FakeAnalytics(), + FileSystem: () => MemoryFileSystem.test(), + ProcessManager: () => FakeProcessManager.any(), + }, + ); + + testUsingContext( + 'legacy analytics disabled will disable new analytics', + () async { + + io.setExitFunctionForTests((int exitCode) {}); + + await runner.run( + [], + () => [], + // This flutterVersion disables crash reporting. + flutterVersion: '[user-branch]/', + shutdownHooks: ShutdownHooks(), + ); + + expect(globals.flutterUsage.enabled, false); + expect(globals.analytics.telemetryEnabled, false); + expect(testLogger.statusText.contains( + 'Please note that analytics ' + 'reporting was already disabled'), true); + }, + overrides: { + Analytics: () => FakeAnalytics(), + FileSystem: () => MemoryFileSystem.test(), + ProcessManager: () => FakeProcessManager.any(), + Usage: () => legacyAnalytics, + }, + ); + }); } class CrashingFlutterCommand extends FlutterCommand {