Files
ReelShort/test/mine/mine_layout_alignment_test.dart
T

216 lines
7.0 KiB
Dart

// Author: fruit.com.cn
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fruitshorts/core/theme/app_colors.dart';
import 'package:fruitshorts/core/theme/app_dimens.dart';
import 'package:fruitshorts/core/theme/app_theme.dart';
import 'package:fruitshorts/l10n/app_localizations.dart';
import 'package:fruitshorts/pages/mine/mine_types.dart';
import 'package:fruitshorts/pages/mine/widgets/mine_action_grid.dart';
import 'package:fruitshorts/pages/mine/widgets/mine_menu.dart';
import 'package:fruitshorts/pages/mine/widgets/mine_stat_icon.dart';
const _layoutTolerance = 0.1;
const _minimumVipHeight = 95.0;
const _premiumVipHeightLimit = 120.0;
void main() {
for (final localeTag in ['en', 'id', 'zh']) {
testWidgets('should_keep_vip_compact_when_locale_is_$localeTag', (
tester,
) async {
await _pumpLocalized(
tester,
const SizedBox(width: 342, child: MineVipBanner(onOpen: _noop)),
locale: Locale(localeTag),
);
final cardRect = tester.getRect(find.byKey(const Key('mine-vip')));
final emblemRect = tester.getRect(
find.byKey(const Key('mine-vip-emblem')),
);
expect(emblemRect.width, greaterThanOrEqualTo(52));
expect(
find.descendant(
of: find.byKey(const Key('mine-vip-emblem')),
matching: find.byType(MineVipEmblem),
),
findsOneWidget,
);
final titleRect = tester.getRect(
find.byKey(const Key('mine-vip-benefit-0')),
);
final brand = tester.widget<Text>(
find.byKey(const Key('mine-vip-brand')),
);
expect(brand.style?.fontSize, greaterThanOrEqualTo(20));
final benefitsRect = tester.getRect(
find.byKey(const Key('mine-vip-benefits')),
);
expect(cardRect.height, lessThan(_premiumVipHeightLimit));
expect(cardRect.height, greaterThanOrEqualTo(_minimumVipHeight));
expect(
emblemRect.top - cardRect.top,
greaterThanOrEqualTo(AppDimens.mineVipPadding),
);
expect(benefitsRect.bottom, lessThan(cardRect.bottom));
expect(emblemRect.left, greaterThan(titleRect.right));
final second = tester.getRect(
find.byKey(const Key('mine-vip-benefit-1')),
);
final third = tester.getRect(find.byKey(const Key('mine-vip-benefit-2')));
final open = tester.getRect(find.byKey(const Key('mine-vip-open')));
expect(titleRect.top, lessThan(second.top));
expect(second.top, third.top);
expect(find.byKey(const Key('mine-vip-benefit-3')), findsNothing);
expect(open.top, greaterThan(titleRect.top));
expect(open.height, greaterThanOrEqualTo(28));
expect(open.right, lessThanOrEqualTo(cardRect.right));
expect(
find.descendant(
of: find.byKey(const Key('mine-vip-benefits')),
matching: find.byType(Icon),
),
findsNothing,
);
expect(tester.takeException(), isNull);
});
}
testWidgets('should_keep_indonesian_vip_readable_when_width_is_narrow', (
tester,
) async {
await _pumpLocalized(
tester,
const SizedBox(width: 292, child: MineVipBanner(onOpen: _noop)),
locale: const Locale('id'),
);
final title = tester.widget<Text>(
find.byKey(const Key('mine-vip-benefit-0')),
);
final cardRect = tester.getRect(find.byKey(const Key('mine-vip')));
expect(title.maxLines, 2);
expect(cardRect.height, lessThan(125));
expect(tester.takeException(), isNull);
});
for (final localeTag in ['en', 'id']) {
testWidgets(
'should_center_visible_grid_content_when_locale_is_$localeTag',
(tester) async {
await _pumpLocalized(
tester,
const MineActionGrid(
primaryActions: MinePrimaryActions(
onWallet: _noop,
onFavorites: _noop,
onDownloads: _noop,
onHistory: _noop,
),
serviceActions: MineServiceActions(
onOpen: _ignoreDestination,
onLanguage: _noop,
),
),
locale: Locale(localeTag),
);
for (final action in _actions) {
_expectVisibleContentCentered(tester, action);
}
},
);
}
testWidgets('should_use_subtle_dividers_when_rendering_grid', (tester) async {
await _pumpLocalized(
tester,
const MineActionGrid(
primaryActions: MinePrimaryActions(
onWallet: _noop,
onFavorites: _noop,
onDownloads: _noop,
onHistory: _noop,
),
serviceActions: MineServiceActions(
onOpen: _ignoreDestination,
onLanguage: _noop,
),
),
);
final dividers = find.descendant(
of: find.byKey(const Key('mine-menu')),
matching: find.byWidgetPredicate(
(widget) =>
widget is Container && widget.color == AppColors.mineGridDivider,
),
);
expect(dividers, findsNWidgets(7));
expect(AppColors.mineGridDivider, const Color(0x10FFFFFF));
});
}
const _actions = [
(Key('mine-wallet'), Key('mine-wallet-icon-surface')),
(Key('mine-favorites'), Key('mine-favorites-icon-surface')),
(Key('mine-downloads'), Key('mine-downloads-icon-surface')),
(Key('mine-history'), Key('mine-history-icon-surface')),
(Key('mine-help'), Key('mine-help-icon-surface')),
(Key('mine-language'), Key('mine-language-icon-surface')),
(Key('mine-settings'), Key('mine-settings-icon-surface')),
(Key('mine-notices'), Key('mine-notices-icon-surface')),
];
void _expectVisibleContentCentered(WidgetTester tester, (Key, Key) action) {
final cellRect = tester.getRect(find.byKey(action.$1));
final iconRect = tester.getRect(find.byKey(action.$2));
final labelRect = tester.getRect(
find.descendant(of: find.byKey(action.$1), matching: find.byType(Text)),
);
final visibleRect = Rect.fromLTRB(
math.min(iconRect.left, labelRect.left),
iconRect.top,
math.max(iconRect.right, labelRect.right),
labelRect.bottom,
);
expect(visibleRect.center.dx, closeTo(cellRect.center.dx, _layoutTolerance));
expect(visibleRect.center.dy, closeTo(cellRect.center.dy, _layoutTolerance));
}
Future<void> _pumpLocalized(
WidgetTester tester,
Widget child, {
Locale locale = const Locale('en'),
}) async {
tester.view.physicalSize = const Size(390, 900);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
MaterialApp(
theme: AppTheme.dark(),
locale: locale,
supportedLocales: AppLocalizations.supportedLocales,
localizationsDelegates: AppLocalizations.localizationsDelegates,
home: Scaffold(
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(AppDimens.pageHorizontal),
child: child,
),
),
),
),
);
await tester.pumpAndSettle();
}
void _noop() {}
void _ignoreDestination(MineDestination _) {}