friction_simulation_test.dart 2.88 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/physics.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  test('Friction simulation positive velocity', () {
10
    final FrictionSimulation friction = FrictionSimulation(0.135, 100.0, 100.0);
11

12 13
    expect(friction.x(0.0), moreOrLessEquals(100.0));
    expect(friction.dx(0.0), moreOrLessEquals(100.0));
14

15 16 17
    expect(friction.x(0.1), moreOrLessEquals(110.0, epsilon: 1.0));
    expect(friction.x(0.5), moreOrLessEquals(131.0, epsilon: 1.0));
    expect(friction.x(2.0), moreOrLessEquals(149.0, epsilon: 1.0));
18

19
    expect(friction.finalX, moreOrLessEquals(149.0, epsilon: 1.0));
20 21

    expect(friction.timeAtX(100.0), 0.0);
22 23 24
    expect(friction.timeAtX(friction.x(0.1)), moreOrLessEquals(0.1));
    expect(friction.timeAtX(friction.x(0.5)), moreOrLessEquals(0.5));
    expect(friction.timeAtX(friction.x(2.0)), moreOrLessEquals(2.0));
25

26 27
    expect(friction.timeAtX(-1.0), double.infinity);
    expect(friction.timeAtX(200.0), double.infinity);
28 29 30
  });

  test('Friction simulation negative velocity', () {
31
    final FrictionSimulation friction = FrictionSimulation(0.135, 100.0, -100.0);
32

33 34
    expect(friction.x(0.0), moreOrLessEquals(100.0));
    expect(friction.dx(0.0), moreOrLessEquals(-100.0));
35

36 37 38
    expect(friction.x(0.1), moreOrLessEquals(91.0, epsilon: 1.0));
    expect(friction.x(0.5), moreOrLessEquals(68.0, epsilon: 1.0));
    expect(friction.x(2.0), moreOrLessEquals(51.0, epsilon: 1.0));
39

40
    expect(friction.finalX, moreOrLessEquals(50, epsilon: 1.0));
41 42

    expect(friction.timeAtX(100.0), 0.0);
43 44 45
    expect(friction.timeAtX(friction.x(0.1)), moreOrLessEquals(0.1));
    expect(friction.timeAtX(friction.x(0.5)), moreOrLessEquals(0.5));
    expect(friction.timeAtX(friction.x(2.0)), moreOrLessEquals(2.0));
46

47 48
    expect(friction.timeAtX(101.0), double.infinity);
    expect(friction.timeAtX(40.0), double.infinity);
49
  });
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

  test('Friction simulation constant deceleration', () {
    final FrictionSimulation friction = FrictionSimulation(0.135, 100.0, -100.0, constantDeceleration: 100);

    expect(friction.x(0.0), moreOrLessEquals(100.0));
    expect(friction.dx(0.0), moreOrLessEquals(-100.0));

    expect(friction.x(0.1), moreOrLessEquals(91.0, epsilon: 1.0));
    expect(friction.x(0.5), moreOrLessEquals(80.0, epsilon: 1.0));
    expect(friction.x(2.0), moreOrLessEquals(80.0, epsilon: 1.0));

    expect(friction.finalX, moreOrLessEquals(80.0, epsilon: 1.0));

    expect(friction.timeAtX(100.0), 0.0);
    expect(friction.timeAtX(friction.x(0.1)), moreOrLessEquals(0.1));
    expect(friction.timeAtX(friction.x(0.2)), moreOrLessEquals(0.2));
    expect(friction.timeAtX(friction.x(0.3)), moreOrLessEquals(0.3));

    expect(friction.timeAtX(101.0), double.infinity);
    expect(friction.timeAtX(40.0), double.infinity);
  });
71
}