friction_simulation_test.dart 1.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2017 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/physics.dart';
import 'package:flutter_test/flutter_test.dart';

const double _kEpsilon = .00001;

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

    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));

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

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

    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));

49 50
    expect(friction.timeAtX(101.0), double.infinity);
    expect(friction.timeAtX(40.0), double.infinity);
51 52
  });
}