launcher_controller_test.dart 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. @override
  39. Future<void> setKeepScreenOn(bool enabled) async {}
  40. }
  41. List<AppEntry> _apps(int count) {
  42. return List.generate(
  43. count,
  44. (index) => AppEntry(
  45. packageName: 'com.example.app$index',
  46. activityName: 'com.example.app$index.Main',
  47. label: 'App $index',
  48. ),
  49. );
  50. }
  51. Future<LauncherController> _controller({
  52. int appCount = 20,
  53. FakeLauncherPlatform? platform,
  54. }) async {
  55. SharedPreferences.setMockInitialValues({});
  56. final prefs = await SharedPreferences.getInstance();
  57. final controller = LauncherController(
  58. platform ?? FakeLauncherPlatform(_apps(appCount)),
  59. SettingsRepository(prefs),
  60. );
  61. controller.initialize();
  62. return controller;
  63. }
  64. void main() {
  65. TestWidgetsFlutterBinding.ensureInitialized();
  66. test('parses the native system-app flag', () {
  67. final system = AppEntry.fromMap({
  68. 'package': 'com.example.system',
  69. 'activity': 'com.example.system.Main',
  70. 'label': 'System',
  71. 'system': true,
  72. });
  73. final user = AppEntry.fromMap({
  74. 'package': 'com.example.user',
  75. 'activity': 'com.example.user.Main',
  76. 'label': 'User',
  77. 'system': false,
  78. });
  79. expect(system.isSystemApp, isTrue);
  80. expect(user.isSystemApp, isFalse);
  81. });
  82. test('home buttons default to enabled and count the fixed buttons', () async {
  83. final controller = await _controller();
  84. addTearDown(controller.dispose);
  85. expect(controller.isSlotEnabled(SlotType.navigation), isTrue);
  86. expect(controller.isSlotEnabled(SlotType.music), isTrue);
  87. expect(controller.isSlotEnabled(SlotType.radio), isTrue);
  88. expect(controller.homeButtonCount, 4);
  89. expect(controller.pinnedApps, isEmpty);
  90. });
  91. test('disabling a slot hides it and keeps the assignment', () async {
  92. final controller = await _controller();
  93. addTearDown(controller.dispose);
  94. final app = _apps(1).first;
  95. await controller.assign(SlotType.navigation, app);
  96. expect(await controller.setSlotEnabled(SlotType.navigation, false), isNull);
  97. expect(controller.isSlotEnabled(SlotType.navigation), isFalse);
  98. expect(controller.homeButtonCount, 3);
  99. expect(controller.assignmentFor(SlotType.navigation)?.label, 'App 0');
  100. expect(await controller.setSlotEnabled(SlotType.navigation, true), isNull);
  101. expect(controller.homeButtonCount, 4);
  102. });
  103. test('pinned apps can be added once and removed again', () async {
  104. final controller = await _controller();
  105. addTearDown(controller.dispose);
  106. final apps = _apps(3);
  107. expect(await controller.addPinnedApp(apps[0]), isNull);
  108. expect(controller.pinnedApps.length, 1);
  109. expect(controller.homeButtonCount, 5);
  110. expect(await controller.addPinnedApp(apps[0]), contains('already'));
  111. await controller.removePinnedApp(apps[0].toAssignment());
  112. expect(controller.pinnedApps, isEmpty);
  113. expect(controller.homeButtonCount, 4);
  114. });
  115. test('the home screen stops at 16 buttons', () async {
  116. final controller = await _controller();
  117. addTearDown(controller.dispose);
  118. final apps = _apps(20);
  119. for (var index = 0; index < 12; index++) {
  120. expect(await controller.addPinnedApp(apps[index]), isNull);
  121. }
  122. expect(controller.homeButtonCount, 16);
  123. expect(await controller.addPinnedApp(apps[12]), contains('full'));
  124. expect(controller.pinnedApps.length, 12);
  125. await controller.removePinnedApp(apps[0].toAssignment());
  126. expect(controller.homeButtonCount, 15);
  127. expect(await controller.addPinnedApp(apps[12]), isNull);
  128. expect(controller.homeButtonCount, 16);
  129. });
  130. test('pinned labels and custom icons persist', () async {
  131. SharedPreferences.setMockInitialValues({});
  132. final prefs = await SharedPreferences.getInstance();
  133. final apps = _apps(3);
  134. final platform = FakeLauncherPlatform(apps);
  135. final first = LauncherController(platform, SettingsRepository(prefs));
  136. first.initialize();
  137. addTearDown(first.dispose);
  138. expect(await first.addPinnedApp(apps[0]), isNull);
  139. final pinned = first.pinnedApps.first;
  140. await first.renamePinnedApp(pinned, 'My shortcut');
  141. await first.setPinnedIcon(pinned, 'assets/icons/star.svg');
  142. final second = LauncherController(platform, SettingsRepository(prefs));
  143. second.initialize();
  144. addTearDown(second.dispose);
  145. expect(second.pinnedApps.length, 1);
  146. expect(second.pinnedApps.first.label, 'My shortcut');
  147. expect(second.pinnedApps.first.iconAsset, 'assets/icons/star.svg');
  148. });
  149. test('pinned apps and slot visibility survive a new controller', () async {
  150. SharedPreferences.setMockInitialValues({});
  151. final prefs = await SharedPreferences.getInstance();
  152. final platform = FakeLauncherPlatform(_apps(3));
  153. final settings = SettingsRepository(prefs);
  154. final first = LauncherController(platform, settings);
  155. first.initialize();
  156. addTearDown(first.dispose);
  157. await first.setSlotEnabled(SlotType.radio, false);
  158. expect(await first.addPinnedApp(_apps(1).first), isNull);
  159. final second = LauncherController(platform, SettingsRepository(prefs));
  160. second.initialize();
  161. addTearDown(second.dispose);
  162. expect(second.isSlotEnabled(SlotType.radio), isFalse);
  163. expect(second.pinnedApps.length, 1);
  164. expect(second.pinnedApps.first.label, 'App 0');
  165. expect(second.homeButtonCount, 4);
  166. });
  167. }