image_painting_event_test.dart 4.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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:async';
import 'dart:convert' show jsonEncode;
import 'dart:developer' as developer;
import 'dart:ui' as ui;

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:vm_service/vm_service.dart';
import 'package:vm_service/vm_service_io.dart';

void main() {
16 17
  LiveTestWidgetsFlutterBinding.ensureInitialized();

18 19
  late VmService vmService;
  late LiveTestWidgetsFlutterBinding binding;
20 21 22 23 24 25 26 27
  setUpAll(() async {
    final developer.ServiceProtocolInfo info =
        await developer.Service.getInfo();

    if (info.serverUri == null) {
      fail('This test _must_ be run with --enable-vmservice.');
    }

28
    vmService = await vmServiceConnectUri('ws://localhost:${info.serverUri!.port}${info.serverUri!.path}ws');
29 30 31
    await vmService.streamListen(EventStreams.kExtension);

    // Initialize bindings
32
    binding = LiveTestWidgetsFlutterBinding.instance;
33 34 35 36 37 38 39 40
    binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.fullyLive;
    binding.attachRootWidget(const SizedBox.expand());
    expect(binding.framesEnabled, true);
    // Pump two frames to make sure we clear out any inter-frame comparisons.
    await binding.endOfFrame;
    await binding.endOfFrame;
  });

41 42
  tearDownAll(() async {
    await vmService.dispose();
43 44
  });

45 46
  test('Image painting events - deduplicates across frames', () async {
    final Completer<Event> completer = Completer<Event>();
47 48 49 50
    vmService
        .onExtensionEvent
        .firstWhere((Event event) => event.extensionKind == 'Flutter.ImageSizesForFrame')
        .then(completer.complete);
51

52
    final ui.Image image = await createTestImage(width: 300, height: 300);
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    final TestCanvas canvas = TestCanvas();
    paintImage(
      canvas: canvas,
      rect: const Rect.fromLTWH(50.0, 75.0, 200.0, 100.0),
      image: image,
      debugImageLabel: 'test.png',
    );

    // Make sure that we don't report an identical image size info if we
    // redraw in the next frame.
    await binding.endOfFrame;

    paintImage(
      canvas: canvas,
      rect: const Rect.fromLTWH(50.0, 75.0, 200.0, 100.0),
      image: image,
      debugImageLabel: 'test.png',
    );
    await binding.endOfFrame;

    final Event event = await completer.future;
    expect(event.extensionKind, 'Flutter.ImageSizesForFrame');
    expect(
76
      jsonEncode(event.extensionData!.data),
77
      contains('"test.png":{"source":"test.png","displaySize":{"width":600.0,"height":300.0},"imageSize":{"width":300.0,"height":300.0},"displaySizeInBytes":960000,"decodedSizeInBytes":480000}'),
78
    );
79
  }, skip: isBrowser); // [intended] uses dart:isolate and io.
80 81 82

  test('Image painting events - deduplicates across frames', () async {
    final Completer<Event> completer = Completer<Event>();
83 84 85 86
    vmService
        .onExtensionEvent
        .firstWhere((Event event) => event.extensionKind == 'Flutter.ImageSizesForFrame')
        .then(completer.complete);
87

88
    final ui.Image image = await createTestImage(width: 300, height: 300);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    final TestCanvas canvas = TestCanvas();
    paintImage(
      canvas: canvas,
      rect: const Rect.fromLTWH(50.0, 75.0, 200.0, 100.0),
      image: image,
      debugImageLabel: 'test.png',
    );

    paintImage(
      canvas: canvas,
      rect: const Rect.fromLTWH(50.0, 75.0, 300.0, 300.0),
      image: image,
      debugImageLabel: 'test.png',
    );
    await binding.endOfFrame;

    final Event event = await completer.future;
    expect(event.extensionKind, 'Flutter.ImageSizesForFrame');
    expect(
108
      jsonEncode(event.extensionData!.data),
109
      contains('"test.png":{"source":"test.png","displaySize":{"width":900.0,"height":900.0},"imageSize":{"width":300.0,"height":300.0},"displaySizeInBytes":4320000,"decodedSizeInBytes":480000}'),
110
    );
111
  }, skip: isBrowser); // [intended] uses dart:isolate and io.
112 113 114 115 116 117
}

class TestCanvas implements Canvas {
  @override
  void noSuchMethod(Invocation invocation) {}
}