paint_image_test.dart 1.68 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
import 'dart:async';
import 'dart:typed_data';
7 8 9 10
import 'dart:ui' as ui;

import 'package:flutter/painting.dart';

11
import '../flutter_test_alternative.dart';
12 13 14 15

class TestImage implements ui.Image {
  TestImage({ this.width, this.height });

16
  @override
17
  final int width;
18 19

  @override
20 21
  final int height;

22
  @override
23
  void dispose() { }
24 25

  @override
26
  Future<ByteData> toByteData({ ui.ImageByteFormat format = ui.ImageByteFormat.rawRgba }) async {
27
    throw UnsupportedError('Cannot encode test image');
28
  }
29 30 31 32 33
}

class TestCanvas implements Canvas {
  final List<Invocation> invocations = <Invocation>[];

34
  @override
35 36 37 38 39 40
  void noSuchMethod(Invocation invocation) {
    invocations.add(invocation);
  }
}

void main() {
41
  test('Cover and align', () {
42 43
    final TestImage image = TestImage(width: 300, height: 300);
    final TestCanvas canvas = TestCanvas();
44 45
    paintImage(
      canvas: canvas,
Dan Field's avatar
Dan Field committed
46
      rect: const Rect.fromLTWH(50.0, 75.0, 200.0, 100.0),
47
      image: image,
48
      fit: BoxFit.cover,
49
      alignment: const Alignment(-1.0, 0.0),
50 51
    );

52
    final Invocation command = canvas.invocations.firstWhere((Invocation invocation) {
53 54 55 56 57
      return invocation.memberName == #drawImageRect;
    });

    expect(command, isNotNull);
    expect(command.positionalArguments[0], equals(image));
Dan Field's avatar
Dan Field committed
58 59
    expect(command.positionalArguments[1], equals(const Rect.fromLTWH(0.0, 75.0, 300.0, 150.0)));
    expect(command.positionalArguments[2], equals(const Rect.fromLTWH(50.0, 75.0, 200.0, 100.0)));
60
  });
61 62

  // See also the DecorationImage tests in: decoration_test.dart
63
}