slivers_layout_test.dart 2.14 KB
Newer Older
1 2 3 4 5
// 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/rendering.dart';
6
import '../flutter_test_alternative.dart';
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

import 'rendering_tester.dart';

int layouts = 0;

class RenderLayoutWatcher extends RenderProxyBox {
  RenderLayoutWatcher(RenderBox child) : super(child);
  @override
  void performLayout() {
    layouts += 1;
    super.performLayout();
  }
}

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

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

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

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

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

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

}