drawer_test.dart 5.14 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
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
7 8 9
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

10 11
import '../widgets/semantics_tester.dart';

12 13
void main() {
  testWidgets('Drawer control test', (WidgetTester tester) async {
14
    const Key containerKey = Key('container');
15

16
    await tester.pumpWidget(
17 18 19 20
      MaterialApp(
        home: Scaffold(
          drawer: Drawer(
            child: ListView(
21
              children: <Widget>[
22 23
                DrawerHeader(
                  child: Container(
24 25 26 27 28
                    key: containerKey,
                    child: const Text('header'),
                  ),
                ),
                const ListTile(
29 30
                  leading: Icon(Icons.archive),
                  title: Text('Archive'),
31 32 33 34 35 36
                ),
              ],
            ),
          ),
        ),
      ),
37 38 39
    );

    expect(find.text('Archive'), findsNothing);
40
    final ScaffoldState state = tester.firstState(find.byType(Scaffold));
41
    state.openDrawer();
42

43 44 45
    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
    expect(find.text('Archive'), findsOneWidget);
46 47 48 49 50 51 52 53 54

    RenderBox box = tester.renderObject(find.byType(DrawerHeader));
    expect(box.size.height, equals(160.0 + 8.0 + 1.0)); // height + bottom margin + bottom edge

    final double drawerWidth = box.size.width;
    final double drawerHeight = box.size.height;

    box = tester.renderObject(find.byKey(containerKey));
    expect(box.size.width, equals(drawerWidth - 2 * 16.0));
55
    expect(box.size.height, equals(drawerHeight - 2 * 16.0));
56 57

    expect(find.text('header'), findsOneWidget);
58
  });
59

Dan Field's avatar
Dan Field committed
60
  testWidgets('Drawer dismiss barrier has label', (WidgetTester tester) async {
61
    final SemanticsTester semantics = SemanticsTester(tester);
62
    await tester.pumpWidget(
63 64
      const MaterialApp(
        home: Scaffold(
65
          drawer: Drawer(),
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        ),
      ),
    );

    final ScaffoldState state = tester.firstState(find.byType(Scaffold));
    state.openDrawer();

    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

    expect(semantics, includesNodeWith(
      label: const DefaultMaterialLocalizations().modalBarrierDismissLabel,
      actions: <SemanticsAction>[SemanticsAction.tap],
    ));

    semantics.dispose();
Dan Field's avatar
Dan Field committed
82
  }, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS,  TargetPlatform.macOS }));
83

Dan Field's avatar
Dan Field committed
84
  testWidgets('Drawer dismiss barrier has no label', (WidgetTester tester) async {
85
    final SemanticsTester semantics = SemanticsTester(tester);
86
    await tester.pumpWidget(
87 88
      const MaterialApp(
        home: Scaffold(
89
            drawer: Drawer(),
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        ),
      ),
    );

    final ScaffoldState state = tester.firstState(find.byType(Scaffold));
    state.openDrawer();

    await tester.pump();
    await tester.pump(const Duration(seconds: 1));

    expect(semantics, isNot(includesNodeWith(
      label: const DefaultMaterialLocalizations().modalBarrierDismissLabel,
      actions: <SemanticsAction>[SemanticsAction.tap],
    )));

    semantics.dispose();
Dan Field's avatar
Dan Field committed
106
  }, variant: TargetPlatformVariant.only(TargetPlatform.android));
107

108 109 110 111 112 113 114 115 116
  testWidgets('Scaffold drawerScrimColor', (WidgetTester tester) async {
    // The scrim is a Container within a Semantics node labeled "Dismiss",
    // within a DrawerController. Sorry.
    Container getScrim() {
      return tester.widget<Container>(
        find.descendant(
          of: find.descendant(
            of: find.byType(DrawerController),
            matching: find.byWidgetPredicate((Widget widget) {
117 118
              return widget is Semantics
                  && widget.properties.label == 'Dismiss';
119 120 121 122 123 124
            }),
          ),
          matching: find.byType(Container),
        ),
      );
    }
125

126 127 128
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
    Widget buildFrame({ Color drawerScrimColor }) {
      return MaterialApp(
129
        home: Scaffold(
130 131 132 133 134 135 136 137 138 139 140
          key: scaffoldKey,
          drawerScrimColor: drawerScrimColor,
          drawer: Drawer(
            child: Builder(
              builder: (BuildContext context) {
                return GestureDetector(
                  onTap: () { Navigator.pop(context); }, // close drawer
                );
              },
            ),
          ),
141
        ),
142 143
      );
    }
144

145
    // Default drawerScrimColor
146

147 148 149
    await tester.pumpWidget(buildFrame(drawerScrimColor: null));
    scaffoldKey.currentState.openDrawer();
    await tester.pumpAndSettle();
150

151
    expect(getScrim().color, Colors.black54);
152

153 154 155 156 157 158 159 160 161 162
    await tester.tap(find.byType(Drawer));
    await tester.pumpAndSettle();
    expect(find.byType(Drawer), findsNothing);

    // Specific drawerScrimColor

    await tester.pumpWidget(buildFrame(drawerScrimColor: const Color(0xFF323232)));
    scaffoldKey.currentState.openDrawer();
    await tester.pumpAndSettle();

163
    expect(getScrim().color, const Color(0xFF323232));
164 165 166 167

    await tester.tap(find.byType(Drawer));
    await tester.pumpAndSettle();
    expect(find.byType(Drawer), findsNothing);
168
  });
169
}