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

import 'package:flutter/cupertino.dart';
import 'package:flutter_test/flutter_test.dart';

import '../rendering/mock_canvas.dart';

10
const Color _kScrollbarColor = Color(0x59000000);
11 12 13 14

// The `y` offset has to be larger than `ScrollDragController._bigThresholdBreakDistance`
// to prevent [motionStartDistanceThreshold] from affecting the actual drag distance.
const Offset _kGestureOffset = Offset(0, -25);
15
const Radius _kScrollbarRadius = Radius.circular(1.5);
16

17 18
void main() {
  testWidgets('Paints iOS spec', (WidgetTester tester) async {
19 20 21 22 23 24 25 26 27 28
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: MediaQuery(
          data: MediaQueryData(),
          child: CupertinoScrollbar(
            child: SingleChildScrollView(
              child: SizedBox(width: 4000.0, height: 4000.0),
            ),
          ),
29 30
        ),
      ),
31 32
    );

33
    expect(find.byType(CupertinoScrollbar), isNot(paints..rrect()));
34

35
    final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(SingleChildScrollView)));
36
    await gesture.moveBy(_kGestureOffset);
37
    // Move back to original position.
38
    await gesture.moveBy(Offset.zero.translate(-_kGestureOffset.dx, -_kGestureOffset.dy));
39 40 41
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 500));
    expect(find.byType(CupertinoScrollbar), paints..rrect(
42
      color: _kScrollbarColor,
43
      rrect: RRect.fromRectAndRadius(
Dan Field's avatar
Dan Field committed
44
        const Rect.fromLTWH(
45
          800.0 - 3 - 3, // Screen width - margin - thickness.
46
          3.0, // Initial position is the top margin.
47
          3, // Thickness.
48
          // Fraction in viewport * scrollbar height - top, bottom margin.
49 50
          600.0 / 4000.0 * (600.0 - 2 * 3),
        ),
51
        _kScrollbarRadius,
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
      ),
    ));
  });

  testWidgets('Paints iOS spec with nav bar', (WidgetTester tester) async {
    await tester.pumpWidget(
      CupertinoApp(
        home: MediaQuery(
          data: const MediaQueryData(
            padding: EdgeInsets.fromLTRB(0, 20, 0, 34),
          ),
          child: CupertinoPageScaffold(
            navigationBar: const CupertinoNavigationBar(
              middle: Text('Title'),
              backgroundColor: Color(0x11111111),
            ),
            child: CupertinoScrollbar(
              child: ListView(
70
                children: const <Widget>[SizedBox(width: 4000, height: 4000)],
71 72 73 74 75 76 77 78 79 80
              ),
            ),
          ),
        ),
      ),
    );

    final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(ListView)));
    await gesture.moveBy(_kGestureOffset);
    // Move back to original position.
81
    await gesture.moveBy(Offset(-_kGestureOffset.dx, -_kGestureOffset.dy));
82 83 84 85 86 87 88
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 500));

    expect(find.byType(CupertinoScrollbar), paints..rrect(
      color: _kScrollbarColor,
      rrect: RRect.fromRectAndRadius(
        const Rect.fromLTWH(
89
          800.0 - 3 - 3, // Screen width - margin - thickness.
90
          44 + 20 + 3.0, // nav bar height + top margin
91
          3, // Thickness.
92 93 94
          // Fraction visible * (viewport size - padding - margin)
          // where Fraction visible = (viewport size - padding) / content size
          (600.0 - 34 - 44 - 20) / 4000.0 * (600.0 - 2 * 3 - 34 - 44 - 20),
95
        ),
96
        _kScrollbarRadius,
97 98 99
      ),
    ));
  });
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

  testWidgets("should not paint when there isn't enough space", (WidgetTester tester) async {
    await tester.pumpWidget(
      CupertinoApp(
        home: MediaQuery(
          data: const MediaQueryData(
            padding: EdgeInsets.fromLTRB(0, 20, 0, 34),
          ),
          child: CupertinoPageScaffold(
            navigationBar: const CupertinoNavigationBar(
              middle: Text('Title'),
              backgroundColor: Color(0x11111111),
            ),
            child: CupertinoScrollbar(
              child: ListView(
                physics: const AlwaysScrollableScrollPhysics(parent: BouncingScrollPhysics()),
                children: const <Widget> [SizedBox(width: 10, height: 10)],
              ),
            ),
          ),
        ),
      ),
    );
    final TestGesture gesture = await tester.startGesture(tester.getCenter(find.byType(ListView)));
    await gesture.moveBy(_kGestureOffset);
    // Move back to original position.
    await gesture.moveBy(Offset(-_kGestureOffset.dx, -_kGestureOffset.dy));
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 500));

    expect(find.byType(CupertinoScrollbar), isNot(paints..rrect()));

    // The scrollbar should not appear even when overscrolled.
    final TestGesture overscrollGesture = await tester.startGesture(tester.getCenter(find.byType(ListView)));
    await overscrollGesture.moveBy(_kGestureOffset);

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 500));
    expect(find.byType(CupertinoScrollbar), isNot(paints..rrect()));
  });
140
}