scrollable_fling_test.dart 6.46 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
import 'package:flutter/gestures.dart' show DragStartBehavior;
6 7
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
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 49 50 51
    // Regression test for https://github.com/flutter/flutter/issues/83632
    // Before changing these values, ensure the fling results in a distance that
    // makes sense. See issue for more context.
    expect(androidResult, greaterThan(394.0));
    expect(androidResult, lessThan(395.0));
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    await pumpTest(tester, TargetPlatform.linux);
    await tester.fling(find.byType(ListView), const Offset(0.0, -dragOffset), 1000.0);
    expect(getCurrentOffset(), dragOffset);
    await tester.pump(); // trigger fling
    expect(getCurrentOffset(), dragOffset);
    await tester.pump(const Duration(seconds: 5));
    final double linuxResult = getCurrentOffset();

    await pumpTest(tester, TargetPlatform.windows);
    await tester.fling(find.byType(ListView), const Offset(0.0, -dragOffset), 1000.0);
    expect(getCurrentOffset(), dragOffset);
    await tester.pump(); // trigger fling
    expect(getCurrentOffset(), dragOffset);
    await tester.pump(const Duration(seconds: 5));
    final double windowsResult = getCurrentOffset();

69
    await pumpTest(tester, TargetPlatform.iOS);
70
    await tester.fling(find.byType(ListView), const Offset(0.0, -dragOffset), 1000.0);
71 72
    // Scroll starts ease into the scroll on iOS.
    expect(getCurrentOffset(), moreOrLessEquals(210.71026666666666));
73
    await tester.pump(); // trigger fling
74
    expect(getCurrentOffset(), moreOrLessEquals(210.71026666666666));
75
    await tester.pump(const Duration(seconds: 5));
Dan Field's avatar
Dan Field committed
76
    final double iOSResult = getCurrentOffset();
77

Dan Field's avatar
Dan Field committed
78 79 80 81 82 83 84 85 86 87 88
    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
89 90 91 92 93 94 95 96
    expect(linuxResult, lessThan(iOSResult)); // iOS is slipperier than Linux
    expect(linuxResult, lessThan(macOSResult)); // macOS is slipperier than Linux
    expect(windowsResult, lessThan(iOSResult)); // iOS is slipperier than Windows
    expect(windowsResult, lessThan(macOSResult)); // macOS is slipperier than Windows
    expect(windowsResult, equals(androidResult));
    expect(windowsResult, equals(androidResult));
    expect(linuxResult, equals(androidResult));
    expect(linuxResult, equals(androidResult));
97
  });
98 99

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

    expect(log, equals(<String>[]));
Adam Barth's avatar
Adam Barth committed
115
    await tester.tap(find.byType(Scrollable));
116
    await tester.pump(const Duration(milliseconds: 50));
117
    expect(log, equals(<String>['tap 21']));
Adam Barth's avatar
Adam Barth committed
118
    await tester.fling(find.byType(Scrollable), const Offset(0.0, -200.0), 1000.0);
119
    await tester.pump(const Duration(milliseconds: 50));
120 121
    expect(log, equals(<String>['tap 21']));
    await tester.tap(find.byType(Scrollable)); // should stop the fling but not tap anything
122
    await tester.pump(const Duration(milliseconds: 50));
123
    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 35']));
127
  });
128 129

  testWidgets('fling and wait and tap', (WidgetTester tester) async {
130
    final List<String> log = <String>[];
131
    await tester.pumpWidget(
132
      Directionality(
133
        textDirection: TextDirection.ltr,
134 135 136 137 138 139 140
        child: ListView(
          dragStartBehavior: DragStartBehavior.down,
          children: List<Widget>.generate(250, (int i) => GestureDetector(
            onTap: () { log.add('tap $i'); },
            child: Text('$i', style: testFont),
          )),
        ),
141 142
      ),
    );
143 144

    expect(log, equals(<String>[]));
Adam Barth's avatar
Adam Barth committed
145
    await tester.tap(find.byType(Scrollable));
146
    await tester.pump(const Duration(milliseconds: 50));
147
    expect(log, equals(<String>['tap 21']));
Adam Barth's avatar
Adam Barth committed
148
    await tester.fling(find.byType(Scrollable), const Offset(0.0, -200.0), 1000.0);
149
    await tester.pump(const Duration(milliseconds: 50));
150 151 152
    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
153
    await tester.tap(find.byType(Scrollable));
154
    await tester.pump(const Duration(milliseconds: 50));
155
    expect(log, equals(<String>['tap 21', 'tap 48']));
156
  });
157
}