service_extensions_test.dart 42.7 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7 8 9 10
// TODO(gspencergoog): Remove this tag once this test's state leaks/test
// dependencies have been fixed.
// https://github.com/flutter/flutter/issues/85160
// Fails with "flutter test --test-randomize-ordering-seed=123"
@Tags(<String>['no-shuffle'])

11 12 13 14 15 16 17 18 19 20
import 'dart:async';
import 'dart:convert';
import 'dart:ui' as ui;

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
21
import 'package:flutter_test/flutter_test.dart';
22 23

class TestServiceExtensionsBinding extends BindingBase
24 25
  with SchedulerBinding,
       ServicesBinding,
26
       GestureBinding,
27
       PaintingBinding,
28
       SemanticsBinding,
29
       RendererBinding,
30 31
       WidgetsBinding,
       TestDefaultBinaryMessengerBinding {
32 33 34

  final Map<String, ServiceExtensionCallback> extensions = <String, ServiceExtensionCallback>{};

35 36
  final Map<String, List<Map<String, dynamic>>> eventsDispatched = <String, List<Map<String, dynamic>>>{};

37
  @override
38
  @protected
39
  void registerServiceExtension({
40 41
    required String name,
    required ServiceExtensionCallback callback,
42 43 44 45 46
  }) {
    expect(extensions.containsKey(name), isFalse);
    extensions[name] = callback;
  }

47
  @override
48
  void postEvent(String eventKind, Map<String, dynamic> eventData) {
49 50 51 52 53 54 55 56 57 58 59 60
    getEventsDispatched(eventKind).add(eventData);
  }

  List<Map<String, dynamic>> getEventsDispatched(String eventKind) {
    return eventsDispatched.putIfAbsent(eventKind, () => <Map<String, dynamic>>[]);
  }

  Iterable<Map<String, dynamic>> getServiceExtensionStateChangedEvents(String extensionName) {
    return getEventsDispatched('Flutter.ServiceExtensionStateChanged')
      .where((Map<String, dynamic> event) => event['extension'] == extensionName);
  }

61
  Future<Map<String, dynamic>> testExtension(String name, Map<String, String> arguments) {
62
    expect(extensions.containsKey(name), isTrue);
63
    return extensions[name]!(arguments);
64 65 66
  }

  int reassembled = 0;
67
  bool pendingReassemble = false;
68
  @override
69
  Future<void> performReassemble() {
70
    reassembled += 1;
71
    pendingReassemble = true;
72
    return super.performReassemble();
73 74 75 76 77
  }

  bool frameScheduled = false;
  @override
  void scheduleFrame() {
78
    ensureFrameCallbacksRegistered();
79 80
    frameScheduled = true;
  }
81
  Future<void> doFrame() async {
82
    frameScheduled = false;
83
    binding.platformDispatcher.onBeginFrame?.call(Duration.zero);
84
    await flushMicrotasks();
85 86
    binding.platformDispatcher.onDrawFrame?.call();
    binding.platformDispatcher.onReportTimings?.call(<ui.FrameTiming>[]);
87 88
  }

89 90 91 92 93 94 95 96 97 98 99
  @override
  void scheduleForcedFrame() {
    expect(true, isFalse);
  }

  @override
  void scheduleWarmUpFrame() {
    expect(pendingReassemble, isTrue);
    pendingReassemble = false;
  }

100 101
  Future<void> flushMicrotasks() {
    final Completer<void> completer = Completer<void>();
102
    Timer.run(completer.complete);
103 104 105 106
    return completer.future;
  }
}

107
late TestServiceExtensionsBinding binding;
108

109
Future<Map<String, dynamic>> hasReassemble(Future<Map<String, dynamic>> pendingResult) async {
110 111 112 113 114 115
  bool completed = false;
  pendingResult.whenComplete(() { completed = true; });
  expect(binding.frameScheduled, isFalse);
  await binding.flushMicrotasks();
  expect(binding.frameScheduled, isTrue);
  expect(completed, isFalse);
116
  await binding.flushMicrotasks();
117
  await binding.doFrame();
118 119 120 121 122 123
  await binding.flushMicrotasks();
  expect(completed, isTrue);
  expect(binding.frameScheduled, isFalse);
  return pendingResult;
}

124
void main() {
125
  final List<String?> console = <String?>[];
126

127
  setUpAll(() async {
128
    binding = TestServiceExtensionsBinding()..scheduleFrame();
129
    expect(binding.frameScheduled, isTrue);
130 131 132 133 134 135

    // We need to test this service extension here because the result is true
    // after the first binding.doFrame() call.
    Map<String, dynamic> firstFrameResult;
    expect(binding.debugDidSendFirstFrameEvent, isFalse);
    firstFrameResult = await binding.testExtension('didSendFirstFrameEvent', <String, String>{});
136
    expect(firstFrameResult, <String, String>{'enabled': 'false'});
137

138 139 140 141
    expect(binding.firstFrameRasterized, isFalse);
    firstFrameResult = await binding.testExtension('didSendFirstFrameRasterizedEvent', <String, String>{});
    expect(firstFrameResult, <String, String>{'enabled': 'false'});

142
    await binding.doFrame();
143 144 145

    expect(binding.debugDidSendFirstFrameEvent, isTrue);
    firstFrameResult = await binding.testExtension('didSendFirstFrameEvent', <String, String>{});
146
    expect(firstFrameResult, <String, String>{'enabled': 'true'});
147

148 149 150 151
    expect(binding.firstFrameRasterized, isTrue);
    firstFrameResult = await binding.testExtension('didSendFirstFrameRasterizedEvent', <String, String>{});
    expect(firstFrameResult, <String, String>{'enabled': 'true'});

152 153 154
    expect(binding.frameScheduled, isFalse);

    expect(debugPrint, equals(debugPrintThrottled));
155
    debugPrint = (String? message, { int? wrapWidth }) {
156 157 158 159
      console.add(message);
    };
  });

160 161 162
  tearDownAll(() async {
    // See widget_inspector_test.dart for tests of the ext.flutter.inspector
    // service extensions included in this count.
163
    int widgetInspectorExtensionCount = 16;
164 165 166 167 168 169
    if (WidgetInspectorService.instance.isWidgetCreationTracked()) {
      // Some inspector extensions are only exposed if widget creation locations
      // are tracked.
      widgetInspectorExtensionCount += 2;
    }

170 171
    // The following service extensions are disabled in web:
    // 1. exit
172 173
    // 2. showPerformanceOverlay
    const int disabledExtensions = kIsWeb ? 2 : 0;
174 175 176 177 178 179 180 181

    // The expected number of registered service extensions in the Flutter
    // framework, excluding any that are for the widget inspector
    // (see widget_inspector_test.dart for tests of the ext.flutter.inspector
    // service extensions).
    const int serviceExtensionCount = 36;

    expect(binding.extensions.length, serviceExtensionCount + widgetInspectorExtensionCount - disabledExtensions);
182 183 184 185 186

    expect(console, isEmpty);
    debugPrint = debugPrintThrottled;
  });

187 188 189
  // The following list is alphabetical, one test per extension.

  test('Service extensions - debugAllowBanner', () async {
190
    Map<String, dynamic> result;
191 192 193 194

    expect(binding.frameScheduled, isFalse);
    expect(WidgetsApp.debugAllowBannerOverride, true);
    result = await binding.testExtension('debugAllowBanner', <String, String>{});
195
    expect(result, <String, String>{'enabled': 'true'});
196
    expect(WidgetsApp.debugAllowBannerOverride, true);
197 198
    result = await binding.testExtension('debugAllowBanner', <String, String>{'enabled': 'false'});
    expect(result, <String, String>{'enabled': 'false'});
199 200
    expect(WidgetsApp.debugAllowBannerOverride, false);
    result = await binding.testExtension('debugAllowBanner', <String, String>{});
201
    expect(result, <String, String>{'enabled': 'false'});
202
    expect(WidgetsApp.debugAllowBannerOverride, false);
203 204
    result = await binding.testExtension('debugAllowBanner', <String, String>{'enabled': 'true'});
    expect(result, <String, String>{'enabled': 'true'});
205 206
    expect(WidgetsApp.debugAllowBannerOverride, true);
    result = await binding.testExtension('debugAllowBanner', <String, String>{});
207
    expect(result, <String, String>{'enabled': 'true'});
208 209 210 211 212
    expect(WidgetsApp.debugAllowBannerOverride, true);
    expect(binding.frameScheduled, isFalse);
  });

  test('Service extensions - debugDumpApp', () async {
213
    final Map<String, dynamic> result = await binding.testExtension('debugDumpApp', <String, String>{});
214

215 216 217
    expect(result, <String, dynamic>{
      'data': matches('TestServiceExtensionsBinding - DEBUG MODE\n<no tree currently mounted>'),
    });
218 219 220
  });

  test('Service extensions - debugDumpRenderTree', () async {
221
    await binding.doFrame();
222 223 224 225
    final Map<String, dynamic> result = await binding.testExtension('debugDumpRenderTree', <String, String>{});

    expect(result, <String, dynamic>{
      'data': matches(
226
        r'^'
227
        r'RenderView#[0-9a-f]{5}\n'
228
        r'   debug mode enabled - [a-zA-Z]+\n'
229 230 231
        r'   window size: Size\(2400\.0, 1800\.0\) \(in physical pixels\)\n'
        r'   device pixel ratio: 3\.0 \(physical pixels per logical pixel\)\n'
        r'   configuration: Size\(800\.0, 600\.0\) at 3\.0x \(in logical pixels\)\n'
232
        r'$',
233
      ),
234
    });
235 236
  });

237 238
  test('Service extensions - debugDumpLayerTree', () async {
    await binding.doFrame();
239 240 241 242
    final Map<String, dynamic> result = await binding.testExtension('debugDumpLayerTree', <String, String>{});

    expect(result, <String, dynamic>{
      'data': matches(
243
        r'^'
244 245
        r'TransformLayer#[0-9a-f]{5}\n'
        r'   owner: RenderView#[0-9a-f]{5}\n'
246
        r'   creator: RenderView\n'
247
        r'   engine layer: (TransformEngineLayer|PersistedTransform)#[0-9a-f]{5}\n'
248
        r'   handles: 1\n'
249 250
        r'   offset: Offset\(0\.0, 0\.0\)\n'
        r'   transform:\n'
251 252
        r'     \[0] 3\.0,0\.0,0\.0,0\.0\n'
        r'     \[1] 0\.0,3\.0,0\.0,0\.0\n'
253 254
        r'     \[2] 0\.0,0\.0,1\.0,0\.0\n'
        r'     \[3] 0\.0,0\.0,0\.0,1\.0\n'
255
        r'$',
256
      ),
257
    });
258 259
  });

260
  test('Service extensions - debugDumpSemanticsTreeInTraversalOrder', () async {
261
    await binding.doFrame();
262 263 264
    final Map<String, dynamic> result = await binding.testExtension('debugDumpSemanticsTreeInTraversalOrder', <String, String>{});

    expect(result, <String, String>{
265
      'data': 'Semantics not collected.',
266
    });
267 268 269 270
  });

  test('Service extensions - debugDumpSemanticsTreeInInverseHitTestOrder', () async {
    await binding.doFrame();
271 272 273
    final Map<String, dynamic> result = await binding.testExtension('debugDumpSemanticsTreeInInverseHitTestOrder', <String, String>{});

    expect(result, <String, String>{
274
      'data': 'Semantics not collected.',
275
    });
276 277
  });

278
  test('Service extensions - debugPaint', () async {
279 280
    final Iterable<Map<String, dynamic>> extensionChangedEvents = binding.getServiceExtensionStateChangedEvents('ext.flutter.debugPaint');
    Map<String, dynamic> extensionChangedEvent;
281 282
    Map<String, dynamic> result;
    Future<Map<String, dynamic>> pendingResult;
283 284 285 286 287
    bool completed;

    expect(binding.frameScheduled, isFalse);
    expect(debugPaintSizeEnabled, false);
    result = await binding.testExtension('debugPaint', <String, String>{});
288
    expect(result, <String, String>{'enabled': 'false'});
289
    expect(debugPaintSizeEnabled, false);
290
    expect(extensionChangedEvents, isEmpty);
291
    expect(binding.frameScheduled, isFalse);
292
    pendingResult = binding.testExtension('debugPaint', <String, String>{'enabled': 'true'});
293 294 295 296 297
    completed = false;
    pendingResult.whenComplete(() { completed = true; });
    await binding.flushMicrotasks();
    expect(binding.frameScheduled, isTrue);
    expect(completed, isFalse);
298
    await binding.doFrame();
299 300 301 302
    await binding.flushMicrotasks();
    expect(completed, isTrue);
    expect(binding.frameScheduled, isFalse);
    result = await pendingResult;
303
    expect(result, <String, String>{'enabled': 'true'});
304
    expect(debugPaintSizeEnabled, true);
305 306 307 308
    expect(extensionChangedEvents.length, 1);
    extensionChangedEvent = extensionChangedEvents.last;
    expect(extensionChangedEvent['extension'], 'ext.flutter.debugPaint');
    expect(extensionChangedEvent['value'], 'true');
309
    result = await binding.testExtension('debugPaint', <String, String>{});
310
    expect(result, <String, String>{'enabled': 'true'});
311
    expect(debugPaintSizeEnabled, true);
312
    expect(extensionChangedEvents.length, 1);
313
    expect(binding.frameScheduled, isFalse);
314
    pendingResult = binding.testExtension('debugPaint', <String, String>{'enabled': 'false'});
315 316
    await binding.flushMicrotasks();
    expect(binding.frameScheduled, isTrue);
317
    await binding.doFrame();
318 319
    expect(binding.frameScheduled, isFalse);
    result = await pendingResult;
320
    expect(result, <String, String>{'enabled': 'false'});
321
    expect(debugPaintSizeEnabled, false);
322 323 324 325
    expect(extensionChangedEvents.length, 2);
    extensionChangedEvent = extensionChangedEvents.last;
    expect(extensionChangedEvent['extension'], 'ext.flutter.debugPaint');
    expect(extensionChangedEvent['value'], 'false');
326
    result = await binding.testExtension('debugPaint', <String, String>{});
327
    expect(result, <String, String>{'enabled': 'false'});
328
    expect(debugPaintSizeEnabled, false);
329
    expect(extensionChangedEvents.length, 2);
330 331 332
    expect(binding.frameScheduled, isFalse);
  });

333
  test('Service extensions - debugPaintBaselinesEnabled', () async {
334 335
    Map<String, dynamic> result;
    Future<Map<String, dynamic>> pendingResult;
336 337 338 339 340
    bool completed;

    expect(binding.frameScheduled, isFalse);
    expect(debugPaintBaselinesEnabled, false);
    result = await binding.testExtension('debugPaintBaselinesEnabled', <String, String>{});
341
    expect(result, <String, String>{'enabled': 'false'});
342 343
    expect(debugPaintBaselinesEnabled, false);
    expect(binding.frameScheduled, isFalse);
344
    pendingResult = binding.testExtension('debugPaintBaselinesEnabled', <String, String>{'enabled': 'true'});
345 346 347 348 349 350 351 352 353 354
    completed = false;
    pendingResult.whenComplete(() { completed = true; });
    await binding.flushMicrotasks();
    expect(binding.frameScheduled, isTrue);
    expect(completed, isFalse);
    await binding.doFrame();
    await binding.flushMicrotasks();
    expect(completed, isTrue);
    expect(binding.frameScheduled, isFalse);
    result = await pendingResult;
355
    expect(result, <String, String>{'enabled': 'true'});
356 357
    expect(debugPaintBaselinesEnabled, true);
    result = await binding.testExtension('debugPaintBaselinesEnabled', <String, String>{});
358
    expect(result, <String, String>{'enabled': 'true'});
359 360
    expect(debugPaintBaselinesEnabled, true);
    expect(binding.frameScheduled, isFalse);
361
    pendingResult = binding.testExtension('debugPaintBaselinesEnabled', <String, String>{'enabled': 'false'});
362 363 364 365 366
    await binding.flushMicrotasks();
    expect(binding.frameScheduled, isTrue);
    await binding.doFrame();
    expect(binding.frameScheduled, isFalse);
    result = await pendingResult;
367
    expect(result, <String, String>{'enabled': 'false'});
368 369
    expect(debugPaintBaselinesEnabled, false);
    result = await binding.testExtension('debugPaintBaselinesEnabled', <String, String>{});
370
    expect(result, <String, String>{'enabled': 'false'});
371 372 373 374
    expect(debugPaintBaselinesEnabled, false);
    expect(binding.frameScheduled, isFalse);
  });

375 376
  test('Service extensions - invertOversizedImages', () async {
    Map<String, dynamic> result;
377 378
    Future<Map<String, dynamic>> pendingResult;
    bool completed;
379 380 381 382 383 384

    expect(binding.frameScheduled, isFalse);
    expect(debugInvertOversizedImages, false);
    result = await binding.testExtension('invertOversizedImages', <String, String>{});
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugInvertOversizedImages, false);
385 386 387 388 389 390 391 392 393 394 395 396 397
    expect(binding.frameScheduled, isFalse);

    pendingResult = binding.testExtension('invertOversizedImages', <String, String>{'enabled': 'true'});
    completed = false;
    pendingResult.whenComplete(() { completed = true; });
    await binding.flushMicrotasks();
    expect(binding.frameScheduled, isTrue);
    expect(completed, isFalse);
    await binding.doFrame();
    await binding.flushMicrotasks();
    expect(completed, isTrue);
    expect(binding.frameScheduled, isFalse);
    result = await pendingResult;
398 399
    expect(result, <String, String>{'enabled': 'true'});
    expect(debugInvertOversizedImages, true);
400

401 402 403
    result = await binding.testExtension('invertOversizedImages', <String, String>{});
    expect(result, <String, String>{'enabled': 'true'});
    expect(debugInvertOversizedImages, true);
404 405 406 407 408 409 410 411
    expect(binding.frameScheduled, isFalse);

    pendingResult = binding.testExtension('invertOversizedImages', <String, String>{'enabled': 'false'});
    await binding.flushMicrotasks();
    expect(binding.frameScheduled, isTrue);
    await binding.doFrame();
    expect(binding.frameScheduled, isFalse);
    result = await pendingResult;
412 413
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugInvertOversizedImages, false);
414

415 416 417 418 419 420
    result = await binding.testExtension('invertOversizedImages', <String, String>{});
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugInvertOversizedImages, false);
    expect(binding.frameScheduled, isFalse);
  });

421 422 423 424 425 426 427
  test('Service extensions - profileWidgetBuilds', () async {
    Map<String, dynamic> result;

    expect(binding.frameScheduled, isFalse);
    expect(debugProfileBuildsEnabled, false);

    result = await binding.testExtension('profileWidgetBuilds', <String, String>{});
428
    expect(result, <String, String>{'enabled': 'false'});
429 430
    expect(debugProfileBuildsEnabled, false);

431 432
    result = await binding.testExtension('profileWidgetBuilds', <String, String>{'enabled': 'true'});
    expect(result, <String, String>{'enabled': 'true'});
433 434 435
    expect(debugProfileBuildsEnabled, true);

    result = await binding.testExtension('profileWidgetBuilds', <String, String>{});
436
    expect(result, <String, String>{'enabled': 'true'});
437 438
    expect(debugProfileBuildsEnabled, true);

439 440
    result = await binding.testExtension('profileWidgetBuilds', <String, String>{'enabled': 'false'});
    expect(result, <String, String>{'enabled': 'false'});
441 442 443
    expect(debugProfileBuildsEnabled, false);

    result = await binding.testExtension('profileWidgetBuilds', <String, String>{});
444
    expect(result, <String, String>{'enabled': 'false'});
445 446
    expect(debugProfileBuildsEnabled, false);

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
    expect(binding.frameScheduled, isFalse);
  });

  test('Service extensions - profileUserWidgetBuilds', () async {
    Map<String, dynamic> result;

    expect(binding.frameScheduled, isFalse);
    expect(debugProfileBuildsEnabledUserWidgets, false);

    result = await binding.testExtension('profileUserWidgetBuilds', <String, String>{});
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugProfileBuildsEnabledUserWidgets, false);

    result = await binding.testExtension('profileUserWidgetBuilds', <String, String>{'enabled': 'true'});
    expect(result, <String, String>{'enabled': 'true'});
    expect(debugProfileBuildsEnabledUserWidgets, true);

    result = await binding.testExtension('profileUserWidgetBuilds', <String, String>{});
    expect(result, <String, String>{'enabled': 'true'});
    expect(debugProfileBuildsEnabledUserWidgets, true);

    result = await binding.testExtension('profileUserWidgetBuilds', <String, String>{'enabled': 'false'});
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugProfileBuildsEnabledUserWidgets, false);

    result = await binding.testExtension('profileUserWidgetBuilds', <String, String>{});
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugProfileBuildsEnabledUserWidgets, false);

476 477 478
    expect(binding.frameScheduled, isFalse);
  });

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
  test('Service extensions - profileRenderObjectPaints', () async {
    Map<String, dynamic> result;

    expect(binding.frameScheduled, isFalse);
    expect(debugProfileBuildsEnabled, false);

    result = await binding.testExtension('profileRenderObjectPaints', <String, String>{});
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugProfilePaintsEnabled, false);

    result = await binding.testExtension('profileRenderObjectPaints', <String, String>{'enabled': 'true'});
    expect(result, <String, String>{'enabled': 'true'});
    expect(debugProfilePaintsEnabled, true);

    result = await binding.testExtension('profileRenderObjectPaints', <String, String>{});
    expect(result, <String, String>{'enabled': 'true'});
    expect(debugProfilePaintsEnabled, true);

    result = await binding.testExtension('profileRenderObjectPaints', <String, String>{'enabled': 'false'});
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugProfilePaintsEnabled, false);

    result = await binding.testExtension('profileRenderObjectPaints', <String, String>{});
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugProfilePaintsEnabled, false);

    expect(binding.frameScheduled, isFalse);
  });

  test('Service extensions - profileRenderObjectLayouts', () async {
    Map<String, dynamic> result;

    expect(binding.frameScheduled, isFalse);
    expect(debugProfileLayoutsEnabled, false);

    result = await binding.testExtension('profileRenderObjectLayouts', <String, String>{});
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugProfileLayoutsEnabled, false);

    result = await binding.testExtension('profileRenderObjectLayouts', <String, String>{'enabled': 'true'});
    expect(result, <String, String>{'enabled': 'true'});
    expect(debugProfileLayoutsEnabled, true);

    result = await binding.testExtension('profileRenderObjectLayouts', <String, String>{});
    expect(result, <String, String>{'enabled': 'true'});
    expect(debugProfileLayoutsEnabled, true);

    result = await binding.testExtension('profileRenderObjectLayouts', <String, String>{'enabled': 'false'});
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugProfileLayoutsEnabled, false);

    result = await binding.testExtension('profileRenderObjectLayouts', <String, String>{});
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugProfileLayoutsEnabled, false);

    expect(binding.frameScheduled, isFalse);
  });

537
  test('Service extensions - evict', () async {
538
    Map<String, dynamic> result;
539 540 541
    bool completed;

    completed = false;
542
    TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.setMockMessageHandler('flutter/assets', (ByteData? message) async {
543
      expect(utf8.decode(message!.buffer.asUint8List()), 'test');
544
      completed = true;
545
      return ByteData(5); // 0x0000000000
546 547
    });
    bool data;
548 549 550 551
    data = await rootBundle.loadStructuredData<bool>('test', (String value) async {
      expect(value, '\x00\x00\x00\x00\x00');
      return true;
    });
552 553 554
    expect(data, isTrue);
    expect(completed, isTrue);
    completed = false;
555
    data = await rootBundle.loadStructuredData('test', (String value) async {
556
      throw Error();
557
    });
558 559
    expect(data, isTrue);
    expect(completed, isFalse);
560 561
    result = await binding.testExtension('evict', <String, String>{'value': 'test'});
    expect(result, <String, String>{'value': ''});
562
    expect(completed, isFalse);
563 564 565 566
    data = await rootBundle.loadStructuredData<bool>('test', (String value) async {
      expect(value, '\x00\x00\x00\x00\x00');
      return false;
    });
567 568
    expect(data, isFalse);
    expect(completed, isTrue);
569
    TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.setMockMessageHandler('flutter/assets', null);
570 571 572 573
  });

  test('Service extensions - exit', () async {
    // no test for _calling_ 'exit', because that should terminate the process!
574 575
    // Not expecting extension to be available for web platform.
    expect(binding.extensions.containsKey('exit'), !isBrowser);
576 577
  });

578
  test('Service extensions - platformOverride', () async {
579 580
    final Iterable<Map<String, dynamic>> extensionChangedEvents = binding.getServiceExtensionStateChangedEvents('ext.flutter.platformOverride');
    Map<String, dynamic> extensionChangedEvent;
581
    Map<String, dynamic> result;
582 583 584 585 586 587

    expect(binding.reassembled, 0);
    expect(defaultTargetPlatform, TargetPlatform.android);
    result = await binding.testExtension('platformOverride', <String, String>{});
    expect(result, <String, String>{'value': 'android'});
    expect(defaultTargetPlatform, TargetPlatform.android);
588
    expect(extensionChangedEvents, isEmpty);
589 590 591 592
    result = await hasReassemble(binding.testExtension('platformOverride', <String, String>{'value': 'iOS'}));
    expect(result, <String, String>{'value': 'iOS'});
    expect(binding.reassembled, 1);
    expect(defaultTargetPlatform, TargetPlatform.iOS);
593 594 595 596
    expect(extensionChangedEvents.length, 1);
    extensionChangedEvent = extensionChangedEvents.last;
    expect(extensionChangedEvent['extension'], 'ext.flutter.platformOverride');
    expect(extensionChangedEvent['value'], 'iOS');
Dan Field's avatar
Dan Field committed
597 598 599 600 601 602 603 604
    result = await hasReassemble(binding.testExtension('platformOverride', <String, String>{'value': 'macOS'}));
    expect(result, <String, String>{'value': 'macOS'});
    expect(binding.reassembled, 2);
    expect(defaultTargetPlatform, TargetPlatform.macOS);
    expect(extensionChangedEvents.length, 2);
    extensionChangedEvent = extensionChangedEvents.last;
    expect(extensionChangedEvent['extension'], 'ext.flutter.platformOverride');
    expect(extensionChangedEvent['value'], 'macOS');
605 606
    result = await hasReassemble(binding.testExtension('platformOverride', <String, String>{'value': 'android'}));
    expect(result, <String, String>{'value': 'android'});
Dan Field's avatar
Dan Field committed
607
    expect(binding.reassembled, 3);
608
    expect(defaultTargetPlatform, TargetPlatform.android);
Dan Field's avatar
Dan Field committed
609
    expect(extensionChangedEvents.length, 3);
610 611 612
    extensionChangedEvent = extensionChangedEvents.last;
    expect(extensionChangedEvent['extension'], 'ext.flutter.platformOverride');
    expect(extensionChangedEvent['value'], 'android');
613 614
    result = await hasReassemble(binding.testExtension('platformOverride', <String, String>{'value': 'fuchsia'}));
    expect(result, <String, String>{'value': 'fuchsia'});
Dan Field's avatar
Dan Field committed
615
    expect(binding.reassembled, 4);
616
    expect(defaultTargetPlatform, TargetPlatform.fuchsia);
Dan Field's avatar
Dan Field committed
617
    expect(extensionChangedEvents.length, 4);
618 619 620
    extensionChangedEvent = extensionChangedEvents.last;
    expect(extensionChangedEvent['extension'], 'ext.flutter.platformOverride');
    expect(extensionChangedEvent['value'], 'fuchsia');
621 622
    result = await hasReassemble(binding.testExtension('platformOverride', <String, String>{'value': 'default'}));
    expect(result, <String, String>{'value': 'android'});
Dan Field's avatar
Dan Field committed
623
    expect(binding.reassembled, 5);
624
    expect(defaultTargetPlatform, TargetPlatform.android);
Dan Field's avatar
Dan Field committed
625
    expect(extensionChangedEvents.length, 5);
626 627 628
    extensionChangedEvent = extensionChangedEvents.last;
    expect(extensionChangedEvent['extension'], 'ext.flutter.platformOverride');
    expect(extensionChangedEvent['value'], 'android');
629 630
    result = await hasReassemble(binding.testExtension('platformOverride', <String, String>{'value': 'iOS'}));
    expect(result, <String, String>{'value': 'iOS'});
Dan Field's avatar
Dan Field committed
631
    expect(binding.reassembled, 6);
632
    expect(defaultTargetPlatform, TargetPlatform.iOS);
Dan Field's avatar
Dan Field committed
633
    expect(extensionChangedEvents.length, 6);
634 635 636
    extensionChangedEvent = extensionChangedEvents.last;
    expect(extensionChangedEvent['extension'], 'ext.flutter.platformOverride');
    expect(extensionChangedEvent['value'], 'iOS');
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
    result = await hasReassemble(binding.testExtension('platformOverride', <String, String>{'value': 'linux'}));
    expect(result, <String, String>{'value': 'linux'});
    expect(binding.reassembled, 7);
    expect(defaultTargetPlatform, TargetPlatform.linux);
    expect(extensionChangedEvents.length, 7);
    extensionChangedEvent = extensionChangedEvents.last;
    expect(extensionChangedEvent['extension'], 'ext.flutter.platformOverride');
    expect(extensionChangedEvent['value'], 'linux');
    result = await hasReassemble(binding.testExtension('platformOverride', <String, String>{'value': 'windows'}));
    expect(result, <String, String>{'value': 'windows'});
    expect(binding.reassembled, 8);
    expect(defaultTargetPlatform, TargetPlatform.windows);
    expect(extensionChangedEvents.length, 8);
    extensionChangedEvent = extensionChangedEvents.last;
    expect(extensionChangedEvent['extension'], 'ext.flutter.platformOverride');
    expect(extensionChangedEvent['value'], 'windows');
653 654
    result = await hasReassemble(binding.testExtension('platformOverride', <String, String>{'value': 'bogus'}));
    expect(result, <String, String>{'value': 'android'});
655
    expect(binding.reassembled, 9);
656
    expect(defaultTargetPlatform, TargetPlatform.android);
657
    expect(extensionChangedEvents.length, 9);
658 659 660
    extensionChangedEvent = extensionChangedEvents.last;
    expect(extensionChangedEvent['extension'], 'ext.flutter.platformOverride');
    expect(extensionChangedEvent['value'], 'android');
661 662 663
    binding.reassembled = 0;
  });

664
  test('Service extensions - repaintRainbow', () async {
665 666
    Map<String, dynamic> result;
    Future<Map<String, dynamic>> pendingResult;
667 668 669 670 671
    bool completed;

    expect(binding.frameScheduled, isFalse);
    expect(debugRepaintRainbowEnabled, false);
    result = await binding.testExtension('repaintRainbow', <String, String>{});
672
    expect(result, <String, String>{'enabled': 'false'});
673 674
    expect(debugRepaintRainbowEnabled, false);
    expect(binding.frameScheduled, isFalse);
675
    pendingResult = binding.testExtension('repaintRainbow', <String, String>{'enabled': 'true'});
676 677 678 679 680 681
    completed = false;
    pendingResult.whenComplete(() { completed = true; });
    await binding.flushMicrotasks();
    expect(completed, true);
    expect(binding.frameScheduled, isFalse);
    result = await pendingResult;
682
    expect(result, <String, String>{'enabled': 'true'});
683 684
    expect(debugRepaintRainbowEnabled, true);
    result = await binding.testExtension('repaintRainbow', <String, String>{});
685
    expect(result, <String, String>{'enabled': 'true'});
686 687
    expect(debugRepaintRainbowEnabled, true);
    expect(binding.frameScheduled, isFalse);
688
    pendingResult = binding.testExtension('repaintRainbow', <String, String>{'enabled': 'false'});
689 690 691 692 693
    completed = false;
    pendingResult.whenComplete(() { completed = true; });
    await binding.flushMicrotasks();
    expect(completed, false);
    expect(binding.frameScheduled, isTrue);
694
    await binding.doFrame();
695 696 697 698
    await binding.flushMicrotasks();
    expect(completed, true);
    expect(binding.frameScheduled, isFalse);
    result = await pendingResult;
699
    expect(result, <String, String>{'enabled': 'false'});
700 701
    expect(debugRepaintRainbowEnabled, false);
    result = await binding.testExtension('repaintRainbow', <String, String>{});
702
    expect(result, <String, String>{'enabled': 'false'});
703 704 705 706
    expect(debugRepaintRainbowEnabled, false);
    expect(binding.frameScheduled, isFalse);
  });

707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
  test('Service extensions - debugDisableClipLayers', () async {
    Map<String, dynamic> result;
    Future<Map<String, dynamic>> pendingResult;
    bool completed;

    expect(binding.frameScheduled, isFalse);
    expect(debugDisableClipLayers, false);
    result = await binding.testExtension('debugDisableClipLayers', <String, String>{});
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugDisableClipLayers, false);
    expect(binding.frameScheduled, isFalse);
    pendingResult = binding.testExtension('debugDisableClipLayers', <String, String>{'enabled': 'true'});
    completed = false;
    pendingResult.whenComplete(() { completed = true; });
    await binding.flushMicrotasks();
    expect(binding.frameScheduled, isTrue);
    expect(completed, isFalse);
    await binding.doFrame();
    await binding.flushMicrotasks();
    expect(completed, isTrue);
    expect(binding.frameScheduled, isFalse);
    result = await pendingResult;
    expect(result, <String, String>{'enabled': 'true'});
    expect(debugDisableClipLayers, true);
    result = await binding.testExtension('debugDisableClipLayers', <String, String>{});
    expect(result, <String, String>{'enabled': 'true'});
    expect(debugDisableClipLayers, true);
    expect(binding.frameScheduled, isFalse);
    pendingResult = binding.testExtension('debugDisableClipLayers', <String, String>{'enabled': 'false'});
    await binding.flushMicrotasks();
    expect(binding.frameScheduled, isTrue);
    await binding.doFrame();
    expect(binding.frameScheduled, isFalse);
    result = await pendingResult;
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugDisableClipLayers, false);
    result = await binding.testExtension('debugDisableClipLayers', <String, String>{});
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugDisableClipLayers, false);
    expect(binding.frameScheduled, isFalse);
  });

  test('Service extensions - debugDisablePhysicalShapeLayers', () async {
    Map<String, dynamic> result;
    Future<Map<String, dynamic>> pendingResult;
    bool completed;

    expect(binding.frameScheduled, isFalse);
    expect(debugDisablePhysicalShapeLayers, false);
    result = await binding.testExtension('debugDisablePhysicalShapeLayers', <String, String>{});
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugDisablePhysicalShapeLayers, false);
    expect(binding.frameScheduled, isFalse);
    pendingResult = binding.testExtension('debugDisablePhysicalShapeLayers', <String, String>{'enabled': 'true'});
    completed = false;
    pendingResult.whenComplete(() { completed = true; });
    await binding.flushMicrotasks();
    expect(binding.frameScheduled, isTrue);
    expect(completed, isFalse);
    await binding.doFrame();
    await binding.flushMicrotasks();
    expect(completed, isTrue);
    expect(binding.frameScheduled, isFalse);
    result = await pendingResult;
    expect(result, <String, String>{'enabled': 'true'});
    expect(debugDisablePhysicalShapeLayers, true);
    result = await binding.testExtension('debugDisablePhysicalShapeLayers', <String, String>{});
    expect(result, <String, String>{'enabled': 'true'});
    expect(debugDisablePhysicalShapeLayers, true);
    expect(binding.frameScheduled, isFalse);
    pendingResult = binding.testExtension('debugDisablePhysicalShapeLayers', <String, String>{'enabled': 'false'});
    await binding.flushMicrotasks();
    expect(binding.frameScheduled, isTrue);
    await binding.doFrame();
    expect(binding.frameScheduled, isFalse);
    result = await pendingResult;
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugDisablePhysicalShapeLayers, false);
    result = await binding.testExtension('debugDisablePhysicalShapeLayers', <String, String>{});
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugDisablePhysicalShapeLayers, false);
    expect(binding.frameScheduled, isFalse);
  });

  test('Service extensions - debugDisableOpacityLayers', () async {
    Map<String, dynamic> result;
    Future<Map<String, dynamic>> pendingResult;
    bool completed;

    expect(binding.frameScheduled, isFalse);
    expect(debugDisableOpacityLayers, false);
    result = await binding.testExtension('debugDisableOpacityLayers', <String, String>{});
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugDisableOpacityLayers, false);
    expect(binding.frameScheduled, isFalse);
    pendingResult = binding.testExtension('debugDisableOpacityLayers', <String, String>{'enabled': 'true'});
    completed = false;
    pendingResult.whenComplete(() { completed = true; });
    await binding.flushMicrotasks();
    expect(binding.frameScheduled, isTrue);
    expect(completed, isFalse);
    await binding.doFrame();
    await binding.flushMicrotasks();
    expect(completed, isTrue);
    expect(binding.frameScheduled, isFalse);
    result = await pendingResult;
    expect(result, <String, String>{'enabled': 'true'});
    expect(debugDisableOpacityLayers, true);
    result = await binding.testExtension('debugDisableOpacityLayers', <String, String>{});
    expect(result, <String, String>{'enabled': 'true'});
    expect(debugDisableOpacityLayers, true);
    expect(binding.frameScheduled, isFalse);
    pendingResult = binding.testExtension('debugDisableOpacityLayers', <String, String>{'enabled': 'false'});
    await binding.flushMicrotasks();
    expect(binding.frameScheduled, isTrue);
    await binding.doFrame();
    expect(binding.frameScheduled, isFalse);
    result = await pendingResult;
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugDisableOpacityLayers, false);
    result = await binding.testExtension('debugDisableOpacityLayers', <String, String>{});
    expect(result, <String, String>{'enabled': 'false'});
    expect(debugDisableOpacityLayers, false);
    expect(binding.frameScheduled, isFalse);
  });

833
  test('Service extensions - reassemble', () async {
834 835
    Map<String, dynamic> result;
    Future<Map<String, dynamic>> pendingResult;
836 837 838 839 840 841 842 843 844
    bool completed;

    completed = false;
    expect(binding.reassembled, 0);
    pendingResult = binding.testExtension('reassemble', <String, String>{});
    pendingResult.whenComplete(() { completed = true; });
    await binding.flushMicrotasks();
    expect(binding.frameScheduled, isTrue);
    expect(completed, false);
845
    await binding.flushMicrotasks();
846
    await binding.doFrame();
847 848 849 850 851 852 853 854 855
    await binding.flushMicrotasks();
    expect(completed, true);
    expect(binding.frameScheduled, isFalse);
    result = await pendingResult;
    expect(result, <String, String>{});
    expect(binding.reassembled, 1);
  });

  test('Service extensions - showPerformanceOverlay', () async {
856
    Map<String, dynamic> result;
857

858 859 860 861 862 863
    // The performance overlay service extension is disabled on the web.
    if (kIsWeb) {
      expect(binding.extensions.containsKey('showPerformanceOverlay'), isFalse);
      return;
    }

864 865 866
    expect(binding.frameScheduled, isFalse);
    expect(WidgetsApp.showPerformanceOverlayOverride, false);
    result = await binding.testExtension('showPerformanceOverlay', <String, String>{});
867
    expect(result, <String, String>{'enabled': 'false'});
868
    expect(WidgetsApp.showPerformanceOverlayOverride, false);
869 870
    result = await binding.testExtension('showPerformanceOverlay', <String, String>{'enabled': 'true'});
    expect(result, <String, String>{'enabled': 'true'});
871 872
    expect(WidgetsApp.showPerformanceOverlayOverride, true);
    result = await binding.testExtension('showPerformanceOverlay', <String, String>{});
873
    expect(result, <String, String>{'enabled': 'true'});
874
    expect(WidgetsApp.showPerformanceOverlayOverride, true);
875 876
    result = await binding.testExtension('showPerformanceOverlay', <String, String>{'enabled': 'false'});
    expect(result, <String, String>{'enabled': 'false'});
877 878
    expect(WidgetsApp.showPerformanceOverlayOverride, false);
    result = await binding.testExtension('showPerformanceOverlay', <String, String>{});
879
    expect(result, <String, String>{'enabled': 'false'});
880 881 882 883
    expect(WidgetsApp.showPerformanceOverlayOverride, false);
    expect(binding.frameScheduled, isFalse);
  });

884 885 886 887 888
  test('Service extensions - debugWidgetInspector', () async {
    Map<String, dynamic> result;
    expect(binding.frameScheduled, isFalse);
    expect(WidgetsApp.debugShowWidgetInspectorOverride, false);
    result = await binding.testExtension('debugWidgetInspector', <String, String>{});
889
    expect(result, <String, String>{'enabled': 'false'});
890
    expect(WidgetsApp.debugShowWidgetInspectorOverride, false);
891 892
    result = await binding.testExtension('debugWidgetInspector', <String, String>{'enabled': 'true'});
    expect(result, <String, String>{'enabled': 'true'});
893 894
    expect(WidgetsApp.debugShowWidgetInspectorOverride, true);
    result = await binding.testExtension('debugWidgetInspector', <String, String>{});
895
    expect(result, <String, String>{'enabled': 'true'});
896
    expect(WidgetsApp.debugShowWidgetInspectorOverride, true);
897 898
    result = await binding.testExtension('debugWidgetInspector', <String, String>{'enabled': 'false'});
    expect(result, <String, String>{'enabled': 'false'});
899 900
    expect(WidgetsApp.debugShowWidgetInspectorOverride, false);
    result = await binding.testExtension('debugWidgetInspector', <String, String>{});
901
    expect(result, <String, String>{'enabled': 'false'});
902 903 904 905
    expect(WidgetsApp.debugShowWidgetInspectorOverride, false);
    expect(binding.frameScheduled, isFalse);
  });

906
  test('Service extensions - timeDilation', () async {
907 908
    final Iterable<Map<String, dynamic>> extensionChangedEvents = binding.getServiceExtensionStateChangedEvents('ext.flutter.timeDilation');
    Map<String, dynamic> extensionChangedEvent;
909
    Map<String, dynamic> result;
910 911 912 913

    expect(binding.frameScheduled, isFalse);
    expect(timeDilation, 1.0);
    result = await binding.testExtension('timeDilation', <String, String>{});
914
    expect(result, <String, String>{'timeDilation': 1.0.toString()});
915
    expect(timeDilation, 1.0);
916
    expect(extensionChangedEvents, isEmpty);
917
    result = await binding.testExtension('timeDilation', <String, String>{'timeDilation': '100.0'});
918
    expect(result, <String, String>{'timeDilation': 100.0.toString()});
919
    expect(timeDilation, 100.0);
920 921 922
    expect(extensionChangedEvents.length, 1);
    extensionChangedEvent = extensionChangedEvents.last;
    expect(extensionChangedEvent['extension'], 'ext.flutter.timeDilation');
923
    expect(extensionChangedEvent['value'], 100.0.toString());
924
    result = await binding.testExtension('timeDilation', <String, String>{});
925
    expect(result, <String, String>{'timeDilation': 100.0.toString()});
926
    expect(timeDilation, 100.0);
927
    expect(extensionChangedEvents.length, 1);
928
    result = await binding.testExtension('timeDilation', <String, String>{'timeDilation': '1.0'});
929
    expect(result, <String, String>{'timeDilation': 1.0.toString()});
930
    expect(timeDilation, 1.0);
931 932 933
    expect(extensionChangedEvents.length, 2);
    extensionChangedEvent = extensionChangedEvents.last;
    expect(extensionChangedEvent['extension'], 'ext.flutter.timeDilation');
934
    expect(extensionChangedEvent['value'], 1.0.toString());
935
    result = await binding.testExtension('timeDilation', <String, String>{});
936
    expect(result, <String, String>{'timeDilation': 1.0.toString()});
937
    expect(timeDilation, 1.0);
938
    expect(extensionChangedEvents.length, 2);
939 940 941
    expect(binding.frameScheduled, isFalse);
  });

942
  test('Service extensions - brightnessOverride', () async {
943
    Map<String, dynamic> result;
944 945 946 947 948
    result = await binding.testExtension('brightnessOverride', <String, String>{});
    final String brightnessValue = result['value'] as String;

    expect(brightnessValue, 'Brightness.light');
  });
949 950 951 952 953 954 955 956 957 958 959 960 961

  test('Service extensions - activeDevToolsServerAddress', () async {
    Map<String, dynamic> result;
    result = await binding.testExtension('activeDevToolsServerAddress', <String, String>{});
    String serverAddress = result['value'] as String;
    expect(serverAddress, '');
    result = await binding.testExtension('activeDevToolsServerAddress', <String, String>{'value': 'http://127.0.0.1:9101'});
    serverAddress = result['value'] as String;
    expect(serverAddress, 'http://127.0.0.1:9101');
    result = await binding.testExtension('activeDevToolsServerAddress', <String, String>{'value': 'http://127.0.0.1:9102'});
    serverAddress = result['value'] as String;
    expect(serverAddress, 'http://127.0.0.1:9102');
  });
962 963 964 965 966 967 968 969 970 971 972 973 974

  test('Service extensions - connectedVmServiceUri', () async {
    Map<String, dynamic> result;
    result = await binding.testExtension('connectedVmServiceUri', <String, String>{});
    String serverAddress = result['value'] as String;
    expect(serverAddress, '');
    result = await binding.testExtension('connectedVmServiceUri', <String, String>{'value': 'http://127.0.0.1:54669/kMUMseKAnog=/'});
    serverAddress = result['value'] as String;
    expect(serverAddress, 'http://127.0.0.1:54669/kMUMseKAnog=/');
    result = await binding.testExtension('connectedVmServiceUri', <String, String>{'value': 'http://127.0.0.1:54000/kMUMseKAnog=/'});
    serverAddress = result['value'] as String;
    expect(serverAddress, 'http://127.0.0.1:54000/kMUMseKAnog=/');
  });
975
}