diff --git a/lib/web_ui/lib/src/engine/platform_dispatcher.dart b/lib/web_ui/lib/src/engine/platform_dispatcher.dart index a1b5305b86663..89eaee544b411 100644 --- a/lib/web_ui/lib/src/engine/platform_dispatcher.dart +++ b/lib/web_ui/lib/src/engine/platform_dispatcher.dart @@ -333,9 +333,20 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher { final MethodCall decoded = codec.decodeMethodCall(data); switch (decoded.method) { case 'Skia.setResourceCacheMaxBytes': - if (decoded.arguments is int) { - rasterizer?.setSkiaResourceCacheMaxBytes(decoded.arguments); + if (useCanvasKit) { + // If we're in CanvasKit mode, we must also have a rasterizer. + assert(rasterizer != null); + assert( + decoded.arguments is int, + 'Argument to Skia.setResourceCacheMaxBytes must be an int, but was ${decoded.arguments.runtimeType}', + ); + final int cacheSizeInBytes = decoded.arguments as int; + rasterizer!.setSkiaResourceCacheMaxBytes(cacheSizeInBytes); } + + // Also respond in HTML mode. Otherwise, apps would have to detect + // CanvasKit vs HTML before invoking this method. + _replyToPlatformMessage(callback, codec.encodeSuccessEnvelope([true])); break; } return; @@ -896,9 +907,9 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher { /// messages and responses have to be exchanged asynchronously. We simulate /// that by adding a zero-length delay to the reply. void _replyToPlatformMessage( - ui.PlatformMessageResponseCallback? callback, - ByteData? data, - ) { + ui.PlatformMessageResponseCallback? callback, + ByteData? data, + ) { Future.delayed(Duration.zero).then((_) { if (callback != null) { callback(data); diff --git a/lib/web_ui/test/canvaskit/platform_dispatcher_test.dart b/lib/web_ui/test/canvaskit/platform_dispatcher_test.dart new file mode 100644 index 0000000000000..605731e565920 --- /dev/null +++ b/lib/web_ui/test/canvaskit/platform_dispatcher_test.dart @@ -0,0 +1,46 @@ +// Copyright 2013 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. + +// @dart = 2.12 +import 'dart:async'; +import 'dart:typed_data'; + +import 'package:ui/src/engine.dart'; +import 'package:ui/ui.dart' as ui; + +import 'package:test/bootstrap/browser.dart'; +import 'package:test/test.dart'; + +import 'common.dart'; + +void main() { + internalBootstrapBrowserTest(() => testMain); +} + +void testMain() { + group('PlatformDispatcher', () { + setUpCanvasKitTest(); + + test('responds to flutter/skia Skia.setResourceCacheMaxBytes', () async { + const MethodCodec codec = JSONMethodCodec(); + final Completer completer = Completer(); + ui.PlatformDispatcher.instance.sendPlatformMessage( + 'flutter/skia', + codec.encodeMethodCall(MethodCall( + 'Skia.setResourceCacheMaxBytes', + 512 * 1000 * 1000, + )), + completer.complete, + ); + + final ByteData? response = await completer.future; + expect(response, isNotNull); + expect( + codec.decodeEnvelope(response!), + [true], + ); + }); + // TODO: https://github.com/flutter/flutter/issues/60040 + }, skip: isIosSafari); +} diff --git a/lib/web_ui/test/engine/platform_dispatcher_test.dart b/lib/web_ui/test/engine/platform_dispatcher_test.dart new file mode 100644 index 0000000000000..56704f7231ca5 --- /dev/null +++ b/lib/web_ui/test/engine/platform_dispatcher_test.dart @@ -0,0 +1,41 @@ +// Copyright 2013 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. + +// @dart = 2.12 +import 'dart:async'; +import 'dart:typed_data'; + +import 'package:ui/src/engine.dart'; +import 'package:ui/ui.dart' as ui; + +import 'package:test/bootstrap/browser.dart'; +import 'package:test/test.dart'; + +void main() { + internalBootstrapBrowserTest(() => testMain); +} + +void testMain() { + group('PlatformDispatcher', () { + test('responds to flutter/skia Skia.setResourceCacheMaxBytes', () async { + const MethodCodec codec = JSONMethodCodec(); + final Completer completer = Completer(); + ui.PlatformDispatcher.instance.sendPlatformMessage( + 'flutter/skia', + codec.encodeMethodCall(MethodCall( + 'Skia.setResourceCacheMaxBytes', + 512 * 1000 * 1000, + )), + completer.complete, + ); + + final ByteData? response = await completer.future; + expect(response, isNotNull); + expect( + codec.decodeEnvelope(response!), + [true], + ); + }); + }); +}