-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test orientation recipe (#11657)
_Description of what this PR is changing or adding, and why:_ Adds a recipe that tests if the orientation of an app is in portrait or landscape mode. _Issues fixed by this PR (if any):_ Fixes: #2005 _PRs or commits this PR depends on (if any):_ ## Presubmit checklist - [x] This PR is marked as draft with an explanation if not meant to land until a future stable release. - [x] This PR doesn’t contain automatically generated corrections (Grammarly or similar). - [x] This PR follows the [Google Developer Documentation Style Guidelines](https://developers.google.com/style) — for example, it doesn’t use _i.e._ or _e.g._, and it avoids _I_ and _we_ (first person). - [x] This PR uses [semantic line breaks](https://github.com/dart-lang/site-shared/blob/main/doc/writing-for-dart-and-flutter-websites.md#semantic-line-breaks) of 80 characters or fewer.
- Loading branch information
Showing
6 changed files
with
412 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
examples/cookbook/testing/widget/orientation_tests/lib/main.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
void main() { | ||
runApp(const MyApp()); | ||
} | ||
|
||
class MyApp extends StatelessWidget { | ||
const MyApp({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
const appTitle = 'Orientation Demo'; | ||
|
||
return const MaterialApp( | ||
title: appTitle, | ||
home: OrientationList( | ||
title: appTitle, | ||
), | ||
); | ||
} | ||
} | ||
|
||
class OrientationList extends StatelessWidget { | ||
final String title; | ||
|
||
const OrientationList({super.key, required this.title}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: Text(title)), | ||
body: OrientationBuilder( | ||
builder: (context, orientation) { | ||
return GridView.count( | ||
// Create a grid with 2 columns in portrait mode, or 3 columns in | ||
// landscape mode. | ||
crossAxisCount: orientation == Orientation.portrait ? 2 : 3, | ||
// Generate 100 widgets that display their index in the List. | ||
children: List.generate(100, (index) { | ||
return Center( | ||
child: Text( | ||
'Item $index', | ||
style: Theme.of(context).textTheme.displayLarge, | ||
), | ||
); | ||
}), | ||
); | ||
}, | ||
), | ||
); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
examples/cookbook/testing/widget/orientation_tests/pubspec.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: orientation_tests | ||
description: Orientation widget testing. | ||
publish_to: none | ||
|
||
resolution: workspace | ||
environment: | ||
sdk: ^3.6.0 | ||
|
||
dependencies: | ||
flutter: | ||
sdk: flutter | ||
path_provider: ^2.1.2 | ||
path: ^1.9.0 | ||
flutter_test: | ||
sdk: flutter | ||
|
||
flutter: | ||
uses-material-design: true |
68 changes: 68 additions & 0 deletions
68
examples/cookbook/testing/widget/orientation_tests/test/widget_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// #docregion OrientationWidgetTest | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:orientation_tests/main.dart'; | ||
|
||
void main() { | ||
group('Orientation', () { | ||
// #docregion portrait-mode-test | ||
// Check if portrait mode displays correctly. | ||
testWidgets('Displays 2 columns in portrait mode', (tester) async { | ||
// Build the app. | ||
await tester.pumpWidget(const MyApp()); | ||
|
||
// Change to portrait. | ||
tester.view.physicalSize = const Size(600, 800); | ||
tester.view.devicePixelRatio = 1.0; | ||
addTearDown(() { | ||
tester.view.resetPhysicalSize(); | ||
}); | ||
await tester.pump(); | ||
|
||
// Verify initial orientation is portrait. | ||
Orientation orientation = | ||
MediaQuery.of(tester.element(find.byType(OrientationList))) | ||
.orientation; | ||
expect(orientation, Orientation.portrait); | ||
|
||
// Verify there are only 2 columns in portrait mode. | ||
final gridViewFinder = find.byType(GridView); | ||
GridView gridView = tester.widget<GridView>(gridViewFinder); | ||
SliverGridDelegateWithFixedCrossAxisCount delegate = | ||
gridView.gridDelegate as SliverGridDelegateWithFixedCrossAxisCount; | ||
expect(delegate.crossAxisCount, 2); | ||
}); | ||
// #enddocregion portrait-mode-test | ||
|
||
// #docregion landscape-mode-test | ||
// Check if landscape mode displays correctly. | ||
testWidgets('Displays 3 columns in landscape mode', (tester) async { | ||
// Build the app. | ||
await tester.pumpWidget(const MyApp()); | ||
|
||
// Change to landscape. | ||
tester.view.physicalSize = const Size(800, 600); | ||
tester.view.devicePixelRatio = 1.0; | ||
addTearDown(() { | ||
tester.view.resetPhysicalSize(); | ||
}); | ||
await tester.pump(); | ||
|
||
// Verify initial orientation is landscape. | ||
Orientation orientation = | ||
MediaQuery.of(tester.element(find.byType(OrientationList))) | ||
.orientation; | ||
expect(orientation, Orientation.landscape); | ||
|
||
// Verify there are only 3 columns in landscape mode. | ||
final gridViewFinder = find.byType(GridView); | ||
GridView gridView = tester.widget<GridView>(gridViewFinder); | ||
SliverGridDelegateWithFixedCrossAxisCount delegate = | ||
gridView.gridDelegate as SliverGridDelegateWithFixedCrossAxisCount; | ||
expect(delegate.crossAxisCount, 3); | ||
}); | ||
// #enddocregion landscape-mode-test | ||
}); | ||
} | ||
// #enddocregion OrientationWidgetTest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.