popup_menu_test.dart 1.76 KB
Newer Older
1 2 3 4 5 6 7 8
// 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_test/flutter_test.dart';
import 'package:flutter/material.dart';

void main() {
9
  testWidgets('Navigator.push works within a PopupMenuButton', (WidgetTester tester) async {
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    await tester.pumpWidget(
      new MaterialApp(
        routes: <String, WidgetBuilder> {
          '/next': (BuildContext context) {
            return new Text('Next');
          }
        },
        home: new Material(
          child: new Center(
            child: new Builder(
              builder: (BuildContext context) {
                return new PopupMenuButton<int>(
                  onSelected: (int value) {
                    Navigator.pushNamed(context, '/next');
                  },
                  itemBuilder: (BuildContext context) {
                    return <PopupMenuItem<int>>[
                      new PopupMenuItem<int>(
                        value: 1,
                        child: new Text('One')
                      )
                    ];
                  }
                );
              }
            )
          )
        )
      )
    );

    await tester.tap(find.byType(Builder));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // finish the menu animation

    expect(find.text('One'), findsOneWidget);
    expect(find.text('Next'), findsNothing);

    await tester.tap(find.text('One'));
49 50 51
    await tester.pump(); // return the future
    await tester.pump(); // start the navigation
    await tester.pump(const Duration(seconds: 1)); // end the navigation
52 53 54 55 56

    expect(find.text('One'), findsNothing);
    expect(find.text('Next'), findsOneWidget);
  });
}