safe_area_test.dart 7.27 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1 2 3 4 5 6 7 8 9
// Copyright 2015 The Chromium 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_test/flutter_test.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

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

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

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    testWidgets('SafeArea - nested', (WidgetTester tester) async {
      await tester.pumpWidget(
        const MediaQuery(
          data: const MediaQueryData(padding: const EdgeInsets.all(20.0)),
          child: const SafeArea(
            top: false,
            child: const SafeArea(
              right: false,
              child: const Placeholder(),
            ),
          ),
        ),
      );
      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 {
      const Widget child = const SafeArea(
        bottom: false,
Ian Hickson's avatar
Ian Hickson committed
60 61
        child: const SafeArea(
          left: false,
62
          bottom: false,
Ian Hickson's avatar
Ian Hickson committed
63 64
          child: const Placeholder(),
        ),
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
      );
      await tester.pumpWidget(
        const MediaQuery(
          data: const MediaQueryData(padding: const EdgeInsets.all(20.0)),
          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(
          data: const MediaQueryData(padding: const EdgeInsets.only(
            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));
    });
Ian Hickson's avatar
Ian Hickson committed
88 89
  });

90 91 92 93 94 95 96 97 98 99 100 101 102 103
  group('SliverSafeArea', () {
    Widget buildWidget(EdgeInsets mediaPadding, Widget sliver) {
      return new MediaQuery(
        data: new MediaQueryData(padding: mediaPadding),
        child: new Directionality(
          textDirection: TextDirection.ltr,
          child: new Viewport(
            offset: new ViewportOffset.fixed(0.0),
            axisDirection: AxisDirection.down,
            slivers: <Widget>[
              const SliverToBoxAdapter(child: const SizedBox(width: 800.0, height: 100.0, child: const Text('before'))),
              sliver,
              const SliverToBoxAdapter(child: const SizedBox(width: 800.0, height: 100.0, child: const Text('after'))),
            ],
Ian Hickson's avatar
Ian Hickson committed
104 105
          ),
        ),
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
      );
    }

    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));
          return new Rect.fromPoints(topLeft, bottomRight);
        }
      ).toList();
      expect(testAnswers, equals(expectedRects));
    }

    testWidgets('SliverSafeArea - basic', (WidgetTester tester) async {
      await tester.pumpWidget(
        buildWidget(
          const EdgeInsets.all(20.0),
          const SliverSafeArea(
            left: false,
            sliver: const SliverToBoxAdapter(child: const SizedBox(width: 800.0, height: 100.0, child: const Text('padded'))),
          ),
        ),
      );
      verify(tester, <Rect>[
        new Rect.fromLTWH(0.0, 0.0, 800.0, 100.0),
        new Rect.fromLTWH(0.0, 120.0, 780.0, 100.0),
        new Rect.fromLTWH(0.0, 240.0, 800.0, 100.0),
      ]);
    });

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    testWidgets('SliverSafeArea - basic', (WidgetTester tester) async {
      await tester.pumpWidget(
        buildWidget(
          const EdgeInsets.all(20.0),
          const SliverSafeArea(
            top: false,
            minimum: const EdgeInsets.fromLTRB(0.0, 10.0, 20.0, 30.0),
            sliver: const SliverToBoxAdapter(child: const SizedBox(width: 800.0, height: 100.0, child: const Text('padded'))),
          ),
        ),
      );
      verify(tester, <Rect>[
        new Rect.fromLTWH(0.0, 0.0, 800.0, 100.0),
        new Rect.fromLTWH(20.0, 110.0, 760.0, 100.0),
        new Rect.fromLTWH(0.0, 240.0, 800.0, 100.0),
      ]);
    });

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    testWidgets('SliverSafeArea - nested', (WidgetTester tester) async {
      await tester.pumpWidget(
        buildWidget(
          const EdgeInsets.all(20.0),
          const SliverSafeArea(
            top: false,
            sliver: const SliverSafeArea(
              right: false,
              sliver: const SliverToBoxAdapter(child: const SizedBox(width: 800.0, height: 100.0, child: const Text('padded'))),
            ),
          ),
        ),
      );
      verify(tester, <Rect>[
        new Rect.fromLTWH(0.0, 0.0, 800.0, 100.0),
        new Rect.fromLTWH(20.0, 120.0, 760.0, 100.0),
        new Rect.fromLTWH(0.0, 240.0, 800.0, 100.0),
      ]);
    });
Ian Hickson's avatar
Ian Hickson committed
174

175 176
    testWidgets('SliverSafeArea - changing', (WidgetTester tester) async {
      const Widget sliver = const SliverSafeArea(
Ian Hickson's avatar
Ian Hickson committed
177
        bottom: false,
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        sliver: const SliverSafeArea(
          left: false,
          bottom: false,
          sliver: const SliverToBoxAdapter(child: const SizedBox(width: 800.0, height: 100.0, child: const Text('padded'))),
        ),
      );
      await tester.pumpWidget(
        buildWidget(
          const EdgeInsets.all(20.0),
          sliver,
        ),
      );
      verify(tester, <Rect>[
        new Rect.fromLTWH(0.0, 0.0, 800.0, 100.0),
        new Rect.fromLTWH(20.0, 120.0, 760.0, 100.0),
        new Rect.fromLTWH(0.0, 220.0, 800.0, 100.0),
      ]);

      await tester.pumpWidget(
        buildWidget(
          const EdgeInsets.only(
            left: 100.0,
            top: 30.0,
            right: 0.0,
            bottom: 40.0,
          ),
          sliver,
        ),
      );
      verify(tester, <Rect>[
        new Rect.fromLTWH(0.0, 0.0, 800.0, 100.0),
        new Rect.fromLTWH(100.0, 130.0, 700.0, 100.0),
        new Rect.fromLTWH(0.0, 230.0, 800.0, 100.0),
      ]);
    });
Ian Hickson's avatar
Ian Hickson committed
213 214
  });
}