shortcut_grid_test.dart 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import 'package:cardash_launcher/core/theme/app_theme.dart';
  2. import 'package:cardash_launcher/features/home/widgets/shortcut_grid.dart';
  3. import 'package:cardash_launcher/features/home/widgets/shortcut_tile.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_test/flutter_test.dart';
  6. List<ShortcutSpec> _specs(int count) {
  7. return List.generate(
  8. count,
  9. (index) => ShortcutSpec(
  10. title: 'App $index',
  11. subtitle: index < 5 ? 'Select app' : null,
  12. iconAsset: 'assets/icons/app-window.svg',
  13. assigned: true,
  14. onTap: () {},
  15. onLongPress: () {},
  16. longPressHint: 'remove from Home',
  17. ),
  18. );
  19. }
  20. Future<void> _pumpGrid(
  21. WidgetTester tester, {
  22. required Size size,
  23. required bool compact,
  24. int count = 16,
  25. }) async {
  26. await tester.pumpWidget(
  27. MaterialApp(
  28. theme: AppTheme.light(),
  29. home: Scaffold(
  30. body: Center(
  31. child: SizedBox(
  32. width: size.width,
  33. height: size.height,
  34. child: ShortcutGrid(shortcuts: _specs(count), compact: compact),
  35. ),
  36. ),
  37. ),
  38. ),
  39. );
  40. await tester.pump();
  41. }
  42. void main() {
  43. TestWidgetsFlutterBinding.ensureInitialized();
  44. testWidgets('16 buttons fit at 1280x800 without overflow', (tester) async {
  45. await _pumpGrid(tester, size: const Size(700, 610), compact: false);
  46. expect(tester.takeException(), isNull);
  47. expect(find.byType(ShortcutTile), findsNWidgets(16));
  48. final first = tester.widget<ShortcutTile>(find.byType(ShortcutTile).first);
  49. expect(first.size, lessThanOrEqualTo(196));
  50. expect(first.size, greaterThan(64));
  51. });
  52. testWidgets('16 buttons fit at the 1280x720 layout', (tester) async {
  53. await _pumpGrid(tester, size: const Size(755, 500), compact: false);
  54. expect(tester.takeException(), isNull);
  55. expect(find.byType(ShortcutTile), findsNWidgets(16));
  56. final first = tester.widget<ShortcutTile>(find.byType(ShortcutTile).first);
  57. expect(first.size, lessThanOrEqualTo(196));
  58. expect(first.size, greaterThan(64));
  59. });
  60. testWidgets('16 buttons fit at the 1024x600 compact layout', (tester) async {
  61. await _pumpGrid(tester, size: const Size(559, 476), compact: true);
  62. expect(tester.takeException(), isNull);
  63. expect(find.byType(ShortcutTile), findsNWidgets(16));
  64. final first = tester.widget<ShortcutTile>(find.byType(ShortcutTile).first);
  65. expect(first.size, lessThanOrEqualTo(168));
  66. expect(first.size, greaterThan(64));
  67. });
  68. testWidgets('a short grid is anchored to the bottom edge', (tester) async {
  69. await _pumpGrid(
  70. tester,
  71. size: const Size(600, 400),
  72. compact: false,
  73. count: 4,
  74. );
  75. expect(tester.takeException(), isNull);
  76. final grid = tester.getRect(find.byType(ShortcutGrid));
  77. final wrap = tester.getRect(find.byType(Wrap));
  78. final last = tester.getRect(find.byType(ShortcutTile).last);
  79. expect(last.bottom, moreOrLessEquals(grid.bottom, epsilon: 0.5));
  80. expect(wrap.center.dx, moreOrLessEquals(grid.center.dx, epsilon: 0.5));
  81. });
  82. }