launcher_controller_test.dart 6.6 KB

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