view_test.dart 5.2 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10
// 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';
import 'package:flutter_test/flutter_test.dart';

import 'rendering_tester.dart';

void main() {
11 12
  TestRenderingFlutterBinding.ensureInitialized();

13 14 15 16 17 18 19 20 21
  // Create non-const instances, otherwise tests pass even if the
  // operator override is incorrect.
  ViewConfiguration createViewConfiguration({
    Size size = const Size(20, 20),
    double devicePixelRatio = 2.0,
  }) {
    return ViewConfiguration(size: size, devicePixelRatio: devicePixelRatio);
  }

22 23
  group('RenderView', () {
    test('accounts for device pixel ratio in paintBounds', () {
24
      layout(RenderAspectRatio(aspectRatio: 1.0));
25
      pumpFrame();
26 27
      final Size logicalSize = TestRenderingFlutterBinding.instance.renderView.configuration.size;
      final double devicePixelRatio = TestRenderingFlutterBinding.instance.renderView.configuration.devicePixelRatio;
28
      final Size physicalSize = logicalSize * devicePixelRatio;
29
      expect(TestRenderingFlutterBinding.instance.renderView.paintBounds, Offset.zero & physicalSize);
30
    });
31 32 33 34

    test('does not replace the root layer unnecessarily', () {
      final RenderView view = RenderView(
        configuration: createViewConfiguration(),
35
        view: RendererBinding.instance.platformDispatcher.views.single,
36 37 38 39 40 41 42 43 44 45 46
      );
      final PipelineOwner owner = PipelineOwner();
      view.attach(owner);
      view.prepareInitialFrame();
      final ContainerLayer firstLayer = view.debugLayer!;
      view.configuration = createViewConfiguration();
      expect(identical(view.debugLayer, firstLayer), true);

      view.configuration = createViewConfiguration(devicePixelRatio: 5.0);
      expect(identical(view.debugLayer, firstLayer), false);
    });
47

48
    test('does not replace the root layer unnecessarily when view resizes', () {
49 50
      final RenderView view = RenderView(
        configuration: createViewConfiguration(size: const Size(100.0, 100.0)),
51
        view: RendererBinding.instance.platformDispatcher.views.single,
52 53 54 55 56 57 58 59
      );
      final PipelineOwner owner = PipelineOwner();
      view.attach(owner);
      view.prepareInitialFrame();
      final ContainerLayer firstLayer = view.debugLayer!;
      view.configuration = createViewConfiguration(size: const Size(100.0, 1117.0));
      expect(identical(view.debugLayer, firstLayer), true);
    });
60 61 62 63 64 65 66 67 68 69 70
  });

  test('ViewConfiguration == and hashCode', () {
    final ViewConfiguration viewConfigurationA = createViewConfiguration();
    final ViewConfiguration viewConfigurationB = createViewConfiguration();
    final ViewConfiguration viewConfigurationC = createViewConfiguration(devicePixelRatio: 3.0);

    expect(viewConfigurationA == viewConfigurationB, true);
    expect(viewConfigurationA != viewConfigurationC, true);
    expect(viewConfigurationA.hashCode, viewConfigurationB.hashCode);
    expect(viewConfigurationA.hashCode != viewConfigurationC.hashCode, true);
71
  });
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

  test('invokes DebugPaintCallback', () {
    final PaintPattern paintsOrangeRect = paints..rect(
      color: orange,
      rect: orangeRect,
    );
    final PaintPattern paintsGreenRect = paints..rect(
      color: green,
      rect: greenRect,
    );
    final PaintPattern paintOrangeAndGreenRect = paints
      ..rect(
        color: orange,
        rect: orangeRect,
      )
      ..rect(
        color: green,
        rect: greenRect,
      );
    void paintCallback(PaintingContext context, Offset offset, RenderView renderView) {
      context.canvas.drawRect(
        greenRect,
        Paint()..color = green,
      );
    }

    layout(TestRenderObject());
    expect(
      TestRenderingFlutterBinding.instance.renderView,
      paintsOrangeRect,
    );
    expect(
      TestRenderingFlutterBinding.instance.renderView,
      isNot(paintsGreenRect),
    );

    RenderView.debugAddPaintCallback(paintCallback);
    expect(
      TestRenderingFlutterBinding.instance.renderView,
      paintOrangeAndGreenRect,
    );

    RenderView.debugRemovePaintCallback(paintCallback);
    expect(
      TestRenderingFlutterBinding.instance.renderView,
      paintsOrangeRect,
    );
    expect(
      TestRenderingFlutterBinding.instance.renderView,
      isNot(paintsGreenRect),
    );
  });
124 125 126 127 128 129 130 131 132 133

  test('Config can be set and changed after instantiation without calling prepareInitialFrame first', () {
    final RenderView view = RenderView(
      view: RendererBinding.instance.platformDispatcher.views.single,
    );
    view.configuration = const ViewConfiguration(size: Size(100, 200), devicePixelRatio: 3.0);
    view.configuration = const ViewConfiguration(size: Size(200, 300), devicePixelRatio: 2.0);
    PipelineOwner().rootNode = view;
    view.prepareInitialFrame();
  });
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
}

const Color orange = Color(0xFFFF9000);
const Color green = Color(0xFF0FF900);
const Rect orangeRect = Rect.fromLTWH(10, 10, 50, 75);
const Rect greenRect = Rect.fromLTWH(20, 20, 100, 150);

class TestRenderObject extends RenderBox {
  @override
  void performLayout() {
    size = constraints.biggest;
  }

  @override
  void paint(PaintingContext context, Offset offset) {
    context.canvas.drawRect(
      orangeRect,
      Paint()..color = orange,
    );
  }
154
}