fix(alarm): defer the Dart fade-in until the native handoff confirms
Build & Deploy PluriWave / Análisis de código (push) Successful in 37s
Build & Deploy PluriWave / Build APK + AAB release (push) Successful in 1m50s

The native service and the Flutter player each ran their own 5%-to-
target fade-in, and both could drive audible volume at the handoff,
producing a jump or ramp reset. The Dart ramp now starts exactly once
from the handoff-confirmation path: the player still pre-starts at 5%,
and _confirmarAudioFlutterListo() starts the ramp in a finally block
so it runs whether the native confirmation succeeds or fails — the
alarm can never stay stuck at 5% if the native side is already gone.

Work unit 3/3 of alarm-volume-ramp-restore (fade-in dedup).
This commit is contained in:
2026-07-11 09:57:06 +02:00
parent a6e1177752
commit 66a19525bd
4 changed files with 193 additions and 14 deletions
+18
View File
@@ -30,6 +30,18 @@ class FakePuertoAlarmasAndroid implements PuertoAlarmasAndroid {
/// could not otherwise produce.
bool fallaProgramar = false;
/// Test-only gate (Slice 3: fade-in dedup at handoff). When set,
/// [confirmarAudioFlutter] suspends on this completer before resolving,
/// letting a test observe the pre-confirm state deterministically instead
/// of racing real stream/timer scheduling. Complete it to let the call
/// proceed.
Completer<void>? puertaConfirmarAudioFlutter;
/// Test-only failure switch (Slice 3 edge case): when true,
/// [confirmarAudioFlutter] throws after the gate above (if any) resolves,
/// simulating a dead/never-there native channel at handoff.
bool fallaConfirmarAudioFlutter = false;
/// Simulates a native -> Flutter `alarmFired` MethodChannel event.
void emitirEvento(EventoAlarmaAndroid evento) => _eventos.add(evento);
@@ -64,6 +76,12 @@ class FakePuertoAlarmasAndroid implements PuertoAlarmasAndroid {
@override
Future<void> confirmarAudioFlutter(String alarmaId) async {
if (puertaConfirmarAudioFlutter != null) {
await puertaConfirmarAudioFlutter!.future;
}
if (fallaConfirmarAudioFlutter) {
throw StateError('fake confirmarAudioFlutter failure');
}
detenidas.add(alarmaId);
}
@@ -31,6 +31,12 @@ class _Entorno {
Future<_Entorno> _montarPantalla(
WidgetTester tester, {
int snoozeMinutos = 5,
// Slice 3 (fade-in dedup): existing callers rely on the radio already
// being "reproduciendo" by mount time, which cancels the fallback timer
// synchronously and leaves nothing to observe mid-handoff. Fade-in-gate
// tests need a live `_estadoSub` subscriber instead, so they set this to
// false and emit `reproduciendo` themselves after the widget mounts.
bool audioYaReproduciendo = true,
}) async {
tester.view.physicalSize = const Size(1440, 3200);
tester.view.devicePixelRatio = 1.0;
@@ -38,7 +44,9 @@ Future<_Entorno> _montarPantalla(
addTearDown(tester.view.resetDevicePixelRatio);
final audio = FakeServicioAudio();
audio.emitirEstado(EstadoReproduccion.reproduciendo);
if (audioYaReproduciendo) {
audio.emitirEstado(EstadoReproduccion.reproduciendo);
}
final radio = EstadoRadio(
audio: audio,
favoritos: FakeServicioFavoritos(),
@@ -158,4 +166,66 @@ void main() {
);
},
);
group('fade-in dedup en el handoff (Slice 3)', () {
testWidgets('el fade-in de Dart se retiene hasta que el nativo confirma el '
'handoff, y arranca justo despues (camino radio)', (tester) async {
final entorno = await _montarPantalla(
tester,
audioYaReproduciendo: false,
);
entorno.android.puertaConfirmarAudioFlutter = Completer<void>();
// Antes de que la radio confirme "reproduciendo", solo debe existir
// el volumen de arranque previo (0.05): el ramp real hacia
// alarma.volumen todavia NO debe haber arrancado.
expect(entorno.android.detenidas, isEmpty);
expect(entorno.audio.volumenesAplicados, [0.05]);
// La radio confirma que esta reproduciendo -> dispara
// _confirmarAudioFlutterListo(), que queda bloqueado en la puerta
// (todavia no hay confirmacion nativa real).
entorno.audio.emitirEstado(EstadoReproduccion.reproduciendo);
await tester.pumpAndSettle();
expect(
entorno.android.detenidas,
isEmpty,
reason: 'confirmarAudioFlutter sigue bloqueado en la puerta de prueba',
);
expect(
entorno.audio.volumenesAplicados,
[0.05],
reason: 'el fade-in de Dart no debe arrancar antes del handoff',
);
// Se libera la puerta: recien ahi "confirma" el nativo, y solo
// entonces debe arrancar el fade-in de Dart (una unica rampa
// audible a la vez).
entorno.android.puertaConfirmarAudioFlutter!.complete();
await tester.pumpAndSettle();
expect(entorno.android.detenidas, contains('ring1'));
expect(entorno.audio.volumenesAplicados, [0.05, 0.85]);
});
testWidgets('si confirmar el audio con el nativo falla, el fade-in de Dart '
'arranca igual (el nativo esta muerto o nunca corrio)', (tester) async {
final entorno = await _montarPantalla(
tester,
audioYaReproduciendo: false,
);
entorno.android.fallaConfirmarAudioFlutter = true;
entorno.audio.emitirEstado(EstadoReproduccion.reproduciendo);
await tester.pumpAndSettle();
// La confirmacion nativa fallo (el fake lanza antes de registrar en
// `detenidas`), pero el fade-in de Dart debe arrancar de todas
// formas: si el lado nativo esta muerto o nunca corrio, Dart es la
// unica fuente audible, y el ring no debe quedar pegado en
// _volumenInicialFadeIn para siempre.
expect(entorno.android.detenidas, isEmpty);
expect(entorno.audio.volumenesAplicados, [0.05, 0.85]);
});
});
}