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

5 6
import 'dart:ui' as ui show window;

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

import 'rendering_tester.dart';

class TestLayout {
  TestLayout() {
14
    // incoming constraints are tight 800x600
15 16
    root = RenderPositionedBox(
      child: RenderConstrainedBox(
17
        additionalConstraints: const BoxConstraints.tightFor(width: 800.0),
18 19
        child: RenderCustomPaint(
          painter: TestCallbackPainter(
20
            onPaint: () { painted = true; },
21
          ),
22
          child: child = RenderConstrainedBox(
23 24
            additionalConstraints: const BoxConstraints.tightFor(height: 10.0, width: 10.0),
          ),
25
        ),
26
      ),
27 28
    );
  }
29 30
  late RenderBox root;
  late RenderBox child;
31 32 33 34
  bool painted = false;
}

void main() {
35 36
  const ViewConfiguration testConfiguration = ViewConfiguration(
    size: Size(800.0, 600.0),
37 38
  );

39
  test('onscreen layout does not affect offscreen', () {
40 41
    final TestLayout onscreen = TestLayout();
    final TestLayout offscreen = TestLayout();
42 43 44 45 46
    expect(onscreen.child.hasSize, isFalse);
    expect(onscreen.painted, isFalse);
    expect(offscreen.child.hasSize, isFalse);
    expect(offscreen.painted, isFalse);
    // Attach the offscreen to a custom render view and owner
47
    final RenderView renderView = RenderView(configuration: testConfiguration, window: ui.window);
48
    final PipelineOwner pipelineOwner = PipelineOwner();
49 50
    renderView.attach(pipelineOwner);
    renderView.child = offscreen.root;
51 52
    renderView.prepareInitialFrame();
    pipelineOwner.requestVisualUpdate();
53
    // Lay out the onscreen in the default binding
54
    layout(onscreen.root, phase: EnginePhase.paint);
55
    expect(onscreen.child.hasSize, isTrue);
56
    expect(onscreen.painted, isTrue);
57 58 59 60 61 62 63 64
    expect(onscreen.child.size, equals(const Size(800.0, 10.0)));
    // Make sure the offscreen didn't get laid out
    expect(offscreen.child.hasSize, isFalse);
    expect(offscreen.painted, isFalse);
    // Now lay out the offscreen
    pipelineOwner.flushLayout();
    expect(offscreen.child.hasSize, isTrue);
    expect(offscreen.painted, isFalse);
65 66 67
    pipelineOwner.flushCompositingBits();
    pipelineOwner.flushPaint();
    expect(offscreen.painted, isTrue);
68 69
  });
  test('offscreen layout does not affect onscreen', () {
70 71
    final TestLayout onscreen = TestLayout();
    final TestLayout offscreen = TestLayout();
72 73 74 75 76
    expect(onscreen.child.hasSize, isFalse);
    expect(onscreen.painted, isFalse);
    expect(offscreen.child.hasSize, isFalse);
    expect(offscreen.painted, isFalse);
    // Attach the offscreen to a custom render view and owner
77
    final RenderView renderView = RenderView(configuration: testConfiguration, window: ui.window);
78
    final PipelineOwner pipelineOwner = PipelineOwner();
79 80
    renderView.attach(pipelineOwner);
    renderView.child = offscreen.root;
81 82
    renderView.prepareInitialFrame();
    pipelineOwner.requestVisualUpdate();
83 84 85 86
    // Lay out the offscreen
    pipelineOwner.flushLayout();
    expect(offscreen.child.hasSize, isTrue);
    expect(offscreen.painted, isFalse);
87 88 89
    pipelineOwner.flushCompositingBits();
    pipelineOwner.flushPaint();
    expect(offscreen.painted, isTrue);
90 91 92 93
    // Make sure the onscreen didn't get laid out
    expect(onscreen.child.hasSize, isFalse);
    expect(onscreen.painted, isFalse);
    // Now lay out the onscreen in the default binding
94
    layout(onscreen.root, phase: EnginePhase.paint);
95
    expect(onscreen.child.hasSize, isTrue);
96
    expect(onscreen.painted, isTrue);
97 98 99
    expect(onscreen.child.size, equals(const Size(800.0, 10.0)));
  });
}