fractional_offset_test.dart 1.97 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
// @dart = 2.8

7 8 9 10 11
import 'package:flutter/painting.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  test('FractionalOffset control test', () {
12 13
    const FractionalOffset a = FractionalOffset(0.5, 0.25);
    const FractionalOffset b = FractionalOffset(1.25, 0.75);
14 15 16 17 18 19 20 21 22 23 24 25

    expect(a, hasOneLineDescription);
    expect(a.hashCode, equals(const FractionalOffset(0.5, 0.25).hashCode));
    expect(a.toString(), equals('FractionalOffset(0.5, 0.3)'));

    expect(-a, const FractionalOffset(-0.5, -0.25));
    expect(a - b, const FractionalOffset(-0.75, -0.5));
    expect(a + b, const FractionalOffset(1.75, 1.0));
    expect(a * 2.0, const FractionalOffset(1.0, 0.5));
    expect(a / 2.0, const FractionalOffset(0.25, 0.125));
    expect(a ~/ 2.0, const FractionalOffset(0.0, 0.0));
    expect(a % 5.0, const FractionalOffset(0.5, 0.25));
26 27 28
  });

  test('FractionalOffset.lerp()', () {
29 30
    const FractionalOffset a = FractionalOffset.topLeft;
    const FractionalOffset b = FractionalOffset.topCenter;
31
    expect(FractionalOffset.lerp(a, b, 0.25), equals(const FractionalOffset(0.125, 0.0)));
32 33

    expect(FractionalOffset.lerp(null, null, 0.25), isNull);
34 35
    expect(FractionalOffset.lerp(null, b, 0.25), equals(const FractionalOffset(0.5, 0.5 - 0.125)));
    expect(FractionalOffset.lerp(a, null, 0.25), equals(const FractionalOffset(0.125, 0.125)));
36
  });
37 38

  test('FractionalOffset.fromOffsetAndSize()', () {
39
    final FractionalOffset a = FractionalOffset.fromOffsetAndSize(const Offset(100.0, 100.0), const Size(200.0, 400.0));
40 41 42 43
    expect(a, const FractionalOffset(0.5, 0.25));
  });

  test('FractionalOffset.fromOffsetAndRect()', () {
Dan Field's avatar
Dan Field committed
44
    final FractionalOffset a = FractionalOffset.fromOffsetAndRect(const Offset(150.0, 120.0), const Rect.fromLTWH(50.0, 20.0, 200.0, 400.0));
45 46
    expect(a, const FractionalOffset(0.5, 0.25));
  });
47
}