friction_simulation_test.dart 1.92 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 12 13
import 'package:flutter/physics.dart';
import 'package:flutter_test/flutter_test.dart';

const double _kEpsilon = .00001;

void main() {
  test('Friction simulation positive velocity', () {
14
    final FrictionSimulation friction = FrictionSimulation(0.135, 100.0, 100.0);
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

    expect(friction.x(0.0), closeTo(100.0, _kEpsilon));
    expect(friction.dx(0.0), closeTo(100.0, _kEpsilon));

    expect(friction.x(0.1), closeTo(110.0, 1.0));
    expect(friction.x(0.5), closeTo(131.0, 1.0));
    expect(friction.x(2.0), closeTo(149.0, 1.0));

    expect(friction.finalX, closeTo(149.0, 1.0));

    expect(friction.timeAtX(100.0), 0.0);
    expect(friction.timeAtX(friction.x(0.1)), closeTo(0.1, _kEpsilon));
    expect(friction.timeAtX(friction.x(0.5)), closeTo(0.5, _kEpsilon));
    expect(friction.timeAtX(friction.x(2.0)), closeTo(2.0, _kEpsilon));

30 31
    expect(friction.timeAtX(-1.0), double.infinity);
    expect(friction.timeAtX(200.0), double.infinity);
32 33 34
  });

  test('Friction simulation negative velocity', () {
35
    final FrictionSimulation friction = FrictionSimulation(0.135, 100.0, -100.0);
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

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

    expect(friction.x(0.1), closeTo(91.0, 1.0));
    expect(friction.x(0.5), closeTo(68.0, 1.0));
    expect(friction.x(2.0), closeTo(51.0, 1.0));

    expect(friction.finalX, closeTo(50, 1.0));

    expect(friction.timeAtX(100.0), 0.0);
    expect(friction.timeAtX(friction.x(0.1)), closeTo(0.1, _kEpsilon));
    expect(friction.timeAtX(friction.x(0.5)), closeTo(0.5, _kEpsilon));
    expect(friction.timeAtX(friction.x(2.0)), closeTo(2.0, _kEpsilon));

51 52
    expect(friction.timeAtX(101.0), double.infinity);
    expect(friction.timeAtX(40.0), double.infinity);
53 54
  });
}