ModernMemoires/test/preferences_service_test.dart

205 lines
7.5 KiB
Dart

import 'dart:ui';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:moody/utils/definitions/color_pair.dart';
import 'package:moody/utils/logic/preferences_service.dart';
import 'package:shared_preferences/shared_preferences.dart';
class MockSharedPreferences extends Mock implements SharedPreferences {}
void main() {
group('PreferencesService Tests', () {
PreferencesService service = PreferencesService();
setUp(() {
SharedPreferences.setMockInitialValues({}); // Set initial values for mocks
});
test('saveColorPair and loadColorPair should work correctly', () async {
var testColorPair = ColorPair(name: "test", textColor: const Color(0xff123456), backgroundColor: const Color(0xff654321));
// Save color pair
await service.saveColorPair(testColorPair);
// Load color pair
var loadedColorPair = await service.loadColorPair();
// Compare values
expect(loadedColorPair.textColor.value, equals(testColorPair.textColor.value));
expect(loadedColorPair.backgroundColor.value, equals(testColorPair.backgroundColor.value));
});
test('loadColorPair should return default value if none set', () async {
// Load color pair without setting any value
var defaultColorPair = await service.loadColorPair();
// Verify default values
expect(defaultColorPair, equals(colorPairs[0]));
});
// Test for setting and checking PIN
test('setPin and checkPin should work correctly', () async {
const int testPin = 1234;
// Set PIN
await service.setPin(testPin);
// Check PIN
bool isCorrectPin = await service.checkPin(testPin);
expect(isCorrectPin, isTrue);
// Check with incorrect PIN
isCorrectPin = await service.checkPin(9999);
expect(isCorrectPin, isFalse);
});
test('enablePin should enable PIN if it\'s already set', () async {
// Set PIN
const int testPin = 1234;
await service.setPin(testPin);
// Attempt to enable PIN
await service.enablePin();
var isPinEnabled = await service.isPinEnabled();
expect(isPinEnabled, isTrue);
// Disable PIN again
await service.disablePin();
isPinEnabled = await service.isPinEnabled();
expect(isPinEnabled, isFalse);
});
// Test for enabling PIN only if it's set
test('enablePin should only enable if a PIN is set', () async {
// Assuming no PIN is set initially
await service.disablePin();
// Attempt to enable PIN without setting it
await service.enablePin();
var isPinEnabled = await service.isPinEnabled();
expect(isPinEnabled, isFalse);
// Set PIN and then enable it
const int testPin = 1234;
await service.setPin(testPin);
await service.enablePin();
isPinEnabled = await service.isPinEnabled();
expect(isPinEnabled, isTrue);
// Disable PIN again
await service.disablePin();
isPinEnabled = await service.isPinEnabled();
expect(isPinEnabled, isFalse);
});
// Test for isPinEnabled
test('isPinEnabled should return correct status', () async {
// Assuming PIN is not set
bool isPinEnabled = await service.isPinEnabled();
expect(isPinEnabled, isFalse);
// After setting and enabling PIN
await service.setPin(1234);
await service.enablePin();
isPinEnabled = await service.isPinEnabled();
expect(isPinEnabled, isTrue);
});
// Test for calculateStreak
test('calculateStreak should return the correct streak count', () async {
// Scenario 1: Consecutive diary entries
await service.saveDiaryEntry(DiaryEntry(date: DateTime.now(), percentValue: 80, texts: ["Entry 1"]));
await service.saveDiaryEntry(DiaryEntry(date: DateTime.now().subtract(const Duration(days: 1)), percentValue: 90, texts: ["Entry 2"]));
await service.saveDiaryEntry(DiaryEntry(date: DateTime.now().subtract(const Duration(days: 2)), percentValue: 95, texts: ["Entry 3"]));
var streakCount = await service.calculateStreak();
expect(streakCount, equals(3));
// Scenario 2: Missing entries in between
await service.saveDiaryEntry(DiaryEntry(date: DateTime.now().subtract(const Duration(days: 2)), percentValue: 95, texts: ["Entry 2"]));
streakCount = await service.calculateStreak();
expect(streakCount, equals(3)); // Streak is broken due to missing entries
// Scenario 3: No diary entries
// No need to add entries here
streakCount = await service.calculateStreak();
expect(streakCount, equals(3)); // No entries, streak is 0
});
service = PreferencesService();
// Test for getDiaryEntryByCurrentDate
test('getDiaryEntryByCurrentDate should return the correct entry for the current date', () async {
final currentDate = DateTime.now();
final formattedDate = DateTime(currentDate.year, currentDate.month, currentDate.day);
final DiaryEntry testEntry = DiaryEntry(
date: formattedDate,
percentValue: 80,
texts: ["Entry for Today"],
);
// Save a test entry for the current date
await service.saveDiaryEntry(testEntry);
final DiaryEntry? retrievedEntry = await service.getDiaryEntryByCurrentDate();
expect(retrievedEntry?.texts.join(" "), equals((testEntry.texts.join(" "))));
expect(retrievedEntry?.date, equals(formattedDate));
expect(retrievedEntry?.percentValue, testEntry.percentValue);
});
// Test for getDiaryEntryByDate
test('getDiaryEntryByCurrentDate should return the correct entry for the current date', () async {
final currentDate = DateTime.now();
final formattedDate = DateTime(currentDate.year, currentDate.month, currentDate.day);
final DiaryEntry testEntry = DiaryEntry(
date: formattedDate,
percentValue: 80,
texts: ["Entry for Today"],
);
// Save a test entry for the current date
await service.saveDiaryEntry(testEntry);
final DiaryEntry? retrievedEntry = await service.getDiaryEntryByCurrentDate();
expect(retrievedEntry?.texts.join(" "), equals((testEntry.texts.join(" "))));
expect(retrievedEntry?.date, equals(formattedDate));
expect(retrievedEntry?.percentValue, testEntry.percentValue);
});
test('saveDiaryEntry and loadDiaryEntries should work correctly', () async {
final List<DiaryEntry> testEntries = [
DiaryEntry(
date: DateTime(2024, 1, 10), // Updated date format
percentValue: 70,
texts: ["Entry 1"],
),
DiaryEntry(
date: DateTime(2024, 1, 11), // Updated date format
percentValue: 80,
texts: ["Entry 2"],
),
DiaryEntry(
date: DateTime(2024, 1, 12), // Updated date format
percentValue: 90,
texts: ["Entry 3"],
),
];
// Save test entries
for (final entry in testEntries) {
await service.saveDiaryEntry(entry);
}
// Load saved entries
final List<DiaryEntry> loadedEntries = await service.loadDiaryEntries();
// Compare loaded entries with test entries
expect(loadedEntries.length, equals(testEntries.length));
for (int i = 0; i < loadedEntries.length; i++) {
expect(loadedEntries[i].date, equals(testEntries[i].date));
expect(loadedEntries[i].percentValue, equals(testEntries[i].percentValue));
expect(loadedEntries[i].texts, equals(testEntries[i].texts));
}
});
});
}