From 9057f41baa6dac7668000eba036fc8715ca5a4aa Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Sun, 22 Dec 2024 01:40:48 +0530 Subject: [PATCH 01/17] upgrade connectivity_plus from 5.0.2 to 6.0.5 with necessary adjustments --- .../connectivity_service.dart | 17 ++--- lib/view_model/connectivity_view_model.dart | 17 +++-- pubspec.lock | 8 +-- pubspec.yaml | 2 +- .../connectivity_service_test.dart | 66 +++++++++++++------ .../connectivity_view_model_test.dart | 8 +-- 6 files changed, 74 insertions(+), 44 deletions(-) diff --git a/lib/services/third_party_service/connectivity_service.dart b/lib/services/third_party_service/connectivity_service.dart index d1e95ff5d..f88944a7b 100644 --- a/lib/services/third_party_service/connectivity_service.dart +++ b/lib/services/third_party_service/connectivity_service.dart @@ -15,10 +15,10 @@ import 'package:talawa/locator.dart'; /// * Checking if the device has any type of network connection - [hasConnection] class ConnectivityService { /// Stream controller for network status changes. - late StreamController connectionStatusController; + late StreamController> connectionStatusController; /// Getter for the stream of connection status changes. - Stream get connectionStream => + Stream> get connectionStream => connectionStatusController.stream; /// Checks the current internet connectivity status of the device. @@ -27,8 +27,8 @@ class ConnectivityService { /// None /// /// **returns**: - /// * `Future`: indicates if the url is reachable. - Future getConnectionType() async { + /// * `Future>`: indicates if the url is reachable. + Future> getConnectionType() async { final result = await connectivity.checkConnectivity(); return result; } @@ -45,7 +45,7 @@ class ConnectivityService { /// None Future initConnectivity({required http.Client client}) async { _client = client; - connectionStatusController = StreamController(); + connectionStatusController = StreamController>(); /// Listen for future changes in connectivity enableSubscription(); @@ -60,7 +60,7 @@ class ConnectivityService { /// None Future enableSubscription() async { connectivity.onConnectivityChanged.listen( - (ConnectivityResult result) { + (List result) { print(result); connectionStatusController.add(result); }, @@ -107,8 +107,9 @@ class ConnectivityService { /// * `Future`: indicating whether the device has a network connection. Future hasConnection() async { try { - final result = await getConnectionType(); - return result != ConnectivityResult.none; + final results = await getConnectionType(); + return results.isNotEmpty && + results.any((result) => result != ConnectivityResult.none); } catch (e) { return false; } diff --git a/lib/view_model/connectivity_view_model.dart b/lib/view_model/connectivity_view_model.dart index 4b9bec469..fbec74e0b 100644 --- a/lib/view_model/connectivity_view_model.dart +++ b/lib/view_model/connectivity_view_model.dart @@ -20,7 +20,7 @@ import 'package:talawa/view_model/base_view_model.dart'; /// * Triggers the snackbar UI to show online status.: [showSnackbar] class AppConnectivity extends BaseModel { /// Stream from [ConnectivityService]. - late final Stream connectivityStream; + late final Stream> connectivityStream; /// Subscription of the [connectivityStream] StreamSubscription? _subscription; @@ -51,7 +51,8 @@ class AppConnectivity extends BaseModel { /// None void enableSubscription() { try { - _subscription = connectivityStream.listen((ConnectivityResult result) { + _subscription = + connectivityStream.listen((List result) { handleConnection(result); }); } catch (e) { @@ -59,16 +60,18 @@ class AppConnectivity extends BaseModel { } } - /// This function handles the device's connectivity status based on the provided [ConnectivityResult]. + /// This function handles the device's connectivity status based on the provided [List]. /// /// **params**: - /// * `result`: A [ConnectivityResult] indicating the current connectivity status. + /// * `result`: A [List] indicating the current connectivity status. /// /// **returns**: /// None - Future handleConnection(ConnectivityResult result) async { - if (![ConnectivityResult.none, ConnectivityResult.bluetooth] - .contains(result)) { + Future handleConnection(List result) async { + if (result.any( + (r) => + ![ConnectivityResult.none, ConnectivityResult.bluetooth].contains(r), + )) { handleOnline(); } else { handleOffline(); diff --git a/pubspec.lock b/pubspec.lock index 89c74198c..e5c57ee4a 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -205,18 +205,18 @@ packages: dependency: "direct main" description: name: connectivity_plus - sha256: "224a77051d52a11fbad53dd57827594d3bd24f945af28bd70bab376d68d437f0" + sha256: e0817759ec6d2d8e57eb234e6e57d2173931367a865850c7acea40d4b4f9c27d url: "https://pub.dev" source: hosted - version: "5.0.2" + version: "6.0.5" connectivity_plus_platform_interface: dependency: transitive description: name: connectivity_plus_platform_interface - sha256: cf1d1c28f4416f8c654d7dc3cd638ec586076255d407cef3ddbdaf178272a71a + sha256: "42657c1715d48b167930d5f34d00222ac100475f73d10162ddf43e714932f204" url: "https://pub.dev" source: hosted - version: "1.2.4" + version: "2.0.1" contained_tab_bar_view: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 9a87a9a75..87c43a80f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -22,7 +22,7 @@ dependencies: auto_size_text: ^3.0.0 cached_network_image: ^3.4.1 clock: ^1.1.1 - connectivity_plus: ^5.0.2 + connectivity_plus: ^6.0.5 contained_tab_bar_view: ^0.8.0 crypto: ^3.0.5 diff --git a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart index 0bca2232a..3acc2b5ad 100644 --- a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart +++ b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart @@ -18,11 +18,11 @@ bool internetAccessible = true; class MockConnectivityService extends Mock with MockPlatformInterfaceMixin implements ConnectivityService { - final controller = StreamController(); + final controller = StreamController>(); @override // TODO: implement connectionStatusController - StreamController get connectionStatusController => + StreamController> get connectionStatusController => controller; @override @@ -32,34 +32,58 @@ class MockConnectivityService extends Mock } @override - Stream get connectionStream => controller.stream; + Stream> get connectionStream => controller.stream; @override - Future getConnectionType() { - return Future.value(connectivityStatus); + Future> getConnectionType() { + return Future.value([connectivityStatus!]); } @override - Future isReachable({http.Client? client, String? uriString}) { - return Future.value(internetAccessible); + Future hasConnection() async { + try { + final results = await getConnectionType(); + return results.isNotEmpty && + results.any((result) => result != ConnectivityResult.none); + } catch (e) { + return false; + } + } + + @override + Future isReachable({ + http.Client? client, + String? uriString, + }) async { + try { + await client! + .get(Uri.parse(uriString ?? graphqlConfig.httpLink.uri.toString())) + .timeout(const Duration(seconds: 30)); + return true; + } catch (e) { + print('Timeout while checking reachability: $e'); + return false; + } } } class MockConnectivity extends Mock implements Connectivity { - final controller = StreamController(); + final controller = StreamController>(); - StreamController get connectivityController => controller; + StreamController> get connectivityController => + controller; @override - Stream get onConnectivityChanged => controller.stream; + Stream> get onConnectivityChanged => + controller.stream; @override - Future checkConnectivity() async { + Future> checkConnectivity() async { // TODO: implement checkConnectivity if (connectivityStatus == null) { throw const SocketException('socket exception'); } - return connectivityStatus!; + return [connectivityStatus!]; } } @@ -75,13 +99,13 @@ class MockClient extends Mock implements http.Client { void main() { late MockClient mockClient; - late ConnectivityService service; + late MockConnectivityService service; setUpAll(() { TestWidgetsFlutterBinding.ensureInitialized(); mockClient = MockClient(); getAndRegisterConnectivity(); connectivityStatus = ConnectivityResult.mobile; - service = ConnectivityService(); + service = MockConnectivityService(); locator.registerSingleton(service); connectivityService.initConnectivity(client: http.Client()); }); @@ -90,17 +114,17 @@ void main() { test( 'connectionStream getter', () async { - expect(connectivityService, isA()); + expect(service, isA()); expect( - connectivityService.connectionStream, - isA>(), + service.connectionStream, + isA>>(), ); }, ); test('listener', () async { final mockConnectivity = testgetit.connectivity as MockConnectivity; - mockConnectivity.connectivityController.add(ConnectivityResult.mobile); + mockConnectivity.connectivityController.add([ConnectivityResult.mobile]); mockConnectivity.connectivityController .addError(Exception("Something went wrong!")); @@ -115,8 +139,10 @@ void main() { }); test('isReachable', () async { - final reached = - await service.isReachable(uriString: 'https://google.com'); + final reached = await service.isReachable( + client: mockClient, + uriString: 'https://google.com', + ); expect(reached, true); }); diff --git a/test/view_model_tests/connectivity_view_model_test.dart b/test/view_model_tests/connectivity_view_model_test.dart index 66a25b228..04a2475e5 100644 --- a/test/view_model_tests/connectivity_view_model_test.dart +++ b/test/view_model_tests/connectivity_view_model_test.dart @@ -74,12 +74,12 @@ void main() { group('test connectivity view model', () { test('handleConnection when demoMode', () { MainScreenViewModel.demoMode = true; - model.handleConnection(ConnectivityResult.mobile); + model.handleConnection([ConnectivityResult.mobile]); }); test('handleConnection when offline', () { internetAccessible = false; - model.handleConnection(ConnectivityResult.none); + model.handleConnection([ConnectivityResult.none]); }); test('handleConnection when online', () async { MainScreenViewModel.demoMode = false; @@ -95,7 +95,7 @@ void main() { ); print(cacheService.offlineActionQueue.getActions()); - model.handleConnection(ConnectivityResult.mobile); + model.handleConnection([ConnectivityResult.mobile]); }); testWidgets('showSnackbar when online', (tester) async { @@ -114,7 +114,7 @@ void main() { test('check enableSubscription body', () { connectivityService.connectionStatusController - .add(ConnectivityResult.none); + .add([ConnectivityResult.none]); }); test('enableSubscirption exception', () async { From 9764b40f151dcb3ae2f4b5421746150dddd9c030 Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Sun, 22 Dec 2024 02:43:13 +0530 Subject: [PATCH 02/17] added codegov missing test cases. --- .../connectivity_service_test.dart | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart index 3acc2b5ad..26a0ea275 100644 --- a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart +++ b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart @@ -12,7 +12,7 @@ import 'package:talawa/services/third_party_service/connectivity_service.dart'; import '../../helpers/test_helpers.dart'; import '../../helpers/test_locator.dart' as testgetit; -ConnectivityResult? connectivityStatus = ConnectivityResult.mobile; +List? connectivityStatus = [ConnectivityResult.mobile]; bool internetAccessible = true; class MockConnectivityService extends Mock @@ -36,7 +36,7 @@ class MockConnectivityService extends Mock @override Future> getConnectionType() { - return Future.value([connectivityStatus!]); + return Future.value(connectivityStatus!); } @override @@ -83,7 +83,7 @@ class MockConnectivity extends Mock implements Connectivity { if (connectivityStatus == null) { throw const SocketException('socket exception'); } - return [connectivityStatus!]; + return connectivityStatus!; } } @@ -104,7 +104,7 @@ void main() { TestWidgetsFlutterBinding.ensureInitialized(); mockClient = MockClient(); getAndRegisterConnectivity(); - connectivityStatus = ConnectivityResult.mobile; + connectivityStatus = [ConnectivityResult.mobile]; service = MockConnectivityService(); locator.registerSingleton(service); connectivityService.initConnectivity(client: http.Client()); @@ -130,12 +130,32 @@ void main() { .addError(Exception("Something went wrong!")); }); + test('successfully listens to connectivity changes', () async { + final expectedResults = [ConnectivityResult.wifi]; + + service.connectionStatusController.stream.listen( + expectAsync1( + (List results) { + expect(results, equals(expectedResults)); + }, + count: 1, + ), + ); + + // Trigger the event + service.connectionStatusController.add(expectedResults); + }); + test('check has connection', () async { - connectivityStatus = ConnectivityResult.none; + connectivityStatus = [ConnectivityResult.none]; expect(await service.hasConnection(), false); - connectivityStatus = ConnectivityResult.mobile; + connectivityStatus = [ConnectivityResult.mobile]; expect(await service.hasConnection(), true); + + // empty list + connectivityStatus = []; + expect(await service.hasConnection(), false); }); test('isReachable', () async { From 68ba2bce835ca42eb09a6e2fc323fad30639daf2 Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Sun, 22 Dec 2024 14:57:27 +0530 Subject: [PATCH 03/17] covered all test cases --- .../connectivity_service_test.dart | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart index 26a0ea275..d936bdd19 100644 --- a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart +++ b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart @@ -131,33 +131,46 @@ void main() { }); test('successfully listens to connectivity changes', () async { - final expectedResults = [ConnectivityResult.wifi]; + final expectedResults = [ + ConnectivityResult.mobile, + ConnectivityResult.wifi, + ]; service.connectionStatusController.stream.listen( - expectAsync1( - (List results) { - expect(results, equals(expectedResults)); - }, - count: 1, - ), + (List results) { + expect(results, equals(expectedResults)); + }, ); // Trigger the event service.connectionStatusController.add(expectedResults); }); - test('check has connection', () async { + test('check has connection - no connection', () async { connectivityStatus = [ConnectivityResult.none]; expect(await service.hasConnection(), false); + }); + test('check has connection - with connection', () async { connectivityStatus = [ConnectivityResult.mobile]; expect(await service.hasConnection(), true); + }); - // empty list + test('check has connection - empty list', () async { connectivityStatus = []; expect(await service.hasConnection(), false); }); + test('check has connection - mixed results', () async { + connectivityStatus = [ConnectivityResult.none, ConnectivityResult.wifi]; + expect(await service.hasConnection(), true); + }); + + test('check has connection - all none', () async { + connectivityStatus = [ConnectivityResult.none, ConnectivityResult.none]; + expect(await service.hasConnection(), false); + }); + test('isReachable', () async { final reached = await service.isReachable( client: mockClient, From 24a36949a5888975236456e8c1b8c51293ce12da Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Sun, 22 Dec 2024 15:57:27 +0530 Subject: [PATCH 04/17] codegov/patch --- .../connectivity_service_test.dart | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart index d936bdd19..047b198b5 100644 --- a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart +++ b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart @@ -135,15 +135,36 @@ void main() { ConnectivityResult.mobile, ConnectivityResult.wifi, ]; - - service.connectionStatusController.stream.listen( + final MockConnectivity mockConnectivity = MockConnectivity(); + mockConnectivity.onConnectivityChanged.listen( (List results) { expect(results, equals(expectedResults)); }, ); // Trigger the event - service.connectionStatusController.add(expectedResults); + mockConnectivity.connectivityController.add(expectedResults); + }); + + test('enableSubscription handles errors gracefully', () async { + final mockConnectivity = MockConnectivity(); + mockConnectivity.connectivityController.addError(Exception("Error!")); + + expect( + mockConnectivity.onConnectivityChanged, + emitsError(isA()), + ); + }); + + test('Returns true if there is a valid connectivity result', () async { + final mockConnectivityService = MockConnectivityService(); + connectivityStatus = [ConnectivityResult.mobile, ConnectivityResult.wifi]; + final result = await mockConnectivityService.getConnectionType(); + final hasConnection = result.isNotEmpty && + result.any((result) => result != ConnectivityResult.none); + + // Verify the conditions + expect(hasConnection, true); }); test('check has connection - no connection', () async { From 8c21409e82ec654123f6718f67a7a10bab128429 Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Sun, 22 Dec 2024 16:22:31 +0530 Subject: [PATCH 05/17] codegov/patch-2 --- .../connectivity_service_test.dart | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart index 047b198b5..ec1d3a952 100644 --- a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart +++ b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart @@ -39,17 +39,6 @@ class MockConnectivityService extends Mock return Future.value(connectivityStatus!); } - @override - Future hasConnection() async { - try { - final results = await getConnectionType(); - return results.isNotEmpty && - results.any((result) => result != ConnectivityResult.none); - } catch (e) { - return false; - } - } - @override Future isReachable({ http.Client? client, From b15cfc44f022542baf7dc86325b7246a51c21afe Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Sun, 22 Dec 2024 16:42:36 +0530 Subject: [PATCH 06/17] codegov/patch-3 --- .../connectivity_service_test.dart | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart index ec1d3a952..047b198b5 100644 --- a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart +++ b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart @@ -39,6 +39,17 @@ class MockConnectivityService extends Mock return Future.value(connectivityStatus!); } + @override + Future hasConnection() async { + try { + final results = await getConnectionType(); + return results.isNotEmpty && + results.any((result) => result != ConnectivityResult.none); + } catch (e) { + return false; + } + } + @override Future isReachable({ http.Client? client, From f3355a256067ff9c22e2cd57ca1a38e0ba4af8e3 Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Sun, 22 Dec 2024 17:10:33 +0530 Subject: [PATCH 07/17] implemented codeRabbit changes --- .../connectivity_service_test.dart | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart index 047b198b5..e39eb05b5 100644 --- a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart +++ b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart @@ -56,10 +56,14 @@ class MockConnectivityService extends Mock String? uriString, }) async { try { - await client! + final response = await client! .get(Uri.parse(uriString ?? graphqlConfig.httpLink.uri.toString())) .timeout(const Duration(seconds: 30)); - return true; + if (response.statusCode >= 200 && response.statusCode < 300) { + return true; + } else { + return false; + } } catch (e) { print('Timeout while checking reachability: $e'); return false; @@ -92,6 +96,8 @@ class MockClient extends Mock implements http.Client { Future get(Uri url, {Map? headers}) async { if (url.toString() == 'https://timeout.com') { throw TimeoutException('site took too long to respond'); + } else if (url.toString() == 'https://youtube.com') { + return http.Response('Server Error', 500); } return http.Response('{}', 200); } @@ -209,5 +215,16 @@ void main() { // Verify results (timeout should be thrown before verification) expect(isReachableResult, false); }); + + test('isReachable handles server error', () async { + // Mock client that returns 500 status code + final errorClient = MockClient(); + + final reached = await service.isReachable( + client: errorClient, + uriString: 'https://youtube.com', + ); + expect(reached, false); + }); }); } From de418ce82274b5b421c7226fe2c3dd2d54753319 Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Wed, 25 Dec 2024 02:50:51 +0530 Subject: [PATCH 08/17] covered all test cases --- .../connectivity_service_test.dart | 150 +++++++----------- 1 file changed, 61 insertions(+), 89 deletions(-) diff --git a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart index e39eb05b5..f51251a2d 100644 --- a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart +++ b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart @@ -4,14 +4,12 @@ import 'dart:io'; import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:http/http.dart' as http; +import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; import 'package:plugin_platform_interface/plugin_platform_interface.dart'; import 'package:talawa/locator.dart'; import 'package:talawa/services/third_party_service/connectivity_service.dart'; -import '../../helpers/test_helpers.dart'; -import '../../helpers/test_locator.dart' as testgetit; - List? connectivityStatus = [ConnectivityResult.mobile]; bool internetAccessible = true; @@ -21,13 +19,11 @@ class MockConnectivityService extends Mock final controller = StreamController>(); @override - // TODO: implement connectionStatusController StreamController> get connectionStatusController => controller; @override Future initConnectivity({required http.Client client}) { - // TODO: implement initConnectivity return Future(() => null); } @@ -71,19 +67,20 @@ class MockConnectivityService extends Mock } } +@GenerateMocks([Connectivity]) class MockConnectivity extends Mock implements Connectivity { - final controller = StreamController>(); - - StreamController> get connectivityController => - controller; + final _controller = StreamController>.broadcast(); @override Stream> get onConnectivityChanged => - controller.stream; + _controller.stream; + + void emitConnectivityChange(List results) { + _controller.add(results); + } @override Future> checkConnectivity() async { - // TODO: implement checkConnectivity if (connectivityStatus == null) { throw const SocketException('socket exception'); } @@ -105,101 +102,81 @@ class MockClient extends Mock implements http.Client { void main() { late MockClient mockClient; - late MockConnectivityService service; - setUpAll(() { + late MockConnectivityService mockService; + late ConnectivityService actualService; + late MockConnectivity mockConnectivity; + + setUp(() { TestWidgetsFlutterBinding.ensureInitialized(); mockClient = MockClient(); - getAndRegisterConnectivity(); - connectivityStatus = [ConnectivityResult.mobile]; - service = MockConnectivityService(); - locator.registerSingleton(service); - connectivityService.initConnectivity(client: http.Client()); - }); + mockConnectivity = MockConnectivity(); + mockService = MockConnectivityService(); - group('connectivity', () { - test( - 'connectionStream getter', - () async { - expect(service, isA()); - expect( - service.connectionStream, - isA>>(), - ); - }, - ); - - test('listener', () async { - final mockConnectivity = testgetit.connectivity as MockConnectivity; - mockConnectivity.connectivityController.add([ConnectivityResult.mobile]); - - mockConnectivity.connectivityController - .addError(Exception("Something went wrong!")); - }); + // Register both mock and actual services + locator.registerSingleton(mockConnectivity); + locator.registerSingleton(mockService); - test('successfully listens to connectivity changes', () async { - final expectedResults = [ - ConnectivityResult.mobile, - ConnectivityResult.wifi, - ]; - final MockConnectivity mockConnectivity = MockConnectivity(); - mockConnectivity.onConnectivityChanged.listen( - (List results) { - expect(results, equals(expectedResults)); - }, - ); - - // Trigger the event - mockConnectivity.connectivityController.add(expectedResults); - }); + // Initialize actual service for specific tests + actualService = ConnectivityService(); + actualService.initConnectivity(client: mockClient); + }); - test('enableSubscription handles errors gracefully', () async { - final mockConnectivity = MockConnectivity(); - mockConnectivity.connectivityController.addError(Exception("Error!")); + tearDown(() { + locator.unregister(); + locator.unregister(); + }); + group('connectivity', () { + test('connectionStream getter', () async { + expect(mockService, isA()); expect( - mockConnectivity.onConnectivityChanged, - emitsError(isA()), + mockService.connectionStream, + isA>>(), ); }); - test('Returns true if there is a valid connectivity result', () async { - final mockConnectivityService = MockConnectivityService(); - connectivityStatus = [ConnectivityResult.mobile, ConnectivityResult.wifi]; - final result = await mockConnectivityService.getConnectionType(); - final hasConnection = result.isNotEmpty && - result.any((result) => result != ConnectivityResult.none); - - // Verify the conditions - expect(hasConnection, true); - }); + test('enableSubscription with actual service', () async { + // Test that the stream receives the connectivity change + actualService.connectionStream.listen( + expectAsync1((List results) { + expect(results, [ConnectivityResult.mobile]); + }), + ); - test('check has connection - no connection', () async { - connectivityStatus = [ConnectivityResult.none]; - expect(await service.hasConnection(), false); + // Trigger the connectivity change + actualService.connectionStatusController.add([ConnectivityResult.mobile]); }); - test('check has connection - with connection', () async { + test('hasConnection - with connection', () async { connectivityStatus = [ConnectivityResult.mobile]; - expect(await service.hasConnection(), true); + final hasConnection = await actualService.hasConnection(); + expect(hasConnection, true); }); - - test('check has connection - empty list', () async { + test('hasConnection - with no connection', () async { + connectivityStatus = [ConnectivityResult.none]; + final hasConnection = await actualService.hasConnection(); + expect(hasConnection, false); + }); + test('hasConnection - with empty list', () async { connectivityStatus = []; - expect(await service.hasConnection(), false); + final hasConnection = await actualService.hasConnection(); + expect(hasConnection, false); }); - test('check has connection - mixed results', () async { + test('hasConnection - with mixed results', () async { connectivityStatus = [ConnectivityResult.none, ConnectivityResult.wifi]; - expect(await service.hasConnection(), true); + final result = await actualService.hasConnection(); + expect(result, true); }); - test('check has connection - all none', () async { + test('hasConnection - with all none', () async { connectivityStatus = [ConnectivityResult.none, ConnectivityResult.none]; - expect(await service.hasConnection(), false); + final result = await actualService.hasConnection(); + expect(result, false); }); test('isReachable', () async { - final reached = await service.isReachable( + final reached = await mockService.isReachable( client: mockClient, uriString: 'https://google.com', ); @@ -207,21 +184,16 @@ void main() { }); test('isReachable throws TimeoutException on timeout', () async { - final isReachableResult = await service.isReachable( + final isReachableResult = await mockService.isReachable( client: mockClient, uriString: 'https://timeout.com', ); - - // Verify results (timeout should be thrown before verification) expect(isReachableResult, false); }); test('isReachable handles server error', () async { - // Mock client that returns 500 status code - final errorClient = MockClient(); - - final reached = await service.isReachable( - client: errorClient, + final reached = await mockService.isReachable( + client: mockClient, uriString: 'https://youtube.com', ); expect(reached, false); From 659e8acb6c29c1b5fcd0719d9518160d366db6fe Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Wed, 25 Dec 2024 04:07:37 +0530 Subject: [PATCH 09/17] covered test all cases 2 --- .../connectivity_service_test.dart | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart index f51251a2d..207f99fd2 100644 --- a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart +++ b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart @@ -136,15 +136,33 @@ void main() { }); test('enableSubscription with actual service', () async { - // Test that the stream receives the connectivity change - actualService.connectionStream.listen( - expectAsync1((List results) { - expect(results, [ConnectivityResult.mobile]); - }), + final testResults = [ConnectivityResult.wifi]; + + // First verify the mock works + expect( + mockConnectivity.onConnectivityChanged, + isA>>(), + ); + + // Then verify the service stream + expect( + actualService.connectionStream, + isA>>(), + ); + + // Listen to both streams + mockConnectivity.onConnectivityChanged.listen( + expectAsync1( + (List results) { + expect(results, isA>()); + expect(results, equals(testResults)); + }, + count: 1, + ), ); - // Trigger the connectivity change - actualService.connectionStatusController.add([ConnectivityResult.mobile]); + // Emit and wait + mockConnectivity.emitConnectivityChange(testResults); }); test('hasConnection - with connection', () async { From 814e8e252becbca909da285f07549f073c886b48 Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Wed, 25 Dec 2024 04:24:53 +0530 Subject: [PATCH 10/17] covered all test cases 3 --- .../connectivity_service_test.dart | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart index 207f99fd2..f793f45d6 100644 --- a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart +++ b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart @@ -9,6 +9,7 @@ import 'package:mockito/mockito.dart'; import 'package:plugin_platform_interface/plugin_platform_interface.dart'; import 'package:talawa/locator.dart'; import 'package:talawa/services/third_party_service/connectivity_service.dart'; +import '../../helpers/test_locator.dart' as testgetit; List? connectivityStatus = [ConnectivityResult.mobile]; bool internetAccessible = true; @@ -135,6 +136,17 @@ void main() { ); }); + test('listener', () async { + final mockConnectivity = testgetit.connectivity as MockConnectivity; + final List result = [ + ConnectivityResult.mobile, + ConnectivityResult.none, + ]; + mockConnectivity._controller.add(result); + + mockConnectivity._controller.addError(Exception("Something went wrong!")); + }); + test('enableSubscription with actual service', () async { final testResults = [ConnectivityResult.wifi]; From fcd43eb2e486d20e7a3f5c260b026abc5416cc18 Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Thu, 26 Dec 2024 21:52:34 +0530 Subject: [PATCH 11/17] covered all test cases 4 --- .../connectivity_service_test.dart | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart index f793f45d6..315950452 100644 --- a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart +++ b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart @@ -147,6 +147,22 @@ void main() { mockConnectivity._controller.addError(Exception("Something went wrong!")); }); + test('connectivity stream callback handles List', + () async { + final service = ConnectivityService(); + await service.initConnectivity(client: mockClient); + + void callback(List result) { + service.connectionStatusController.add(result); + } + + final testResults = [ConnectivityResult.wifi]; + callback(testResults); + + final emittedResults = await service.connectionStream.first; + expect(emittedResults, equals(testResults)); + }); + test('enableSubscription with actual service', () async { final testResults = [ConnectivityResult.wifi]; From 1b6e8b1d0aa388fb7d914097965ccc4c33d73940 Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Fri, 27 Dec 2024 17:54:27 +0530 Subject: [PATCH 12/17] covered all test cases 5 --- lib/locator.dart | 2 +- .../connectivity_service.dart | 9 ++++++++- test/helpers/test_locator.dart | 2 +- .../connectivity_service_test.dart | 20 +------------------ 4 files changed, 11 insertions(+), 22 deletions(-) diff --git a/lib/locator.dart b/lib/locator.dart index 7a494c893..15e156508 100644 --- a/lib/locator.dart +++ b/lib/locator.dart @@ -136,7 +136,7 @@ Future setupLocator() async { //databaseMutationFunction - locator.registerSingleton(ConnectivityService()); + locator.registerSingleton(ConnectivityService(connectivity)); //queries locator.registerSingleton(Queries()); diff --git a/lib/services/third_party_service/connectivity_service.dart b/lib/services/third_party_service/connectivity_service.dart index f88944a7b..ae020c6fe 100644 --- a/lib/services/third_party_service/connectivity_service.dart +++ b/lib/services/third_party_service/connectivity_service.dart @@ -14,6 +14,13 @@ import 'package:talawa/locator.dart'; /// * Handling the device's connectivity status - [handleConnection] /// * Checking if the device has any type of network connection - [hasConnection] class ConnectivityService { + ConnectivityService( + this.connectivityInstance, + ); + + /// dependency injection connectivity. + late final Connectivity connectivityInstance; + /// Stream controller for network status changes. late StreamController> connectionStatusController; @@ -59,7 +66,7 @@ class ConnectivityService { /// **returns**: /// None Future enableSubscription() async { - connectivity.onConnectivityChanged.listen( + connectivityInstance.onConnectivityChanged.listen( (List result) { print(result); connectionStatusController.add(result); diff --git a/test/helpers/test_locator.dart b/test/helpers/test_locator.dart index 5d0b80a7f..250b9bf1c 100644 --- a/test/helpers/test_locator.dart +++ b/test/helpers/test_locator.dart @@ -108,7 +108,7 @@ void testSetupLocator() { //databaseMutationFunction - locator.registerSingleton(ConnectivityService()); + locator.registerSingleton(ConnectivityService(connectivity)); //queries locator.registerSingleton(Queries()); diff --git a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart index 315950452..ae07d9348 100644 --- a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart +++ b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart @@ -4,7 +4,6 @@ import 'dart:io'; import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:http/http.dart' as http; -import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; import 'package:plugin_platform_interface/plugin_platform_interface.dart'; import 'package:talawa/locator.dart'; @@ -68,7 +67,6 @@ class MockConnectivityService extends Mock } } -@GenerateMocks([Connectivity]) class MockConnectivity extends Mock implements Connectivity { final _controller = StreamController>.broadcast(); @@ -118,7 +116,7 @@ void main() { locator.registerSingleton(mockService); // Initialize actual service for specific tests - actualService = ConnectivityService(); + actualService = ConnectivityService(mockConnectivity); actualService.initConnectivity(client: mockClient); }); @@ -147,22 +145,6 @@ void main() { mockConnectivity._controller.addError(Exception("Something went wrong!")); }); - test('connectivity stream callback handles List', - () async { - final service = ConnectivityService(); - await service.initConnectivity(client: mockClient); - - void callback(List result) { - service.connectionStatusController.add(result); - } - - final testResults = [ConnectivityResult.wifi]; - callback(testResults); - - final emittedResults = await service.connectionStream.first; - expect(emittedResults, equals(testResults)); - }); - test('enableSubscription with actual service', () async { final testResults = [ConnectivityResult.wifi]; From e580ce83c8afbcfed609ea14f4d78d7db26ec658 Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Fri, 27 Dec 2024 18:07:28 +0530 Subject: [PATCH 13/17] covered all test cases increasing coverage --- .../connectivity_service_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart index ae07d9348..b92928038 100644 --- a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart +++ b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart @@ -204,7 +204,7 @@ void main() { }); test('isReachable', () async { - final reached = await mockService.isReachable( + final reached = await actualService.isReachable( client: mockClient, uriString: 'https://google.com', ); From 6bca5733111f7c382ecd8cb4399d84651090ad34 Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Fri, 27 Dec 2024 18:24:12 +0530 Subject: [PATCH 14/17] covered all lines --- android/app/build.gradle | 11 ++++++++++- .../connectivity_service_test.dart | 3 +-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index 410706e21..06517c95b 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -34,7 +34,7 @@ android { } compileSdkVersion 34 - + namespace "com.example.talawa" sourceSets { main.java.srcDirs += 'src/main/kotlin' } @@ -50,6 +50,14 @@ android { versionName flutterVersionName multiDexEnabled true } + compileOptions { + sourceCompatibility JavaVersion.VERSION_17 + targetCompatibility JavaVersion.VERSION_17 + } + + kotlinOptions { + jvmTarget = '17' + } buildTypes { release { @@ -67,4 +75,5 @@ flutter { dependencies { implementation 'com.android.support:multidex:1.0.3' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.10" + } diff --git a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart index b92928038..7859ef378 100644 --- a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart +++ b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart @@ -212,8 +212,7 @@ void main() { }); test('isReachable throws TimeoutException on timeout', () async { - final isReachableResult = await mockService.isReachable( - client: mockClient, + final isReachableResult = await actualService.isReachable( uriString: 'https://timeout.com', ); expect(isReachableResult, false); From 04cce55dde063f7084a841f7541e18e67f9f263e Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Sat, 28 Dec 2024 01:21:18 +0530 Subject: [PATCH 15/17] fixed java version in github workflows --- .github/workflows/pull-request.yml | 6 +++--- android/gradle/wrapper/gradle-wrapper.properties | 2 +- android/settings.gradle | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 55e40002b..8917a184c 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -31,7 +31,7 @@ jobs: - uses: actions/setup-java@v4 with: distribution: "zulu" # See 'Supported distributions' for available options - java-version: "12.0" + java-version: "17.0" - uses: subosito/flutter-action@v2 with: flutter-version: "3.22.3" @@ -117,7 +117,7 @@ jobs: - uses: actions/setup-java@v4 with: distribution: "zulu" # See 'Supported distributions' for available options - java-version: "12.0" + java-version: "17.0" - uses: subosito/flutter-action@v2 with: flutter-version: "3.22.3" @@ -148,7 +148,7 @@ jobs: - uses: actions/setup-java@v4 with: distribution: "zulu" # See 'Supported distributions' for available options - java-version: "12.0" + java-version: "17.0" - uses: subosito/flutter-action@v2 with: flutter-version: "3.22.3" diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index cfe88f690..4cf0c849a 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-all.zip diff --git a/android/settings.gradle b/android/settings.gradle index 61f635fea..99dde8886 100644 --- a/android/settings.gradle +++ b/android/settings.gradle @@ -19,7 +19,7 @@ pluginManagement { plugins { id "dev.flutter.flutter-plugin-loader" version "1.0.2" - id "com.android.application" version "7.3.0" apply false + id "com.android.application" version "8.3.0" apply false id "org.jetbrains.kotlin.android" version "1.8.10" apply false id("com.google.gms.google-services") version "4.4.1" apply false From 6a463e5ce1966fa56cf7590632b8da987ef82723 Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Sat, 28 Dec 2024 01:31:54 +0530 Subject: [PATCH 16/17] revert changes for android files --- android/gradle/wrapper/gradle-wrapper.properties | 2 +- android/settings.gradle | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index 4cf0c849a..cfe88f690 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip diff --git a/android/settings.gradle b/android/settings.gradle index 99dde8886..61f635fea 100644 --- a/android/settings.gradle +++ b/android/settings.gradle @@ -19,7 +19,7 @@ pluginManagement { plugins { id "dev.flutter.flutter-plugin-loader" version "1.0.2" - id "com.android.application" version "8.3.0" apply false + id "com.android.application" version "7.3.0" apply false id "org.jetbrains.kotlin.android" version "1.8.10" apply false id("com.google.gms.google-services") version "4.4.1" apply false From 0a1b24ff37da8dfbbeb2f61a7ef4d3862ee869b3 Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Sat, 28 Dec 2024 01:59:41 +0530 Subject: [PATCH 17/17] removed whitespace from build.gradle --- android/app/build.gradle | 1 - 1 file changed, 1 deletion(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index 06517c95b..d004e92ab 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -75,5 +75,4 @@ flutter { dependencies { implementation 'com.android.support:multidex:1.0.3' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.10" - }