diff --git a/packages/flutter/lib/src/services/binding.dart b/packages/flutter/lib/src/services/binding.dart index 4dd3e72c8f..d0acf572e2 100644 --- a/packages/flutter/lib/src/services/binding.dart +++ b/packages/flutter/lib/src/services/binding.dart @@ -89,12 +89,7 @@ mixin ServicesBinding on BindingBase, SchedulerBinding { } Stream _addLicenses() async* { - // We use timers here (rather than scheduleTask from the scheduler binding) - // because the services layer can't use the scheduler binding (the scheduler - // binding uses the services layer to manage its lifecycle events). Timers - // are what scheduleTask uses under the hood anyway. The only difference is - // that these will just run next, instead of being prioritized relative to - // the other tasks that might be running. Using _something_ here to break + // Using _something_ here to break // this into two parts is important because isolates take a while to copy // data at the moment, and if we receive the data in the same event loop // iteration as we send the data to the next isolate, we are definitely @@ -105,14 +100,14 @@ mixin ServicesBinding on BindingBase, SchedulerBinding { // https://github.com/dart-lang/sdk/issues/31960 // TODO(ianh): Remove this complexity once these bugs are fixed. final Completer rawLicenses = Completer(); - Timer.run(() async { + scheduleTask(() async { rawLicenses.complete(rootBundle.loadString('LICENSE', cache: false)); - }); + }, Priority.animation); await rawLicenses.future; final Completer> parsedLicenses = Completer>(); - Timer.run(() async { + scheduleTask(() async { parsedLicenses.complete(compute(_parseLicenses, await rawLicenses.future, debugLabel: 'parseLicenses')); - }); + }, Priority.animation); await parsedLicenses.future; yield* Stream.fromIterable(await parsedLicenses.future); } diff --git a/packages/flutter/test/services/binding_test.dart b/packages/flutter/test/services/binding_test.dart new file mode 100644 index 0000000000..4c32ec43c9 --- /dev/null +++ b/packages/flutter/test/services/binding_test.dart @@ -0,0 +1,69 @@ +// 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 'dart:typed_data'; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/scheduler.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter/src/services/binding.dart'; + +import 'package:flutter_test/flutter_test.dart'; + +const String license1 = ''' +L1Package1 +L1Package2 +L1Package3 + +L1Paragraph1 + +L1Paragraph2 + +L1Paragraph3'''; + +const String license2 = ''' +L2Package1 +L2Package2 +L2Package3 + +L2Paragraph1 + +L2Paragraph2 + +L2Paragraph3'''; + +const String licenses = ''' +$license1 +-------------------------------------------------------------------------------- +$license2 +'''; + +class TestBinding extends BindingBase with SchedulerBinding, ServicesBinding { + @override + BinaryMessenger createBinaryMessenger() { + return super.createBinaryMessenger() + ..setMockMessageHandler('flutter/assets', (ByteData message) async { + if (const StringCodec().decodeMessage(message) == 'LICENSE') { + return const StringCodec().encodeMessage(licenses); + } + return null; + }); + } +} + +void main() { + test('Adds rootBundle LICENSES to LicenseRegistry', () async { + TestBinding(); // The test binding registers a mock handler that returns licenses for the LICENSE key + + final List licenses = await LicenseRegistry.licenses.toList(); + + expect(licenses[0].packages, equals(['L1Package1', 'L1Package2', 'L1Package3'])); + expect(licenses[0].paragraphs.map((LicenseParagraph p) => p.text), + equals(['L1Paragraph1', 'L1Paragraph2', 'L1Paragraph3'])); + + expect(licenses[1].packages, equals(['L2Package1', 'L2Package2', 'L2Package3'])); + expect(licenses[1].paragraphs.map((LicenseParagraph p) => p.text), + equals(['L2Paragraph1', 'L2Paragraph2', 'L2Paragraph3'])); + }); +}