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

7 8 9 10 11 12 13
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

void main() {
  testWidgets('no overlap with floating action button', (WidgetTester tester) async {
    await tester.pumpWidget(
14 15
      const MaterialApp(
        home: Scaffold(
16
          floatingActionButton: FloatingActionButton(
17 18
            onPressed: null,
          ),
19 20 21
          bottomNavigationBar: ShapeListener(
            BottomAppBar(
              child: SizedBox(height: 100.0),
22
            ),
23
          ),
24 25 26 27 28 29
        ),
      ),
    );

    final ShapeListenerState shapeListenerState = tester.state(find.byType(ShapeListener));
    final RenderBox renderBox = tester.renderObject(find.byType(BottomAppBar));
30
    final Path expectedPath = Path()
31 32 33 34 35 36 37 38
      ..addRect(Offset.zero & renderBox.size);

    final Path actualPath = shapeListenerState.cache.value;
    expect(
      actualPath,
      coversSameAreaAs(
        expectedPath,
        areaToCompare: (Offset.zero & renderBox.size).inflate(5.0),
39
      ),
40 41
    );
  });
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  testWidgets('custom shape', (WidgetTester tester) async {
    final Key key = UniqueKey();
    Future<void> pump(FloatingActionButtonLocation location) async {
      await tester.pumpWidget(
        SizedBox(
          width: 200,
          height: 200,
          child: RepaintBoundary(
            key: key,
            child: MaterialApp(
              home: Scaffold(
                floatingActionButton: FloatingActionButton(
                  onPressed: () { },
                ),
                floatingActionButtonLocation: location,
                bottomNavigationBar: BottomAppBar(
                  shape: AutomaticNotchedShape(
                    BeveledRectangleBorder(borderRadius: BorderRadius.circular(50.0)),
61
                    ContinuousRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
62 63 64 65 66 67 68 69 70 71 72 73 74 75
                  ),
                  notchMargin: 10.0,
                  color: Colors.green,
                  child: const SizedBox(height: 100.0),
                ),
              ),
            ),
          ),
        ),
      );
    }
    await pump(FloatingActionButtonLocation.endDocked);
    await expectLater(
      find.byKey(key),
76
      matchesGoldenFile('bottom_app_bar.custom_shape.1.png'),
77 78 79 80 81
    );
    await pump(FloatingActionButtonLocation.centerDocked);
    await tester.pumpAndSettle();
    await expectLater(
      find.byKey(key),
82
      matchesGoldenFile('bottom_app_bar.custom_shape.2.png'),
83
    );
84 85
  }, skip: isBrowser); // https://github.com/flutter/flutter/issues/51675,
  // https://github.com/flutter/flutter/issues/44572
86

87 88
  testWidgets('color defaults to Theme.bottomAppBarColor', (WidgetTester tester) async {
    await tester.pumpWidget(
89 90
      MaterialApp(
        home: Builder(
91
          builder: (BuildContext context) {
92
            return Theme(
93 94
              data: Theme.of(context).copyWith(bottomAppBarColor: const Color(0xffffff00)),
              child: const Scaffold(
95
                floatingActionButton: FloatingActionButton(
96 97
                  onPressed: null,
                ),
98
                bottomNavigationBar: BottomAppBar(),
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
              ),
            );
          }
        ),
      ),
    );

    final PhysicalShape physicalShape =
      tester.widget(find.byType(PhysicalShape).at(0));

    expect(physicalShape.color, const Color(0xffffff00));
  });

  testWidgets('color overrides theme color', (WidgetTester tester) async {
    await tester.pumpWidget(
114 115
      MaterialApp(
        home: Builder(
116
          builder: (BuildContext context) {
117
            return Theme(
118 119
              data: Theme.of(context).copyWith(bottomAppBarColor: const Color(0xffffff00)),
              child: const Scaffold(
120
                floatingActionButton: FloatingActionButton(
121 122
                  onPressed: null,
                ),
123 124
                bottomNavigationBar: BottomAppBar(
                  color: Color(0xff0000ff)
125 126 127 128 129 130 131 132 133 134 135 136 137 138
                ),
              ),
            );
          }
        ),
      ),
    );

    final PhysicalShape physicalShape =
      tester.widget(find.byType(PhysicalShape).at(0));

    expect(physicalShape.color, const Color(0xff0000ff));
  });

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  testWidgets('dark theme applies an elevation overlay color', (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData.from(colorScheme: const ColorScheme.dark()),
        home: Scaffold(
          bottomNavigationBar: BottomAppBar(
            color: const ColorScheme.dark().surface,
          ),
        ),
      )
    );

    final PhysicalShape physicalShape = tester.widget(find.byType(PhysicalShape).at(0));

    // For the default dark theme the overlay color for elevation 8 is 0xFF2D2D2D
    expect(physicalShape.color, const Color(0xFF2D2D2D));
  });

157 158
  // This is a regression test for a bug we had where toggling the notch on/off
  // would crash, as the shouldReclip method of ShapeBorderClipper or
159
  // _BottomAppBarClipper would try an illegal downcast.
160
  testWidgets('toggle shape to null', (WidgetTester tester) async {
161
    await tester.pumpWidget(
162 163
      const MaterialApp(
        home: Scaffold(
164 165
          bottomNavigationBar: BottomAppBar(
            shape: RectangularNotch(),
166 167 168 169 170 171
          ),
        ),
      ),
    );

    await tester.pumpWidget(
172 173
      const MaterialApp(
        home: Scaffold(
174
          bottomNavigationBar: BottomAppBar(
175
            shape: null,
176 177 178 179 180 181
          ),
        ),
      ),
    );

    await tester.pumpWidget(
182 183
      const MaterialApp(
        home: Scaffold(
184 185
          bottomNavigationBar: BottomAppBar(
            shape: RectangularNotch(),
186 187 188 189 190
          ),
        ),
      ),
    );
  });
191 192 193

  testWidgets('no notch when notch param is null', (WidgetTester tester) async {
    await tester.pumpWidget(
194 195
      const MaterialApp(
        home: Scaffold(
196
          bottomNavigationBar: ShapeListener(BottomAppBar(
197 198
            shape: null,
          )),
199
          floatingActionButton: FloatingActionButton(
200
            onPressed: null,
201
            child: Icon(Icons.add),
202 203 204 205 206 207 208 209
          ),
          floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        ),
      ),
    );

    final ShapeListenerState shapeListenerState = tester.state(find.byType(ShapeListener));
    final RenderBox renderBox = tester.renderObject(find.byType(BottomAppBar));
210
    final Path expectedPath = Path()
211 212 213 214 215 216 217 218 219
      ..addRect(Offset.zero & renderBox.size);

    final Path actualPath = shapeListenerState.cache.value;

    expect(
      actualPath,
      coversSameAreaAs(
        expectedPath,
        areaToCompare: (Offset.zero & renderBox.size).inflate(5.0),
220
      ),
221 222 223 224 225
    );
  });

  testWidgets('notch no margin', (WidgetTester tester) async {
    await tester.pumpWidget(
226 227
      const MaterialApp(
        home: Scaffold(
228 229 230 231
          bottomNavigationBar: ShapeListener(
            BottomAppBar(
              child: SizedBox(height: 100.0),
              shape: RectangularNotch(),
232
              notchMargin: 0.0,
233
            ),
234
          ),
235
          floatingActionButton: FloatingActionButton(
236
            onPressed: null,
237
            child: Icon(Icons.add),
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
          ),
          floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        ),
      ),
    );

    final ShapeListenerState shapeListenerState = tester.state(find.byType(ShapeListener));
    final RenderBox babBox = tester.renderObject(find.byType(BottomAppBar));
    final Size babSize = babBox.size;
    final RenderBox fabBox = tester.renderObject(find.byType(FloatingActionButton));
    final Size fabSize = fabBox.size;

    final double fabLeft = (babSize.width / 2.0) - (fabSize.width / 2.0);
    final double fabRight = fabLeft + fabSize.width;
    final double fabBottom = fabSize.height / 2.0;

254
    final Path expectedPath = Path()
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
      ..moveTo(0.0, 0.0)
      ..lineTo(fabLeft, 0.0)
      ..lineTo(fabLeft, fabBottom)
      ..lineTo(fabRight, fabBottom)
      ..lineTo(fabRight, 0.0)
      ..lineTo(babSize.width, 0.0)
      ..lineTo(babSize.width, babSize.height)
      ..lineTo(0.0, babSize.height)
      ..close();

    final Path actualPath = shapeListenerState.cache.value;

    expect(
      actualPath,
      coversSameAreaAs(
        expectedPath,
        areaToCompare: (Offset.zero & babSize).inflate(5.0),
272
      ),
273 274 275 276 277
    );
  });

  testWidgets('notch with margin', (WidgetTester tester) async {
    await tester.pumpWidget(
278 279
      const MaterialApp(
        home: Scaffold(
280 281 282 283
          bottomNavigationBar: ShapeListener(
            BottomAppBar(
              child: SizedBox(height: 100.0),
              shape: RectangularNotch(),
284
              notchMargin: 6.0,
285
            ),
286
          ),
287
          floatingActionButton: FloatingActionButton(
288
            onPressed: null,
289
            child: Icon(Icons.add),
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
          ),
          floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        ),
      ),
    );

    final ShapeListenerState shapeListenerState = tester.state(find.byType(ShapeListener));
    final RenderBox babBox = tester.renderObject(find.byType(BottomAppBar));
    final Size babSize = babBox.size;
    final RenderBox fabBox = tester.renderObject(find.byType(FloatingActionButton));
    final Size fabSize = fabBox.size;

    final double fabLeft = (babSize.width / 2.0) - (fabSize.width / 2.0) - 6.0;
    final double fabRight = fabLeft + fabSize.width + 6.0;
    final double fabBottom = 6.0 + fabSize.height / 2.0;

306
    final Path expectedPath = Path()
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
      ..moveTo(0.0, 0.0)
      ..lineTo(fabLeft, 0.0)
      ..lineTo(fabLeft, fabBottom)
      ..lineTo(fabRight, fabBottom)
      ..lineTo(fabRight, 0.0)
      ..lineTo(babSize.width, 0.0)
      ..lineTo(babSize.width, babSize.height)
      ..lineTo(0.0, babSize.height)
      ..close();

    final Path actualPath = shapeListenerState.cache.value;

    expect(
      actualPath,
      coversSameAreaAs(
        expectedPath,
        areaToCompare: (Offset.zero & babSize).inflate(5.0),
324
      ),
325 326
    );
  });
327 328 329

  testWidgets('observes safe area', (WidgetTester tester) async {
    await tester.pumpWidget(
330 331
      const MaterialApp(
        home: MediaQuery(
332 333
          data: MediaQueryData(
            padding: EdgeInsets.all(50.0),
334
          ),
335 336 337 338
          child: Scaffold(
            bottomNavigationBar: BottomAppBar(
              child: Center(
                child: Text('safe'),
339 340 341 342 343 344 345 346 347 348 349 350
              ),
            ),
          ),
        ),
      ),
    );

    expect(
      tester.getBottomLeft(find.widgetWithText(Center, 'safe')),
      const Offset(50.0, 550.0),
    );
  });
351 352 353

  testWidgets('clipBehavior is propagated', (WidgetTester tester) async {
    await tester.pumpWidget(
354 355
      const MaterialApp(
        home: Scaffold(
356 357 358 359 360 361 362 363 364 365 366 367 368 369
          bottomNavigationBar:
              BottomAppBar(
                child: SizedBox(height: 100.0),
                shape: RectangularNotch(),
                notchMargin: 0.0,
              ),
        ),
      ),
    );

    PhysicalShape physicalShape = tester.widget(find.byType(PhysicalShape));
    expect(physicalShape.clipBehavior, Clip.none);

    await tester.pumpWidget(
370 371
      const MaterialApp(
        home: Scaffold(
372 373 374 375 376 377 378 379 380 381 382 383 384 385
          bottomNavigationBar:
          BottomAppBar(
            child: SizedBox(height: 100.0),
            shape: RectangularNotch(),
            notchMargin: 0.0,
            clipBehavior: Clip.antiAliasWithSaveLayer,
          ),
        ),
      ),
    );

    physicalShape = tester.widget(find.byType(PhysicalShape));
    expect(physicalShape.clipBehavior, Clip.antiAliasWithSaveLayer);
  });
386 387 388 389
}

// The bottom app bar clip path computation is only available at paint time.
// In order to examine the notch path we implement this caching painter which
390
// at paint time looks for a descendant PhysicalShape and caches the
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
// clip path it is using.
class ClipCachePainter extends CustomPainter {
  ClipCachePainter(this.context);

  Path value;
  BuildContext context;

  @override
  void paint(Canvas canvas, Size size) {
    final RenderPhysicalShape physicalShape = findPhysicalShapeChild(context);
    value = physicalShape.clipper.getClip(size);
  }

  RenderPhysicalShape findPhysicalShapeChild(BuildContext context) {
    RenderPhysicalShape result;
    context.visitChildElements((Element e) {
      final RenderObject renderObject = e.findRenderObject();
      if (renderObject.runtimeType == RenderPhysicalShape) {
        assert(result == null);
410
        result = renderObject as RenderPhysicalShape;
411 412 413 414 415 416 417 418 419 420 421 422 423 424
      } else {
        result = findPhysicalShapeChild(e);
      }
    });
    return result;
  }

  @override
  bool shouldRepaint(ClipCachePainter oldDelegate) {
    return true;
  }
}

class ShapeListener extends StatefulWidget {
425
  const ShapeListener(this.child, { Key key }) : super(key: key);
426 427 428 429

  final Widget child;

  @override
430
  State createState() => ShapeListenerState();
431 432 433 434 435 436

}

class ShapeListenerState extends State<ShapeListener> {
  @override
  Widget build(BuildContext context) {
437
    return CustomPaint(
438
      child: widget.child,
439
      painter: cache,
440 441 442 443 444 445 446 447
    );
  }

  ClipCachePainter cache;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
448
    cache = ClipCachePainter(context);
449 450 451
  }

}
452

453
class RectangularNotch extends NotchedShape {
454 455 456 457
  const RectangularNotch();

  @override
  Path getOuterPath(Rect host, Rect guest) {
458 459
    if (guest == null)
      return Path()..addRect(host);
460
    return Path()
461 462 463 464 465 466 467 468 469 470 471
      ..moveTo(host.left, host.top)
      ..lineTo(guest.left, host.top)
      ..lineTo(guest.left, guest.bottom)
      ..lineTo(guest.right, guest.bottom)
      ..lineTo(guest.right, host.top)
      ..lineTo(host.right, host.top)
      ..lineTo(host.right, host.bottom)
      ..lineTo(host.left, host.bottom)
      ..close();
  }
}