independent_layout_test.dart 3.7 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
    devicePixelRatio: 1.0,
38 39
  );

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