From 43bb6186d2543f55457929c5ca8709f180fff7af Mon Sep 17 00:00:00 2001 From: Valentin Vignal <32538273+ValentinVignal@users.noreply.github.com> Date: Fri, 1 Nov 2024 14:41:13 +0800 Subject: [PATCH] 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` --- dev/bots/check_code_samples.dart | 1 - .../notification.0_test.dart | 125 ++++++++++++++++++ 2 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 examples/api/test/widgets/notification_listener/notification.0_test.dart diff --git a/dev/bots/check_code_samples.dart b/dev/bots/check_code_samples.dart index 13118675bd..d42b498bf3 100644 --- a/dev/bots/check_code_samples.dart +++ b/dev/bots/check_code_samples.dart @@ -319,5 +319,4 @@ final Set _knownMissingTests = { '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', }; diff --git a/examples/api/test/widgets/notification_listener/notification.0_test.dart b/examples/api/test/widgets/notification_listener/notification.0_test.dart new file mode 100644 index 0000000000..d8c9f9d2f1 --- /dev/null +++ b/examples/api/test/widgets/notification_listener/notification.0_test.dart @@ -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 logs = []; + 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 [ + '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 logs = []; + 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 [ + 'Scrolling has started', + 'Scrolling has ended', + 'Scrolling has started', + ]), + ); + await tester.pumpAndSettle(); + expect( + logs, + orderedEquals(const [ + '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 [ + 'Scrolling has started', + 'Scrolling has ended', + 'Scrolling has started', + 'Scrolling has ended', + 'Scrolling has started', + ]), + ); + + await tester.pumpAndSettle(); + + expect( + logs, + orderedEquals(const [ + 'Scrolling has started', + 'Scrolling has ended', + 'Scrolling has started', + 'Scrolling has ended', + 'Scrolling has started', + 'Scrolling has ended', + ]), + ); + + debugPrint = originalDebugPrint; + }); +}