flutter_flutter/dev/tools/vitool/test/vitool_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

628 lines
20 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 'dart:math';
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart' as path;
import 'package:vitool/vitool.dart';
void main() {
test('parsePixels', () {
expect(parsePixels('23px'), 23);
expect(parsePixels('9px'), 9);
expect(() {
parsePixels('9pt');
}, throwsArgumentError);
});
test('parsePoints', () {
expect(parsePoints('1.0, 2.0'), const <Point<double>>[Point<double>(1.0, 2.0)]);
expect(parsePoints('12.0, 34.0 5.0, 6.6'), const <Point<double>>[
Point<double>(12.0, 34.0),
Point<double>(5.0, 6.6),
]);
expect(parsePoints('12.0 34.0 5.0 6.6'), const <Point<double>>[
Point<double>(12.0, 34.0),
Point<double>(5.0, 6.6),
]);
});
group('parseSvg', () {
test('empty SVGs', () {
interpretSvg(testAsset('empty_svg_1_48x48.svg'));
interpretSvg(testAsset('empty_svg_2_100x50.svg'));
});
test('illegal SVGs', () {
expect(() {
interpretSvg(testAsset('illegal_svg_multiple_roots.svg'));
}, throwsA(anything));
});
test('SVG size', () {
expect(
interpretSvg(testAsset('empty_svg_1_48x48.svg')).size,
const Point<double>(48.0, 48.0),
);
expect(
interpretSvg(testAsset('empty_svg_2_100x50.svg')).size,
const Point<double>(100.0, 50.0),
);
});
test('horizontal bar', () {
final FrameData frameData = interpretSvg(testAsset('horizontal_bar.svg'));
expect(frameData.paths, <SvgPath>[
const SvgPath('path_1', <SvgPathCommand>[
SvgPathCommand('M', <Point<double>>[Point<double>(0.0, 19.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(48.0, 19.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(48.0, 29.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(0.0, 29.0)]),
SvgPathCommand('Z', <Point<double>>[]),
]),
]);
});
test('leading space path command', () {
interpretSvg(testAsset('leading_space_path_command.svg'));
});
test('SVG illegal path', () {
expect(() {
interpretSvg(testAsset('illegal_path.svg'));
}, throwsA(anything));
});
test('SVG group', () {
final FrameData frameData = interpretSvg(testAsset('bars_group.svg'));
expect(frameData.paths, const <SvgPath>[
SvgPath('path_1', <SvgPathCommand>[
SvgPathCommand('M', <Point<double>>[Point<double>(0.0, 19.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(48.0, 19.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(48.0, 29.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(0.0, 29.0)]),
SvgPathCommand('Z', <Point<double>>[]),
]),
SvgPath('path_2', <SvgPathCommand>[
SvgPathCommand('M', <Point<double>>[Point<double>(0.0, 34.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(48.0, 34.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(48.0, 44.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(0.0, 44.0)]),
SvgPathCommand('Z', <Point<double>>[]),
]),
]);
});
test('SVG group translate', () {
final FrameData frameData = interpretSvg(testAsset('bar_group_translate.svg'));
expect(frameData.paths, const <SvgPath>[
SvgPath('path_1', <SvgPathCommand>[
SvgPathCommand('M', <Point<double>>[Point<double>(0.0, 34.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(48.0, 34.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(48.0, 44.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(0.0, 44.0)]),
SvgPathCommand('Z', <Point<double>>[]),
]),
]);
});
test('SVG group scale', () {
final FrameData frameData = interpretSvg(testAsset('bar_group_scale.svg'));
expect(frameData.paths, const <SvgPath>[
SvgPath('path_1', <SvgPathCommand>[
SvgPathCommand('M', <Point<double>>[Point<double>(0.0, 9.5)]),
SvgPathCommand('L', <Point<double>>[Point<double>(24.0, 9.5)]),
SvgPathCommand('L', <Point<double>>[Point<double>(24.0, 14.5)]),
SvgPathCommand('L', <Point<double>>[Point<double>(0.0, 14.5)]),
SvgPathCommand('Z', <Point<double>>[]),
]),
]);
});
test('SVG group rotate scale', () {
final FrameData frameData = interpretSvg(testAsset('bar_group_rotate_scale.svg'));
expect(frameData.paths, const <PathMatcher>[
PathMatcher(
SvgPath('path_1', <SvgPathCommand>[
SvgPathCommand('L', <Point<double>>[Point<double>(29.0, 0.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(29.0, 48.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(19.0, 48.0)]),
SvgPathCommand('M', <Point<double>>[Point<double>(19.0, 0.0)]),
SvgPathCommand('Z', <Point<double>>[]),
]),
margin: precisionErrorTolerance,
),
]);
});
test('SVG illegal transform', () {
expect(() {
interpretSvg(testAsset('illegal_transform.svg'));
}, throwsA(anything));
});
test('SVG group opacity', () {
final FrameData frameData = interpretSvg(testAsset('bar_group_opacity.svg'));
expect(frameData.paths, const <SvgPath>[
SvgPath('path_1', <SvgPathCommand>[
SvgPathCommand('M', <Point<double>>[Point<double>(0.0, 19.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(48.0, 19.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(48.0, 29.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(0.0, 29.0)]),
SvgPathCommand('Z', <Point<double>>[]),
], opacity: 0.5),
]);
});
test('horizontal bar relative', () {
// This asset uses the relative 'l' command instead of 'L'.
final FrameData frameData = interpretSvg(testAsset('horizontal_bar_relative.svg'));
expect(frameData.paths, const <SvgPath>[
SvgPath('path_1', <SvgPathCommand>[
SvgPathCommand('M', <Point<double>>[Point<double>(0.0, 19.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(48.0, 19.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(48.0, 29.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(0.0, 29.0)]),
SvgPathCommand('Z', <Point<double>>[]),
]),
]);
});
test('close in middle of path', () {
// This asset uses the relative 'l' command instead of 'L'.
final FrameData frameData = interpretSvg(testAsset('close_path_in_middle.svg'));
expect(frameData.paths, const <SvgPath>[
SvgPath('path_1', <SvgPathCommand>[
SvgPathCommand('M', <Point<double>>[Point<double>(50.0, 50.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(60.0, 50.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(60.0, 60.0)]),
SvgPathCommand('Z', <Point<double>>[]),
SvgPathCommand('L', <Point<double>>[Point<double>(50.0, 40.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(40.0, 40.0)]),
SvgPathCommand('Z', <Point<double>>[]),
]),
]);
});
});
group('create PathAnimation', () {
test('single path', () {
const frameData = <FrameData>[
FrameData(Point<double>(10.0, 10.0), <SvgPath>[
SvgPath('path_1', <SvgPathCommand>[
SvgPathCommand('M', <Point<double>>[Point<double>(0.0, 0.0)]),
SvgPathCommand('L', <Point<double>>[Point<double>(10.0, 10.0)]),
]),
]),
];
expect(
PathAnimation.fromFrameData(frameData, 0),
const PathAnimationMatcher(
PathAnimation(
<PathCommandAnimation>[
PathCommandAnimation('M', <List<Point<double>>>[
<Point<double>>[Point<double>(0.0, 0.0)],
]),
PathCommandAnimation('L', <List<Point<double>>>[
<Point<double>>[Point<double>(10.0, 10.0)],
]),
],
opacities: <double>[1.0],
),
),
);
});
test('multiple paths', () {
const frameData = <FrameData>[
FrameData(Point<double>(10.0, 10.0), <SvgPath>[
SvgPath('path_1', <SvgPathCommand>[
SvgPathCommand('M', <Point<double>>[Point<double>(0.0, 0.0)]),
]),
SvgPath('path_2', <SvgPathCommand>[
SvgPathCommand('M', <Point<double>>[Point<double>(5.0, 6.0)]),
]),
]),
];
expect(
PathAnimation.fromFrameData(frameData, 0),
const PathAnimationMatcher(
PathAnimation(
<PathCommandAnimation>[
PathCommandAnimation('M', <List<Point<double>>>[
<Point<double>>[Point<double>(0.0, 0.0)],
]),
],
opacities: <double>[1.0],
),
),
);
expect(
PathAnimation.fromFrameData(frameData, 1),
const PathAnimationMatcher(
PathAnimation(
<PathCommandAnimation>[
PathCommandAnimation('M', <List<Point<double>>>[
<Point<double>>[Point<double>(5.0, 6.0)],
]),
],
opacities: <double>[1.0],
),
),
);
});
test('multiple frames', () {
const frameData = <FrameData>[
FrameData(Point<double>(10.0, 10.0), <SvgPath>[
SvgPath('path_1', <SvgPathCommand>[
SvgPathCommand('M', <Point<double>>[Point<double>(0.0, 0.0)]),
], opacity: 0.5),
]),
FrameData(Point<double>(10.0, 10.0), <SvgPath>[
SvgPath('path_1', <SvgPathCommand>[
SvgPathCommand('M', <Point<double>>[Point<double>(10.0, 10.0)]),
]),
]),
];
expect(
PathAnimation.fromFrameData(frameData, 0),
const PathAnimationMatcher(
PathAnimation(
<PathCommandAnimation>[
PathCommandAnimation('M', <List<Point<double>>>[
<Point<double>>[Point<double>(0.0, 0.0), Point<double>(10.0, 10.0)],
]),
],
opacities: <double>[0.5, 1.0],
),
),
);
});
});
group('create Animation', () {
test('multiple paths', () {
const frameData = <FrameData>[
FrameData(Point<double>(10.0, 10.0), <SvgPath>[
SvgPath('path_1', <SvgPathCommand>[
SvgPathCommand('M', <Point<double>>[Point<double>(0.0, 0.0)]),
]),
SvgPath('path_1', <SvgPathCommand>[
SvgPathCommand('M', <Point<double>>[Point<double>(5.0, 6.0)]),
]),
]),
];
final animation = Animation.fromFrameData(frameData);
expect(
animation.paths[0],
const PathAnimationMatcher(
PathAnimation(
<PathCommandAnimation>[
PathCommandAnimation('M', <List<Point<double>>>[
<Point<double>>[Point<double>(0.0, 0.0)],
]),
],
opacities: <double>[1.0],
),
),
);
expect(
animation.paths[1],
const PathAnimationMatcher(
PathAnimation(
<PathCommandAnimation>[
PathCommandAnimation('M', <List<Point<double>>>[
<Point<double>>[Point<double>(5.0, 6.0)],
]),
],
opacities: <double>[1.0],
),
),
);
expect(animation.size, const Point<double>(10.0, 10.0));
});
});
group('toDart', () {
test('_PathMoveTo', () {
const command = PathCommandAnimation('M', <List<Point<double>>>[
<Point<double>>[Point<double>(1.0, 2.0), Point<double>(3.0, 4.0)],
]);
expect(
command.toDart(),
' const _PathMoveTo(\n'
' const <Offset>[\n'
' const Offset(1.0, 2.0),\n'
' const Offset(3.0, 4.0),\n'
' ],\n'
' ),\n',
);
});
test('_PathLineTo', () {
const command = PathCommandAnimation('L', <List<Point<double>>>[
<Point<double>>[Point<double>(1.0, 2.0), Point<double>(3.0, 4.0)],
]);
expect(
command.toDart(),
' const _PathLineTo(\n'
' const <Offset>[\n'
' const Offset(1.0, 2.0),\n'
' const Offset(3.0, 4.0),\n'
' ],\n'
' ),\n',
);
});
test('_PathCubicTo', () {
const command = PathCommandAnimation('C', <List<Point<double>>>[
<Point<double>>[Point<double>(16.0, 24.0), Point<double>(16.0, 10.0)],
<Point<double>>[Point<double>(16.0, 25.0), Point<double>(16.0, 11.0)],
<Point<double>>[Point<double>(40.0, 40.0), Point<double>(40.0, 40.0)],
]);
expect(
command.toDart(),
' const _PathCubicTo(\n'
' const <Offset>[\n'
' const Offset(16.0, 24.0),\n'
' const Offset(16.0, 10.0),\n'
' ],\n'
' const <Offset>[\n'
' const Offset(16.0, 25.0),\n'
' const Offset(16.0, 11.0),\n'
' ],\n'
' const <Offset>[\n'
' const Offset(40.0, 40.0),\n'
' const Offset(40.0, 40.0),\n'
' ],\n'
' ),\n',
);
});
test('_PathClose', () {
const command = PathCommandAnimation('Z', <List<Point<double>>>[]);
expect(
command.toDart(),
' const _PathClose(\n'
' ),\n',
);
});
test('Unsupported path command', () {
const command = PathCommandAnimation('h', <List<Point<double>>>[]);
expect(() {
command.toDart();
}, throwsA(anything));
});
test('_PathFrames', () {
const pathAnimation = PathAnimation(
<PathCommandAnimation>[
PathCommandAnimation('M', <List<Point<double>>>[
<Point<double>>[Point<double>(0.0, 0.0), Point<double>(10.0, 10.0)],
]),
PathCommandAnimation('L', <List<Point<double>>>[
<Point<double>>[Point<double>(48.0, 10.0), Point<double>(0.0, 0.0)],
]),
],
opacities: <double>[0.5, 1.0],
);
expect(
pathAnimation.toDart(),
' const _PathFrames(\n'
' opacities: const <double>[\n'
' 0.5,\n'
' 1.0,\n'
' ],\n'
' commands: const <_PathCommand>[\n'
' const _PathMoveTo(\n'
' const <Offset>[\n'
' const Offset(0.0, 0.0),\n'
' const Offset(10.0, 10.0),\n'
' ],\n'
' ),\n'
' const _PathLineTo(\n'
' const <Offset>[\n'
' const Offset(48.0, 10.0),\n'
' const Offset(0.0, 0.0),\n'
' ],\n'
' ),\n'
' ],\n'
' ),\n',
);
});
test('Animation', () {
const animation = Animation(Point<double>(48.0, 48.0), <PathAnimation>[
PathAnimation(
<PathCommandAnimation>[
PathCommandAnimation('M', <List<Point<double>>>[
<Point<double>>[Point<double>(0.0, 0.0), Point<double>(10.0, 10.0)],
]),
PathCommandAnimation('L', <List<Point<double>>>[
<Point<double>>[Point<double>(48.0, 10.0), Point<double>(0.0, 0.0)],
]),
],
opacities: <double>[0.5, 1.0],
),
PathAnimation(
<PathCommandAnimation>[
PathCommandAnimation('M', <List<Point<double>>>[
<Point<double>>[Point<double>(0.0, 0.0), Point<double>(10.0, 10.0)],
]),
],
opacities: <double>[0.5, 1.0],
),
]);
expect(
animation.toDart('_AnimatedIconData', r'_$data1'),
'const _AnimatedIconData _\$data1 = const _AnimatedIconData(\n'
' const Size(48.0, 48.0),\n'
' const <_PathFrames>[\n'
' const _PathFrames(\n'
' opacities: const <double>[\n'
' 0.5,\n'
' 1.0,\n'
' ],\n'
' commands: const <_PathCommand>[\n'
' const _PathMoveTo(\n'
' const <Offset>[\n'
' const Offset(0.0, 0.0),\n'
' const Offset(10.0, 10.0),\n'
' ],\n'
' ),\n'
' const _PathLineTo(\n'
' const <Offset>[\n'
' const Offset(48.0, 10.0),\n'
' const Offset(0.0, 0.0),\n'
' ],\n'
' ),\n'
' ],\n'
' ),\n'
' const _PathFrames(\n'
' opacities: const <double>[\n'
' 0.5,\n'
' 1.0,\n'
' ],\n'
' commands: const <_PathCommand>[\n'
' const _PathMoveTo(\n'
' const <Offset>[\n'
' const Offset(0.0, 0.0),\n'
' const Offset(10.0, 10.0),\n'
' ],\n'
' ),\n'
' ],\n'
' ),\n'
' ],\n'
');',
);
});
});
}
// Matches all path commands' points within an error margin.
class PathMatcher extends Matcher {
const PathMatcher(this.actual, {this.margin = 0.0});
final SvgPath actual;
final double margin;
@override
Description describe(Description description) => description.add('$actual$margin)');
@override
bool matches(dynamic item, Map<dynamic, dynamic> matchState) {
if (item == null) {
return item == actual;
}
if (item.runtimeType != actual.runtimeType) {
return false;
}
final other = item as SvgPath;
if (other.id != actual.id || other.opacity != actual.opacity) {
return false;
}
if (other.commands.length != actual.commands.length) {
return false;
}
for (var i = 0; i < other.commands.length; i += 1) {
if (!commandsMatch(actual.commands[i], other.commands[i])) {
return false;
}
}
return true;
}
bool commandsMatch(SvgPathCommand actual, SvgPathCommand other) {
if (other.points.length != actual.points.length) {
return false;
}
for (var i = 0; i < other.points.length; i += 1) {
if ((other.points[i].x - actual.points[i].x).abs() > margin) {
return false;
}
if ((other.points[i].y - actual.points[i].y).abs() > margin) {
return false;
}
}
return true;
}
}
class PathAnimationMatcher extends Matcher {
const PathAnimationMatcher(this.expected);
final PathAnimation expected;
@override
Description describe(Description description) => description.add('$expected');
@override
bool matches(dynamic item, Map<dynamic, dynamic> matchState) {
if (item == null) {
return item == expected;
}
if (item.runtimeType != expected.runtimeType) {
return false;
}
final other = item as PathAnimation;
if (!const ListEquality<double>().equals(other.opacities, expected.opacities)) {
return false;
}
if (other.commands.length != expected.commands.length) {
return false;
}
for (var i = 0; i < other.commands.length; i += 1) {
if (!commandsMatch(expected.commands[i], other.commands[i])) {
return false;
}
}
return true;
}
bool commandsMatch(PathCommandAnimation expected, PathCommandAnimation other) {
if (other.points.length != expected.points.length) {
return false;
}
for (var i = 0; i < other.points.length; i += 1) {
if (!const ListEquality<Point<double>>().equals(other.points[i], expected.points[i])) {
return false;
}
}
return true;
}
}
String testAsset(String name) {
return path.join('test_assets', name);
}