From 6d38831fc85fa81dd770e58d15e47296f22dddf5 Mon Sep 17 00:00:00 2001 From: miechoo Date: Wed, 4 Sep 2024 21:46:16 +0200 Subject: [PATCH] Test of AppBarMediumApp and AppBarLargeApp (#153973) Part of https://github.com/flutter/flutter/issues/130459 This are tests of snippets used in [SliverAppBar.medium const constructor](https://api.flutter.dev/flutter/material/SliverAppBar/SliverAppBar.medium.html) and [SliverAppBar.large const constructor](https://api.flutter.dev/flutter/material/SliverAppBar/SliverAppBar.large.html) Flutter API reference documentation. The only way I've found to distinguish the SliverAppBar.medium and SliverAppBar.large constructors from the regular SliverAppBar is to check if the title is styled according to the [Material Design 3](https://m3.material.io/components/top-app-bar/specs) specification. --- dev/bots/check_code_samples.dart | 2 - .../app_bar/sliver_app_bar.2_test.dart | 42 +++++++++++++++++++ .../app_bar/sliver_app_bar.3_test.dart | 42 +++++++++++++++++++ 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 examples/api/test/material/app_bar/sliver_app_bar.2_test.dart create mode 100644 examples/api/test/material/app_bar/sliver_app_bar.3_test.dart diff --git a/dev/bots/check_code_samples.dart b/dev/bots/check_code_samples.dart index 635c2bc614..c0708f5185 100644 --- a/dev/bots/check_code_samples.dart +++ b/dev/bots/check_code_samples.dart @@ -319,8 +319,6 @@ final Set _knownMissingTests = { '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/app_bar/sliver_app_bar.2_test.dart', - 'examples/api/test/material/app_bar/sliver_app_bar.3_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/test/material/app_bar/sliver_app_bar.2_test.dart b/examples/api/test/material/app_bar/sliver_app_bar.2_test.dart new file mode 100644 index 0000000000..54addf6e2c --- /dev/null +++ b/examples/api/test/material/app_bar/sliver_app_bar.2_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 'package:flutter/material.dart'; +import 'package:flutter_api_samples/material/app_bar/sliver_app_bar.2.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('Visibility and interaction of crucial widgets', (WidgetTester tester) async { + await tester.pumpWidget(const example.AppBarMediumApp()); + + const String title = 'Medium App Bar'; + + expect(find.descendant( + of: find.byType(CustomScrollView), + matching: find.widgetWithText(SliverAppBar, title), + ), findsOne); + + expect(find.descendant( + of: find.byType(SliverAppBar), + matching: find.byType(IconButton), + ), findsExactly(2)); + + // Based on https://m3.material.io/components/top-app-bar/specs the title of + // the SliverAppBar.medium widget is formatted with the headlineSmall style. + final BuildContext context = tester.element(find.byType(MaterialApp)); + final TextStyle expectedTitleStyle = Theme.of(context).textTheme.headlineSmall!; + + // There are two Text widgets: expanded and collapsed. The expanded is first. + final Finder titleFinder = find.text(title).first; + final TextStyle actualTitleStyle = DefaultTextStyle.of(tester.element(titleFinder)).style; + + expect(actualTitleStyle, expectedTitleStyle); + + // Scrolling the screen moves the title up. + expect(tester.getBottomLeft(titleFinder).dy, 96.0); + await tester.drag(titleFinder, const Offset(0.0, -200.0)); + await tester.pump(); + expect(tester.getBottomLeft(titleFinder).dy, 48.0); + }); +} diff --git a/examples/api/test/material/app_bar/sliver_app_bar.3_test.dart b/examples/api/test/material/app_bar/sliver_app_bar.3_test.dart new file mode 100644 index 0000000000..ecfd7a1870 --- /dev/null +++ b/examples/api/test/material/app_bar/sliver_app_bar.3_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 'package:flutter/material.dart'; +import 'package:flutter_api_samples/material/app_bar/sliver_app_bar.3.dart' as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('Visibility and interaction of crucial widgets', (WidgetTester tester) async { + await tester.pumpWidget(const example.AppBarLargeApp()); + + const String title = 'Large App Bar'; + + expect(find.descendant( + of: find.byType(CustomScrollView), + matching: find.widgetWithText(SliverAppBar, title), + ), findsOne); + + expect(find.descendant( + of: find.byType(SliverAppBar), + matching: find.byType(IconButton), + ), findsExactly(2)); + + // Based on https://m3.material.io/components/top-app-bar/specs the title of + // the SliverAppBar.large widget is formatted with the headlineMedium style. + final BuildContext context = tester.element(find.byType(MaterialApp)); + final TextStyle expectedTitleStyle = Theme.of(context).textTheme.headlineMedium!; + + // There are two Text widgets: expanded and collapsed. The expanded is first. + final Finder titleFinder = find.text(title).first; + final TextStyle actualTitleStyle = DefaultTextStyle.of(tester.element(titleFinder)).style; + + expect(actualTitleStyle, expectedTitleStyle); + + // Scrolling the screen moves the title up. + expect(tester.getBottomLeft(titleFinder).dy, 124.0); + await tester.drag(titleFinder, const Offset(0.0, -200.0)); + await tester.pump(); + expect(tester.getBottomLeft(titleFinder).dy, 36.0); + }); +}