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

import 'package:flutter/rendering.dart';
6
import 'package:flutter_test/flutter_test.dart';
7 8 9 10 11 12

import 'rendering_tester.dart';

int layouts = 0;

class RenderLayoutWatcher extends RenderProxyBox {
13
  RenderLayoutWatcher(RenderBox super.child);
14 15 16 17 18 19 20 21
  @override
  void performLayout() {
    layouts += 1;
    super.performLayout();
  }
}

void main() {
22 23
  TestRenderingFlutterBinding.ensureInitialized();

Adam Barth's avatar
Adam Barth committed
24
  test('RenderViewport basic test - impact of layout', () {
25
    RenderSliverToBoxAdapter sliver;
Adam Barth's avatar
Adam Barth committed
26
    RenderViewport viewport;
27
    RenderBox box;
28
    final RenderLayoutWatcher root = RenderLayoutWatcher(
29
      viewport = RenderViewport(
30
        crossAxisDirection: AxisDirection.right,
31
        offset: ViewportOffset.zero(),
32
        children: <RenderSliver>[
33
          sliver = RenderSliverToBoxAdapter(child: box = RenderSizedBox(const Size(100.0, 400.0))),
34 35 36 37 38 39
        ],
      ),
    );
    expect(layouts, 0);
    layout(root);
    expect(layouts, 1);
40
    expect(box.localToGlobal(box.size.center(Offset.zero)), const Offset(400.0, 200.0));
41

42
    sliver.child = box = RenderSizedBox(const Size(100.0, 300.0));
43 44 45
    expect(layouts, 1);
    pumpFrame();
    expect(layouts, 1);
46
    expect(box.localToGlobal(box.size.center(Offset.zero)), const Offset(400.0, 150.0));
47

48
    viewport.offset = ViewportOffset.fixed(20.0);
49 50 51
    expect(layouts, 1);
    pumpFrame();
    expect(layouts, 1);
52
    expect(box.localToGlobal(box.size.center(Offset.zero)), const Offset(400.0, 130.0));
53

54
    viewport.offset = ViewportOffset.fixed(-20.0);
55 56 57
    expect(layouts, 1);
    pumpFrame();
    expect(layouts, 1);
58
    expect(box.localToGlobal(box.size.center(Offset.zero)), const Offset(400.0, 170.0));
59 60 61 62 63

    viewport.anchor = 20.0 / 600.0;
    expect(layouts, 1);
    pumpFrame();
    expect(layouts, 1);
64
    expect(box.localToGlobal(box.size.center(Offset.zero)), const Offset(400.0, 190.0));
65 66 67 68 69

    viewport.axisDirection = AxisDirection.up;
    expect(layouts, 1);
    pumpFrame();
    expect(layouts, 1);
70
    expect(box.localToGlobal(box.size.center(Offset.zero)), const Offset(400.0, 600.0 - 190.0));
71 72 73
  });

}