El editor dejaba la fecha clavada en hoy, asi que al escribir 05:30 siendo las 18:30 el candidato quedaba en el pasado, `calcularProxima` lo rechazaba con razon y la alarma se guardaba sin proxima ejecucion: nunca sonaba. En la practica solo se podian poner alarmas unicas para lo que quedaba de dia. `normalizarFechaUnica` resuelve el dia que el usuario realmente quiere, con la convencion de cualquier despertador: hora ya pasada -> manana, hora por llegar -> hoy. Una fecha elegida a proposito en el futuro no se toca nunca, y una fecha rancia salta a hoy/manana en vez de a `fecha + 1`, que seguiria en el pasado. La regla vive en el editor y NO dentro de `calcularProxima`: esa tiene que seguir siendo literal, porque el recalculo posterior al disparo y el motor nativo dependen de que una alarma unica vencida resuelva a null en vez de resucitar al dia siguiente. De paso el editor pasa a leer el reloj inyectado del servicio en vez de `DateTime.now()`, para que la vista previa, los limites del selector de fecha y el ajuste lean el mismo instante y se puedan fijar en las pruebas.
305 lines
10 KiB
Dart
305 lines
10 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:intl/date_symbol_data_local.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:pluriwave/estado/estado_alarmas.dart';
|
|
import 'package:pluriwave/estado/estado_radio.dart';
|
|
import 'package:pluriwave/l10n/formato_fechas.dart';
|
|
import 'package:pluriwave/l10n/gen/app_localizations.dart';
|
|
import 'package:pluriwave/modelos/alarma_musical.dart';
|
|
import 'package:pluriwave/pantallas/pantalla_alarmas.dart';
|
|
import 'package:pluriwave/servicios/servicio_alarmas.dart';
|
|
import 'package:pluriwave/servicios/servicio_anuncios.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import '../helpers/fakes.dart';
|
|
import '../helpers/fakes_alarmas.dart';
|
|
|
|
/// S5-R4: short dates must follow the active locale, not a hardcoded
|
|
/// DD/MM/YYYY pattern.
|
|
void main() {
|
|
test('en-US usa el orden mes/día, no el formato fijo DD/MM/YYYY', () {
|
|
final fecha = DateTime(2026, 6, 11);
|
|
final resultado = fechaCortaLocalizada('en-US', fecha);
|
|
|
|
expect(resultado, DateFormat.yMd('en-US').format(fecha));
|
|
expect(resultado, isNot('11/06/2026'));
|
|
expect(resultado, '6/11/2026');
|
|
});
|
|
|
|
test('es usa el orden día/mes', () async {
|
|
await initializeDateFormatting('es');
|
|
final fecha = DateTime(2026, 6, 11);
|
|
|
|
expect(
|
|
fechaCortaLocalizada('es', fecha),
|
|
DateFormat.yMd('es').format(fecha),
|
|
);
|
|
});
|
|
|
|
group('WU10 — la seccion Avanzada del editor conserva fecha, respaldo y '
|
|
'sonido', () {
|
|
final l10n = lookupAppLocalizations(const Locale('es'));
|
|
|
|
setUp(() {
|
|
SharedPreferences.setMockInitialValues({});
|
|
});
|
|
|
|
Future<EstadoAlarmas> abrirEditorNuevo(
|
|
WidgetTester tester, {
|
|
DateTime Function()? reloj,
|
|
}) async {
|
|
tester.view.physicalSize = const Size(1440, 3200);
|
|
tester.view.devicePixelRatio = 1.0;
|
|
addTearDown(tester.view.resetPhysicalSize);
|
|
addTearDown(tester.view.resetDevicePixelRatio);
|
|
|
|
final favoritos = FakeServicioFavoritos();
|
|
await favoritos.agregar(emisoraDemo(uuid: 'gamma', nombre: 'Gamma FM'));
|
|
final radio = EstadoRadio(
|
|
esPremium: () => true,
|
|
audio: FakeServicioAudio(),
|
|
favoritos: favoritos,
|
|
radio: FakeServicioRadio(),
|
|
servicioEcualizador: FakeServicioEcualizador(),
|
|
servicioGrabacion: FakeServicioGrabacionRadioInactiva(),
|
|
iniciarAutomaticamente: false,
|
|
);
|
|
addTearDown(radio.dispose);
|
|
await radio.cargarFavoritos();
|
|
|
|
final android = FakePuertoAlarmasAndroid();
|
|
final estadoAlarmas = EstadoAlarmas(
|
|
esPremium: () => true,
|
|
servicio: ServicioAlarmas(reloj: reloj ?? DateTime.now),
|
|
android: android,
|
|
iniciarAutomaticamente: false,
|
|
);
|
|
addTearDown(estadoAlarmas.dispose);
|
|
addTearDown(android.dispose);
|
|
|
|
await tester.pumpWidget(
|
|
MultiProvider(
|
|
providers: [
|
|
ChangeNotifierProvider<EstadoRadio>.value(value: radio),
|
|
ChangeNotifierProvider<EstadoAlarmas>.value(value: estadoAlarmas),
|
|
Provider<ServicioAnuncios>(
|
|
create: (_) => ServicioAnuncios(esPremium: () => true),
|
|
),
|
|
],
|
|
child: MaterialApp(
|
|
locale: const Locale('es'),
|
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
|
home: const Scaffold(body: PantallaAlarmas()),
|
|
),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.tap(find.text(l10n.createAlarmAction));
|
|
await tester.pumpAndSettle();
|
|
|
|
return estadoAlarmas;
|
|
}
|
|
|
|
Future<void> expandirAvanzado(WidgetTester tester) async {
|
|
await tester.ensureVisible(find.text(l10n.alarmAdvancedSectionTitle));
|
|
await tester.tap(find.text(l10n.alarmAdvancedSectionTitle));
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
testWidgets(
|
|
'los circulos de dia de la semana son siempre visibles, incluso '
|
|
'cuando el tipo es Una vez (por defecto en una alarma nueva)',
|
|
(tester) async {
|
|
await abrirEditorNuevo(tester);
|
|
|
|
expect(find.text(l10n.weekdayShortMonday), findsOneWidget);
|
|
expect(find.text(l10n.weekdayShortSunday), findsOneWidget);
|
|
},
|
|
);
|
|
|
|
testWidgets(
|
|
'permite crear una alarma de fecha unica desde la seccion Avanzada '
|
|
'(colapsada por defecto)',
|
|
(tester) async {
|
|
final estadoAlarmas = await abrirEditorNuevo(tester);
|
|
|
|
// A brand-new alarm already defaults to "one time" — the date field
|
|
// is reachable as soon as Advanced is expanded, no mode switch
|
|
// needed first.
|
|
await expandirAvanzado(tester);
|
|
|
|
await tester.ensureVisible(find.text(l10n.dateField));
|
|
await tester.tap(find.text(l10n.dateField));
|
|
await tester.pumpAndSettle();
|
|
|
|
final okLabel =
|
|
MaterialLocalizations.of(
|
|
tester.element(find.text(l10n.dateField)),
|
|
).okButtonLabel;
|
|
await tester.tap(find.text(okLabel));
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.ensureVisible(find.text(l10n.saveAlarmAction));
|
|
await tester.tap(find.text(l10n.saveAlarmAction));
|
|
await tester.pumpAndSettle();
|
|
|
|
final alarma = estadoAlarmas.alarmas.single;
|
|
expect(alarma.tipoProgramacion, TipoProgramacionAlarma.unica);
|
|
expect(alarma.fechaUnica, isNotNull);
|
|
expect(alarma.diasSemana, isEmpty);
|
|
},
|
|
);
|
|
|
|
testWidgets(
|
|
'el selector de emisora de respaldo sigue alcanzable desde Avanzado '
|
|
'y persiste (S2-R9)',
|
|
(tester) async {
|
|
final estadoAlarmas = await abrirEditorNuevo(tester);
|
|
|
|
await expandirAvanzado(tester);
|
|
|
|
await tester.ensureVisible(
|
|
find.byKey(const ValueKey('alarm-fallback-station-field')),
|
|
);
|
|
await tester.tap(
|
|
find.byKey(const ValueKey('alarm-fallback-station-field')),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
// Scoped to the just-opened picker sheet's list: the primary
|
|
// station field auto-selects the sole registered favorite too
|
|
// (`EstadoRadio.emisoraPreferida` falls back to the first
|
|
// favorite), so an unscoped `find.text('Gamma FM')` would match
|
|
// twice — once there, once in this sheet.
|
|
final lista = find.byType(ListView).last;
|
|
expect(
|
|
find.descendant(of: lista, matching: find.text('Gamma FM')),
|
|
findsOneWidget,
|
|
);
|
|
await tester.tap(
|
|
find.descendant(of: lista, matching: find.text('Gamma FM')),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.ensureVisible(find.text(l10n.saveAlarmAction));
|
|
await tester.tap(find.text(l10n.saveAlarmAction));
|
|
await tester.pumpAndSettle();
|
|
|
|
final alarma = estadoAlarmas.alarmas.single;
|
|
expect(alarma.emisoraFallback?.nombre, 'Gamma FM');
|
|
},
|
|
);
|
|
|
|
testWidgets(
|
|
'el dropdown de sonido interno sigue alcanzable desde Avanzado y '
|
|
'persiste',
|
|
(tester) async {
|
|
final estadoAlarmas = await abrirEditorNuevo(tester);
|
|
|
|
await expandirAvanzado(tester);
|
|
|
|
await tester.ensureVisible(
|
|
find.byType(DropdownButtonFormField<SonidoInternoAlarma>),
|
|
);
|
|
await tester.tap(
|
|
find.byType(DropdownButtonFormField<SonidoInternoAlarma>),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.tap(find.text(l10n.soundSoftBell).last);
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.ensureVisible(find.text(l10n.saveAlarmAction));
|
|
await tester.tap(find.text(l10n.saveAlarmAction));
|
|
await tester.pumpAndSettle();
|
|
|
|
final alarma = estadoAlarmas.alarmas.single;
|
|
expect(alarma.sonidoInterno, SonidoInternoAlarma.campanaSuave);
|
|
},
|
|
);
|
|
|
|
// The 18:30 -> 05:30 report: with a real clock the suite could only ever
|
|
// assert vague invariants, so the editor reads the alarm service's
|
|
// injected clock and these two pin it to a fixed wall-clock instant.
|
|
DateTime tardeDelMartes() => DateTime(2026, 9, 22, 18, 30);
|
|
|
|
/// Taps the hour segment [veces] times. Tap increments and wraps, so 11
|
|
/// taps move 18h -> 05h. A vertical drag cannot be used here: the sheet
|
|
/// scrolls, and the scrollable wins the gesture arena.
|
|
Future<void> subirHora(WidgetTester tester, int veces) async {
|
|
final hora = find.byKey(const ValueKey('editor-hora-inline-hora'));
|
|
await tester.ensureVisible(hora);
|
|
for (var i = 0; i < veces; i++) {
|
|
await tester.tap(hora);
|
|
await tester.pump();
|
|
}
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
testWidgets('una alarma de un unico uso puesta a una hora ya pasada hoy se '
|
|
'programa para manana, no se queda sin proxima ejecucion', (
|
|
tester,
|
|
) async {
|
|
final estadoAlarmas = await abrirEditorNuevo(
|
|
tester,
|
|
reloj: tardeDelMartes,
|
|
);
|
|
|
|
// Opens at 18:35 (now + 5 min); 11 taps wrap the hour to 05:35,
|
|
// which already passed today.
|
|
await subirHora(tester, 11);
|
|
|
|
await tester.ensureVisible(find.text(l10n.saveAlarmAction));
|
|
await tester.tap(find.text(l10n.saveAlarmAction));
|
|
await tester.pumpAndSettle();
|
|
|
|
final alarma = estadoAlarmas.alarmas.single;
|
|
expect(alarma.tipoProgramacion, TipoProgramacionAlarma.unica);
|
|
expect(alarma.hora, 5);
|
|
expect(alarma.minuto, 35);
|
|
expect(alarma.proximaProgramable, DateTime(2026, 9, 23, 5, 35));
|
|
});
|
|
|
|
testWidgets(
|
|
'una hora todavia por llegar sigue sonando hoy mismo, no manana',
|
|
(tester) async {
|
|
final estadoAlarmas = await abrirEditorNuevo(
|
|
tester,
|
|
reloj: tardeDelMartes,
|
|
);
|
|
|
|
// 18:35 + 3 taps = 21:35, still ahead of 18:30.
|
|
await subirHora(tester, 3);
|
|
|
|
await tester.ensureVisible(find.text(l10n.saveAlarmAction));
|
|
await tester.tap(find.text(l10n.saveAlarmAction));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(
|
|
estadoAlarmas.alarmas.single.proximaProgramable,
|
|
DateTime(2026, 9, 22, 21, 35),
|
|
);
|
|
},
|
|
);
|
|
|
|
testWidgets('la vista previa deja de decir "sin proxima ejecucion" '
|
|
'cuando la hora ya paso hoy', (tester) async {
|
|
await abrirEditorNuevo(tester, reloj: tardeDelMartes);
|
|
|
|
await subirHora(tester, 11);
|
|
|
|
final aviso = tester.widget<Text>(
|
|
find.descendant(
|
|
of: find.byKey(const ValueKey('next-trigger-preview')),
|
|
matching: find.byType(Text),
|
|
),
|
|
);
|
|
expect(aviso.data, isNot(l10n.alarmNoNextExecution));
|
|
});
|
|
});
|
|
}
|