independent_layout_test.dart 3.73 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 8
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
9
import '../flutter_test_alternative.dart';
10 11 12 13 14

import 'rendering_tester.dart';

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

void main() {
36 37
  const ViewConfiguration testConfiguration = ViewConfiguration(
    size: Size(800.0, 600.0),
38
    devicePixelRatio: 1.0,
39 40
  );

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