opacity_test.dart 5.66 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_test/flutter_test.dart';
6
import 'package:flutter/material.dart';
7
import 'package:flutter/rendering.dart';
8 9 10 11 12 13

import '../rendering/mock_canvas.dart';
import 'semantics_tester.dart';

void main() {
  testWidgets('Opacity', (WidgetTester tester) async {
14
    final SemanticsTester semantics = SemanticsTester(tester);
15 16 17 18 19 20 21 22 23

    // Opacity 1.0: Semantics and painting
    await tester.pumpWidget(
      const Opacity(
        child: Text('a', textDirection: TextDirection.rtl),
        opacity: 1.0,
      ),
    );
    expect(semantics, hasSemantics(
24
      TestSemantics.root(
25
        children: <TestSemantics>[
26
          TestSemantics.rootChild(
27
            id: 1,
Dan Field's avatar
Dan Field committed
28
            rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0),
29 30
            label: 'a',
            textDirection: TextDirection.rtl,
31
          ),
32 33 34 35 36 37 38 39 40 41 42 43 44
        ],
      ),
    ));
    expect(find.byType(Opacity), paints..paragraph());

    // Opacity 0.0: Nothing
    await tester.pumpWidget(
      const Opacity(
        child: Text('a', textDirection: TextDirection.rtl),
        opacity: 0.0,
      ),
    );
    expect(semantics, hasSemantics(
45
      TestSemantics.root(),
46 47 48 49 50 51 52 53 54 55 56 57
    ));
    expect(find.byType(Opacity), paintsNothing);

    // Opacity 0.0 with semantics: Just semantics
    await tester.pumpWidget(
      const Opacity(
        child: Text('a', textDirection: TextDirection.rtl),
        opacity: 0.0,
        alwaysIncludeSemantics: true,
      ),
    );
    expect(semantics, hasSemantics(
58
      TestSemantics.root(
59
        children: <TestSemantics>[
60
          TestSemantics.rootChild(
61
            id: 1,
Dan Field's avatar
Dan Field committed
62
            rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0),
63 64
            label: 'a',
            textDirection: TextDirection.rtl,
65
          ),
66 67 68 69 70 71 72 73 74 75 76 77 78 79
        ],
      ),
    ));
    expect(find.byType(Opacity), paintsNothing);

    // Opacity 0.0 without semantics: Nothing
    await tester.pumpWidget(
      const Opacity(
        child: Text('a', textDirection: TextDirection.rtl),
        opacity: 0.0,
        alwaysIncludeSemantics: false,
      ),
    );
    expect(semantics, hasSemantics(
80
      TestSemantics.root(),
81 82 83 84 85 86 87 88 89 90 91
    ));
    expect(find.byType(Opacity), paintsNothing);

    // Opacity 0.1: Semantics and painting
    await tester.pumpWidget(
      const Opacity(
        child: Text('a', textDirection: TextDirection.rtl),
        opacity: 0.1,
      ),
    );
    expect(semantics, hasSemantics(
92
      TestSemantics.root(
93
        children: <TestSemantics>[
94
          TestSemantics.rootChild(
95
            id: 1,
Dan Field's avatar
Dan Field committed
96
            rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0),
97 98
            label: 'a',
            textDirection: TextDirection.rtl,
99
          ),
100 101 102 103 104 105 106 107 108 109 110 111 112 113
        ],
      ),
    ));
    expect(find.byType(Opacity), paints..paragraph());

    // Opacity 0.1 without semantics: Still has semantics and painting
    await tester.pumpWidget(
      const Opacity(
        child: Text('a', textDirection: TextDirection.rtl),
        opacity: 0.1,
        alwaysIncludeSemantics: false,
      ),
    );
    expect(semantics, hasSemantics(
114
      TestSemantics.root(
115
        children: <TestSemantics>[
116
          TestSemantics.rootChild(
117
            id: 1,
Dan Field's avatar
Dan Field committed
118
            rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0),
119 120
            label: 'a',
            textDirection: TextDirection.rtl,
121
          ),
122 123 124 125 126 127 128 129 130 131 132 133 134 135
        ],
      ),
    ));
    expect(find.byType(Opacity), paints..paragraph());

    // Opacity 0.1 with semantics: Semantics and painting
    await tester.pumpWidget(
      const Opacity(
        child: Text('a', textDirection: TextDirection.rtl),
        opacity: 0.1,
        alwaysIncludeSemantics: true,
      ),
    );
    expect(semantics, hasSemantics(
136
      TestSemantics.root(
137
        children: <TestSemantics>[
138
          TestSemantics.rootChild(
139
            id: 1,
Dan Field's avatar
Dan Field committed
140
            rect: const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0),
141 142
            label: 'a',
            textDirection: TextDirection.rtl,
143
          ),
144 145 146 147 148 149 150
        ],
      ),
    ));
    expect(find.byType(Opacity), paints..paragraph());

    semantics.dispose();
  });
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

  testWidgets('offset is correctly handled in Opacity', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: SingleChildScrollView(
            child: RepaintBoundary(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: List<Widget>.generate(10, (int index) {
                  return Opacity(
                    opacity: 0.5,
                    child: Padding(
                      padding: const EdgeInsets.all(5.0),
                      child: Container(
                          color: Colors.blue,
167
                          height: 50,
168
                      ),
169
                    ),
170 171 172
                  );
                }),
              ),
173 174 175
            ),
          ),
        ),
176
      ),
177 178 179
    );
    await expectLater(
      find.byType(RepaintBoundary).first,
180
      matchesGoldenFile('opacity_test.offset.png'),
181
    );
182
  });
183 184 185 186 187 188 189 190

  testWidgets('empty opacity does not crash', (WidgetTester tester) async {
    await tester.pumpWidget(
      RepaintBoundary(child: Opacity(opacity: 0.5, child: Container())),
    );
    final Element element = find.byType(RepaintBoundary).first.evaluate().single;
    // The following line will send the layer to engine and cause crash if an
    // empty opacity layer is sent.
191
    final OffsetLayer offsetLayer = element.renderObject!.debugLayer! as OffsetLayer;
192
    await offsetLayer.toImage(const Rect.fromLTRB(0.0, 0.0, 1.0, 1.0));
193
  }, skip: isBrowser); // https://github.com/flutter/flutter/issues/42767
194
}