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

import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
7
import 'package:flutter_test/flutter_test.dart';
8 9 10

void main() {
  testWidgets('Does not animate if already at target position', (WidgetTester tester) async {
11
    final ScrollController controller = ScrollController();
12
    await tester.pumpWidget(
13
      Directionality(
14
        textDirection: TextDirection.ltr,
15
        child: ListView(
16
          controller: controller,
17
          children: List<Widget>.generate(80, (int i) => Text('$i', textDirection: TextDirection.ltr)),
18 19 20
        ),
      ),
    );
21 22 23 24 25 26 27 28 29 30

    expectNoAnimation();
    final double currentPosition = controller.position.pixels;
    controller.position.animateTo(currentPosition, duration: const Duration(seconds: 10), curve: Curves.linear);

    expectNoAnimation();
    expect(controller.position.pixels, currentPosition);
  });

  testWidgets('Does not animate if already at target position within tolerance', (WidgetTester tester) async {
31
    final ScrollController controller = ScrollController();
32
    await tester.pumpWidget(
33
      Directionality(
34
        textDirection: TextDirection.ltr,
35
        child: ListView(
36
          controller: controller,
37
          children: List<Widget>.generate(80, (int i) => Text('$i', textDirection: TextDirection.ltr)),
38 39 40
        ),
      ),
    );
41 42 43

    expectNoAnimation();

44
    final double halfTolerance = controller.position.physics.toleranceFor(controller.position).distance / 2;
45 46 47 48 49 50 51 52 53
    expect(halfTolerance, isNonZero);
    final double targetPosition = controller.position.pixels + halfTolerance;
    controller.position.animateTo(targetPosition, duration: const Duration(seconds: 10), curve: Curves.linear);

    expectNoAnimation();
    expect(controller.position.pixels, targetPosition);
  });

  testWidgets('Animates if going to a position outside of tolerance', (WidgetTester tester) async {
54
    final ScrollController controller = ScrollController();
55
    await tester.pumpWidget(
56
      Directionality(
57
        textDirection: TextDirection.ltr,
58
        child: ListView(
59
          controller: controller,
60
          children: List<Widget>.generate(80, (int i) => Text('$i', textDirection: TextDirection.ltr)),
61 62 63
        ),
      ),
    );
64 65 66

    expectNoAnimation();

67
    final double doubleTolerance = controller.position.physics.toleranceFor(controller.position).distance * 2;
68 69 70 71
    expect(doubleTolerance, isNonZero);
    final double targetPosition = controller.position.pixels + doubleTolerance;
    controller.position.animateTo(targetPosition, duration: const Duration(seconds: 10), curve: Curves.linear);

72
    expect(SchedulerBinding.instance.transientCallbackCount, equals(1), reason: 'Expected an animation.');
73 74 75 76
  });
}

void expectNoAnimation() {
77
  expect(SchedulerBinding.instance.transientCallbackCount, equals(0), reason: 'Expected no animation.');
78
}