From 3405f11e5c35f1fce44e8045d90d9ac462990388 Mon Sep 17 00:00:00 2001 From: Valentin Vignal <32538273+ValentinVignal@users.noreply.github.com> Date: Mon, 21 Oct 2024 23:17:14 +0800 Subject: [PATCH] Add test for `flexible_space_bar.0.dart` (#157107) Contributes to https://github.com/flutter/flutter/issues/130459 It adds a test for - `examples/api/test/material/flexible_space_bar/flexible_space_bar.0_test.dart` --- dev/bots/check_code_samples.dart | 1 - .../flexible_space_bar.0.dart | 116 +++++++++--------- .../flexible_space_bar.0_test.dart | 42 +++++++ 3 files changed, 103 insertions(+), 56 deletions(-) create mode 100644 examples/api/test/material/flexible_space_bar/flexible_space_bar.0_test.dart diff --git a/dev/bots/check_code_samples.dart b/dev/bots/check_code_samples.dart index 8616575a730..c1191814be4 100644 --- a/dev/bots/check_code_samples.dart +++ b/dev/bots/check_code_samples.dart @@ -312,7 +312,6 @@ final Set _knownMissingTests = { 'examples/api/test/material/selectable_region/selectable_region.0_test.dart', 'examples/api/test/material/color_scheme/dynamic_content_color.0_test.dart', 'examples/api/test/material/platform_menu_bar/platform_menu_bar.0_test.dart', - 'examples/api/test/material/flexible_space_bar/flexible_space_bar.0_test.dart', 'examples/api/test/material/navigation_rail/navigation_rail.extended_animation.0_test.dart', 'examples/api/test/painting/star_border/star_border.0_test.dart', 'examples/api/test/widgets/navigator/navigator.restorable_push_and_remove_until.0_test.dart', diff --git a/examples/api/lib/material/flexible_space_bar/flexible_space_bar.0.dart b/examples/api/lib/material/flexible_space_bar/flexible_space_bar.0.dart index b1649afa0b9..35d61710141 100644 --- a/examples/api/lib/material/flexible_space_bar/flexible_space_bar.0.dart +++ b/examples/api/lib/material/flexible_space_bar/flexible_space_bar.0.dart @@ -2,77 +2,83 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; /// Flutter code sample for [FlexibleSpaceBar]. -void main() => runApp(const MaterialApp(home: FlexibleSpaceBarExampleApp())); +void main() => runApp(const FlexibleSpaceBarExampleApp()); class FlexibleSpaceBarExampleApp extends StatelessWidget { const FlexibleSpaceBarExampleApp({super.key}); @override Widget build(BuildContext context) { - return Scaffold( - body: CustomScrollView( - physics: const BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()), - slivers: [ - SliverAppBar( - stretch: true, - onStretchTrigger: () { - // Function callback for stretch - return Future.value(); - }, - expandedHeight: 300.0, - flexibleSpace: FlexibleSpaceBar( - stretchModes: const [ - StretchMode.zoomBackground, - StretchMode.blurBackground, - StretchMode.fadeTitle, - ], - centerTitle: true, - title: const Text('Flight Report'), - background: Stack( - fit: StackFit.expand, - children: [ - Image.network( - 'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg', - fit: BoxFit.cover, - ), - const DecoratedBox( - decoration: BoxDecoration( - gradient: LinearGradient( - begin: Alignment(0.0, 0.5), - end: Alignment.center, - colors: [ - Color(0x60000000), - Color(0x00000000), - ], + return MaterialApp( + scrollBehavior: const MaterialScrollBehavior().copyWith( + dragDevices: PointerDeviceKind.values.toSet(), + ), + home: Scaffold( + body: CustomScrollView( + physics: const BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()), + slivers: [ + SliverAppBar( + stretch: true, + onStretchTrigger: () { + // Function callback for stretch + return Future.value(); + }, + expandedHeight: 300.0, + flexibleSpace: FlexibleSpaceBar( + stretchModes: const [ + StretchMode.zoomBackground, + StretchMode.blurBackground, + StretchMode.fadeTitle, + ], + centerTitle: true, + title: const Text('Flight Report'), + background: Stack( + fit: StackFit.expand, + children: [ + Image.network( + 'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl-2.jpg', + fit: BoxFit.cover, + ), + const DecoratedBox( + decoration: BoxDecoration( + gradient: LinearGradient( + begin: Alignment(0.0, 0.5), + end: Alignment.center, + colors: [ + Color(0x60000000), + Color(0x00000000), + ], + ), ), ), + ], + ), + ), + ), + SliverList( + delegate: SliverChildListDelegate( + const [ + ListTile( + leading: Icon(Icons.wb_sunny), + title: Text('Sunday'), + subtitle: Text('sunny, h: 80, l: 65'), ), + ListTile( + leading: Icon(Icons.wb_sunny), + title: Text('Monday'), + subtitle: Text('sunny, h: 80, l: 65'), + ), + // ListTiles++ ], ), ), - ), - SliverList( - delegate: SliverChildListDelegate( - const [ - ListTile( - leading: Icon(Icons.wb_sunny), - title: Text('Sunday'), - subtitle: Text('sunny, h: 80, l: 65'), - ), - ListTile( - leading: Icon(Icons.wb_sunny), - title: Text('Monday'), - subtitle: Text('sunny, h: 80, l: 65'), - ), - // ListTiles++ - ], - ), - ), - ], + ], + ), ), ); } diff --git a/examples/api/test/material/flexible_space_bar/flexible_space_bar.0_test.dart b/examples/api/test/material/flexible_space_bar/flexible_space_bar.0_test.dart new file mode 100644 index 00000000000..82907a4d550 --- /dev/null +++ b/examples/api/test/material/flexible_space_bar/flexible_space_bar.0_test.dart @@ -0,0 +1,42 @@ +// 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:io'; + +import 'package:flutter/material.dart'; +import 'package:flutter_api_samples/material/flexible_space_bar/flexible_space_bar.0.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + // The app being tested loads images via HTTP which the test + // framework defeats by default. + setUpAll(() { + HttpOverrides.global = null; + }); + + testWidgets('The app bar stretches when over-scrolled', (WidgetTester tester) async { + await tester.pumpWidget( + const example.FlexibleSpaceBarExampleApp(), + ); + + expect(find.text('Flight Report'), findsOne); + + expect(find.widgetWithText(ListTile, 'Sunday'), findsOne); + expect(find.widgetWithText(ListTile, 'Monday'), findsOne); + expect(find.text('sunny, h: 80, l: 65'), findsExactly(2)); + expect(find.byIcon(Icons.wb_sunny), findsExactly(2)); + + final Finder appBarContainer = find.byType(Image); + final Size sizeBeforeScroll = tester.getSize(appBarContainer); + final Offset target = tester.getCenter(find.byType(ListTile).first); + final TestGesture gesture = await tester.startGesture(target); + await gesture.moveBy(const Offset(0.0, 100.0)); + await tester.pump(const Duration(milliseconds: 10)); + await gesture.up(); + final Size sizeAfterScroll = tester.getSize(appBarContainer); + + expect(sizeBeforeScroll.height, lessThan(sizeAfterScroll.height)); + // Verifies ScrollBehavior.dragDevices is correctly set. + }, variant: TargetPlatformVariant.all()); +}