scrollable_fling_test.dart 4.97 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_test/flutter_test.dart';
import 'package:flutter/material.dart';
7
import 'package:flutter/gestures.dart' show DragStartBehavior;
8

9 10
const TextStyle testFont = TextStyle(
  color: Color(0xFF00FF00),
11 12 13
  fontFamily: 'Ahem',
);

14
Future<void> pumpTest(WidgetTester tester, TargetPlatform platform) async {
15 16 17
  await tester.pumpWidget(Container());
  await tester.pumpWidget(MaterialApp(
    theme: ThemeData(
18
      platform: platform,
19
    ),
20
    home: Container(
21
      color: const Color(0xFF111111),
22
      child: ListView.builder(
23
        dragStartBehavior: DragStartBehavior.down,
24
        itemBuilder: (BuildContext context, int index) {
25
          return Text('$index', style: testFont);
26 27
        },
      ),
28 29 30 31 32 33 34 35
    ),
  ));
}

const double dragOffset = 213.82;

void main() {
  testWidgets('Flings on different platforms', (WidgetTester tester) async {
36
    double getCurrentOffset() {
Adam Barth's avatar
Adam Barth committed
37
      return tester.state<ScrollableState>(find.byType(Scrollable)).position.pixels;
38 39
    }

40
    await pumpTest(tester, TargetPlatform.android);
41 42
    await tester.fling(find.byType(ListView), const Offset(0.0, -dragOffset), 1000.0);
    expect(getCurrentOffset(), dragOffset);
43
    await tester.pump(); // trigger fling
44
    expect(getCurrentOffset(), dragOffset);
45
    await tester.pump(const Duration(seconds: 5));
Dan Field's avatar
Dan Field committed
46
    final double androidResult = getCurrentOffset();
47 48

    await pumpTest(tester, TargetPlatform.iOS);
49
    await tester.fling(find.byType(ListView), const Offset(0.0, -dragOffset), 1000.0);
50 51
    // Scroll starts ease into the scroll on iOS.
    expect(getCurrentOffset(), moreOrLessEquals(210.71026666666666));
52
    await tester.pump(); // trigger fling
53
    expect(getCurrentOffset(), moreOrLessEquals(210.71026666666666));
54
    await tester.pump(const Duration(seconds: 5));
Dan Field's avatar
Dan Field committed
55
    final double iOSResult = getCurrentOffset();
56

Dan Field's avatar
Dan Field committed
57 58 59 60 61 62 63 64 65 66 67
    await pumpTest(tester, TargetPlatform.macOS);
    await tester.fling(find.byType(ListView), const Offset(0.0, -dragOffset), 1000.0);
    // Scroll starts ease into the scroll on iOS.
    expect(getCurrentOffset(), moreOrLessEquals(210.71026666666666));
    await tester.pump(); // trigger fling
    expect(getCurrentOffset(), moreOrLessEquals(210.71026666666666));
    await tester.pump(const Duration(seconds: 5));
    final double macOSResult = getCurrentOffset();

    expect(androidResult, lessThan(iOSResult)); // iOS is slipperier than Android
    expect(androidResult, lessThan(macOSResult)); // macOS is slipperier than Android
68
  });
69 70

  testWidgets('fling and tap to stop', (WidgetTester tester) async {
71
    final List<String> log = <String>[];
72
    await tester.pumpWidget(
73
      Directionality(
74
        textDirection: TextDirection.ltr,
75 76 77 78 79 80 81
        child: ListView(
          dragStartBehavior: DragStartBehavior.down,
          children: List<Widget>.generate(250, (int i) => GestureDetector(
            onTap: () { log.add('tap $i'); },
            child: Text('$i', style: testFont),
          )),
        ),
82 83
      ),
    );
84 85

    expect(log, equals(<String>[]));
Adam Barth's avatar
Adam Barth committed
86
    await tester.tap(find.byType(Scrollable));
87
    await tester.pump(const Duration(milliseconds: 50));
88
    expect(log, equals(<String>['tap 21']));
Adam Barth's avatar
Adam Barth committed
89
    await tester.fling(find.byType(Scrollable), const Offset(0.0, -200.0), 1000.0);
90
    await tester.pump(const Duration(milliseconds: 50));
91 92
    expect(log, equals(<String>['tap 21']));
    await tester.tap(find.byType(Scrollable)); // should stop the fling but not tap anything
93
    await tester.pump(const Duration(milliseconds: 50));
94
    expect(log, equals(<String>['tap 21']));
Adam Barth's avatar
Adam Barth committed
95
    await tester.tap(find.byType(Scrollable));
96
    await tester.pump(const Duration(milliseconds: 50));
97
    expect(log, equals(<String>['tap 21', 'tap 35']));
98
  }, skip: isBrowser);
99 100

  testWidgets('fling and wait and tap', (WidgetTester tester) async {
101
    final List<String> log = <String>[];
102
    await tester.pumpWidget(
103
      Directionality(
104
        textDirection: TextDirection.ltr,
105 106 107 108 109 110 111
        child: ListView(
          dragStartBehavior: DragStartBehavior.down,
          children: List<Widget>.generate(250, (int i) => GestureDetector(
            onTap: () { log.add('tap $i'); },
            child: Text('$i', style: testFont),
          )),
        ),
112 113
      ),
    );
114 115

    expect(log, equals(<String>[]));
Adam Barth's avatar
Adam Barth committed
116
    await tester.tap(find.byType(Scrollable));
117
    await tester.pump(const Duration(milliseconds: 50));
118
    expect(log, equals(<String>['tap 21']));
Adam Barth's avatar
Adam Barth committed
119
    await tester.fling(find.byType(Scrollable), const Offset(0.0, -200.0), 1000.0);
120
    await tester.pump(const Duration(milliseconds: 50));
121 122 123
    expect(log, equals(<String>['tap 21']));
    await tester.pump(const Duration(seconds: 50)); // long wait, so the fling will have ended at the end of it
    expect(log, equals(<String>['tap 21']));
Adam Barth's avatar
Adam Barth committed
124
    await tester.tap(find.byType(Scrollable));
125
    await tester.pump(const Duration(milliseconds: 50));
126
    expect(log, equals(<String>['tap 21', 'tap 48']));
127
  }, skip: isBrowser);
128
}