scrollable_fling_test.dart 4.43 KB
Newer Older
1 2 3 4 5 6
// Copyright 2015 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_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));
46
    final double result1 = 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));
55
    final double result2 = getCurrentOffset();
56 57 58

    expect(result1, lessThan(result2)); // iOS (result2) is slipperier than Android (result1)
  });
59 60

  testWidgets('fling and tap to stop', (WidgetTester tester) async {
61
    final List<String> log = <String>[];
62
    await tester.pumpWidget(
63
      Directionality(
64
        textDirection: TextDirection.ltr,
65 66 67 68 69 70 71
        child: ListView(
          dragStartBehavior: DragStartBehavior.down,
          children: List<Widget>.generate(250, (int i) => GestureDetector(
            onTap: () { log.add('tap $i'); },
            child: Text('$i', style: testFont),
          )),
        ),
72 73
      ),
    );
74 75

    expect(log, equals(<String>[]));
Adam Barth's avatar
Adam Barth committed
76
    await tester.tap(find.byType(Scrollable));
77
    await tester.pump(const Duration(milliseconds: 50));
78
    expect(log, equals(<String>['tap 21']));
Adam Barth's avatar
Adam Barth committed
79
    await tester.fling(find.byType(Scrollable), const Offset(0.0, -200.0), 1000.0);
80
    await tester.pump(const Duration(milliseconds: 50));
81 82
    expect(log, equals(<String>['tap 21']));
    await tester.tap(find.byType(Scrollable)); // should stop the fling but not tap anything
83
    await tester.pump(const Duration(milliseconds: 50));
84
    expect(log, equals(<String>['tap 21']));
Adam Barth's avatar
Adam Barth committed
85
    await tester.tap(find.byType(Scrollable));
86
    await tester.pump(const Duration(milliseconds: 50));
87
    expect(log, equals(<String>['tap 21', 'tap 35']));
88
  }, skip: isBrowser);
89 90

  testWidgets('fling and wait and tap', (WidgetTester tester) async {
91
    final List<String> log = <String>[];
92
    await tester.pumpWidget(
93
      Directionality(
94
        textDirection: TextDirection.ltr,
95 96 97 98 99 100 101
        child: ListView(
          dragStartBehavior: DragStartBehavior.down,
          children: List<Widget>.generate(250, (int i) => GestureDetector(
            onTap: () { log.add('tap $i'); },
            child: Text('$i', style: testFont),
          )),
        ),
102 103
      ),
    );
104 105

    expect(log, equals(<String>[]));
Adam Barth's avatar
Adam Barth committed
106
    await tester.tap(find.byType(Scrollable));
107
    await tester.pump(const Duration(milliseconds: 50));
108
    expect(log, equals(<String>['tap 21']));
Adam Barth's avatar
Adam Barth committed
109
    await tester.fling(find.byType(Scrollable), const Offset(0.0, -200.0), 1000.0);
110
    await tester.pump(const Duration(milliseconds: 50));
111 112 113
    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
114
    await tester.tap(find.byType(Scrollable));
115
    await tester.pump(const Duration(milliseconds: 50));
116
    expect(log, equals(<String>['tap 21', 'tap 48']));
117
  }, skip: isBrowser);
118
}