back_button_test.dart 2.62 KB
Newer Older
1 2 3 4 5
// Copyright 2016 The Chromium 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/material.dart';
6
import 'package:flutter/rendering.dart';
7 8 9 10 11
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('BackButton control test', (WidgetTester tester) async {
    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 icon', (WidgetTester tester) async {
38 39
    final Key iOSKey = UniqueKey();
    final Key androidKey = UniqueKey();
40 41 42


    await tester.pumpWidget(
43 44
      MaterialApp(
        home: Column(
45
          children: <Widget>[
46 47 48
            Theme(
              data: ThemeData(platform: TargetPlatform.iOS),
              child: BackButtonIcon(key: iOSKey),
49
            ),
50 51 52
            Theme(
              data: ThemeData(platform: TargetPlatform.android),
              child: BackButtonIcon(key: androidKey),
53 54 55 56 57 58 59 60 61 62
            ),
          ],
        ),
      ),
    );

    final Icon iOSIcon = tester.widget(find.descendant(of: find.byKey(iOSKey), matching: find.byType(Icon)));
    final Icon androidIcon = tester.widget(find.descendant(of: find.byKey(androidKey), matching: find.byType(Icon)));
    expect(iOSIcon == androidIcon, false);
  });
63 64 65 66

  testWidgets('BackButton semantics', (WidgetTester tester) async {
    final SemanticsHandle handle = tester.ensureSemantics();
    await tester.pumpWidget(
67
      MaterialApp(
68
        home: const Material(child: Text('Home')),
69 70 71
        routes: <String, WidgetBuilder>{
          '/next': (BuildContext context) {
            return const Material(
72 73
              child: Center(
                child: BackButton(),
74 75 76 77 78 79 80 81 82 83 84
              ),
            );
          },
        },
      ),
    );

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

    await tester.pumpAndSettle();

85
    expect(tester.getSemantics(find.byType(BackButton)), matchesSemantics(
86 87 88 89 90 91 92 93
      label: 'Back',
      isButton: true,
      hasEnabledState: true,
      isEnabled: true,
      hasTapAction: true,
    ));
    handle.dispose();
  });
94
}