slivers_test.dart 4.11 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2016 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/widgets.dart';
import 'package:flutter/rendering.dart';

Future<Null> test(WidgetTester tester, double offset, { double anchor: 0.0 }) {
10 11 12 13 14 15
  return tester.pumpWidget(
    new Directionality(
      textDirection: TextDirection.ltr,
      child: new Viewport(
        anchor: anchor / 600.0,
        offset: new ViewportOffset.fixed(offset),
16
        slivers: const <Widget>[
17 18 19 20 21 22 23 24 25
          const SliverToBoxAdapter(child: const SizedBox(height: 400.0)),
          const SliverToBoxAdapter(child: const SizedBox(height: 400.0)),
          const SliverToBoxAdapter(child: const SizedBox(height: 400.0)),
          const SliverToBoxAdapter(child: const SizedBox(height: 400.0)),
          const SliverToBoxAdapter(child: const SizedBox(height: 400.0)),
        ],
      ),
    ),
  );
26 27
}

28 29 30
void verify(WidgetTester tester, List<Offset> idealPositions, List<bool> idealVisibles) {
  final List<Offset> actualPositions = tester.renderObjectList<RenderBox>(find.byType(SizedBox)).map<Offset>(
    (RenderBox target) => target.localToGlobal(const Offset(0.0, 0.0))
31
  ).toList();
32
  final List<bool> actualVisibles = tester.renderObjectList<RenderSliverToBoxAdapter>(find.byType(SliverToBoxAdapter)).map<bool>(
33 34 35 36 37 38 39
    (RenderSliverToBoxAdapter target) => target.geometry.visible
  ).toList();
  expect(actualPositions, equals(idealPositions));
  expect(actualVisibles, equals(idealVisibles));
}

void main() {
Adam Barth's avatar
Adam Barth committed
40
  testWidgets('Viewport basic test', (WidgetTester tester) async {
41
    await test(tester, 0.0);
Adam Barth's avatar
Adam Barth committed
42
    expect(tester.renderObject<RenderBox>(find.byType(Viewport)).size, equals(const Size(800.0, 600.0)));
43 44 45 46 47 48
    verify(tester, <Offset>[
      const Offset(0.0, 0.0),
      const Offset(0.0, 400.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
49 50 51
    ], <bool>[true, true, false, false, false]);

    await test(tester, 200.0);
52 53 54 55 56 57
    verify(tester, <Offset>[
      const Offset(0.0, -200.0),
      const Offset(0.0, 200.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
58 59 60
    ], <bool>[true, true, false, false, false]);

    await test(tester, 600.0);
61 62 63 64 65 66
    verify(tester, <Offset>[
      const Offset(0.0, -600.0),
      const Offset(0.0, -200.0),
      const Offset(0.0, 200.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
67 68 69
    ], <bool>[false, true, true, false, false]);

    await test(tester, 900.0);
70 71 72 73 74 75
    verify(tester, <Offset>[
      const Offset(0.0, -900.0),
      const Offset(0.0, -500.0),
      const Offset(0.0, -100.0),
      const Offset(0.0, 300.0),
      const Offset(0.0, 600.0),
76 77 78
    ], <bool>[false, false, true, true, false]);
  });

Adam Barth's avatar
Adam Barth committed
79
  testWidgets('Viewport anchor test', (WidgetTester tester) async {
80
    await test(tester, 0.0, anchor: 100.0);
Adam Barth's avatar
Adam Barth committed
81
    expect(tester.renderObject<RenderBox>(find.byType(Viewport)).size, equals(const Size(800.0, 600.0)));
82 83 84 85 86 87
    verify(tester, <Offset>[
      const Offset(0.0, 100.0),
      const Offset(0.0, 500.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
88 89 90
    ], <bool>[true, true, false, false, false]);

    await test(tester, 200.0, anchor: 100.0);
91 92 93 94 95 96
    verify(tester, <Offset>[
      const Offset(0.0, -100.0),
      const Offset(0.0, 300.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
97 98 99
    ], <bool>[true, true, false, false, false]);

    await test(tester, 600.0, anchor: 100.0);
100 101 102 103 104 105
    verify(tester, <Offset>[
      const Offset(0.0, -500.0),
      const Offset(0.0, -100.0),
      const Offset(0.0, 300.0),
      const Offset(0.0, 600.0),
      const Offset(0.0, 600.0),
106 107 108
    ], <bool>[false, true, true, false, false]);

    await test(tester, 900.0, anchor: 100.0);
109 110 111 112 113 114
    verify(tester, <Offset>[
      const Offset(0.0, -800.0),
      const Offset(0.0, -400.0),
      const Offset(0.0, 0.0),
      const Offset(0.0, 400.0),
      const Offset(0.0, 600.0),
115 116 117
    ], <bool>[false, false, true, true, false]);
  });
}