independent_layout_test.dart 3.78 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

import 'rendering_tester.dart';

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

void main() {
33 34
  TestRenderingFlutterBinding.ensureInitialized();

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, view: RendererBinding.instance.platformDispatcher.views.single);
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

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, view: RendererBinding.instance.platformDispatcher.views.single);
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)));
  });
}