launcher_controller_test.dart 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import 'package:cardash_launcher/data/models/app_entry.dart';
  2. import 'package:cardash_launcher/data/models/slot_type.dart';
  3. import 'package:cardash_launcher/data/models/system_status.dart';
  4. import 'package:cardash_launcher/data/repositories/settings_repository.dart';
  5. import 'package:cardash_launcher/data/services/native_bridge.dart';
  6. import 'package:cardash_launcher/features/home/launcher_controller.dart';
  7. import 'package:flutter_test/flutter_test.dart';
  8. import 'package:shared_preferences/shared_preferences.dart';
  9. class FakeLauncherPlatform implements LauncherPlatform {
  10. FakeLauncherPlatform(this.apps);
  11. final List<AppEntry> apps;
  12. @override
  13. Future<List<AppEntry>> listApps() async => apps;
  14. @override
  15. Future<void> launchApp({
  16. required String packageName,
  17. required String activityName,
  18. }) async {}
  19. @override
  20. Future<bool> isDefaultHome() async => true;
  21. @override
  22. Future<bool> requestDefaultHome() async => true;
  23. @override
  24. Future<SystemStatus> getStatus() async => const SystemStatus();
  25. @override
  26. Future<VolumeInfo> getVolume() async => const VolumeInfo();
  27. @override
  28. Future<void> setVolume(int index) async {}
  29. @override
  30. Future<bool> isMusicPlaying() async => false;
  31. @override
  32. Future<bool> sendMediaKey(String action) async => true;
  33. }
  34. List<AppEntry> _apps(int count) {
  35. return List.generate(
  36. count,
  37. (index) => AppEntry(
  38. packageName: 'com.example.app$index',
  39. activityName: 'com.example.app$index.Main',
  40. label: 'App $index',
  41. ),
  42. );
  43. }
  44. Future<LauncherController> _controller({
  45. int appCount = 20,
  46. FakeLauncherPlatform? platform,
  47. }) async {
  48. SharedPreferences.setMockInitialValues({});
  49. final prefs = await SharedPreferences.getInstance();
  50. final controller = LauncherController(
  51. platform ?? FakeLauncherPlatform(_apps(appCount)),
  52. SettingsRepository(prefs),
  53. );
  54. controller.initialize();
  55. return controller;
  56. }
  57. void main() {
  58. TestWidgetsFlutterBinding.ensureInitialized();
  59. test('parses the native system-app flag', () {
  60. final system = AppEntry.fromMap({
  61. 'package': 'com.example.system',
  62. 'activity': 'com.example.system.Main',
  63. 'label': 'System',
  64. 'system': true,
  65. });
  66. final user = AppEntry.fromMap({
  67. 'package': 'com.example.user',
  68. 'activity': 'com.example.user.Main',
  69. 'label': 'User',
  70. 'system': false,
  71. });
  72. expect(system.isSystemApp, isTrue);
  73. expect(user.isSystemApp, isFalse);
  74. });
  75. test('home buttons default to enabled and count the fixed buttons', () async {
  76. final controller = await _controller();
  77. addTearDown(controller.dispose);
  78. expect(controller.isSlotEnabled(SlotType.navigation), isTrue);
  79. expect(controller.isSlotEnabled(SlotType.music), isTrue);
  80. expect(controller.isSlotEnabled(SlotType.radio), isTrue);
  81. expect(controller.homeButtonCount, 4);
  82. expect(controller.pinnedApps, isEmpty);
  83. });
  84. test('disabling a slot hides it and keeps the assignment', () async {
  85. final controller = await _controller();
  86. addTearDown(controller.dispose);
  87. final app = _apps(1).first;
  88. await controller.assign(SlotType.navigation, app);
  89. expect(await controller.setSlotEnabled(SlotType.navigation, false), isNull);
  90. expect(controller.isSlotEnabled(SlotType.navigation), isFalse);
  91. expect(controller.homeButtonCount, 3);
  92. expect(controller.assignmentFor(SlotType.navigation)?.label, 'App 0');
  93. expect(await controller.setSlotEnabled(SlotType.navigation, true), isNull);
  94. expect(controller.homeButtonCount, 4);
  95. });
  96. test('pinned apps can be added once and removed again', () async {
  97. final controller = await _controller();
  98. addTearDown(controller.dispose);
  99. final apps = _apps(3);
  100. expect(await controller.addPinnedApp(apps[0]), isNull);
  101. expect(controller.pinnedApps.length, 1);
  102. expect(controller.homeButtonCount, 5);
  103. expect(await controller.addPinnedApp(apps[0]), contains('already'));
  104. await controller.removePinnedApp(apps[0].toAssignment());
  105. expect(controller.pinnedApps, isEmpty);
  106. expect(controller.homeButtonCount, 4);
  107. });
  108. test('the home screen stops at 16 buttons', () async {
  109. final controller = await _controller();
  110. addTearDown(controller.dispose);
  111. final apps = _apps(20);
  112. for (var index = 0; index < 12; index++) {
  113. expect(await controller.addPinnedApp(apps[index]), isNull);
  114. }
  115. expect(controller.homeButtonCount, 16);
  116. expect(await controller.addPinnedApp(apps[12]), contains('full'));
  117. expect(controller.pinnedApps.length, 12);
  118. await controller.removePinnedApp(apps[0].toAssignment());
  119. expect(controller.homeButtonCount, 15);
  120. expect(await controller.addPinnedApp(apps[12]), isNull);
  121. expect(controller.homeButtonCount, 16);
  122. });
  123. test('pinned labels and custom icons persist', () async {
  124. SharedPreferences.setMockInitialValues({});
  125. final prefs = await SharedPreferences.getInstance();
  126. final apps = _apps(3);
  127. final platform = FakeLauncherPlatform(apps);
  128. final first = LauncherController(platform, SettingsRepository(prefs));
  129. first.initialize();
  130. addTearDown(first.dispose);
  131. expect(await first.addPinnedApp(apps[0]), isNull);
  132. final pinned = first.pinnedApps.first;
  133. await first.renamePinnedApp(pinned, 'My shortcut');
  134. await first.setPinnedIcon(pinned, 'assets/icons/star.svg');
  135. final second = LauncherController(platform, SettingsRepository(prefs));
  136. second.initialize();
  137. addTearDown(second.dispose);
  138. expect(second.pinnedApps.length, 1);
  139. expect(second.pinnedApps.first.label, 'My shortcut');
  140. expect(second.pinnedApps.first.iconAsset, 'assets/icons/star.svg');
  141. });
  142. test('pinned apps and slot visibility survive a new controller', () async {
  143. SharedPreferences.setMockInitialValues({});
  144. final prefs = await SharedPreferences.getInstance();
  145. final platform = FakeLauncherPlatform(_apps(3));
  146. final settings = SettingsRepository(prefs);
  147. final first = LauncherController(platform, settings);
  148. first.initialize();
  149. addTearDown(first.dispose);
  150. await first.setSlotEnabled(SlotType.radio, false);
  151. expect(await first.addPinnedApp(_apps(1).first), isNull);
  152. final second = LauncherController(platform, SettingsRepository(prefs));
  153. second.initialize();
  154. addTearDown(second.dispose);
  155. expect(second.isSlotEnabled(SlotType.radio), isFalse);
  156. expect(second.pinnedApps.length, 1);
  157. expect(second.pinnedApps.first.label, 'App 0');
  158. expect(second.homeButtonCount, 4);
  159. });
  160. }