The alarm's steady-state audio runs on the Flutter media-stream player after the native handoff, so device volume 0 silenced it entirely. The ring now forces STREAM_MUSIC to an audible reference: Dart requests the override before pre-starting alarm audio (fallback WAV included), Kotlin captures the current volume once and restores it idempotently on every exit path (dismiss, snooze, dispose), with a native best-effort backstop in service teardown. The backstop is handoff-aware via PluriWaveAlarmService.flutterOwnsRing: confirmFlutterAudio marks the handoff before triggering the native stop, so the backstop cannot restore the volume mid-ring right as the Flutter player takes over (that would re-silence the alarm at volume 0). The flag resets at every ring start; Flutter process death after handoff remains a documented best-effort gap. The alarm's perceived loudness keeps ramping 5% to the configured volume through the player as before; normal radio playback and call ducking never touch the override. Work unit 2/3 of alarm-volume-ramp-restore (ring volume override).
138 lines
4.2 KiB
Dart
138 lines
4.2 KiB
Dart
import 'package:flutter/services.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pluriwave/modelos/alarma_musical.dart';
|
|
import 'package:pluriwave/modelos/emisora.dart';
|
|
import 'package:pluriwave/servicios/servicio_alarmas_android.dart';
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
const channel = MethodChannel('pluriwave/alarm_scheduler');
|
|
late List<MethodCall> llamadas;
|
|
|
|
setUp(() {
|
|
llamadas = [];
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, (call) async {
|
|
llamadas.add(call);
|
|
switch (call.method) {
|
|
case 'scheduleAlarm':
|
|
return true;
|
|
case 'requestIgnoreBatteryOptimizations':
|
|
return true;
|
|
}
|
|
return null;
|
|
});
|
|
});
|
|
|
|
tearDown(() {
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(channel, null);
|
|
});
|
|
|
|
test(
|
|
'programar incluye emisora de respaldo y fade en el payload nativo',
|
|
() async {
|
|
final servicio = ServicioAlarmasAndroid(channel: channel);
|
|
final alarma = AlarmaMusical(
|
|
id: 'a1',
|
|
nombre: 'Con respaldo',
|
|
hora: 7,
|
|
minuto: 30,
|
|
tipoProgramacion: TipoProgramacionAlarma.diaria,
|
|
diasSemana: const [],
|
|
proximaEjecucion: DateTime(2099, 1, 1, 7, 30),
|
|
emisora: const Emisora(
|
|
uuid: 'uuid-principal',
|
|
nombre: 'Principal FM',
|
|
url: 'https://principal.example/stream',
|
|
),
|
|
emisoraFallback: const Emisora(
|
|
uuid: 'uuid-respaldo',
|
|
nombre: 'Respaldo FM',
|
|
url: 'https://respaldo.example/stream',
|
|
),
|
|
fadeInSegundos: 12,
|
|
);
|
|
|
|
await servicio.programar(alarma);
|
|
|
|
final llamada = llamadas.singleWhere((c) => c.method == 'scheduleAlarm');
|
|
final args = llamada.arguments as Map<Object?, Object?>;
|
|
expect(args['fallbackStationName'], 'Respaldo FM');
|
|
expect(args['fallbackStationUrl'], 'https://respaldo.example/stream');
|
|
expect(args['fadeInSegundos'], 12);
|
|
expect(args['fallbackSound'], SonidoInternoAlarma.amanecer.name);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'programar sin emisora de respaldo envia campos de respaldo nulos',
|
|
() async {
|
|
final servicio = ServicioAlarmasAndroid(channel: channel);
|
|
final alarma = AlarmaMusical(
|
|
id: 'a2',
|
|
nombre: 'Sin respaldo',
|
|
hora: 8,
|
|
minuto: 0,
|
|
tipoProgramacion: TipoProgramacionAlarma.diaria,
|
|
diasSemana: const [],
|
|
proximaEjecucion: DateTime(2099, 1, 1, 8, 0),
|
|
);
|
|
|
|
await servicio.programar(alarma);
|
|
|
|
final llamada = llamadas.singleWhere((c) => c.method == 'scheduleAlarm');
|
|
final args = llamada.arguments as Map<Object?, Object?>;
|
|
expect(args.containsKey('fallbackStationName'), isTrue);
|
|
expect(args['fallbackStationName'], isNull);
|
|
expect(args.containsKey('fallbackStationUrl'), isTrue);
|
|
expect(args['fallbackStationUrl'], isNull);
|
|
expect(args['fadeInSegundos'], 0);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'solicitarExencionBateria invoca requestIgnoreBatteryOptimizations',
|
|
() async {
|
|
final servicio = ServicioAlarmasAndroid(channel: channel);
|
|
|
|
final abierto = await servicio.solicitarExencionBateria();
|
|
|
|
expect(abierto, isTrue);
|
|
expect(
|
|
llamadas.map((c) => c.method),
|
|
contains('requestIgnoreBatteryOptimizations'),
|
|
);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'forzarVolumenMediaParaAlarma invoca overrideMediaVolumeForRing con fraction',
|
|
() async {
|
|
final servicio = ServicioAlarmasAndroid(channel: channel);
|
|
|
|
await servicio.forzarVolumenMediaParaAlarma(1.0);
|
|
|
|
final llamada = llamadas.singleWhere(
|
|
(c) => c.method == 'overrideMediaVolumeForRing',
|
|
);
|
|
expect(llamada.arguments, {'fraction': 1.0});
|
|
},
|
|
);
|
|
|
|
test(
|
|
'restaurarVolumenMedia invoca restoreMediaVolume sin argumentos',
|
|
() async {
|
|
final servicio = ServicioAlarmasAndroid(channel: channel);
|
|
|
|
await servicio.restaurarVolumenMedia();
|
|
|
|
final llamada = llamadas.singleWhere(
|
|
(c) => c.method == 'restoreMediaVolume',
|
|
);
|
|
expect(llamada.arguments, <String, Object?>{});
|
|
},
|
|
);
|
|
}
|