Skip to content

Cupertino icons golden tests #7421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ packages/web_benchmarks/** @yjbanov
packages/webview_flutter/** @bparrishMines
packages/xdg_directories/** @stuartmorgan
third_party/packages/cupertino_icons/** @MitchellGoodwin
third_party/packages/cupertino_icons/test/goldens/** @LongCatIsLooong

# Plugin platform implementation rules. These should stay last, since the last
# matching entry takes precedence.
Expand Down
2 changes: 2 additions & 0 deletions third_party/packages/cupertino_icons/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ environment:
sdk: ^3.3.0

dev_dependencies:
collection: ^1.18.0
flutter:
sdk: flutter
flutter_test:
sdk: flutter
path: ^1.9.0

flutter:
fonts:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2013 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:io' show File, Platform;

import 'package:collection/collection.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart' as path;

import 'icons_list.dart';

// The EM of the font is 512. Keep this a power of 2 for fixed-point arithmetic.
const double iconSize = 128.0;
const int iconsPerRow = 5;
const int iconsPerCol = 5;
const int iconsPerImage = iconsPerRow * iconsPerCol;

void main() async {
// Do not run on web since this test uses dart:io.
// The golden test runs on Linux only to avoid platform rendering differences.
if (kIsWeb || !Platform.isLinux) {
return;
}
final bool isMainChannel = !Platform.environment.containsKey('CHANNEL')
|| Platform.environment['CHANNEL'] == 'main'
|| Platform.environment['CHANNEL'] == 'master';
// Only test against main to avoid rendering differences between flutter channels.
if (!isMainChannel) {
return;
}
// Load font.
final String effectiveFontFamily = const TextStyle(fontFamily: CupertinoIcons.iconFont, package: CupertinoIcons.iconFontPackage).fontFamily!;
final FontLoader fontLoader = FontLoader(effectiveFontFamily);
final String filePath = path.canonicalize('assets/CupertinoIcons.ttf');
final File file = File(filePath);
fontLoader.addFont(file.readAsBytes().then((Uint8List v) => v.buffer.asByteData()));
await fontLoader.load();

assert(icons.isNotEmpty);
for (int index = 0; index < icons.length;) {
final int groupEndCodePoint = (icons[index].codePoint ~/ iconsPerImage + 1) * iconsPerImage;
final int next = icons.indexWhere((IconData icon) => icon.codePoint >= groupEndCodePoint, index);
final int nextIndex = next < 0 ? icons.length : next;
registerTestForIconGroup(icons.slice(index, nextIndex));
index = nextIndex;
}
}

// Generating goldens for each glyph is very slow. Group the sorted icons
// into codepoint-aligned groups (each of which has a max capacity of
// iconsPerRow * iconsPerCol), so the goldens are easier to review when
// symbols are added or removed.
void registerTestForIconGroup(List<IconData> iconGroup) {
assert(iconGroup.isNotEmpty);
String hexCodePoint(int codePoint) => codePoint.toRadixString(16).toUpperCase().padLeft(4, '0');
final int groupStartCodePoint = (iconGroup.first.codePoint ~/ iconsPerImage) * iconsPerImage;
final String range = 'U+${hexCodePoint(groupStartCodePoint)}-${hexCodePoint(groupStartCodePoint + iconsPerImage - 1)}';

testWidgets('font golden test: $range', (WidgetTester tester) async {
addTearDown(tester.view.reset);
const Size canvasSize = Size(iconSize * iconsPerRow, iconSize * iconsPerCol);
tester.view.physicalSize = canvasSize * tester.view.devicePixelRatio;

const Widget fillerBox = SizedBox.square(dimension: iconSize);
final List<Widget> children = List<Widget>.filled(iconsPerImage, fillerBox);
for (final IconData icon in iconGroup) {
children[icon.codePoint - groupStartCodePoint] = Icon(icon, size: iconSize);
}

final Widget widget = Directionality(
textDirection: TextDirection.ltr,
child: Center(
child: SizedBox(
height: iconSize * iconsPerCol,
width: iconSize * iconsPerRow,
child: RepaintBoundary(child: Wrap(children: children)),
),
),
);
await tester.pumpWidget(widget);
await expectLater(find.byType(Wrap) , matchesGoldenFile('goldens/glyph_$range.png'));
});
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions third_party/packages/cupertino_icons/test/icons_list.dart

Large diffs are not rendered by default.