flutter_flutter/packages/flutter/test/material/drawer_button_test.dart
Kate Lovett 9d96df2364
Modernize framework lints (#179089)
WIP

Commits separated as follows:
- Update lints in analysis_options files
- Run `dart fix --apply`
- Clean up leftover analysis issues 
- Run `dart format .` in the right places.

Local analysis and testing passes. Checking CI now.

Part of https://github.com/flutter/flutter/issues/178827
- Adoption of flutter_lints in examples/api coming in a separate change
(cc @loic-sharma)

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-11-26 01:10:39 +00:00

400 lines
12 KiB
Dart

// Copyright 2014 The Flutter 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 'dart:ui' show PointerDeviceKind;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart' show RendererBinding;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('DrawerButton control test', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(body: DrawerButton(), drawer: Drawer()),
),
);
await tester.pumpAndSettle();
expect(find.byType(Drawer), findsNothing);
await tester.tap(find.byType(DrawerButton));
await tester.pumpAndSettle();
expect(find.byType(Drawer), findsOneWidget);
});
testWidgets('DrawerButton onPressed overrides default end drawer open behaviour', (
WidgetTester tester,
) async {
var customCallbackWasCalled = false;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(child: DrawerButton(onPressed: () => customCallbackWasCalled = true)),
drawer: const Drawer(),
),
),
);
await tester.pumpAndSettle();
expect(find.byType(Drawer), findsNothing); // Start off with a closed drawer
expect(customCallbackWasCalled, false); // customCallbackWasCalled should still be false.
await tester.tap(find.byType(DrawerButton));
await tester.pumpAndSettle();
// Drawer is still closed
expect(find.byType(Drawer), findsNothing);
// The custom callback is called, setting customCallbackWasCalled to true.
expect(customCallbackWasCalled, true);
});
testWidgets('DrawerButton icon', (WidgetTester tester) async {
final Key androidKey = UniqueKey();
final Key iOSKey = UniqueKey();
final Key linuxKey = UniqueKey();
final Key macOSKey = UniqueKey();
final Key windowsKey = UniqueKey();
await tester.pumpWidget(
MaterialApp(
home: Column(
children: <Widget>[
Theme(
data: ThemeData(platform: TargetPlatform.android),
child: DrawerButtonIcon(key: androidKey),
),
Theme(
data: ThemeData(platform: TargetPlatform.iOS),
child: DrawerButtonIcon(key: iOSKey),
),
Theme(
data: ThemeData(platform: TargetPlatform.linux),
child: DrawerButtonIcon(key: linuxKey),
),
Theme(
data: ThemeData(platform: TargetPlatform.macOS),
child: DrawerButtonIcon(key: macOSKey),
),
Theme(
data: ThemeData(platform: TargetPlatform.windows),
child: DrawerButtonIcon(key: windowsKey),
),
],
),
),
);
final Icon androidIcon = tester.widget(
find.descendant(of: find.byKey(androidKey), matching: find.byType(Icon)),
);
final Icon iOSIcon = tester.widget(
find.descendant(of: find.byKey(iOSKey), matching: find.byType(Icon)),
);
final Icon linuxIcon = tester.widget(
find.descendant(of: find.byKey(linuxKey), matching: find.byType(Icon)),
);
final Icon macOSIcon = tester.widget(
find.descendant(of: find.byKey(macOSKey), matching: find.byType(Icon)),
);
final Icon windowsIcon = tester.widget(
find.descendant(of: find.byKey(windowsKey), matching: find.byType(Icon)),
);
// All icons for drawer are the same
expect(iOSIcon.icon == androidIcon.icon, isTrue);
expect(linuxIcon.icon == androidIcon.icon, isTrue);
expect(macOSIcon.icon == androidIcon.icon, isTrue);
expect(windowsIcon.icon == androidIcon.icon, isTrue);
});
testWidgets('DrawerButton color', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Material(child: DrawerButton(color: Colors.red)),
),
);
final RichText iconText = tester.firstWidget(
find.descendant(of: find.byType(DrawerButton), matching: find.byType(RichText)),
);
expect(iconText.text.style!.color, Colors.red);
});
testWidgets('DrawerButton color with ButtonStyle', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Material(
child: DrawerButton(
style: ButtonStyle(iconColor: MaterialStatePropertyAll<Color>(Colors.red)),
),
),
),
);
final RichText iconText = tester.firstWidget(
find.descendant(of: find.byType(DrawerButton), matching: find.byType(RichText)),
);
expect(iconText.text.style!.color, Colors.red);
});
testWidgets('DrawerButton semantics', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
const MaterialApp(
home: Material(child: Center(child: DrawerButton())),
),
);
await tester.pumpAndSettle();
final String? expectedLabel;
switch (defaultTargetPlatform) {
case TargetPlatform.android:
expectedLabel = 'Open navigation menu';
case TargetPlatform.fuchsia:
case TargetPlatform.iOS:
case TargetPlatform.linux:
case TargetPlatform.macOS:
case TargetPlatform.windows:
expectedLabel = null;
}
expect(
tester.getSemantics(find.byType(DrawerButton)),
matchesSemantics(
tooltip: 'Open navigation menu',
label: expectedLabel,
isButton: true,
hasEnabledState: true,
isEnabled: true,
hasTapAction: true,
hasFocusAction: defaultTargetPlatform != TargetPlatform.iOS,
isFocusable: true,
),
);
handle.dispose();
}, variant: TargetPlatformVariant.all());
testWidgets('EndDrawerButton control test', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(body: EndDrawerButton(), endDrawer: Drawer()),
),
);
await tester.pumpAndSettle();
expect(find.byType(Drawer), findsNothing);
await tester.tap(find.byType(EndDrawerButton));
await tester.pumpAndSettle();
expect(find.byType(Drawer), findsOneWidget);
});
testWidgets('EndDrawerButton semantics', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
const MaterialApp(
home: Material(child: Center(child: EndDrawerButton())),
),
);
await tester.pumpAndSettle();
final String? expectedLabel;
switch (defaultTargetPlatform) {
case TargetPlatform.android:
expectedLabel = 'Open navigation menu';
case TargetPlatform.fuchsia:
case TargetPlatform.iOS:
case TargetPlatform.linux:
case TargetPlatform.macOS:
case TargetPlatform.windows:
expectedLabel = null;
}
expect(
tester.getSemantics(find.byType(EndDrawerButton)),
matchesSemantics(
tooltip: 'Open navigation menu',
label: expectedLabel,
isButton: true,
hasEnabledState: true,
isEnabled: true,
hasTapAction: true,
hasFocusAction: defaultTargetPlatform != TargetPlatform.iOS,
isFocusable: true,
),
);
handle.dispose();
}, variant: TargetPlatformVariant.all());
testWidgets('EndDrawerButton color', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Material(child: EndDrawerButton(color: Colors.red)),
),
);
final RichText iconText = tester.firstWidget(
find.descendant(of: find.byType(EndDrawerButton), matching: find.byType(RichText)),
);
expect(iconText.text.style!.color, Colors.red);
});
testWidgets('EndDrawerButton color with ButtonStyle', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Material(
child: EndDrawerButton(
style: ButtonStyle(iconColor: MaterialStatePropertyAll<Color>(Colors.red)),
),
),
),
);
final RichText iconText = tester.firstWidget(
find.descendant(of: find.byType(EndDrawerButton), matching: find.byType(RichText)),
);
expect(iconText.text.style!.color, Colors.red);
});
testWidgets('EndDrawerButton onPressed overrides default end drawer open behaviour', (
WidgetTester tester,
) async {
var customCallbackWasCalled = false;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(child: EndDrawerButton(onPressed: () => customCallbackWasCalled = true)),
endDrawer: const Drawer(),
),
),
);
await tester.pumpAndSettle();
expect(find.byType(Drawer), findsNothing); // Start off with a closed drawer
expect(customCallbackWasCalled, false); // customCallbackWasCalled should still be false.
await tester.tap(find.byType(EndDrawerButton));
await tester.pumpAndSettle();
// Drawer is still closed
expect(find.byType(Drawer), findsNothing);
// The custom callback is called, setting customCallbackWasCalled to true.
expect(customCallbackWasCalled, true);
});
testWidgets('DrawerButton has expected default mouse cursor on hover', (
WidgetTester tester,
) async {
await tester.pumpWidget(const MaterialApp(home: Material(child: DrawerButton())));
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
await gesture.addPointer(location: const Offset(1000, 1000));
addTearDown(gesture.removePointer);
expect(
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
SystemMouseCursors.basic,
);
await gesture.moveTo(tester.getCenter(find.byType(DrawerButton)));
await tester.pumpAndSettle();
expect(
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
kIsWeb ? SystemMouseCursors.click : SystemMouseCursors.basic,
);
});
testWidgets('EndDrawerButton has expected default mouse cursor on hover', (
WidgetTester tester,
) async {
await tester.pumpWidget(const MaterialApp(home: Material(child: EndDrawerButton())));
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
await gesture.addPointer(location: const Offset(1000, 1000));
addTearDown(gesture.removePointer);
expect(
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
SystemMouseCursors.basic,
);
await gesture.moveTo(tester.getCenter(find.byType(EndDrawerButton)));
await tester.pumpAndSettle();
expect(
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
kIsWeb ? SystemMouseCursors.click : SystemMouseCursors.basic,
);
});
testWidgets('DrawerButton has expected mouse cursor when explicitly configured', (
WidgetTester tester,
) async {
await tester.pumpWidget(
MaterialApp(
home: Material(
child: DrawerButton(
style: ButtonStyle(
mouseCursor: WidgetStateProperty.all<MouseCursor>(SystemMouseCursors.cell),
),
),
),
),
);
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
await gesture.addPointer(location: tester.getCenter(find.byType(DrawerButton)));
addTearDown(gesture.removePointer);
expect(
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
SystemMouseCursors.cell,
);
});
testWidgets('EndDrawerButton has expected mouse cursor when explicitly configured', (
WidgetTester tester,
) async {
await tester.pumpWidget(
MaterialApp(
home: Material(
child: EndDrawerButton(
style: ButtonStyle(
mouseCursor: WidgetStateProperty.all<MouseCursor>(SystemMouseCursors.cell),
),
),
),
),
);
final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
await gesture.addPointer(location: tester.getCenter(find.byType(EndDrawerButton)));
addTearDown(gesture.removePointer);
expect(
RendererBinding.instance.mouseTracker.debugDeviceActiveCursor(1),
SystemMouseCursors.cell,
);
});
testWidgets('DrawerButton does not crash at zero area', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Center(
child: SizedBox.shrink(child: Scaffold(body: DrawerButton())),
),
),
);
expect(tester.getSize(find.byType(DrawerButton)), Size.zero);
});
}