safe_area_test.dart 11.8 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Ian Hickson's avatar
Ian Hickson committed
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
// @dart = 2.8

Ian Hickson's avatar
Ian Hickson committed
7 8 9
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
10
import 'package:flutter/material.dart';
Ian Hickson's avatar
Ian Hickson committed
11 12

void main() {
13 14 15 16
  group('SafeArea', () {
    testWidgets('SafeArea - basic', (WidgetTester tester) async {
      await tester.pumpWidget(
        const MediaQuery(
17 18
          data: MediaQueryData(padding: EdgeInsets.all(20.0)),
          child: SafeArea(
19
            left: false,
20
            child: Placeholder(),
21 22 23 24 25 26 27
          ),
        ),
      );
      expect(tester.getTopLeft(find.byType(Placeholder)), const Offset(0.0, 20.0));
      expect(tester.getBottomRight(find.byType(Placeholder)), const Offset(780.0, 580.0));
    });

28 29 30
    testWidgets('SafeArea - with minimums', (WidgetTester tester) async {
      await tester.pumpWidget(
        const MediaQuery(
31 32
          data: MediaQueryData(padding: EdgeInsets.all(20.0)),
          child: SafeArea(
33
            top: false,
34 35
            minimum: EdgeInsets.fromLTRB(0.0, 10.0, 20.0, 30.0),
            child: Placeholder(),
36 37 38 39 40 41 42
          ),
        ),
      );
      expect(tester.getTopLeft(find.byType(Placeholder)), const Offset(20.0, 10.0));
      expect(tester.getBottomRight(find.byType(Placeholder)), const Offset(780.0, 570.0));
    });

43 44 45
    testWidgets('SafeArea - nested', (WidgetTester tester) async {
      await tester.pumpWidget(
        const MediaQuery(
46 47
          data: MediaQueryData(padding: EdgeInsets.all(20.0)),
          child: SafeArea(
48
            top: false,
49
            child: SafeArea(
50
              right: false,
51
              child: Placeholder(),
52 53 54 55 56 57 58 59 60
            ),
          ),
        ),
      );
      expect(tester.getTopLeft(find.byType(Placeholder)), const Offset(20.0, 20.0));
      expect(tester.getBottomRight(find.byType(Placeholder)), const Offset(780.0, 580.0));
    });

    testWidgets('SafeArea - changing', (WidgetTester tester) async {
61
      const Widget child = SafeArea(
62
        bottom: false,
63
        child: SafeArea(
Ian Hickson's avatar
Ian Hickson committed
64
          left: false,
65
          bottom: false,
66
          child: Placeholder(),
Ian Hickson's avatar
Ian Hickson committed
67
        ),
68 69 70
      );
      await tester.pumpWidget(
        const MediaQuery(
71
          data: MediaQueryData(padding: EdgeInsets.all(20.0)),
72 73 74 75 76 77 78
          child: child,
        ),
      );
      expect(tester.getTopLeft(find.byType(Placeholder)), const Offset(20.0, 20.0));
      expect(tester.getBottomRight(find.byType(Placeholder)), const Offset(780.0, 600.0));
      await tester.pumpWidget(
        const MediaQuery(
79
          data: MediaQueryData(padding: EdgeInsets.only(
80 81 82 83 84 85 86 87 88 89 90
            left: 100.0,
            top: 30.0,
            right: 0.0,
            bottom: 40.0,
          )),
          child: child,
        ),
      );
      expect(tester.getTopLeft(find.byType(Placeholder)), const Offset(100.0, 30.0));
      expect(tester.getBottomRight(find.byType(Placeholder)), const Offset(800.0, 600.0));
    });
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    testWidgets('SafeArea - properties', (WidgetTester tester) async {
      final SafeArea child = SafeArea(
        left: true,
        right: false,
        bottom: false,
        child: Container()
      );
      final DiagnosticPropertiesBuilder properties = DiagnosticPropertiesBuilder();
      child.debugFillProperties(properties);

      expect(properties.properties.any((DiagnosticsNode n) => n is FlagProperty && n.toString() == 'avoid left padding'), true);
      expect(properties.properties.any((DiagnosticsNode n) => n is FlagProperty && n.toString() == 'avoid right padding'), false);
      expect(properties.properties.any((DiagnosticsNode n) => n is FlagProperty && n.toString() == 'avoid top padding'), true);
      expect(properties.properties.any((DiagnosticsNode n) => n is FlagProperty && n.toString() == 'avoid bottom padding'), false);
    });

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    group('SafeArea maintains bottom viewPadding when specified for consumed bottom padding', () {
      Widget boilerplate(Widget child) {
        return Localizations(
          locale: const Locale('en', 'us'),
          delegates: const <LocalizationsDelegate<dynamic>>[
            DefaultWidgetsLocalizations.delegate,
            DefaultMaterialLocalizations.delegate,
          ],
          child: Directionality(textDirection: TextDirection.ltr, child: child),
        );
      }

      testWidgets('SafeArea alone.', (WidgetTester tester) async {
        final Widget child = boilerplate(SafeArea(
          maintainBottomViewPadding: true,
          child: Column(
            children: const <Widget>[
125
              Expanded(child: Placeholder()),
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
            ],
          ),
        ));

        await tester.pumpWidget(
          MediaQuery(
            data: const MediaQueryData(
              padding: EdgeInsets.symmetric(vertical: 20.0),
              viewPadding: EdgeInsets.only(bottom: 20.0),
            ),
            child: child,
          ),
        );
        final Offset initialPoint = tester.getCenter(find.byType(Placeholder));
        // Consume bottom padding - as if by the keyboard opening
        await tester.pumpWidget(
          MediaQuery(
            data: const MediaQueryData(
              padding: EdgeInsets.only(top: 20.0),
              viewPadding: EdgeInsets.only(bottom: 20.0),
              viewInsets: EdgeInsets.only(bottom: 300.0),
            ),
            child: child,
          ),
        );
        final Offset finalPoint = tester.getCenter(find.byType(Placeholder));
        expect(initialPoint, finalPoint);
      });

      testWidgets('SafeArea with nested Scaffold', (WidgetTester tester) async {
        final Widget child = boilerplate(SafeArea(
          maintainBottomViewPadding: true,
          child: Scaffold(
            resizeToAvoidBottomInset: false,
            body: Column(
              children: const <Widget>[
162
                Expanded(child: Placeholder()),
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
              ],
            ),
          ),
        ));

        await tester.pumpWidget(
          MediaQuery(
            data: const MediaQueryData(
              padding: EdgeInsets.symmetric(vertical: 20.0),
              viewPadding: EdgeInsets.only(bottom: 20.0),
            ),
            child: child,
          ),
        );
        final Offset initialPoint = tester.getCenter(find.byType(Placeholder));
        // Consume bottom padding - as if by the keyboard opening
        await tester.pumpWidget(
          MediaQuery(
            data: const MediaQueryData(
              padding: EdgeInsets.only(top: 20.0),
              viewPadding: EdgeInsets.only(bottom: 20.0),
              viewInsets: EdgeInsets.only(bottom: 300.0),
            ),
            child: child,
          ),
        );
        final Offset finalPoint = tester.getCenter(find.byType(Placeholder));
        expect(initialPoint, finalPoint);
      });
    });
Ian Hickson's avatar
Ian Hickson committed
193 194
  });

195 196
  group('SliverSafeArea', () {
    Widget buildWidget(EdgeInsets mediaPadding, Widget sliver) {
197 198 199
      return MediaQuery(
        data: MediaQueryData(padding: mediaPadding),
        child: Directionality(
200
          textDirection: TextDirection.ltr,
201 202
          child: Viewport(
            offset: ViewportOffset.fixed(0.0),
203 204
            axisDirection: AxisDirection.down,
            slivers: <Widget>[
205
              const SliverToBoxAdapter(child: SizedBox(width: 800.0, height: 100.0, child: Text('before'))),
206
              sliver,
207
              const SliverToBoxAdapter(child: SizedBox(width: 800.0, height: 100.0, child: Text('after'))),
208
            ],
Ian Hickson's avatar
Ian Hickson committed
209 210
          ),
        ),
211 212 213 214 215 216 217 218
      );
    }

    void verify(WidgetTester tester, List<Rect> expectedRects) {
      final List<Rect> testAnswers = tester.renderObjectList<RenderBox>(find.byType(SizedBox)).map<Rect>(
        (RenderBox target) {
          final Offset topLeft = target.localToGlobal(Offset.zero);
          final Offset bottomRight = target.localToGlobal(target.size.bottomRight(Offset.zero));
219
          return Rect.fromPoints(topLeft, bottomRight);
220 221 222 223 224 225 226 227 228 229 230
        }
      ).toList();
      expect(testAnswers, equals(expectedRects));
    }

    testWidgets('SliverSafeArea - basic', (WidgetTester tester) async {
      await tester.pumpWidget(
        buildWidget(
          const EdgeInsets.all(20.0),
          const SliverSafeArea(
            left: false,
231
            sliver: SliverToBoxAdapter(child: SizedBox(width: 800.0, height: 100.0, child: Text('padded'))),
232 233 234 235
          ),
        ),
      );
      verify(tester, <Rect>[
Dan Field's avatar
Dan Field committed
236 237 238
        const Rect.fromLTWH(0.0, 0.0, 800.0, 100.0),
        const Rect.fromLTWH(0.0, 120.0, 780.0, 100.0),
        const Rect.fromLTWH(0.0, 240.0, 800.0, 100.0),
239 240 241
      ]);
    });

242 243 244 245 246 247
    testWidgets('SliverSafeArea - basic', (WidgetTester tester) async {
      await tester.pumpWidget(
        buildWidget(
          const EdgeInsets.all(20.0),
          const SliverSafeArea(
            top: false,
248 249
            minimum: EdgeInsets.fromLTRB(0.0, 10.0, 20.0, 30.0),
            sliver: SliverToBoxAdapter(child: SizedBox(width: 800.0, height: 100.0, child: Text('padded'))),
250 251 252 253
          ),
        ),
      );
      verify(tester, <Rect>[
Dan Field's avatar
Dan Field committed
254 255 256
        const Rect.fromLTWH(0.0, 0.0, 800.0, 100.0),
        const Rect.fromLTWH(20.0, 110.0, 760.0, 100.0),
        const Rect.fromLTWH(0.0, 240.0, 800.0, 100.0),
257 258 259
      ]);
    });

260 261 262 263 264 265
    testWidgets('SliverSafeArea - nested', (WidgetTester tester) async {
      await tester.pumpWidget(
        buildWidget(
          const EdgeInsets.all(20.0),
          const SliverSafeArea(
            top: false,
266
            sliver: SliverSafeArea(
267
              right: false,
268
              sliver: SliverToBoxAdapter(child: SizedBox(width: 800.0, height: 100.0, child: Text('padded'))),
269 270 271 272 273
            ),
          ),
        ),
      );
      verify(tester, <Rect>[
Dan Field's avatar
Dan Field committed
274 275 276
        const Rect.fromLTWH(0.0, 0.0, 800.0, 100.0),
        const Rect.fromLTWH(20.0, 120.0, 760.0, 100.0),
        const Rect.fromLTWH(0.0, 240.0, 800.0, 100.0),
277 278
      ]);
    });
Ian Hickson's avatar
Ian Hickson committed
279

280
    testWidgets('SliverSafeArea - changing', (WidgetTester tester) async {
281
      const Widget sliver = SliverSafeArea(
Ian Hickson's avatar
Ian Hickson committed
282
        bottom: false,
283
        sliver: SliverSafeArea(
284 285
          left: false,
          bottom: false,
286
          sliver: SliverToBoxAdapter(child: SizedBox(width: 800.0, height: 100.0, child: Text('padded'))),
287 288 289 290 291 292 293 294 295
        ),
      );
      await tester.pumpWidget(
        buildWidget(
          const EdgeInsets.all(20.0),
          sliver,
        ),
      );
      verify(tester, <Rect>[
Dan Field's avatar
Dan Field committed
296 297 298
        const Rect.fromLTWH(0.0, 0.0, 800.0, 100.0),
        const Rect.fromLTWH(20.0, 120.0, 760.0, 100.0),
        const Rect.fromLTWH(0.0, 220.0, 800.0, 100.0),
299 300 301 302 303 304 305 306 307 308 309 310 311 312
      ]);

      await tester.pumpWidget(
        buildWidget(
          const EdgeInsets.only(
            left: 100.0,
            top: 30.0,
            right: 0.0,
            bottom: 40.0,
          ),
          sliver,
        ),
      );
      verify(tester, <Rect>[
Dan Field's avatar
Dan Field committed
313 314 315
        const Rect.fromLTWH(0.0, 0.0, 800.0, 100.0),
        const Rect.fromLTWH(100.0, 130.0, 700.0, 100.0),
        const Rect.fromLTWH(0.0, 230.0, 800.0, 100.0),
316 317
      ]);
    });
Ian Hickson's avatar
Ian Hickson committed
318
  });
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

  testWidgets('SliverSafeArea - properties', (WidgetTester tester) async {
    const SliverSafeArea child = SliverSafeArea(
      left: true,
      right: false,
      bottom: false,
      sliver: SliverToBoxAdapter(child: SizedBox(width: 800.0, height: 100.0, child: Text('padded'))),
    );
    final DiagnosticPropertiesBuilder properties = DiagnosticPropertiesBuilder();
    child.debugFillProperties(properties);

    expect(properties.properties.any((DiagnosticsNode n) => n is FlagProperty && n.toString() == 'avoid left padding'), true);
    expect(properties.properties.any((DiagnosticsNode n) => n is FlagProperty && n.toString() == 'avoid right padding'), false);
    expect(properties.properties.any((DiagnosticsNode n) => n is FlagProperty && n.toString() == 'avoid top padding'), true);
    expect(properties.properties.any((DiagnosticsNode n) => n is FlagProperty && n.toString() == 'avoid bottom padding'), false);
  });
Ian Hickson's avatar
Ian Hickson committed
335
}