Skip to content

Commit

Permalink
Add test for custom error widget and did err-handling in qrScan
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayush0Chaudhary committed Jan 26, 2023
1 parent e4b9b80 commit 01432d1
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 8 deletions.
19 changes: 15 additions & 4 deletions lib/view_model/pre_auth_view_models/set_url_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:talawa/locator.dart';
import 'package:talawa/services/size_config.dart';
import 'package:talawa/utils/validators.dart';
Expand Down Expand Up @@ -87,8 +90,10 @@ class SetUrlViewModel extends BaseModel {
graphqlConfig.getOrgUrl();
navigationService.pushScreen(navigateTo, arguments: argument);
} else {
navigationService
.showSnackBar("URL doesn't exist/no connection please check");
// navigationService
// .showSnackBar("URL doesn't exist/no connection please check");
navigationService.showTalawaErrorWidget(
"URL doesn't exist/no connection please check");
}
}
}
Expand Down Expand Up @@ -165,9 +170,15 @@ class SetUrlViewModel extends BaseModel {
graphqlConfig.getOrgUrl();
Navigator.pop(navigationService.navigatorKey.currentContext!);
navigationService.pushScreen('/selectOrg', arguments: orgId);
} on CameraException catch (e) {
navigationService.showTalawaErrorWidget("The Camera is not working");
} on QrEmbeddedImageException catch (e) {
navigationService.showTalawaErrorDialog("The QR is not Working");
} on QrUnsupportedVersionException catch (e) {
navigationService
.showTalawaErrorDialog("This QR version is not Supported.");
} on Exception catch (e) {
print(e);
print('invalid app qr');
navigationService.showTalawaErrorWidget("This QR is not for the App");
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:hive/hive.dart';
import 'package:mockito/mockito.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:talawa/locator.dart';
import 'package:talawa/services/size_config.dart';
import 'package:talawa/utils/validators.dart';
Expand Down Expand Up @@ -89,8 +90,8 @@ Future<void> main() async {

await model.checkURLandNavigate('/', 'arguments');

verify(navigationService
.showSnackBar("URL doesn't exist/no connection please check"));
verify(navigationService.showTalawaErrorWidget(
"URL doesn't exist/no connection please check"));
});

testWidgets('Check if scanQR() is working fine', (tester) async {
Expand Down Expand Up @@ -123,7 +124,82 @@ Future<void> main() async {
.onQRViewCreated(controller);
});

testWidgets('Check if _onQRViewCreated() is working fine when throws',
testWidgets(
'Check if _onQRViewCreated() is working fine when throws CameraException',
(tester) async {
await tester.pumpWidget(
MaterialApp(
home: TestWidget(model),
navigatorKey: navigationService.navigatorKey,
),
);

final controller = MockQRViewController();
when(controller.scannedDataStream).thenAnswer((_) async* {
yield Barcode('qr?orgId=1&scan', BarcodeFormat.qrcode, null);
});
// when(controller.stopCamera())
// .thenThrow(Exception({"errorType": "error"}));

when(controller.stopCamera())
.thenThrow(CameraException("200", "cameraException"));
await tester.tap(find.byType(FloatingActionButton));
await tester.pump();

(tester.widget(find.byType(QRView)) as QRView)
.onQRViewCreated(controller);
});
testWidgets(
'Check if _onQRViewCreated() is working fine when throws QrEmbeddedImageException',
(tester) async {
await tester.pumpWidget(
MaterialApp(
home: TestWidget(model),
navigatorKey: navigationService.navigatorKey,
),
);

final controller = MockQRViewController();
when(controller.scannedDataStream).thenAnswer((_) async* {
yield Barcode('qr?orgId=1&scan', BarcodeFormat.qrcode, null);
});
// when(controller.stopCamera())
// .thenThrow(Exception({"errorType": "error"}));

when(controller.stopCamera())
.thenThrow(QrEmbeddedImageException("error"));
await tester.tap(find.byType(FloatingActionButton));
await tester.pump();

(tester.widget(find.byType(QRView)) as QRView)
.onQRViewCreated(controller);
});
testWidgets(
'Check if _onQRViewCreated() is working fine when throws QrUnsupportedVersionException',
(tester) async {
await tester.pumpWidget(
MaterialApp(
home: TestWidget(model),
navigatorKey: navigationService.navigatorKey,
),
);

final controller = MockQRViewController();
when(controller.scannedDataStream).thenAnswer((_) async* {
yield Barcode('qr?orgId=1&scan', BarcodeFormat.qrcode, null);
});
// when(controller.stopCamera())
// .thenThrow(Exception({"errorType": "error"}));

when(controller.stopCamera()).thenThrow(QrUnsupportedVersionException(0));
await tester.tap(find.byType(FloatingActionButton));
await tester.pump();

(tester.widget(find.byType(QRView)) as QRView)
.onQRViewCreated(controller);
});
testWidgets(
'Check if _onQRViewCreated() is working fine when throws Exception',
(tester) async {
await tester.pumpWidget(
MaterialApp(
Expand All @@ -136,8 +212,10 @@ Future<void> main() async {
when(controller.scannedDataStream).thenAnswer((_) async* {
yield Barcode('qr?orgId=1&scan', BarcodeFormat.qrcode, null);
});
when(controller.stopCamera()).thenThrow(Exception());
// when(controller.stopCamera())
// .thenThrow(Exception({"errorType": "error"}));

when(controller.stopCamera()).thenThrow(Exception(0));
await tester.tap(find.byType(FloatingActionButton));
await tester.pump();

Expand Down
73 changes: 73 additions & 0 deletions test/widget_tests/widgets/talawa_error_dialog_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:talawa/services/navigation_service.dart';
import 'package:talawa/services/size_config.dart';
import 'package:talawa/utils/app_localization.dart';
import 'package:talawa/widgets/talawa_error_dialog.dart';

import '../../helpers/test_locator.dart';
import 'custom_alert_dialog_test.dart';

Widget createTalawaErrorWidget({
bool reverse = false,
String? dialogTitle,
bool passSecondaryFunc = true,
}) {
return MaterialApp(
navigatorKey: locator<NavigationService>().navigatorKey,
navigatorObservers: [],
locale: const Locale('en'),
supportedLocales: [
const Locale('en', 'US'),
const Locale('es', 'ES'),
const Locale('fr', 'FR'),
const Locale('hi', 'IN'),
const Locale('zh', 'CN'),
const Locale('de', 'DE'),
const Locale('ja', 'JP'),
const Locale('pt', 'PT'),
],
localizationsDelegates: [
const AppLocalizationsDelegate(isTest: true),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
home: Scaffold(
body: TextButton(
child: const Text('Open'),
onPressed: () {
navigationService.showTalawaErrorDialog("Test Error");
},
),
),
);
}

void main() {
SizeConfig().test();
testSetupLocator();
group('Test for TalawaErrorWidget', () {
testWidgets('Check if the Error Widget shows up', (tester) async {
await tester.pumpWidget(createTalawaErrorWidget());
await tester.pump();
await tester.tap(find.textContaining('Open'));
await tester.pump(const Duration(seconds: 1));

expect(find.byType(TalawaErrorDialog), findsOneWidget);
});

testWidgets('Check if the close Button is working', (tester) async {
await tester.pumpWidget(createTalawaErrorWidget());
await tester.pump();

await tester.tap(find.textContaining('Open'));
await tester.pump(const Duration(seconds: 1));

await tester.tap(find.textContaining('Close'));
await tester.pump(const Duration(seconds: 1));

expect(find.byType(TalawaErrorDialog), findsNothing);
});
});
}
58 changes: 58 additions & 0 deletions test/widget_tests/widgets/talawa_error_widget_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:talawa/services/navigation_service.dart';
import 'package:talawa/services/size_config.dart';
import 'package:talawa/utils/app_localization.dart';
import 'package:talawa/widgets/talawa_error_widget.dart';

import '../../helpers/test_locator.dart';

Widget createTalawaErrorWidget({
bool reverse = false,
String? dialogTitle,
bool passSecondaryFunc = true,
}) {
return MaterialApp(
navigatorKey: locator<NavigationService>().navigatorKey,
navigatorObservers: [],
locale: const Locale('en'),
supportedLocales: [
const Locale('en', 'US'),
const Locale('es', 'ES'),
const Locale('fr', 'FR'),
const Locale('hi', 'IN'),
const Locale('zh', 'CN'),
const Locale('de', 'DE'),
const Locale('ja', 'JP'),
const Locale('pt', 'PT'),
],
localizationsDelegates: [
const AppLocalizationsDelegate(isTest: true),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
home: Scaffold(
body: TextButton(
child: const Text('Open'),
onPressed: () {
navigationService.showTalawaErrorWidget("Test Error");
},
),
),
);
}

void main() {
SizeConfig().test();
testSetupLocator();
group('Test for TalawaErrorWidget', () {
testWidgets('Check if the Snackbar shows up', (tester) async {
await tester.pumpWidget(createTalawaErrorWidget());
await tester.pump();
await tester.tap(find.textContaining('Open'));
await tester.pump(const Duration(seconds: 1));
expect(find.byType(TalawaErrorWidget), findsOneWidget);
});
});
}

0 comments on commit 01432d1

Please sign in to comment.