back_button_test.dart 10.9 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
import 'package:flutter/foundation.dart';
6 7
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
8

9
void main() {
10
  testWidgets('BackButton control test', (WidgetTester tester) async {
11
    await tester.pumpWidget(
12
      MaterialApp(
13
        home: const Material(child: Text('Home')),
14 15
        routes: <String, WidgetBuilder>{
          '/next': (BuildContext context) {
16
            return const Material(
17 18
              child: Center(
                child: BackButton(),
19
              ),
20 21
            );
          },
22
        },
23
      ),
24 25 26 27
    );

    tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/next');

28
    await tester.pumpAndSettle();
29 30 31

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

32
    await tester.pumpAndSettle();
33 34 35

    expect(find.text('Home'), findsOneWidget);
  });
36

37
  testWidgets('BackButton onPressed overrides default pop behavior', (WidgetTester tester) async {
38
    bool customCallbackWasCalled = false;
39 40 41 42 43 44 45
    await tester.pumpWidget(
      MaterialApp(
        home: const Material(child: Text('Home')),
        routes: <String, WidgetBuilder>{
          '/next': (BuildContext context) {
            return Material(
              child: Center(
46
                child: BackButton(onPressed: () => customCallbackWasCalled = true),
47 48
              ),
            );
49
          },
50
        },
51
      ),
52
    );
53

54
    tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/next');
55

56
    await tester.pumpAndSettle();
57

58 59
    expect(find.text('Home'), findsNothing); // Start off on the second page.
    expect(customCallbackWasCalled, false); // customCallbackWasCalled should still be false.
60
    await tester.tap(find.byType(BackButton));
61

62
    await tester.pumpAndSettle();
63

64 65 66
    // We're still on the second page.
    expect(find.text('Home'), findsNothing);
    // But the custom callback is called.
67
    expect(customCallbackWasCalled, true);
68 69
  });

70
  testWidgets('BackButton icon', (WidgetTester tester) async {
71
    final Key androidKey = UniqueKey();
Dan Field's avatar
Dan Field committed
72
    final Key iOSKey = UniqueKey();
73
    final Key linuxKey = UniqueKey();
Dan Field's avatar
Dan Field committed
74
    final Key macOSKey = UniqueKey();
75
    final Key windowsKey = UniqueKey();
76

77
    await tester.pumpWidget(
78 79
      MaterialApp(
        home: Column(
80
          children: <Widget>[
Dan Field's avatar
Dan Field committed
81 82 83 84
            Theme(
              data: ThemeData(platform: TargetPlatform.android),
              child: BackButtonIcon(key: androidKey),
            ),
85 86 87
            Theme(
              data: ThemeData(platform: TargetPlatform.iOS),
              child: BackButtonIcon(key: iOSKey),
88
            ),
89 90 91 92
            Theme(
              data: ThemeData(platform: TargetPlatform.linux),
              child: BackButtonIcon(key: linuxKey),
            ),
93
            Theme(
Dan Field's avatar
Dan Field committed
94 95
              data: ThemeData(platform: TargetPlatform.macOS),
              child: BackButtonIcon(key: macOSKey),
96
            ),
97 98 99 100
            Theme(
              data: ThemeData(platform: TargetPlatform.windows),
              child: BackButtonIcon(key: windowsKey),
            ),
101 102 103 104 105
          ],
        ),
      ),
    );

106
    final Icon androidIcon = tester.widget(find.descendant(of: find.byKey(androidKey), matching: find.byType(Icon)));
Dan Field's avatar
Dan Field committed
107
    final Icon iOSIcon = tester.widget(find.descendant(of: find.byKey(iOSKey), matching: find.byType(Icon)));
108
    final Icon linuxIcon = tester.widget(find.descendant(of: find.byKey(linuxKey), matching: find.byType(Icon)));
Dan Field's avatar
Dan Field committed
109
    final Icon macOSIcon = tester.widget(find.descendant(of: find.byKey(macOSKey), matching: find.byType(Icon)));
110
    final Icon windowsIcon = tester.widget(find.descendant(of: find.byKey(windowsKey), matching: find.byType(Icon)));
111
    expect(iOSIcon.icon == androidIcon.icon, kIsWeb ? isTrue : isFalse);
112
    expect(linuxIcon.icon == androidIcon.icon, isTrue);
113
    expect(macOSIcon.icon == androidIcon.icon, kIsWeb ? isTrue : isFalse);
Dan Field's avatar
Dan Field committed
114
    expect(macOSIcon.icon == iOSIcon.icon, isTrue);
115
    expect(windowsIcon.icon == androidIcon.icon, isTrue);
116
  });
117

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

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

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

    final RichText iconText = tester.firstWidget(find.descendant(
151 152
      of: find.byType(BackButton),
      matching: find.byType(RichText),
153
    ));
154 155 156
    expect(iconText.text.style!.color, Colors.red);
  });

157
  testWidgets('BackButton.style.iconColor parameter overrides BackButton.color', (WidgetTester tester) async {
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(useMaterial3: true),
        home: const Material(
          child: BackButton(
            color: Colors.green,
            style: ButtonStyle(
              iconColor: MaterialStatePropertyAll<Color>(Colors.red),
            ),
          ),
        ),
      ),
    );

    final RichText iconText = tester.firstWidget(find.descendant(
      of: find.byType(BackButton),
      matching: find.byType(RichText),
    ));

    expect(iconText.text.style!.color, Colors.red);
178 179
  });

180
  testWidgets('BackButton semantics', (WidgetTester tester) async {
181 182
    final SemanticsHandle handle = tester.ensureSemantics();
    await tester.pumpWidget(
183
      MaterialApp(
184
        home: const Material(child: Text('Home')),
185 186 187
        routes: <String, WidgetBuilder>{
          '/next': (BuildContext context) {
            return const Material(
188 189
              child: Center(
                child: BackButton(),
190 191 192 193 194 195 196 197 198 199
              ),
            );
          },
        },
      ),
    );

    tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/next');

    await tester.pumpAndSettle();
200
    final String? expectedLabel;
201
    switch (defaultTargetPlatform) {
202 203 204 205 206 207 208 209 210
      case TargetPlatform.android:
        expectedLabel = 'Back';
      case TargetPlatform.fuchsia:
      case TargetPlatform.iOS:
      case TargetPlatform.linux:
      case TargetPlatform.macOS:
      case TargetPlatform.windows:
        expectedLabel = null;
    }
211
    expect(tester.getSemantics(find.byType(BackButton)), matchesSemantics(
212
      tooltip: 'Back',
213
      label: expectedLabel,
214 215 216 217 218 219 220
      isButton: true,
      hasEnabledState: true,
      isEnabled: true,
      hasTapAction: true,
      isFocusable: true,
    ));
    handle.dispose();
221 222
  }, variant: TargetPlatformVariant.all());

223
  testWidgets('CloseButton semantics', (WidgetTester tester) async {
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    final SemanticsHandle handle = tester.ensureSemantics();
    await tester.pumpWidget(
      MaterialApp(
        home: const Material(child: Text('Home')),
        routes: <String, WidgetBuilder>{
          '/next': (BuildContext context) {
            return const Material(
              child: Center(
                child: CloseButton(),
              ),
            );
          },
        },
      ),
    );

    tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/next');

    await tester.pumpAndSettle();
    final String? expectedLabel;
244
    switch (defaultTargetPlatform) {
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
      case TargetPlatform.android:
        expectedLabel = 'Close';
      case TargetPlatform.fuchsia:
      case TargetPlatform.iOS:
      case TargetPlatform.linux:
      case TargetPlatform.macOS:
      case TargetPlatform.windows:
        expectedLabel = null;
    }
    expect(tester.getSemantics(find.byType(CloseButton)), matchesSemantics(
      tooltip: 'Close',
      label: expectedLabel,
      isButton: true,
      hasEnabledState: true,
      isEnabled: true,
      hasTapAction: true,
      isFocusable: true,
    ));
    handle.dispose();
  }, variant: TargetPlatformVariant.all());
265

266
  testWidgets('CloseButton color', (WidgetTester tester) async {
267 268 269 270 271 272 273 274 275 276 277
    await tester.pumpWidget(
      const MaterialApp(
        home: Material(
          child: CloseButton(
            color: Colors.red,
          ),
        ),
      ),
    );

    final RichText iconText = tester.firstWidget(find.descendant(
278 279
      of: find.byType(CloseButton),
      matching: find.byType(RichText),
280
    ));
281
    expect(iconText.text.style!.color, Colors.red);
282
  });
283

284
  testWidgets('CloseButton color with ButtonStyle', (WidgetTester tester) async {
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(useMaterial3: true),
        home: const Material(
          child: CloseButton(
            style: ButtonStyle(
              iconColor: MaterialStatePropertyAll<Color>(Colors.red),
            ),
          ),
        ),
      ),
    );

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

305
  testWidgets('CloseButton.style.iconColor parameter overrides CloseButton.color', (WidgetTester tester) async {
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(useMaterial3: true),
        home: const Material(
          child: CloseButton(
            color: Colors.green,
            style: ButtonStyle(
              iconColor: MaterialStatePropertyAll<Color>(Colors.red),
            ),
          ),
        ),
      ),
    );

    final RichText iconText = tester.firstWidget(find.descendant(
      of: find.byType(CloseButton),
      matching: find.byType(RichText),
    ));

    expect(iconText.text.style!.color, Colors.red);
  });

328
  testWidgets('CloseButton onPressed overrides default pop behavior', (WidgetTester tester) async {
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    bool customCallbackWasCalled = false;
    await tester.pumpWidget(
      MaterialApp(
        home: const Material(child: Text('Home')),
        routes: <String, WidgetBuilder>{
          '/next': (BuildContext context) {
            return Material(
              child: Center(
                child: CloseButton(onPressed: () => customCallbackWasCalled = true),
              ),
            );
          },
        },
      ),
    );

    tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/next');

    await tester.pumpAndSettle();
    expect(find.text('Home'), findsNothing); // Start off on the second page.
    expect(customCallbackWasCalled, false); // customCallbackWasCalled should still be false.
    await tester.tap(find.byType(CloseButton));

    await tester.pumpAndSettle();

    // We're still on the second page.
    expect(find.text('Home'), findsNothing);
    // The custom callback is called, setting customCallbackWasCalled to true.
    expect(customCallbackWasCalled, true);
  });
359
}