diff --git a/packages/flutter_tools/lib/runner.dart b/packages/flutter_tools/lib/runner.dart index e8014321b6e1..699fe6771ae0 100644 --- a/packages/flutter_tools/lib/runner.dart +++ b/packages/flutter_tools/lib/runner.dart @@ -201,10 +201,10 @@ Future _handleToolError( globals.analytics.send(Event.exception(exception: error.runtimeType.toString())); await asyncGuard(() async { final CrashReportSender crashReportSender = CrashReportSender( - usage: globals.flutterUsage, platform: globals.platform, logger: globals.logger, operatingSystemUtils: globals.os, + analytics: globals.analytics, ); await crashReportSender.sendReport( error: error, diff --git a/packages/flutter_tools/lib/src/reporting/crash_reporting.dart b/packages/flutter_tools/lib/src/reporting/crash_reporting.dart index df68535ae59f..d30c2b43ccb2 100644 --- a/packages/flutter_tools/lib/src/reporting/crash_reporting.dart +++ b/packages/flutter_tools/lib/src/reporting/crash_reporting.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'package:file/file.dart'; import 'package:http/http.dart' as http; +import 'package:unified_analytics/unified_analytics.dart'; import '../base/file_system.dart'; import '../base/io.dart'; @@ -15,7 +16,6 @@ import '../base/platform.dart'; import '../doctor.dart'; import '../project.dart'; import 'github_template.dart'; -import 'reporting.dart'; /// Tells crash backend that the error is from the Flutter CLI. const String _kProductId = 'Flutter_Tools'; @@ -105,21 +105,21 @@ class CrashReporter { class CrashReportSender { CrashReportSender({ http.Client? client, - required Usage usage, required Platform platform, required Logger logger, required OperatingSystemUtils operatingSystemUtils, + required Analytics analytics, }) : _client = client ?? http.Client(), - _usage = usage, _platform = platform, _logger = logger, - _operatingSystemUtils = operatingSystemUtils; + _operatingSystemUtils = operatingSystemUtils, + _analytics = analytics; final http.Client _client; - final Usage _usage; final Platform _platform; final Logger _logger; final OperatingSystemUtils _operatingSystemUtils; + final Analytics _analytics; bool _crashReportSent = false; @@ -154,7 +154,7 @@ class CrashReportSender { final String flutterVersion = getFlutterVersion(); // We don't need to report exceptions happening on user branches - if (_usage.suppressAnalytics || RegExp(r'^\[user-branch\]\/').hasMatch(flutterVersion)) { + if (!_analytics.okToSend || RegExp(r'^\[user-branch\]\/').hasMatch(flutterVersion)) { return; } @@ -168,7 +168,7 @@ class CrashReportSender { ); final http.MultipartRequest req = http.MultipartRequest('POST', uri); - req.fields['uuid'] = _usage.clientId; + req.fields['uuid'] = _analytics.clientId; req.fields['product'] = _kProductId; req.fields['version'] = flutterVersion; req.fields['osName'] = _platform.operatingSystem; diff --git a/packages/flutter_tools/test/general.shard/crash_reporting_test.dart b/packages/flutter_tools/test/general.shard/crash_reporting_test.dart index be8e2394375d..942225e55ebe 100644 --- a/packages/flutter_tools/test/general.shard/crash_reporting_test.dart +++ b/packages/flutter_tools/test/general.shard/crash_reporting_test.dart @@ -13,26 +13,30 @@ import 'package:flutter_tools/src/base/platform.dart'; import 'package:flutter_tools/src/doctor.dart'; import 'package:flutter_tools/src/project.dart'; import 'package:flutter_tools/src/reporting/crash_reporting.dart'; -import 'package:flutter_tools/src/reporting/reporting.dart'; import 'package:http/http.dart'; import 'package:http/testing.dart'; import 'package:test/fake.dart'; +import 'package:unified_analytics/unified_analytics.dart'; import '../src/common.dart'; import '../src/fake_process_manager.dart'; +import '../src/fakes.dart'; void main() { late BufferLogger logger; - late FileSystem fs; - late TestUsage testUsage; + late MemoryFileSystem fs; late Platform platform; late OperatingSystemUtils operatingSystemUtils; late StackTrace stackTrace; + late FakeAnalytics fakeAnalytics; setUp(() async { logger = BufferLogger.test(); fs = MemoryFileSystem.test(); - testUsage = TestUsage(); + fakeAnalytics = getInitializedFakeAnalyticsInstance( + fs: fs, + fakeFlutterVersion: FakeFlutterVersion(), + ); platform = FakePlatform(environment: {}); operatingSystemUtils = OperatingSystemUtils( @@ -63,7 +67,7 @@ void main() { 'version': 'test-version', }, )); - expect(crashInfo.fields?['uuid'], testUsage.clientId); + expect(crashInfo.fields?['uuid'], fakeAnalytics.clientId); expect(crashInfo.fields?['product'], 'Flutter_Tools'); expect(crashInfo.fields?['version'], 'test-version'); expect(crashInfo.fields?['osName'], 'linux'); @@ -104,14 +108,14 @@ void main() { }); testWithoutContext('suppress analytics', () async { - testUsage.suppressAnalytics = true; + fakeAnalytics.suppressTelemetry(); final CrashReportSender crashReportSender = CrashReportSender( client: CrashingCrashReportSender(const SocketException('no internets')), - usage: testUsage, platform: platform, logger: logger, operatingSystemUtils: operatingSystemUtils, + analytics: fakeAnalytics, ); await crashReportSender.sendReport( @@ -126,7 +130,7 @@ void main() { group('allow analytics', () { setUp(() async { - testUsage.suppressAnalytics = false; + await fakeAnalytics.setTelemetry(true); }); testWithoutContext('should send crash reports', () async { @@ -134,10 +138,10 @@ void main() { final CrashReportSender crashReportSender = CrashReportSender( client: MockCrashReportSender(requestInfo), - usage: testUsage, platform: platform, logger: logger, operatingSystemUtils: operatingSystemUtils, + analytics: fakeAnalytics, ); await crashReportSender.sendReport( @@ -153,10 +157,10 @@ void main() { testWithoutContext('should print an explanatory message when there is a SocketException', () async { final CrashReportSender crashReportSender = CrashReportSender( client: CrashingCrashReportSender(const SocketException('no internets')), - usage: testUsage, platform: platform, logger: logger, operatingSystemUtils: operatingSystemUtils, + analytics: fakeAnalytics, ); await crashReportSender.sendReport( @@ -172,10 +176,10 @@ void main() { testWithoutContext('should print an explanatory message when there is an HttpException', () async { final CrashReportSender crashReportSender = CrashReportSender( client: CrashingCrashReportSender(const HttpException('no internets')), - usage: testUsage, platform: platform, logger: logger, operatingSystemUtils: operatingSystemUtils, + analytics: fakeAnalytics, ); await crashReportSender.sendReport( @@ -191,10 +195,10 @@ void main() { testWithoutContext('should print an explanatory message when there is a ClientException', () async { final CrashReportSender crashReportSender = CrashReportSender( client: CrashingCrashReportSender(const HttpException('no internets')), - usage: testUsage, platform: platform, logger: logger, operatingSystemUtils: operatingSystemUtils, + analytics: fakeAnalytics, ); await crashReportSender.sendReport( @@ -212,10 +216,10 @@ void main() { final CrashReportSender crashReportSender = CrashReportSender( client: MockCrashReportSender(requestInfo), - usage: testUsage, platform: platform, logger: logger, operatingSystemUtils: operatingSystemUtils, + analytics: fakeAnalytics, ); await crashReportSender.sendReport( @@ -266,10 +270,10 @@ void main() { final CrashReportSender crashReportSender = CrashReportSender( client: mockClient, - usage: testUsage, platform: platform, logger: logger, operatingSystemUtils: operatingSystemUtils, + analytics: fakeAnalytics, ); await crashReportSender.sendReport( @@ -303,10 +307,10 @@ void main() { final CrashReportSender crashReportSender = CrashReportSender( client: mockClient, - usage: testUsage, platform: environmentPlatform, logger: logger, operatingSystemUtils: operatingSystemUtils, + analytics: fakeAnalytics, ); await crashReportSender.sendReport(