drawer_button_test.dart 9.19 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 The Flutter 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/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
8

9
void main() {
10
  testWidgets('DrawerButton control test', (WidgetTester tester) async {
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    await tester.pumpWidget(
      const MaterialApp(
        home: Scaffold(
          body: DrawerButton(),
          drawer: Drawer(),
        ),
      ),
    );

    await tester.pumpAndSettle();

    expect(find.byType(Drawer), findsNothing);

    await tester.tap(find.byType(DrawerButton));

    await tester.pumpAndSettle();

    expect(find.byType(Drawer), findsOneWidget);
  });

31
  testWidgets('DrawerButton onPressed overrides default end drawer open behaviour',
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
      (WidgetTester tester) async {
    bool customCallbackWasCalled = false;
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: DrawerButton(
                onPressed: () => customCallbackWasCalled = true),
          ),
          drawer: const Drawer(),
        ),
      ),
    );

    await tester.pumpAndSettle();
    expect(find.byType(Drawer), findsNothing); // Start off with a closed drawer
    expect(customCallbackWasCalled,
        false); // customCallbackWasCalled should still be false.
    await tester.tap(find.byType(DrawerButton));

    await tester.pumpAndSettle();

    // Drawer is still closed
    expect(find.byType(Drawer), findsNothing);
    // The custom callback is called, setting customCallbackWasCalled to true.
    expect(customCallbackWasCalled, true);
  });

60
  testWidgets('DrawerButton icon', (WidgetTester tester) async {
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    final Key androidKey = UniqueKey();
    final Key iOSKey = UniqueKey();
    final Key linuxKey = UniqueKey();
    final Key macOSKey = UniqueKey();
    final Key windowsKey = UniqueKey();

    await tester.pumpWidget(
      MaterialApp(
        home: Column(
          children: <Widget>[
            Theme(
              data: ThemeData(platform: TargetPlatform.android),
              child: DrawerButtonIcon(key: androidKey),
            ),
            Theme(
              data: ThemeData(platform: TargetPlatform.iOS),
              child: DrawerButtonIcon(key: iOSKey),
            ),
            Theme(
              data: ThemeData(platform: TargetPlatform.linux),
              child: DrawerButtonIcon(key: linuxKey),
            ),
            Theme(
              data: ThemeData(platform: TargetPlatform.macOS),
              child: DrawerButtonIcon(key: macOSKey),
            ),
            Theme(
              data: ThemeData(platform: TargetPlatform.windows),
              child: DrawerButtonIcon(key: windowsKey),
            ),
          ],
        ),
      ),
    );

    final Icon androidIcon = tester.widget(find.descendant(
        of: find.byKey(androidKey), matching: find.byType(Icon)));
    final Icon iOSIcon = tester.widget(
        find.descendant(of: find.byKey(iOSKey), matching: find.byType(Icon)));
    final Icon linuxIcon = tester.widget(
        find.descendant(of: find.byKey(linuxKey), matching: find.byType(Icon)));
    final Icon macOSIcon = tester.widget(
        find.descendant(of: find.byKey(macOSKey), matching: find.byType(Icon)));
    final Icon windowsIcon = tester.widget(find.descendant(
        of: find.byKey(windowsKey), matching: find.byType(Icon)));

    // All icons for drawer are the same
    expect(iOSIcon.icon == androidIcon.icon, isTrue);
    expect(linuxIcon.icon == androidIcon.icon, isTrue);
    expect(macOSIcon.icon == androidIcon.icon, isTrue);
    expect(windowsIcon.icon == androidIcon.icon, isTrue);
  });

114
  testWidgets('DrawerButton color', (WidgetTester tester) async {
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    await tester.pumpWidget(
      const MaterialApp(
        home: Material(
          child: DrawerButton(
            color: Colors.red,
          ),
        ),
      ),
    );

    final RichText iconText = tester.firstWidget(find.descendant(
      of: find.byType(DrawerButton),
      matching: find.byType(RichText),
    ));
    expect(iconText.text.style!.color, Colors.red);
  });

  testWidgets('DrawerButton color with ButtonStyle', (WidgetTester tester) async {
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(useMaterial3: true),
        home: const Material(
          child: DrawerButton(
            style: ButtonStyle(
              iconColor: MaterialStatePropertyAll<Color>(Colors.red),
            ),
          ),
        ),
      ),
    );

    final RichText iconText = tester.firstWidget(find.descendant(
      of: find.byType(DrawerButton),
      matching: find.byType(RichText),
    ));
    expect(iconText.text.style!.color, Colors.red);
  });

153
  testWidgets('DrawerButton semantics', (WidgetTester tester) async {
154 155 156 157 158 159 160 161 162 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
    final SemanticsHandle handle = tester.ensureSemantics();
    await tester.pumpWidget(
      const MaterialApp(
        home: Material(
          child: Center(
            child: DrawerButton(),
          ),
        ),
      ),
    );

    await tester.pumpAndSettle();

    final String? expectedLabel;
    switch (defaultTargetPlatform) {
      case TargetPlatform.android:
        expectedLabel = 'Open navigation menu';
      case TargetPlatform.fuchsia:
      case TargetPlatform.iOS:
      case TargetPlatform.linux:
      case TargetPlatform.macOS:
      case TargetPlatform.windows:
        expectedLabel = null;
    }
    expect(tester.getSemantics(find.byType(DrawerButton)), matchesSemantics(
      tooltip: 'Open navigation menu',
      label: expectedLabel,
      isButton: true,
      hasEnabledState: true,
      isEnabled: true,
      hasTapAction: true,
      isFocusable: true,
    ));
    handle.dispose();
  }, variant: TargetPlatformVariant.all());

190
  testWidgets('EndDrawerButton control test', (WidgetTester tester) async {
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    await tester.pumpWidget(
      const MaterialApp(
        home: Scaffold(
          body: EndDrawerButton(),
          endDrawer: Drawer(),
        ),
      ),
    );

    await tester.pumpAndSettle();

    expect(find.byType(Drawer), findsNothing);

    await tester.tap(find.byType(EndDrawerButton));

    await tester.pumpAndSettle();

    expect(find.byType(Drawer), findsOneWidget);
  });

211
  testWidgets('EndDrawerButton semantics', (WidgetTester tester) async {
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    final SemanticsHandle handle = tester.ensureSemantics();
    await tester.pumpWidget(
      const MaterialApp(
        home: Material(
          child: Center(
            child: EndDrawerButton(),
          ),
        ),
      ),
    );

    await tester.pumpAndSettle();
    final String? expectedLabel;
    switch (defaultTargetPlatform) {
      case TargetPlatform.android:
        expectedLabel = 'Open navigation menu';
      case TargetPlatform.fuchsia:
      case TargetPlatform.iOS:
      case TargetPlatform.linux:
      case TargetPlatform.macOS:
      case TargetPlatform.windows:
        expectedLabel = null;
    }
    expect(tester.getSemantics(find.byType(EndDrawerButton)), matchesSemantics(
      tooltip: 'Open navigation menu',
      label: expectedLabel,
      isButton: true,
      hasEnabledState: true,
      isEnabled: true,
      hasTapAction: true,
      isFocusable: true,
    ));
    handle.dispose();
  }, variant: TargetPlatformVariant.all());

247
  testWidgets('EndDrawerButton color', (WidgetTester tester) async {
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    await tester.pumpWidget(
      const MaterialApp(
        home: Material(
          child: EndDrawerButton(
            color: Colors.red,
          ),
        ),
      ),
    );

    final RichText iconText = tester.firstWidget(find.descendant(
      of: find.byType(EndDrawerButton),
      matching: find.byType(RichText),
    ));
    expect(iconText.text.style!.color, Colors.red);
  });

  testWidgets('EndDrawerButton color with ButtonStyle', (WidgetTester tester) async {
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(useMaterial3: true),
        home: const Material(
          child: EndDrawerButton(
            style: ButtonStyle(
              iconColor: MaterialStatePropertyAll<Color>(Colors.red),
            ),
          ),
        ),
      ),
    );

    final RichText iconText = tester.firstWidget(find.descendant(
      of: find.byType(EndDrawerButton),
      matching: find.byType(RichText),
    ));
    expect(iconText.text.style!.color, Colors.red);
  });

286
  testWidgets('EndDrawerButton onPressed overrides default end drawer open behaviour',
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
      (WidgetTester tester) async {
    bool customCallbackWasCalled = false;
    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: Center(
            child: EndDrawerButton(onPressed: () => customCallbackWasCalled = true),
          ),
          endDrawer: const Drawer(),
        ),
      ),
    );

    await tester.pumpAndSettle();
    expect(find.byType(Drawer), findsNothing); // Start off with a closed drawer
    expect(customCallbackWasCalled,
        false); // customCallbackWasCalled should still be false.
    await tester.tap(find.byType(EndDrawerButton));

    await tester.pumpAndSettle();

    // Drawer is still closed
    expect(find.byType(Drawer), findsNothing);
    // The custom callback is called, setting customCallbackWasCalled to true.
    expect(customCallbackWasCalled, true);
  });
}