system_controller_test.dart 3.2 KB

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