proxy_sliver_test.dart 6.32 KB
Newer Older
1 2 3 4 5 6
// Copyright 2014 The Flutter Authors. All rights reserved.
// 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/widgets.dart';
7
import 'package:flutter_test/flutter_test.dart';
8 9 10 11

import 'rendering_tester.dart';

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

14 15 16 17 18
  test('RenderSliverOpacity does not composite if it is transparent', () {
    final RenderSliverOpacity renderSliverOpacity = RenderSliverOpacity(
      opacity: 0.0,
      sliver: RenderSliverToBoxAdapter(
        child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
19
      ),
20 21 22 23 24 25 26 27 28 29 30 31 32
    );

    final RenderViewport root = RenderViewport(
      crossAxisDirection: AxisDirection.right,
      offset: ViewportOffset.zero(),
      cacheExtent: 250.0,
      children: <RenderSliver>[renderSliverOpacity],
    );

    layout(root, phase: EnginePhase.composite);
    expect(renderSliverOpacity.needsCompositing, false);
  });

Dan Field's avatar
Dan Field committed
33
  test('RenderSliverOpacity does composite if it is opaque', () {
34 35 36
    final RenderSliverOpacity renderSliverOpacity = RenderSliverOpacity(
      sliver: RenderSliverToBoxAdapter(
        child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
37
      ),
38 39 40 41 42 43 44 45 46 47
    );

    final RenderViewport root = RenderViewport(
      crossAxisDirection: AxisDirection.right,
      offset: ViewportOffset.zero(),
      cacheExtent: 250.0,
      children: <RenderSliver>[renderSliverOpacity],
    );

    layout(root, phase: EnginePhase.composite);
Dan Field's avatar
Dan Field committed
48
    expect(renderSliverOpacity.needsCompositing, true);
49
  });
50

51 52 53 54 55
  test('RenderSliverOpacity reuses its layer', () {
    final RenderSliverOpacity renderSliverOpacity = RenderSliverOpacity(
      opacity: 0.5,
      sliver: RenderSliverToBoxAdapter(
        child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
56
      ),
57 58 59 60 61 62
    );

    final RenderViewport root = RenderViewport(
      crossAxisDirection: AxisDirection.right,
      offset: ViewportOffset.zero(),
      cacheExtent: 250.0,
63
      children: <RenderSliver>[renderSliverOpacity],
64 65 66 67
    );

    expect(renderSliverOpacity.debugLayer, null);
    layout(root, phase: EnginePhase.paint, constraints: BoxConstraints.tight(const Size(10, 10)));
68
    final ContainerLayer layer = renderSliverOpacity.debugLayer!;
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    expect(layer, isNotNull);

    // Mark for repaint otherwise pumpFrame is a noop.
    renderSliverOpacity.markNeedsPaint();
    expect(renderSliverOpacity.debugNeedsPaint, true);
    pumpFrame(phase: EnginePhase.paint);
    expect(renderSliverOpacity.debugNeedsPaint, false);
    expect(renderSliverOpacity.debugLayer, same(layer));
  });

  test('RenderSliverAnimatedOpacity does not composite if it is transparent', () async {
    final Animation<double> opacityAnimation = AnimationController(
      vsync: FakeTickerProvider(),
    )..value = 0.0;

    final RenderSliverAnimatedOpacity renderSliverAnimatedOpacity = RenderSliverAnimatedOpacity(
      opacity: opacityAnimation,
      sliver: RenderSliverToBoxAdapter(
        child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
88
      ),
89 90 91 92 93 94 95 96 97 98 99 100 101
    );

    final RenderViewport root = RenderViewport(
      crossAxisDirection: AxisDirection.right,
      offset: ViewportOffset.zero(),
      cacheExtent: 250.0,
      children: <RenderSliver>[renderSliverAnimatedOpacity],
    );

    layout(root, phase: EnginePhase.composite);
    expect(renderSliverAnimatedOpacity.needsCompositing, false);
  });

102
  test('RenderSliverAnimatedOpacity does composite if it is partially opaque', () {
103 104
    final Animation<double> opacityAnimation = AnimationController(
      vsync: FakeTickerProvider(),
105
    )..value = 0.5;
106 107 108 109 110

    final RenderSliverAnimatedOpacity renderSliverAnimatedOpacity = RenderSliverAnimatedOpacity(
      opacity: opacityAnimation,
      sliver: RenderSliverToBoxAdapter(
        child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
111
      ),
112 113 114 115 116 117 118 119 120 121
    );

    final RenderViewport root = RenderViewport(
      crossAxisDirection: AxisDirection.right,
      offset: ViewportOffset.zero(),
      cacheExtent: 250.0,
      children: <RenderSliver>[renderSliverAnimatedOpacity],
    );

    layout(root, phase: EnginePhase.composite);
Dan Field's avatar
Dan Field committed
122
    expect(renderSliverAnimatedOpacity.needsCompositing, true);
123 124
  });

125
  test('RenderSliverAnimatedOpacity does composite if it is opaque', () {
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    final Animation<double> opacityAnimation = AnimationController(
      vsync: FakeTickerProvider(),
    )..value = 1.0;

    final RenderSliverAnimatedOpacity renderSliverAnimatedOpacity = RenderSliverAnimatedOpacity(
      opacity: opacityAnimation,
      sliver: RenderSliverToBoxAdapter(
        child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
      ),
    );

    final RenderViewport root = RenderViewport(
      crossAxisDirection: AxisDirection.right,
      offset: ViewportOffset.zero(),
      cacheExtent: 250.0,
      children: <RenderSliver>[renderSliverAnimatedOpacity],
    );

    layout(root, phase: EnginePhase.composite);
145
    expect(renderSliverAnimatedOpacity.needsCompositing, true);
146 147
  });

148 149 150 151 152 153 154 155 156
  test('RenderSliverAnimatedOpacity reuses its layer', () {
    final Animation<double> opacityAnimation = AnimationController(
      vsync: FakeTickerProvider(),
    )..value = 0.5;  // must not be 0 or 1.0. Otherwise, it won't create a layer

    final RenderSliverAnimatedOpacity renderSliverAnimatedOpacity = RenderSliverAnimatedOpacity(
      opacity: opacityAnimation,
      sliver: RenderSliverToBoxAdapter(
        child: RenderSizedBox(const Size(1.0, 1.0)), // size doesn't matter
157
      ),
158 159 160 161 162 163
    );

    final RenderViewport root = RenderViewport(
      crossAxisDirection: AxisDirection.right,
      offset: ViewportOffset.zero(),
      cacheExtent: 250.0,
164
      children: <RenderSliver>[renderSliverAnimatedOpacity],
165 166 167 168
    );

    expect(renderSliverAnimatedOpacity.debugLayer, null);
    layout(root, phase: EnginePhase.paint, constraints: BoxConstraints.tight(const Size(10, 10)));
169
    final ContainerLayer layer = renderSliverAnimatedOpacity.debugLayer!;
170 171 172 173 174 175 176 177 178 179
    expect(layer, isNotNull);

    // Mark for repaint otherwise pumpFrame is a noop.
    renderSliverAnimatedOpacity.markNeedsPaint();
    expect(renderSliverAnimatedOpacity.debugNeedsPaint, true);
    pumpFrame(phase: EnginePhase.paint);
    expect(renderSliverAnimatedOpacity.debugNeedsPaint, false);
    expect(renderSliverAnimatedOpacity.debugLayer, same(layer));
  });
}