system_controller_test.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/update_info.dart';
  4. import 'package:cardash_launcher/data/models/system_status.dart';
  5. import 'package:cardash_launcher/data/services/native_bridge.dart';
  6. import 'package:cardash_launcher/features/home/system_controller.dart';
  7. import 'package:flutter_test/flutter_test.dart';
  8. class _MediaPlatform implements LauncherPlatform {
  9. bool playing = false;
  10. MediaPlayback media = const MediaPlayback();
  11. VolumeInfo volume = const VolumeInfo(index: 8, max: 15);
  12. final List<String> keys = [];
  13. final List<int> volumeWrites = [];
  14. @override
  15. Future<List<AppEntry>> listApps() async => const [];
  16. @override
  17. Future<void> launchApp({
  18. required String packageName,
  19. required String activityName,
  20. }) async {}
  21. @override
  22. Future<bool> isDefaultHome() async => true;
  23. @override
  24. Future<bool> requestDefaultHome() async => true;
  25. @override
  26. Future<SystemStatus> getStatus() async => const SystemStatus();
  27. @override
  28. Future<VolumeInfo> getVolume() async => volume;
  29. @override
  30. Future<void> setVolume(int index) async {
  31. volumeWrites.add(index);
  32. }
  33. @override
  34. Future<bool> isMusicPlaying() async => playing;
  35. @override
  36. Future<bool> sendMediaKey(String action) async {
  37. keys.add(action);
  38. if (action == 'play') playing = true;
  39. if (action == 'pause') playing = false;
  40. return true;
  41. }
  42. @override
  43. Future<MediaPlayback> getMediaState() async => media;
  44. @override
  45. Future<bool> openNotificationAccessSettings() async => true;
  46. @override
  47. Future<void> setKeepScreenOn(bool enabled) async {}
  48. @override
  49. Future<bool> openUrl(String url) async => true;
  50. @override
  51. Future<UpdateInstallResult> downloadAndInstallUpdate(String url) async {
  52. return const UpdateInstallResult(
  53. started: false,
  54. permissionRequired: false,
  55. message: 'not started',
  56. );
  57. }
  58. }
  59. void main() {
  60. test('pause sends the pause key and clears playing state', () async {
  61. final platform = _MediaPlatform()..playing = true;
  62. final controller = SystemController(platform);
  63. await controller.start();
  64. addTearDown(controller.dispose);
  65. expect(controller.isMusicPlaying, isTrue);
  66. await controller.pauseMusic();
  67. expect(platform.keys, contains('pause'));
  68. expect(controller.isMusicPlaying, isFalse);
  69. controller.stop();
  70. });
  71. test('media state supplies the title, progress and playing flag', () async {
  72. final platform = _MediaPlatform()
  73. ..media = const MediaPlayback(
  74. hasAccess: true,
  75. hasSession: true,
  76. playing: true,
  77. title: 'Night Drive',
  78. artist: 'The Dashboard',
  79. durationMs: 240000,
  80. positionMs: 60000,
  81. );
  82. final controller = SystemController(platform);
  83. await controller.start();
  84. addTearDown(controller.dispose);
  85. expect(controller.hasMediaAccess, isTrue);
  86. expect(controller.media.displayTitle, 'Night Drive — The Dashboard');
  87. expect(controller.media.progress, 0.25);
  88. expect(controller.isMusicPlaying, isTrue);
  89. controller.stop();
  90. });
  91. test('play resumes and reports whether audio started', () async {
  92. final platform = _MediaPlatform();
  93. final controller = SystemController(platform);
  94. await controller.start();
  95. addTearDown(controller.dispose);
  96. expect(controller.isMusicPlaying, isFalse);
  97. expect(await controller.playMusic(), isTrue);
  98. expect(platform.keys, contains('play'));
  99. expect(controller.isMusicPlaying, isTrue);
  100. controller.stop();
  101. });
  102. test('fixed car volume ignores software volume changes', () async {
  103. final platform = _MediaPlatform()
  104. ..volume = const VolumeInfo(index: 100, max: 100, fixed: true);
  105. final controller = SystemController(platform);
  106. await controller.start();
  107. addTearDown(controller.dispose);
  108. expect(controller.isVolumeFixed, isTrue);
  109. await controller.setVolume(25);
  110. await controller.toggleMute();
  111. expect(platform.volumeWrites, isEmpty);
  112. expect(controller.volume.index, 100);
  113. controller.stop();
  114. });
  115. }