flutter_flutter/packages/flutter/test/widgets/orientation_builder_test.dart
Tomoo Kikuchi 35a9f5b6ac
Add DeviceOrientationBuilder widget by MediaQuery orientation (#177437)
<!--
Thanks for filing a pull request!
Reviewers are typically assigned within a week of filing a request.
To learn more about code review, see our documentation on Tree Hygiene:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
-->

 ## Issues Fixed

  Fixes https://github.com/flutter/flutter/issues/177435

On foldable Android devices, `OrientationBuilder` and
`MediaQuery.orientation` can report different orientations.
`OrientationBuilder` calculates orientation from layout constraints
(width vs height),
while `MediaQuery` reports the actual device orientation from the
platform. This PR adds a new `DeviceOrientationBuilder` widget that uses
`MediaQuery.orientationOf()` to ensure consistency with the
  device's actual orientation.

  ## Breaking Changes

No breaking changes. This PR does not modify any existing code in the
flutter/tests repo or require a migration guide.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-11-10 20:44:36 +00:00

254 lines
7.9 KiB
Dart

// 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/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('OrientationBuilder', () {
testWidgets('OrientationBuilder determines orientation from constraints', (
WidgetTester tester,
) async {
Orientation? orientation;
await tester.pumpWidget(
MediaQuery(
data: const MediaQueryData(
size: Size(800.0, 600.0),
// Device orientation is landscape.
),
child: Center(
child: SizedBox(
// Widget constraints are portrait (100 wide, 200 tall).
width: 100.0,
height: 200.0,
child: OrientationBuilder(
builder: (BuildContext context, Orientation o) {
orientation = o;
return Container();
},
),
),
),
),
);
// OrientationBuilder should report portrait because width (100) < height (200).
expect(orientation, Orientation.portrait);
});
testWidgets('OrientationBuilder reports landscape when width > height', (
WidgetTester tester,
) async {
Orientation? orientation;
await tester.pumpWidget(
MediaQuery(
data: const MediaQueryData(size: Size(600.0, 800.0)),
child: Center(
child: SizedBox(
// Widget constraints are landscape (200 wide, 100 tall).
width: 200.0,
height: 100.0,
child: OrientationBuilder(
builder: (BuildContext context, Orientation o) {
orientation = o;
return Container();
},
),
),
),
),
);
// OrientationBuilder should report landscape because width (200) > height (100).
expect(orientation, Orientation.landscape);
});
testWidgets('OrientationBuilder rebuilds when constraints change', (WidgetTester tester) async {
Orientation? orientation;
Widget buildTestWidget({required double width, required double height}) {
return MediaQuery(
data: const MediaQueryData(size: Size(800.0, 600.0)),
child: Center(
child: SizedBox(
width: width,
height: height,
child: OrientationBuilder(
builder: (BuildContext context, Orientation o) {
orientation = o;
return Container();
},
),
),
),
);
}
// First, test portrait orientation.
await tester.pumpWidget(buildTestWidget(width: 100.0, height: 200.0));
expect(orientation, Orientation.portrait);
// Then, test landscape orientation.
await tester.pumpWidget(buildTestWidget(width: 200.0, height: 100.0));
expect(orientation, Orientation.landscape);
});
});
group('DeviceOrientationBuilder', () {
testWidgets('DeviceOrientationBuilder uses MediaQuery orientation', (
WidgetTester tester,
) async {
Orientation? deviceOrientation;
await tester.pumpWidget(
MediaQuery(
data: const MediaQueryData(
size: Size(800.0, 600.0),
// Device is in landscape orientation.
),
child: Center(
child: SizedBox(
// Widget constraints are portrait, but device is landscape
width: 100.0,
height: 200.0,
child: DeviceOrientationBuilder(
builder: (BuildContext context, Orientation o) {
deviceOrientation = o;
return Container();
},
),
),
),
),
);
// DeviceOrientationBuilder should report landscape based on MediaQuery
// even though the widget's constraints are portrait.
expect(deviceOrientation, Orientation.landscape);
});
testWidgets('DeviceOrientationBuilder reports portrait when device is portrait', (
WidgetTester tester,
) async {
Orientation? deviceOrientation;
await tester.pumpWidget(
MediaQuery(
data: const MediaQueryData(
size: Size(600.0, 800.0),
// Device is in portrait orientation.
),
child: Center(
child: SizedBox(
// Widget constraints are landscape, but device is portrait.
width: 200.0,
height: 100.0,
child: DeviceOrientationBuilder(
builder: (BuildContext context, Orientation o) {
deviceOrientation = o;
return Container();
},
),
),
),
),
);
// DeviceOrientationBuilder should report portrait based on MediaQuery
// even though the widget's constraints are landscape.
expect(deviceOrientation, Orientation.portrait);
});
testWidgets('DeviceOrientationBuilder rebuilds when MediaQuery orientation changes', (
WidgetTester tester,
) async {
Orientation? deviceOrientation;
Widget buildTestWidget({required Size size}) {
return MediaQuery(
data: MediaQueryData(size: size),
child: DeviceOrientationBuilder(
builder: (BuildContext context, Orientation o) {
deviceOrientation = o;
return Container();
},
),
);
}
// First, test portrait orientation.
await tester.pumpWidget(buildTestWidget(size: const Size(600.0, 800.0)));
expect(deviceOrientation, Orientation.portrait);
// Then, test landscape orientation.
await tester.pumpWidget(buildTestWidget(size: const Size(800.0, 600.0)));
expect(deviceOrientation, Orientation.landscape);
});
testWidgets('DeviceOrientationBuilder differs from OrientationBuilder', (
WidgetTester tester,
) async {
Orientation? layoutOrientation;
Orientation? deviceOrientation;
await tester.pumpWidget(
MediaQuery(
data: const MediaQueryData(
size: Size(800.0, 600.0),
// Device orientation is landscape.
),
child: Center(
child: SizedBox(
// Widget constraints are portrait.
width: 100.0,
height: 200.0,
child: Column(
children: <Widget>[
Expanded(
child: OrientationBuilder(
builder: (BuildContext context, Orientation o) {
layoutOrientation = o;
return Container();
},
),
),
Expanded(
child: DeviceOrientationBuilder(
builder: (BuildContext context, Orientation o) {
deviceOrientation = o;
return Container();
},
),
),
],
),
),
),
),
);
// This demonstrates the key difference:
// - OrientationBuilder reports based on widget constraints (portrait)
// - DeviceOrientationBuilder reports based on device orientation (landscape)
expect(
layoutOrientation,
Orientation.portrait,
reason: 'OrientationBuilder should use widget constraints',
);
expect(
deviceOrientation,
Orientation.landscape,
reason: 'DeviceOrientationBuilder should use MediaQuery orientation',
);
expect(
layoutOrientation,
isNot(equals(deviceOrientation)),
reason: 'The two builders can report different orientations',
);
});
});
}