150 lines
5.5 KiB
Dart
150 lines
5.5 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:barcode_reader_app/core/services/feedback_service.dart';
|
|
import 'package:barcode_reader_app/features/connection/data/datasources/connection_local_datasource.dart';
|
|
import 'package:barcode_reader_app/features/connection/data/datasources/connection_remote_datasource.dart';
|
|
import 'package:barcode_reader_app/features/connection/data/datasources/udp_discovery_datasource.dart';
|
|
import 'package:barcode_reader_app/features/connection/data/repositories/connection_repository_impl.dart';
|
|
import 'package:barcode_reader_app/features/connection/domain/entities/server_connection_info.dart';
|
|
import 'package:barcode_reader_app/features/connection/domain/repositories/connection_repository.dart';
|
|
import 'package:barcode_reader_app/features/connection/presentation/cubit/connection_cubit.dart';
|
|
import 'package:barcode_reader_app/features/scanner/data/datasources/scan_history_datasource.dart';
|
|
import 'package:barcode_reader_app/features/scanner/data/repositories/scan_repository_impl.dart';
|
|
import 'package:barcode_reader_app/features/scanner/domain/entities/scan_record.dart';
|
|
import 'package:barcode_reader_app/features/scanner/presentation/cubit/history_cubit.dart';
|
|
import 'package:barcode_reader_app/features/scanner/presentation/cubit/scanner_cubit.dart';
|
|
import 'package:barcode_reader_app/features/settings/data/datasources/settings_local_datasource.dart';
|
|
import 'package:barcode_reader_app/features/settings/data/repositories/settings_repository_impl.dart';
|
|
import 'package:barcode_reader_app/features/settings/presentation/cubit/settings_cubit.dart';
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
late SharedPreferences prefs;
|
|
late SettingsRepositoryImpl settingsRepo;
|
|
late ConnectionRepositoryImpl connectionRepo;
|
|
late ScanRepositoryImpl scanRepo;
|
|
|
|
setUp(() async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
prefs = await SharedPreferences.getInstance();
|
|
|
|
settingsRepo = SettingsRepositoryImpl(SettingsLocalDataSource(prefs));
|
|
connectionRepo = ConnectionRepositoryImpl(
|
|
localDataSource: ConnectionLocalDataSource(prefs),
|
|
remoteDataSource: ConnectionRemoteDataSource(),
|
|
discoveryDataSource: const UdpDiscoveryDataSource(),
|
|
);
|
|
scanRepo = ScanRepositoryImpl(ScanHistoryDataSource(prefs));
|
|
});
|
|
|
|
tearDown(() {
|
|
connectionRepo.dispose();
|
|
});
|
|
|
|
group('SettingsCubit', () {
|
|
test('updates suffix and persists', () async {
|
|
final cubit = SettingsCubit(settingsRepo);
|
|
await cubit.loadSettings();
|
|
expect(cubit.state.settings.suffix, 'enter');
|
|
|
|
await cubit.updateSuffix('tab');
|
|
expect(cubit.state.settings.suffix, 'tab');
|
|
|
|
final reloaded = await settingsRepo.loadSettings();
|
|
expect(reloaded.suffix, 'tab');
|
|
});
|
|
|
|
test('updates continuous mode and feedback settings', () async {
|
|
final cubit = SettingsCubit(settingsRepo);
|
|
await cubit.loadSettings();
|
|
|
|
await cubit.updateContinuousMode(true);
|
|
expect(cubit.state.settings.continuousMode, true);
|
|
|
|
await cubit.updateFeedback(vibrate: false, sound: false);
|
|
expect(cubit.state.settings.vibrate, false);
|
|
expect(cubit.state.settings.sound, false);
|
|
});
|
|
});
|
|
|
|
group('HistoryCubit', () {
|
|
test('adds scan and filters correctly', () async {
|
|
final cubit = HistoryCubit(scanRepo);
|
|
await cubit.loadHistory();
|
|
expect(cubit.state.history, isEmpty);
|
|
|
|
final record1 = ScanRecord(
|
|
id: '1',
|
|
code: '1234567890',
|
|
format: 'EAN_13',
|
|
timestamp: DateTime.now(),
|
|
sentSuccessfully: true,
|
|
);
|
|
final record2 = ScanRecord(
|
|
id: '2',
|
|
code: 'ABC-999',
|
|
format: 'CODE_128',
|
|
timestamp: DateTime.now(),
|
|
sentSuccessfully: false,
|
|
);
|
|
|
|
await cubit.addScan(record1);
|
|
await cubit.addScan(record2);
|
|
|
|
expect(cubit.state.history.length, 2);
|
|
|
|
cubit.setSearchQuery('ABC');
|
|
expect(cubit.state.filteredHistory.length, 1);
|
|
expect(cubit.state.filteredHistory.first.code, 'ABC-999');
|
|
|
|
cubit.setSearchQuery('');
|
|
expect(cubit.state.filteredHistory.length, 2);
|
|
|
|
await cubit.deleteScan('1');
|
|
expect(cubit.state.history.length, 1);
|
|
expect(cubit.state.history.first.id, '2');
|
|
|
|
await cubit.clearHistory();
|
|
expect(cubit.state.history, isEmpty);
|
|
});
|
|
});
|
|
|
|
group('ConnectionCubit', () {
|
|
test('starts disconnected and handles manual disconnect', () async {
|
|
final cubit = ConnectionCubit(connectionRepo);
|
|
await cubit.initialize();
|
|
|
|
expect(cubit.state.status, ConnectionStatus.disconnected);
|
|
|
|
await cubit.disconnect();
|
|
expect(cubit.state.status, ConnectionStatus.disconnected);
|
|
expect(cubit.state.statusMessage, 'Disconnesso manualmente');
|
|
});
|
|
});
|
|
|
|
group('ScannerCubit', () {
|
|
test('toggles torch and camera', () {
|
|
final settingsCubit = SettingsCubit(settingsRepo);
|
|
final connectionCubit = ConnectionCubit(connectionRepo);
|
|
final historyCubit = HistoryCubit(scanRepo);
|
|
|
|
final scannerCubit = ScannerCubit(
|
|
connectionCubit: connectionCubit,
|
|
settingsCubit: settingsCubit,
|
|
historyCubit: historyCubit,
|
|
feedbackService: const FeedbackService(),
|
|
);
|
|
|
|
expect(scannerCubit.state.isTorchOn, false);
|
|
scannerCubit.toggleTorch();
|
|
expect(scannerCubit.state.isTorchOn, true);
|
|
|
|
expect(scannerCubit.state.isFrontCamera, false);
|
|
scannerCubit.toggleCamera();
|
|
expect(scannerCubit.state.isFrontCamera, true);
|
|
});
|
|
});
|
|
}
|
|
|