system_controller_test.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/system_status.dart';
  4. import 'package:cardash_launcher/data/services/native_bridge.dart';
  5. import 'package:cardash_launcher/features/home/system_controller.dart';
  6. import 'package:flutter_test/flutter_test.dart';
  7. class _MediaPlatform implements LauncherPlatform {
  8. bool playing = false;
  9. MediaPlayback media = const MediaPlayback();
  10. final List<String> keys = [];
  11. @override
  12. Future<List<AppEntry>> listApps() async => const [];
  13. @override
  14. Future<void> launchApp({
  15. required String packageName,
  16. required String activityName,
  17. }) async {}
  18. @override
  19. Future<bool> isDefaultHome() async => true;
  20. @override
  21. Future<bool> requestDefaultHome() async => true;
  22. @override
  23. Future<SystemStatus> getStatus() async => const SystemStatus();
  24. @override
  25. Future<VolumeInfo> getVolume() async => const VolumeInfo(index: 8, max: 15);
  26. @override
  27. Future<void> setVolume(int index) async {}
  28. @override
  29. Future<bool> isMusicPlaying() async => playing;
  30. @override
  31. Future<bool> sendMediaKey(String action) async {
  32. keys.add(action);
  33. if (action == 'play') playing = true;
  34. if (action == 'pause') playing = false;
  35. return true;
  36. }
  37. @override
  38. Future<MediaPlayback> getMediaState() async => media;
  39. @override
  40. Future<bool> openNotificationAccessSettings() async => true;
  41. }
  42. void main() {
  43. test('pause sends the pause key and clears playing state', () async {
  44. final platform = _MediaPlatform()..playing = true;
  45. final controller = SystemController(platform);
  46. await controller.start();
  47. addTearDown(controller.dispose);
  48. expect(controller.isMusicPlaying, isTrue);
  49. await controller.pauseMusic();
  50. expect(platform.keys, contains('pause'));
  51. expect(controller.isMusicPlaying, isFalse);
  52. controller.stop();
  53. });
  54. test('media state supplies the title, progress and playing flag', () async {
  55. final platform = _MediaPlatform()
  56. ..media = const MediaPlayback(
  57. hasAccess: true,
  58. hasSession: true,
  59. playing: true,
  60. title: 'Night Drive',
  61. artist: 'The Dashboard',
  62. durationMs: 240000,
  63. positionMs: 60000,
  64. );
  65. final controller = SystemController(platform);
  66. await controller.start();
  67. addTearDown(controller.dispose);
  68. expect(controller.hasMediaAccess, isTrue);
  69. expect(controller.media.displayTitle, 'Night Drive — The Dashboard');
  70. expect(controller.media.progress, 0.25);
  71. expect(controller.isMusicPlaying, isTrue);
  72. controller.stop();
  73. });
  74. test('play resumes and reports whether audio started', () async {
  75. final platform = _MediaPlatform();
  76. final controller = SystemController(platform);
  77. await controller.start();
  78. addTearDown(controller.dispose);
  79. expect(controller.isMusicPlaying, isFalse);
  80. expect(await controller.playMusic(), isTrue);
  81. expect(platform.keys, contains('play'));
  82. expect(controller.isMusicPlaying, isTrue);
  83. controller.stop();
  84. });
  85. }