mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
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
214 lines
6.5 KiB
Dart
214 lines
6.5 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/widgets.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
class TestCustomPainter extends CustomPainter {
|
|
TestCustomPainter({required this.log, this.name});
|
|
|
|
final List<String?> log;
|
|
final String? name;
|
|
|
|
@override
|
|
void paint(Canvas canvas, Size size) {
|
|
log.add(name);
|
|
}
|
|
|
|
@override
|
|
bool shouldRepaint(TestCustomPainter oldPainter) => true;
|
|
}
|
|
|
|
class MockCanvas extends Fake implements Canvas {
|
|
int saveCount = 0;
|
|
int saveCountDelta = 1;
|
|
|
|
@override
|
|
int getSaveCount() {
|
|
return saveCount += saveCountDelta;
|
|
}
|
|
|
|
@override
|
|
void save() {}
|
|
}
|
|
|
|
class MockPaintingContext extends Fake implements PaintingContext {
|
|
@override
|
|
final MockCanvas canvas = MockCanvas();
|
|
}
|
|
|
|
void main() {
|
|
testWidgets('Control test for custom painting', (WidgetTester tester) async {
|
|
final log = <String?>[];
|
|
await tester.pumpWidget(
|
|
CustomPaint(
|
|
painter: TestCustomPainter(log: log, name: 'background'),
|
|
foregroundPainter: TestCustomPainter(log: log, name: 'foreground'),
|
|
child: CustomPaint(
|
|
painter: TestCustomPainter(log: log, name: 'child'),
|
|
),
|
|
),
|
|
);
|
|
|
|
expect(log, equals(<String>['background', 'child', 'foreground']));
|
|
});
|
|
|
|
testWidgets('Throws FlutterError on custom painter incorrect restore/save calls', (
|
|
WidgetTester tester,
|
|
) async {
|
|
final GlobalKey target = GlobalKey();
|
|
final log = <String?>[];
|
|
await tester.pumpWidget(
|
|
CustomPaint(
|
|
key: target,
|
|
isComplex: true,
|
|
painter: TestCustomPainter(log: log),
|
|
),
|
|
);
|
|
final renderCustom = target.currentContext!.findRenderObject()! as RenderCustomPaint;
|
|
final paintingContext = MockPaintingContext();
|
|
final MockCanvas canvas = paintingContext.canvas;
|
|
|
|
FlutterError getError() {
|
|
late FlutterError error;
|
|
try {
|
|
renderCustom.paint(paintingContext, Offset.zero);
|
|
} on FlutterError catch (e) {
|
|
error = e;
|
|
}
|
|
return error;
|
|
}
|
|
|
|
FlutterError error = getError();
|
|
expect(
|
|
error.toStringDeep(),
|
|
equalsIgnoringHashCodes(
|
|
'FlutterError\n'
|
|
' The TestCustomPainter#00000() custom painter called canvas.save()\n'
|
|
' or canvas.saveLayer() at least 1 more time than it called\n'
|
|
' canvas.restore().\n'
|
|
' This leaves the canvas in an inconsistent state and will probably\n'
|
|
' result in a broken display.\n'
|
|
' You must pair each call to save()/saveLayer() with a later\n'
|
|
' matching call to restore().\n',
|
|
),
|
|
);
|
|
|
|
canvas.saveCountDelta = -1;
|
|
error = getError();
|
|
expect(
|
|
error.toStringDeep(),
|
|
equalsIgnoringHashCodes(
|
|
'FlutterError\n'
|
|
' The TestCustomPainter#00000() custom painter called\n'
|
|
' canvas.restore() 1 more time than it called canvas.save() or\n'
|
|
' canvas.saveLayer().\n'
|
|
' This leaves the canvas in an inconsistent state and will result\n'
|
|
' in a broken display.\n'
|
|
' You should only call restore() if you first called save() or\n'
|
|
' saveLayer().\n',
|
|
),
|
|
);
|
|
|
|
canvas.saveCountDelta = 2;
|
|
error = getError();
|
|
expect(error.toStringDeep(), contains('2 more times'));
|
|
|
|
canvas.saveCountDelta = -2;
|
|
error = getError();
|
|
expect(error.toStringDeep(), contains('2 more times'));
|
|
});
|
|
|
|
testWidgets('CustomPaint sizing', (WidgetTester tester) async {
|
|
final GlobalKey target = GlobalKey();
|
|
|
|
await tester.pumpWidget(Center(child: CustomPaint(key: target)));
|
|
expect(target.currentContext!.size, Size.zero);
|
|
|
|
await tester.pumpWidget(
|
|
Center(
|
|
child: CustomPaint(key: target, child: Container()),
|
|
),
|
|
);
|
|
expect(target.currentContext!.size, const Size(800.0, 600.0));
|
|
|
|
await tester.pumpWidget(
|
|
Center(
|
|
child: CustomPaint(key: target, size: const Size(20.0, 20.0)),
|
|
),
|
|
);
|
|
expect(target.currentContext!.size, const Size(20.0, 20.0));
|
|
|
|
await tester.pumpWidget(
|
|
Center(
|
|
child: CustomPaint(key: target, size: const Size(2000.0, 100.0)),
|
|
),
|
|
);
|
|
expect(target.currentContext!.size, const Size(800.0, 100.0));
|
|
|
|
await tester.pumpWidget(
|
|
Center(
|
|
child: CustomPaint(key: target, child: Container()),
|
|
),
|
|
);
|
|
expect(target.currentContext!.size, const Size(800.0, 600.0));
|
|
|
|
await tester.pumpWidget(
|
|
Center(
|
|
child: CustomPaint(key: target, child: const SizedBox.shrink()),
|
|
),
|
|
);
|
|
expect(target.currentContext!.size, Size.zero);
|
|
});
|
|
|
|
testWidgets('Raster cache hints', (WidgetTester tester) async {
|
|
final GlobalKey target = GlobalKey();
|
|
|
|
final log = <String?>[];
|
|
await tester.pumpWidget(
|
|
CustomPaint(
|
|
key: target,
|
|
isComplex: true,
|
|
painter: TestCustomPainter(log: log),
|
|
),
|
|
);
|
|
var renderCustom = target.currentContext!.findRenderObject()! as RenderCustomPaint;
|
|
expect(renderCustom.isComplex, true);
|
|
expect(renderCustom.willChange, false);
|
|
|
|
await tester.pumpWidget(
|
|
CustomPaint(
|
|
key: target,
|
|
willChange: true,
|
|
foregroundPainter: TestCustomPainter(log: log),
|
|
),
|
|
);
|
|
renderCustom = target.currentContext!.findRenderObject()! as RenderCustomPaint;
|
|
expect(renderCustom.isComplex, false);
|
|
expect(renderCustom.willChange, true);
|
|
});
|
|
|
|
test('Raster cache hints cannot be set with null painters', () {
|
|
expect(() => CustomPaint(isComplex: true), throwsAssertionError);
|
|
expect(() => CustomPaint(willChange: true), throwsAssertionError);
|
|
});
|
|
|
|
test('RenderCustomPaint consults preferred size for intrinsics when it has no child', () {
|
|
final inner = RenderCustomPaint(preferredSize: const Size(20, 30));
|
|
expect(inner.getMinIntrinsicWidth(double.infinity), 20);
|
|
expect(inner.getMaxIntrinsicWidth(double.infinity), 20);
|
|
expect(inner.getMinIntrinsicHeight(double.infinity), 30);
|
|
expect(inner.getMaxIntrinsicHeight(double.infinity), 30);
|
|
});
|
|
|
|
test('RenderCustomPaint does not return infinity for its intrinsics', () {
|
|
final inner = RenderCustomPaint(preferredSize: Size.infinite);
|
|
expect(inner.getMinIntrinsicWidth(double.infinity), 0);
|
|
expect(inner.getMaxIntrinsicWidth(double.infinity), 0);
|
|
expect(inner.getMinIntrinsicHeight(double.infinity), 0);
|
|
expect(inner.getMaxIntrinsicHeight(double.infinity), 0);
|
|
});
|
|
}
|