|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | +import 'package:flutter_api_samples/material/dropdown_menu/dropdown_menu.1.dart' as example; |
| 7 | +import 'package:flutter_test/flutter_test.dart'; |
| 8 | + |
| 9 | +void main() { |
| 10 | + testWidgets('The DropdownMenu should display a menu with the list of entries the user can select', (WidgetTester tester) async { |
| 11 | + await tester.pumpWidget( |
| 12 | + const example.DropdownMenuApp(), |
| 13 | + ); |
| 14 | + |
| 15 | + expect(find.widgetWithText(TextField, 'One'), findsOne); |
| 16 | + final Finder menu = find.byType(DropdownMenu<String>); |
| 17 | + expect(menu, findsOne); |
| 18 | + |
| 19 | + Finder findMenuItem(String label) { |
| 20 | + return find.widgetWithText(MenuItemButton, label).last; |
| 21 | + } |
| 22 | + |
| 23 | + await tester.tap(menu); |
| 24 | + await tester.pumpAndSettle(); |
| 25 | + expect(findMenuItem('One'), findsOne); |
| 26 | + expect(findMenuItem('Two'), findsOne); |
| 27 | + expect(findMenuItem('Three'), findsOne); |
| 28 | + expect(findMenuItem('Four'), findsOne); |
| 29 | + |
| 30 | + await tester.tap(findMenuItem('Two')); |
| 31 | + |
| 32 | + // The DropdownMenu's onSelected callback is delayed |
| 33 | + // with SchedulerBinding.instance.addPostFrameCallback |
| 34 | + // to give the focus a chance to return to where it was |
| 35 | + // before the menu appeared. The pumpAndSettle() |
| 36 | + // give the callback a chance to run. |
| 37 | + await tester.pumpAndSettle(); |
| 38 | + |
| 39 | + expect(find.widgetWithText(TextField, 'Two'), findsOne); |
| 40 | + }); |
| 41 | + |
| 42 | + testWidgets('DropdownMenu has focus when tapping on the text field', (WidgetTester tester) async { |
| 43 | + await tester.pumpWidget( |
| 44 | + const example.DropdownMenuApp(), |
| 45 | + ); |
| 46 | + |
| 47 | + // Make sure the dropdown menus are there. |
| 48 | + final Finder menu = find.byType(DropdownMenu<String>); |
| 49 | + expect(menu, findsOne); |
| 50 | + |
| 51 | + // Tap on the menu and make sure it is focused. |
| 52 | + await tester.tap(menu); |
| 53 | + await tester.pumpAndSettle(); |
| 54 | + expect(FocusScope.of(tester.element(menu)).hasFocus, isTrue); |
| 55 | + }); |
| 56 | +} |
0 commit comments