Skip to content

Commit 61a368c

Browse files
authored
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.
1 parent 10d98eb commit 61a368c

File tree

6 files changed

+412
-0
lines changed

6 files changed

+412
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import 'package:flutter/material.dart';
2+
3+
void main() {
4+
runApp(const MyApp());
5+
}
6+
7+
class MyApp extends StatelessWidget {
8+
const MyApp({super.key});
9+
10+
@override
11+
Widget build(BuildContext context) {
12+
const appTitle = 'Orientation Demo';
13+
14+
return const MaterialApp(
15+
title: appTitle,
16+
home: OrientationList(
17+
title: appTitle,
18+
),
19+
);
20+
}
21+
}
22+
23+
class OrientationList extends StatelessWidget {
24+
final String title;
25+
26+
const OrientationList({super.key, required this.title});
27+
28+
@override
29+
Widget build(BuildContext context) {
30+
return Scaffold(
31+
appBar: AppBar(title: Text(title)),
32+
body: OrientationBuilder(
33+
builder: (context, orientation) {
34+
return GridView.count(
35+
// Create a grid with 2 columns in portrait mode, or 3 columns in
36+
// landscape mode.
37+
crossAxisCount: orientation == Orientation.portrait ? 2 : 3,
38+
// Generate 100 widgets that display their index in the List.
39+
children: List.generate(100, (index) {
40+
return Center(
41+
child: Text(
42+
'Item $index',
43+
style: Theme.of(context).textTheme.displayLarge,
44+
),
45+
);
46+
}),
47+
);
48+
},
49+
),
50+
);
51+
}
52+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: orientation_tests
2+
description: Orientation widget testing.
3+
publish_to: none
4+
5+
resolution: workspace
6+
environment:
7+
sdk: ^3.6.0
8+
9+
dependencies:
10+
flutter:
11+
sdk: flutter
12+
path_provider: ^2.1.2
13+
path: ^1.9.0
14+
flutter_test:
15+
sdk: flutter
16+
17+
flutter:
18+
uses-material-design: true
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// #docregion OrientationWidgetTest
2+
3+
import 'package:flutter/material.dart';
4+
import 'package:flutter_test/flutter_test.dart';
5+
import 'package:orientation_tests/main.dart';
6+
7+
void main() {
8+
group('Orientation', () {
9+
// #docregion portrait-mode-test
10+
// Check if portrait mode displays correctly.
11+
testWidgets('Displays 2 columns in portrait mode', (tester) async {
12+
// Build the app.
13+
await tester.pumpWidget(const MyApp());
14+
15+
// Change to portrait.
16+
tester.view.physicalSize = const Size(600, 800);
17+
tester.view.devicePixelRatio = 1.0;
18+
addTearDown(() {
19+
tester.view.resetPhysicalSize();
20+
});
21+
await tester.pump();
22+
23+
// Verify initial orientation is portrait.
24+
Orientation orientation =
25+
MediaQuery.of(tester.element(find.byType(OrientationList)))
26+
.orientation;
27+
expect(orientation, Orientation.portrait);
28+
29+
// Verify there are only 2 columns in portrait mode.
30+
final gridViewFinder = find.byType(GridView);
31+
GridView gridView = tester.widget<GridView>(gridViewFinder);
32+
SliverGridDelegateWithFixedCrossAxisCount delegate =
33+
gridView.gridDelegate as SliverGridDelegateWithFixedCrossAxisCount;
34+
expect(delegate.crossAxisCount, 2);
35+
});
36+
// #enddocregion portrait-mode-test
37+
38+
// #docregion landscape-mode-test
39+
// Check if landscape mode displays correctly.
40+
testWidgets('Displays 3 columns in landscape mode', (tester) async {
41+
// Build the app.
42+
await tester.pumpWidget(const MyApp());
43+
44+
// Change to landscape.
45+
tester.view.physicalSize = const Size(800, 600);
46+
tester.view.devicePixelRatio = 1.0;
47+
addTearDown(() {
48+
tester.view.resetPhysicalSize();
49+
});
50+
await tester.pump();
51+
52+
// Verify initial orientation is landscape.
53+
Orientation orientation =
54+
MediaQuery.of(tester.element(find.byType(OrientationList)))
55+
.orientation;
56+
expect(orientation, Orientation.landscape);
57+
58+
// Verify there are only 3 columns in landscape mode.
59+
final gridViewFinder = find.byType(GridView);
60+
GridView gridView = tester.widget<GridView>(gridViewFinder);
61+
SliverGridDelegateWithFixedCrossAxisCount delegate =
62+
gridView.gridDelegate as SliverGridDelegateWithFixedCrossAxisCount;
63+
expect(delegate.crossAxisCount, 3);
64+
});
65+
// #enddocregion landscape-mode-test
66+
});
67+
}
68+
// #enddocregion OrientationWidgetTest

examples/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ workspace:
9191
- cookbook/testing/unit/mocking
9292
- cookbook/testing/widget/finders
9393
- cookbook/testing/widget/introduction
94+
- cookbook/testing/widget/orientation_tests
9495
- cookbook/testing/widget/scrolling
9596
- cookbook/testing/widget/tap_drag
9697
- data-and-backend/json

src/content/cookbook/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,5 @@ reference to help you build up an application.
128128
- [An introduction to widget testing](/cookbook/testing/widget/introduction)
129129
- [Find widgets](/cookbook/testing/widget/finders)
130130
- [Handle scrolling](/cookbook/testing/widget/scrolling)
131+
- [Test the orientation of an app](/cookbook/testing/widget/orientation)
131132
- [Tap, drag, and enter text](/cookbook/testing/widget/tap-drag)

0 commit comments

Comments
 (0)