flutter_flutter/packages/flutter/test/rendering/aspect_ratio_test.dart
Kate Lovett 9d96df2364
Modernize framework lints (#179089)
WIP

Commits separated as follows:
- Update lints in analysis_options files
- Run `dart fix --apply`
- Clean up leftover analysis issues 
- Run `dart format .` in the right places.

Local analysis and testing passes. Checking CI now.

Part of https://github.com/flutter/flutter/issues/178827
- Adoption of flutter_lints in examples/api coming in a separate change
(cc @loic-sharma)

## Pre-launch Checklist

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

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**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-26 01:10:39 +00:00

202 lines
6.6 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/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'rendering_tester.dart';
void main() {
TestRenderingFlutterBinding.ensureInitialized();
test('RenderAspectRatio: Intrinsic sizing 2.0', () {
final box = RenderAspectRatio(aspectRatio: 2.0);
expect(box.getMinIntrinsicWidth(200.0), 400.0);
expect(box.getMinIntrinsicWidth(400.0), 800.0);
expect(box.getMaxIntrinsicWidth(200.0), 400.0);
expect(box.getMaxIntrinsicWidth(400.0), 800.0);
expect(box.getMinIntrinsicHeight(200.0), 100.0);
expect(box.getMinIntrinsicHeight(400.0), 200.0);
expect(box.getMaxIntrinsicHeight(200.0), 100.0);
expect(box.getMaxIntrinsicHeight(400.0), 200.0);
expect(box.getMinIntrinsicWidth(double.infinity), 0.0);
expect(box.getMaxIntrinsicWidth(double.infinity), 0.0);
expect(box.getMinIntrinsicHeight(double.infinity), 0.0);
expect(box.getMaxIntrinsicHeight(double.infinity), 0.0);
});
test('RenderAspectRatio: Intrinsic sizing 0.5', () {
final box = RenderAspectRatio(aspectRatio: 0.5);
expect(box.getMinIntrinsicWidth(200.0), 100.0);
expect(box.getMinIntrinsicWidth(400.0), 200.0);
expect(box.getMaxIntrinsicWidth(200.0), 100.0);
expect(box.getMaxIntrinsicWidth(400.0), 200.0);
expect(box.getMinIntrinsicHeight(200.0), 400.0);
expect(box.getMinIntrinsicHeight(400.0), 800.0);
expect(box.getMaxIntrinsicHeight(200.0), 400.0);
expect(box.getMaxIntrinsicHeight(400.0), 800.0);
expect(box.getMinIntrinsicWidth(double.infinity), 0.0);
expect(box.getMaxIntrinsicWidth(double.infinity), 0.0);
expect(box.getMinIntrinsicHeight(double.infinity), 0.0);
expect(box.getMaxIntrinsicHeight(double.infinity), 0.0);
});
test('RenderAspectRatio: Intrinsic sizing 2.0', () {
final box = RenderAspectRatio(aspectRatio: 2.0, child: RenderSizedBox(const Size(90.0, 70.0)));
expect(box.getMinIntrinsicWidth(200.0), 400.0);
expect(box.getMinIntrinsicWidth(400.0), 800.0);
expect(box.getMaxIntrinsicWidth(200.0), 400.0);
expect(box.getMaxIntrinsicWidth(400.0), 800.0);
expect(box.getMinIntrinsicHeight(200.0), 100.0);
expect(box.getMinIntrinsicHeight(400.0), 200.0);
expect(box.getMaxIntrinsicHeight(200.0), 100.0);
expect(box.getMaxIntrinsicHeight(400.0), 200.0);
expect(box.getMinIntrinsicWidth(double.infinity), 90.0);
expect(box.getMaxIntrinsicWidth(double.infinity), 90.0);
expect(box.getMinIntrinsicHeight(double.infinity), 70.0);
expect(box.getMaxIntrinsicHeight(double.infinity), 70.0);
});
test('RenderAspectRatio: Intrinsic sizing 0.5', () {
final box = RenderAspectRatio(aspectRatio: 0.5, child: RenderSizedBox(const Size(90.0, 70.0)));
expect(box.getMinIntrinsicWidth(200.0), 100.0);
expect(box.getMinIntrinsicWidth(400.0), 200.0);
expect(box.getMaxIntrinsicWidth(200.0), 100.0);
expect(box.getMaxIntrinsicWidth(400.0), 200.0);
expect(box.getMinIntrinsicHeight(200.0), 400.0);
expect(box.getMinIntrinsicHeight(400.0), 800.0);
expect(box.getMaxIntrinsicHeight(200.0), 400.0);
expect(box.getMaxIntrinsicHeight(400.0), 800.0);
expect(box.getMinIntrinsicWidth(double.infinity), 90.0);
expect(box.getMaxIntrinsicWidth(double.infinity), 90.0);
expect(box.getMinIntrinsicHeight(double.infinity), 70.0);
expect(box.getMaxIntrinsicHeight(double.infinity), 70.0);
});
test('RenderAspectRatio: Unbounded', () {
final RenderBox box = RenderConstrainedOverflowBox(
maxWidth: double.infinity,
maxHeight: double.infinity,
child: RenderAspectRatio(aspectRatio: 0.5, child: RenderSizedBox(const Size(90.0, 70.0))),
);
final errors = <FlutterErrorDetails>[];
layout(
box,
onErrors: () {
errors.addAll(TestRenderingFlutterBinding.instance.takeAllFlutterErrorDetails());
},
);
expect(errors, hasLength(2));
expect(errors.first.exception, isFlutterError);
expect(
(errors.first.exception as FlutterError).toStringDeep(),
'FlutterError\n'
' RenderAspectRatio has unbounded constraints.\n'
' This RenderAspectRatio was given an aspect ratio of 0.5 but was\n'
' given both unbounded width and unbounded height constraints.\n'
' Because both constraints were unbounded, this render object\n'
" doesn't know how much size to consume.\n",
);
// The second error message is a generic message generated by the Dart VM. Not worth testing.
});
test('RenderAspectRatio: Sizing', () {
RenderConstrainedOverflowBox outside;
RenderAspectRatio inside;
layout(
outside = RenderConstrainedOverflowBox(child: inside = RenderAspectRatio(aspectRatio: 1.0)),
);
pumpFrame();
expect(inside.size, const Size(800.0, 600.0));
outside.minWidth = 0.0;
outside.minHeight = 0.0;
outside.maxWidth = 100.0;
outside.maxHeight = 90.0;
pumpFrame();
expect(inside.size, const Size(90.0, 90.0));
outside.maxWidth = 90.0;
outside.maxHeight = 100.0;
pumpFrame();
expect(inside.size, const Size(90.0, 90.0));
outside.maxWidth = double.infinity;
outside.maxHeight = 90.0;
pumpFrame();
expect(inside.size, const Size(90.0, 90.0));
outside.maxWidth = 90.0;
outside.maxHeight = double.infinity;
pumpFrame();
expect(inside.size, const Size(90.0, 90.0));
inside.aspectRatio = 2.0;
outside.maxWidth = 100.0;
outside.maxHeight = 90.0;
pumpFrame();
expect(inside.size, const Size(100.0, 50.0));
outside.maxWidth = 90.0;
outside.maxHeight = 100.0;
pumpFrame();
expect(inside.size, const Size(90.0, 45.0));
outside.maxWidth = double.infinity;
outside.maxHeight = 90.0;
pumpFrame();
expect(inside.size, const Size(180.0, 90.0));
outside.maxWidth = 90.0;
outside.maxHeight = double.infinity;
pumpFrame();
expect(inside.size, const Size(90.0, 45.0));
outside.minWidth = 80.0;
outside.minHeight = 80.0;
outside.maxWidth = 100.0;
outside.maxHeight = 90.0;
pumpFrame();
expect(inside.size, const Size(100.0, 80.0));
outside.maxWidth = 90.0;
outside.maxHeight = 100.0;
pumpFrame();
expect(inside.size, const Size(90.0, 80.0));
outside.maxWidth = double.infinity;
outside.maxHeight = 90.0;
pumpFrame();
expect(inside.size, const Size(180.0, 90.0));
outside.maxWidth = 90.0;
outside.maxHeight = double.infinity;
pumpFrame();
expect(inside.size, const Size(90.0, 80.0));
});
}