system_controller_test.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. @override
  42. Future<void> setKeepScreenOn(bool enabled) async {}
  43. }
  44. void main() {
  45. test('pause sends the pause key and clears playing state', () async {
  46. final platform = _MediaPlatform()..playing = true;
  47. final controller = SystemController(platform);
  48. await controller.start();
  49. addTearDown(controller.dispose);
  50. expect(controller.isMusicPlaying, isTrue);
  51. await controller.pauseMusic();
  52. expect(platform.keys, contains('pause'));
  53. expect(controller.isMusicPlaying, isFalse);
  54. controller.stop();
  55. });
  56. test('media state supplies the title, progress and playing flag', () async {
  57. final platform = _MediaPlatform()
  58. ..media = const MediaPlayback(
  59. hasAccess: true,
  60. hasSession: true,
  61. playing: true,
  62. title: 'Night Drive',
  63. artist: 'The Dashboard',
  64. durationMs: 240000,
  65. positionMs: 60000,
  66. );
  67. final controller = SystemController(platform);
  68. await controller.start();
  69. addTearDown(controller.dispose);
  70. expect(controller.hasMediaAccess, isTrue);
  71. expect(controller.media.displayTitle, 'Night Drive — The Dashboard');
  72. expect(controller.media.progress, 0.25);
  73. expect(controller.isMusicPlaying, isTrue);
  74. controller.stop();
  75. });
  76. test('play resumes and reports whether audio started', () async {
  77. final platform = _MediaPlatform();
  78. final controller = SystemController(platform);
  79. await controller.start();
  80. addTearDown(controller.dispose);
  81. expect(controller.isMusicPlaying, isFalse);
  82. expect(await controller.playMusic(), isTrue);
  83. expect(platform.keys, contains('play'));
  84. expect(controller.isMusicPlaying, isTrue);
  85. controller.stop();
  86. });
  87. }