migrateOldKeys() tests

This commit is contained in:
Vadim 2023-06-22 22:05:55 +02:00
parent ed09fadd07
commit f611434d51
2 changed files with 65 additions and 2 deletions

View file

@ -30,10 +30,11 @@ class UserPreferencesService {
final SharedPreferences _sharedPreferences; final SharedPreferences _sharedPreferences;
UserPreferencesService(this._sharedPreferences) { UserPreferencesService(this._sharedPreferences) {
_migrateOldKeys(); migrateOldKeys();
} }
Future<void> _migrateOldKeys() async { @visibleForTesting
Future<void> migrateOldKeys() async {
final legacyIsoIndex = _sharedPreferences.getInt("curIsoIndex"); final legacyIsoIndex = _sharedPreferences.getInt("curIsoIndex");
if (legacyIsoIndex != null) { if (legacyIsoIndex != null) {
iso = IsoValue.values[legacyIsoIndex]; iso = IsoValue.values[legacyIsoIndex];

View file

@ -22,6 +22,68 @@ void main() {
service = UserPreferencesService(sharedPreferences); service = UserPreferencesService(sharedPreferences);
}); });
group('migrateOldKeys()', () {
test('no legacy keys', () async {
when(() => sharedPreferences.getInt("curIsoIndex")).thenReturn(null);
when(() => sharedPreferences.getInt("curndIndex")).thenReturn(null);
when(() => sharedPreferences.getDouble("cameraCalibr")).thenReturn(null);
when(() => sharedPreferences.getDouble("sensorCalibr")).thenReturn(null);
when(() => sharedPreferences.getBool("vibrate")).thenReturn(null);
when(() => sharedPreferences.remove("curIsoIndex")).thenAnswer((_) async => true);
when(() => sharedPreferences.remove("curndIndex")).thenAnswer((_) async => true);
when(() => sharedPreferences.remove("cameraCalibr")).thenAnswer((_) async => true);
when(() => sharedPreferences.remove("sensorCalibr")).thenAnswer((_) async => true);
when(() => sharedPreferences.remove("vibrate")).thenAnswer((_) async => true);
await service.migrateOldKeys();
verifyNever(() => sharedPreferences.remove("curIsoIndex"));
verifyNever(() => sharedPreferences.remove("curndIndex"));
verifyNever(() => sharedPreferences.remove("cameraCalibr"));
verifyNever(() => sharedPreferences.remove("sensorCalibr"));
verifyNever(() => sharedPreferences.remove("vibrate"));
});
test('migrate all keys', () async {
when(() => sharedPreferences.getInt("curIsoIndex")).thenReturn(1);
when(() => sharedPreferences.getInt("curndIndex")).thenReturn(0);
when(() => sharedPreferences.getDouble("cameraCalibr")).thenReturn(1.0);
when(() => sharedPreferences.getDouble("sensorCalibr")).thenReturn(-1.0);
when(() => sharedPreferences.getBool("vibrate")).thenReturn(false);
when(
() => sharedPreferences.setInt(UserPreferencesService.isoKey, IsoValue.values[1].value),
).thenAnswer((_) => Future.value(true));
when(
() => sharedPreferences.setInt(UserPreferencesService.ndFilterKey, NdValue.values[0].value),
).thenAnswer((_) => Future.value(true));
when(
() => sharedPreferences.setDouble(UserPreferencesService.cameraEvCalibrationKey, 1.0),
).thenAnswer((_) => Future.value(true));
when(
() => sharedPreferences.setDouble(UserPreferencesService.lightSensorEvCalibrationKey, -1.0),
).thenAnswer((_) => Future.value(true));
when(
() => sharedPreferences.setBool(UserPreferencesService.hapticsKey, false),
).thenAnswer((_) => Future.value(true));
when(() => sharedPreferences.remove("curIsoIndex")).thenAnswer((_) async => true);
when(() => sharedPreferences.remove("curndIndex")).thenAnswer((_) async => true);
when(() => sharedPreferences.remove("cameraCalibr")).thenAnswer((_) async => true);
when(() => sharedPreferences.remove("sensorCalibr")).thenAnswer((_) async => true);
when(() => sharedPreferences.remove("vibrate")).thenAnswer((_) async => true);
await service.migrateOldKeys();
verify(() => sharedPreferences.remove("curIsoIndex")).called(1);
verify(() => sharedPreferences.remove("curndIndex")).called(1);
verify(() => sharedPreferences.remove("cameraCalibr")).called(1);
verify(() => sharedPreferences.remove("sensorCalibr")).called(1);
verify(() => sharedPreferences.remove("vibrate")).called(1);
});
});
group('iso', () { group('iso', () {
test('get default', () { test('get default', () {
when(() => sharedPreferences.getInt(UserPreferencesService.isoKey)).thenReturn(null); when(() => sharedPreferences.getInt(UserPreferencesService.isoKey)).thenReturn(null);