bottom_app_bar_test.dart 13.5 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11
// 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/material.dart';
import 'package:flutter/rendering.dart';

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

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

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

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  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)),
59
                    ContinuousRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
60 61 62 63 64 65 66 67 68 69 70 71 72 73
                  ),
                  notchMargin: 10.0,
                  color: Colors.green,
                  child: const SizedBox(height: 100.0),
                ),
              ),
            ),
          ),
        ),
      );
    }
    await pump(FloatingActionButtonLocation.endDocked);
    await expectLater(
      find.byKey(key),
74
      matchesGoldenFile('bottom_app_bar.custom_shape.1.png'),
75 76 77 78 79
    );
    await pump(FloatingActionButtonLocation.centerDocked);
    await tester.pumpAndSettle();
    await expectLater(
      find.byKey(key),
80
      matchesGoldenFile('bottom_app_bar.custom_shape.2.png'),
81
    );
82
  }, skip: isBrowser);
83

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

    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(
111 112
      MaterialApp(
        home: Builder(
113
          builder: (BuildContext context) {
114
            return Theme(
115 116
              data: Theme.of(context).copyWith(bottomAppBarColor: const Color(0xffffff00)),
              child: const Scaffold(
117
                floatingActionButton: FloatingActionButton(
118 119
                  onPressed: null,
                ),
120 121
                bottomNavigationBar: BottomAppBar(
                  color: Color(0xff0000ff)
122 123 124 125 126 127 128 129 130 131 132 133 134 135
                ),
              ),
            );
          }
        ),
      ),
    );

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

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

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  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));
  });

154 155
  // 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
156
  // _BottomAppBarClipper would try an illegal downcast.
157
  testWidgets('toggle shape to null', (WidgetTester tester) async {
158
    await tester.pumpWidget(
159 160
      const MaterialApp(
        home: Scaffold(
161 162
          bottomNavigationBar: BottomAppBar(
            shape: RectangularNotch(),
163 164 165 166 167 168
          ),
        ),
      ),
    );

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

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

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

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

    final Path actualPath = shapeListenerState.cache.value;

    expect(
      actualPath,
      coversSameAreaAs(
        expectedPath,
        areaToCompare: (Offset.zero & renderBox.size).inflate(5.0),
217
      ),
218 219 220 221 222
    );
  });

  testWidgets('notch no margin', (WidgetTester tester) async {
    await tester.pumpWidget(
223 224
      const MaterialApp(
        home: Scaffold(
225 226 227 228
          bottomNavigationBar: ShapeListener(
            BottomAppBar(
              child: SizedBox(height: 100.0),
              shape: RectangularNotch(),
229
              notchMargin: 0.0,
230
            ),
231
          ),
232
          floatingActionButton: FloatingActionButton(
233
            onPressed: null,
234
            child: Icon(Icons.add),
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
          ),
          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;

251
    final Path expectedPath = Path()
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
      ..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),
269
      ),
270 271 272 273 274
    );
  });

  testWidgets('notch with margin', (WidgetTester tester) async {
    await tester.pumpWidget(
275 276
      const MaterialApp(
        home: Scaffold(
277 278 279 280
          bottomNavigationBar: ShapeListener(
            BottomAppBar(
              child: SizedBox(height: 100.0),
              shape: RectangularNotch(),
281
              notchMargin: 6.0,
282
            ),
283
          ),
284
          floatingActionButton: FloatingActionButton(
285
            onPressed: null,
286
            child: Icon(Icons.add),
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
          ),
          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;

303
    final Path expectedPath = Path()
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
      ..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),
321
      ),
322 323
    );
  });
324 325 326

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

    expect(
      tester.getBottomLeft(find.widgetWithText(Center, 'safe')),
      const Offset(50.0, 550.0),
    );
  });
348 349 350

  testWidgets('clipBehavior is propagated', (WidgetTester tester) async {
    await tester.pumpWidget(
351 352
      const MaterialApp(
        home: Scaffold(
353 354 355 356 357 358 359 360 361 362 363 364 365 366
          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(
367 368
      const MaterialApp(
        home: Scaffold(
369 370 371 372 373 374 375 376 377 378 379 380 381 382
          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);
  });
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
}

// 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
// at paint time looks for for a descendant PhysicalShape and caches the
// 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);
407
        result = renderObject as RenderPhysicalShape;
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
      } else {
        result = findPhysicalShapeChild(e);
      }
    });
    return result;
  }

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

class ShapeListener extends StatefulWidget {
  const ShapeListener(this.child);

  final Widget child;

  @override
427
  State createState() => ShapeListenerState();
428 429 430 431 432 433

}

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

  ClipCachePainter cache;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
445
    cache = ClipCachePainter(context);
446 447 448
  }

}
449

450
class RectangularNotch extends NotchedShape {
451 452 453 454
  const RectangularNotch();

  @override
  Path getOuterPath(Rect host, Rect guest) {
455 456
    if (guest == null)
      return Path()..addRect(host);
457
    return Path()
458 459 460 461 462 463 464 465 466 467 468 469
      ..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();
  }
}