box_fit_test.dart 3.02 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 The Chromium 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 'package:flutter/painting.dart';

7
import '../flutter_test_alternative.dart';
8 9

void main() {
10
  test('applyBoxFit', () {
11 12
    FittedSizes result;

13
    result = applyBoxFit(BoxFit.scaleDown, const Size(100.0, 1000.0), const Size(200.0, 2000.0));
14 15 16
    expect(result.source, equals(const Size(100.0, 1000.0)));
    expect(result.destination, equals(const Size(100.0, 1000.0)));

17
    result = applyBoxFit(BoxFit.scaleDown, const Size(300.0, 3000.0), const Size(200.0, 2000.0));
18 19
    expect(result.source, equals(const Size(300.0, 3000.0)));
    expect(result.destination, equals(const Size(200.0, 2000.0)));
20

21
    result = applyBoxFit(BoxFit.fitWidth, const Size(2000.0, 400.0), const Size(1000.0, 100.0));
22 23 24
    expect(result.source, equals(const Size(2000.0, 200.0)));
    expect(result.destination, equals(const Size(1000.0, 100.0)));

25
    result = applyBoxFit(BoxFit.fitHeight, const Size(400.0, 2000.0), const Size(100.0, 1000.0));
26 27
    expect(result.source, equals(const Size(200.0, 2000.0)));
    expect(result.destination, equals(const Size(100.0, 1000.0)));
28 29 30 31 32 33 34 35

    _testZeroAndNegativeSizes(BoxFit.fill);
    _testZeroAndNegativeSizes(BoxFit.contain);
    _testZeroAndNegativeSizes(BoxFit.cover);
    _testZeroAndNegativeSizes(BoxFit.fitWidth);
    _testZeroAndNegativeSizes(BoxFit.fitHeight);
    _testZeroAndNegativeSizes(BoxFit.none);
    _testZeroAndNegativeSizes(BoxFit.scaleDown);
36 37
  });
}
38 39 40

void _testZeroAndNegativeSizes(BoxFit fit) {
    FittedSizes result;
41

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    result = applyBoxFit(fit, const Size(-400.0, 2000.0), const Size(100.0, 1000.0));
    expect(result.source, equals(Size.zero));
    expect(result.destination, equals(Size.zero));

    result = applyBoxFit(fit, const Size(400.0, -2000.0), const Size(100.0, 1000.0));
    expect(result.source, equals(Size.zero));
    expect(result.destination, equals(Size.zero));

    result = applyBoxFit(fit, const Size(400.0, 2000.0), const Size(-100.0, 1000.0));
    expect(result.source, equals(Size.zero));
    expect(result.destination, equals(Size.zero));

    result = applyBoxFit(fit, const Size(400.0, 2000.0), const Size(100.0, -1000.0));
    expect(result.source, equals(Size.zero));
    expect(result.destination, equals(Size.zero));

    result = applyBoxFit(fit, const Size(0.0, 2000.0), const Size(100.0, 1000.0));
    expect(result.source, equals(Size.zero));
    expect(result.destination, equals(Size.zero));

    result = applyBoxFit(fit, const Size(400.0, 0.0), const Size(100.0, 1000.0));
    expect(result.source, equals(Size.zero));
    expect(result.destination, equals(Size.zero));

    result = applyBoxFit(fit, const Size(400.0, 2000.0), const Size(0.0, 1000.0));
    expect(result.source, equals(Size.zero));
    expect(result.destination, equals(Size.zero));

    result = applyBoxFit(fit, const Size(400.0, 2000.0), const Size(100.0, 0.0));
    expect(result.source, equals(Size.zero));
    expect(result.destination, equals(Size.zero));
}