Add test for notification.0.dart (#157909)

Contributes to https://github.com/flutter/flutter/issues/130459

It adds a test for
- `examples/api/lib/widgets/notification_listener/notification.0.dart`
This commit is contained in:
Valentin Vignal 2024-11-01 14:41:13 +08:00 committed by GitHub
parent d836ca5a68
commit 43bb6186d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 125 additions and 1 deletions

View File

@ -319,5 +319,4 @@ final Set<String> _knownMissingTests = <String>{
'examples/api/test/widgets/scrollbar/raw_scrollbar.0_test.dart',
'examples/api/test/widgets/interactive_viewer/interactive_viewer.constrained.0_test.dart',
'examples/api/test/widgets/interactive_viewer/interactive_viewer.transformation_controller.0_test.dart',
'examples/api/test/widgets/notification_listener/notification.0_test.dart',
};

View File

@ -0,0 +1,125 @@
// 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 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_api_samples/widgets/notification_listener/notification.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Shows all elements', (WidgetTester tester) async {
final DebugPrintCallback originalDebugPrint = debugPrint;
debugPrint = (String? message, {int? wrapWidth}) {};
await tester.pumpWidget(
const example.NotificationExampleApp(),
);
expect(find.byType(NestedScrollView), findsOne);
expect(find.widgetWithText(SliverAppBar, 'Notification Sample'), findsOne);
expect(find.byType(TabBarView), findsOne);
expect(find.widgetWithText(Tab, 'Months'), findsOne);
expect(find.widgetWithText(Tab, 'Days'), findsOne);
expect(find.widgetWithText(ListTile, 'January'), findsOne);
expect(find.widgetWithText(ListTile, 'February'), findsOne);
expect(find.widgetWithText(ListTile, 'March'), findsOne);
await tester.tap(find.text('Days'));
await tester.pumpAndSettle();
expect(find.widgetWithText(ListTile, 'Sunday'), findsOne);
expect(find.widgetWithText(ListTile, 'Monday'), findsOne);
expect(find.widgetWithText(ListTile, 'Tuesday'), findsOne);
debugPrint = originalDebugPrint;
});
testWidgets('Scrolling the scroll view triggers the notification', (WidgetTester tester) async {
final DebugPrintCallback originalDebugPrint = debugPrint;
final List<String> logs = <String>[];
debugPrint = (String? message, {int? wrapWidth}) {
logs.add(message!);
};
await tester.pumpWidget(
const example.NotificationExampleApp(),
);
final TestPointer testPointer = TestPointer(1, PointerDeviceKind.mouse);
testPointer.hover(tester.getCenter(find.byType(NestedScrollView)));
await tester.sendEventToBinding(testPointer.scroll(const Offset(0.0, 10)));
expect(
logs,
orderedEquals(const <String>[
'Scrolling has started',
'Scrolling has started',
'Scrolling has ended',
'Scrolling has ended',
]),
);
debugPrint = originalDebugPrint;
});
testWidgets('Changing tabs triggers the notification', (WidgetTester tester) async {
final DebugPrintCallback originalDebugPrint = debugPrint;
final List<String> logs = <String>[];
debugPrint = (String? message, {int? wrapWidth}) {
logs.add(message!);
};
await tester.pumpWidget(
const example.NotificationExampleApp(),
);
final TestPointer testPointer = TestPointer(1, PointerDeviceKind.mouse);
testPointer.hover(tester.getCenter(find.byType(NestedScrollView)));
await tester.sendEventToBinding(testPointer.scroll(const Offset(500, 0)));
expect(
logs,
orderedEquals(const <String>[
'Scrolling has started',
'Scrolling has ended',
'Scrolling has started',
]),
);
await tester.pumpAndSettle();
expect(
logs,
orderedEquals(const <String>[
'Scrolling has started',
'Scrolling has ended',
'Scrolling has started',
'Scrolling has ended',
]),
);
await tester.tap(find.text('Months'));
await tester.pump();
expect(
logs,
orderedEquals(const <String>[
'Scrolling has started',
'Scrolling has ended',
'Scrolling has started',
'Scrolling has ended',
'Scrolling has started',
]),
);
await tester.pumpAndSettle();
expect(
logs,
orderedEquals(const <String>[
'Scrolling has started',
'Scrolling has ended',
'Scrolling has started',
'Scrolling has ended',
'Scrolling has started',
'Scrolling has ended',
]),
);
debugPrint = originalDebugPrint;
});
}