image_painting_event_test.dart 3.93 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
  late VmService vmService;
  late LiveTestWidgetsFlutterBinding binding;
18 19 20 21 22 23 24 25
  setUpAll(() async {
    final developer.ServiceProtocolInfo info =
        await developer.Service.getInfo();

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

26
    vmService = await vmServiceConnectUri('ws://localhost:${info.serverUri!.port}${info.serverUri!.path}ws');
27 28 29 30 31 32 33 34 35 36 37 38
    await vmService.streamListen(EventStreams.kExtension);

    // Initialize bindings
    binding = LiveTestWidgetsFlutterBinding();
    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;
  });

39 40
  tearDownAll(() async {
    await vmService.dispose();
41 42
  });

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

50
    final ui.Image image = await createTestImage(width: 300, height: 300);
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    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(
74
      jsonEncode(event.extensionData!.data),
75
      '{"test.png":{"source":"test.png","displaySize":{"width":600.0,"height":300.0},"imageSize":{"width":300.0,"height":300.0},"displaySizeInBytes":960000,"decodedSizeInBytes":480000}}',
76
    );
77
  }, skip: isBrowser); // [intended] uses dart:isolate and io.
78 79 80

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

86
    final ui.Image image = await createTestImage(width: 300, height: 300);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    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(
106
      jsonEncode(event.extensionData!.data),
107
      '{"test.png":{"source":"test.png","displaySize":{"width":900.0,"height":900.0},"imageSize":{"width":300.0,"height":300.0},"displaySizeInBytes":4320000,"decodedSizeInBytes":480000}}',
108
    );
109
  }, skip: isBrowser); // [intended] uses dart:isolate and io.
110 111 112 113 114 115
}

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