back_button_test.dart 2.67 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 12
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('BackButton control test', (WidgetTester tester) async {
    await tester.pumpWidget(
      new 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

  testWidgets('BackButton icon', (WidgetTester tester) async {
    final Key iOSKey = new UniqueKey();
    final Key androidKey = new UniqueKey();


    await tester.pumpWidget(
      new MaterialApp(
        home: new Column(
          children: <Widget>[
            new Theme(
              data: new ThemeData(platform: TargetPlatform.iOS),
              child: new BackButtonIcon(key: iOSKey),
            ),
            new Theme(
              data: new ThemeData(platform: TargetPlatform.android),
              child: new BackButtonIcon(key: androidKey),
            ),
          ],
        ),
      ),
    );

    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 67

  testWidgets('BackButton semantics', (WidgetTester tester) async {
    final SemanticsHandle handle = tester.ensureSemantics();
    await tester.pumpWidget(
      new 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 85 86 87 88 89 90 91 92 93
              ),
            );
          },
        },
      ),
    );

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

    await tester.pumpAndSettle();

    expect(tester.getSemanticsData(find.byType(BackButton)), matchesSemanticsData(
      label: 'Back',
      isButton: true,
      hasEnabledState: true,
      isEnabled: true,
      hasTapAction: true,
    ));
    handle.dispose();
  });
94
}