feat(alarms): native reliability fixes and end-to-end snooze
- Use mediaPlayback|systemExempted FGS type with FOREGROUND_SERVICE_SYSTEM_EXEMPTED so alarms fire on Android 14+ (FOREGROUND_SERVICE_ALARM does not exist in the SDK) - Deduplicate fire notifications: the foreground service FSI notification is the single owner; receiver path removed - Notification channel v2 with alarm sound URI and USAGE_ALARM attributes, one-time guarded migration from legacy channels - Pass fallback station through the MethodChannel (NativeAlarmSpec schemaVersion 3) with a three-stage audio chain: primary -> fallback station -> bundled WAV - Native fade-in volume ramp honoring fadeInSegundos when the app is killed - Request battery-optimization exemption once, tracked with a persisted asked-once flag - Fix snooze end-to-end: native ACTION_SNOOZE now reports back to Flutter (snoozed event + cold-start sync), snooze anchor unified to occurrence+minutes on both sides, periodic recalc no longer erases an active snooze - Add snooze buttons (3/5/10/custom) to the ringing screen with shared audio teardown - Redesign ringing screen on PluriWaveScaffold with reduced-motion-aware entry animation (new PluriAnimate helper) - Alarm editor: live next-trigger preview, searchable station pickers (primary and fallback), configurable snooze duration, volume floor down to 0 - New alarm strings localized across all 13 locales - New unit/widget tests for the snooze flow, alarm bridge payloads, ringing screen and editor (77 tests green) - SDD artifacts for the app-quality-and-native-alarms change (explore, proposal, spec, design, tasks, apply progress)
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pluriwave/modelos/alarma_musical.dart';
|
||||
import 'package:pluriwave/servicios/servicio_alarmas.dart';
|
||||
import 'package:pluriwave/servicios/servicio_alarmas_android.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
setUp(() {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
});
|
||||
|
||||
group('ServicioAlarmas.posponerEjecucion (ancla unificada)', () {
|
||||
Future<ServicioAlarmas> servicioConAlarma(DateTime ahora) async {
|
||||
final servicio = ServicioAlarmas(reloj: () => ahora);
|
||||
await servicio.guardarAlarma(
|
||||
const AlarmaMusical(
|
||||
id: 'p1',
|
||||
nombre: 'Snooze',
|
||||
hora: 8,
|
||||
minuto: 30,
|
||||
tipoProgramacion: TipoProgramacionAlarma.diaria,
|
||||
diasSemana: [],
|
||||
),
|
||||
);
|
||||
return servicio;
|
||||
}
|
||||
|
||||
test('ancla en ejecucion + minutos cuando el objetivo es futuro', () async {
|
||||
final servicio = await servicioConAlarma(DateTime(2026, 6, 11, 8, 0));
|
||||
|
||||
final config = await servicio.posponerEjecucion(
|
||||
'p1',
|
||||
DateTime(2026, 6, 11, 8, 30),
|
||||
10,
|
||||
);
|
||||
|
||||
final alarma = config.alarmas.single;
|
||||
expect(alarma.snoozeHasta, DateTime(2026, 6, 11, 8, 40));
|
||||
expect(alarma.snoozeOrigen, DateTime(2026, 6, 11, 8, 30));
|
||||
|
||||
final recargada = await servicio.cargar();
|
||||
expect(
|
||||
recargada.alarmas.single.snoozeHasta,
|
||||
DateTime(2026, 6, 11, 8, 40),
|
||||
reason: 'el snooze debe quedar persistido',
|
||||
);
|
||||
});
|
||||
|
||||
test('clava a ahora + minutos cuando el objetivo ya paso', () async {
|
||||
final servicio = await servicioConAlarma(DateTime(2026, 6, 11, 9, 0));
|
||||
|
||||
final config = await servicio.posponerEjecucion(
|
||||
'p1',
|
||||
DateTime(2026, 6, 11, 8, 30),
|
||||
5,
|
||||
);
|
||||
|
||||
expect(config.alarmas.single.snoozeHasta, DateTime(2026, 6, 11, 9, 5));
|
||||
});
|
||||
|
||||
test('respeta minutos personalizados fuera de 3/5/10', () async {
|
||||
final servicio = await servicioConAlarma(DateTime(2026, 6, 11, 8, 0));
|
||||
|
||||
final config = await servicio.posponerEjecucion(
|
||||
'p1',
|
||||
DateTime(2026, 6, 11, 8, 30),
|
||||
7,
|
||||
);
|
||||
|
||||
expect(config.alarmas.single.snoozeHasta, DateTime(2026, 6, 11, 8, 37));
|
||||
});
|
||||
});
|
||||
|
||||
group('Puente Android (MethodChannel)', () {
|
||||
const channel = MethodChannel('pluriwave/alarm_scheduler');
|
||||
late List<MethodCall> llamadas;
|
||||
late List<Map<String, Object?>> snoozesNativos;
|
||||
|
||||
setUp(() {
|
||||
llamadas = [];
|
||||
snoozesNativos = [];
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(channel, (call) async {
|
||||
llamadas.add(call);
|
||||
switch (call.method) {
|
||||
case 'scheduleAlarm':
|
||||
return true;
|
||||
case 'getNativeSnoozeState':
|
||||
return snoozesNativos;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.setMockMethodCallHandler(channel, null);
|
||||
});
|
||||
|
||||
test('programar incluye snoozeUntilMillis y snoozeOriginMillis', () async {
|
||||
final servicio = ServicioAlarmasAndroid(channel: channel);
|
||||
final snoozeHasta = DateTime(2099, 1, 1, 7, 35);
|
||||
final snoozeOrigen = DateTime(2099, 1, 1, 7, 30);
|
||||
final alarma = AlarmaMusical(
|
||||
id: 'pay1',
|
||||
nombre: 'Con snooze',
|
||||
hora: 7,
|
||||
minuto: 30,
|
||||
tipoProgramacion: TipoProgramacionAlarma.diaria,
|
||||
diasSemana: const [],
|
||||
proximaEjecucion: DateTime(2099, 1, 2, 7, 30),
|
||||
snoozeHasta: snoozeHasta,
|
||||
snoozeOrigen: snoozeOrigen,
|
||||
);
|
||||
|
||||
await servicio.programar(alarma);
|
||||
|
||||
final llamada = llamadas.singleWhere((c) => c.method == 'scheduleAlarm');
|
||||
final args = llamada.arguments as Map<Object?, Object?>;
|
||||
expect(args['snoozeUntilMillis'], snoozeHasta.millisecondsSinceEpoch);
|
||||
expect(args['snoozeOriginMillis'], snoozeOrigen.millisecondsSinceEpoch);
|
||||
});
|
||||
|
||||
test(
|
||||
'obtenerEstadoSnoozeNativo invoca getNativeSnoozeState y parsea',
|
||||
() async {
|
||||
final servicio = ServicioAlarmasAndroid(channel: channel);
|
||||
final hasta = DateTime(2026, 6, 11, 7, 40);
|
||||
final origen = DateTime(2026, 6, 11, 7, 30);
|
||||
snoozesNativos.add({
|
||||
'alarmId': 'nat1',
|
||||
'snoozeUntilMillis': hasta.millisecondsSinceEpoch,
|
||||
'snoozeOriginMillis': origen.millisecondsSinceEpoch,
|
||||
});
|
||||
|
||||
final estados = await servicio.obtenerEstadoSnoozeNativo();
|
||||
|
||||
expect(llamadas.map((c) => c.method), contains('getNativeSnoozeState'));
|
||||
expect(estados, hasLength(1));
|
||||
expect(estados.single.alarmaId, 'nat1');
|
||||
expect(estados.single.snoozeHasta, hasta);
|
||||
expect(estados.single.snoozeOrigen, origen);
|
||||
},
|
||||
);
|
||||
|
||||
test('obtenerEstadoSnoozeNativo tolera lista vacia o nula', () async {
|
||||
final servicio = ServicioAlarmasAndroid(channel: channel);
|
||||
|
||||
expect(await servicio.obtenerEstadoSnoozeNativo(), isEmpty);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user