-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: added network info implementation in core/platform with test…
… driven development
- Loading branch information
Showing
14 changed files
with
340 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" | ||
#include "Generated.xcconfig" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" | ||
#include "Generated.xcconfig" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Uncomment this line to define a global platform for your project | ||
# platform :ios, '11.0' | ||
|
||
# CocoaPods analytics sends network stats synchronously affecting flutter build latency. | ||
ENV['COCOAPODS_DISABLE_STATS'] = 'true' | ||
|
||
project 'Runner', { | ||
'Debug' => :debug, | ||
'Profile' => :release, | ||
'Release' => :release, | ||
} | ||
|
||
def flutter_root | ||
generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__) | ||
unless File.exist?(generated_xcode_build_settings_path) | ||
raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first" | ||
end | ||
|
||
File.foreach(generated_xcode_build_settings_path) do |line| | ||
matches = line.match(/FLUTTER_ROOT\=(.*)/) | ||
return matches[1].strip if matches | ||
end | ||
raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get" | ||
end | ||
|
||
require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root) | ||
|
||
flutter_ios_podfile_setup | ||
|
||
target 'Runner' do | ||
use_frameworks! | ||
use_modular_headers! | ||
|
||
flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__)) | ||
target 'RunnerTests' do | ||
inherit! :search_paths | ||
end | ||
end | ||
|
||
post_install do |installer| | ||
installer.pods_project.targets.each do |target| | ||
flutter_additional_ios_build_settings(target) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,27 @@ | ||
import 'package:connectivity_plus/connectivity_plus.dart'; | ||
import 'package:news_app/core/error/exceptions.dart'; | ||
|
||
abstract class NetworkInfo { | ||
Future<bool> checkConnection(); | ||
} | ||
|
||
class NetworkInfoImplementation extends NetworkInfo { | ||
final Connectivity connectivity; | ||
|
||
NetworkInfoImplementation(this.connectivity); | ||
|
||
@override | ||
Future<bool> checkConnection() async { | ||
final ConnectivityResult result = await connectivity.checkConnectivity(); | ||
switch (result) { | ||
case ConnectivityResult.wifi: | ||
case ConnectivityResult.ethernet: | ||
case ConnectivityResult.mobile: | ||
return true; | ||
case ConnectivityResult.bluetooth: | ||
case ConnectivityResult.none: | ||
default: | ||
throw ConnectivityException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" | ||
#include "ephemeral/Flutter-Generated.xcconfig" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" | ||
#include "ephemeral/Flutter-Generated.xcconfig" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
platform :osx, '10.14' | ||
|
||
# CocoaPods analytics sends network stats synchronously affecting flutter build latency. | ||
ENV['COCOAPODS_DISABLE_STATS'] = 'true' | ||
|
||
project 'Runner', { | ||
'Debug' => :debug, | ||
'Profile' => :release, | ||
'Release' => :release, | ||
} | ||
|
||
def flutter_root | ||
generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'ephemeral', 'Flutter-Generated.xcconfig'), __FILE__) | ||
unless File.exist?(generated_xcode_build_settings_path) | ||
raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure \"flutter pub get\" is executed first" | ||
end | ||
|
||
File.foreach(generated_xcode_build_settings_path) do |line| | ||
matches = line.match(/FLUTTER_ROOT\=(.*)/) | ||
return matches[1].strip if matches | ||
end | ||
raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Flutter-Generated.xcconfig, then run \"flutter pub get\"" | ||
end | ||
|
||
require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root) | ||
|
||
flutter_macos_podfile_setup | ||
|
||
target 'Runner' do | ||
use_frameworks! | ||
use_modular_headers! | ||
|
||
flutter_install_all_macos_pods File.dirname(File.realpath(__FILE__)) | ||
target 'RunnerTests' do | ||
inherit! :search_paths | ||
end | ||
end | ||
|
||
post_install do |installer| | ||
installer.pods_project.targets.each do |target| | ||
flutter_additional_macos_build_settings(target) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import 'package:connectivity_plus/connectivity_plus.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/annotations.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
import 'package:news_app/core/error/exceptions.dart'; | ||
import 'package:news_app/core/platform/network_info.dart'; | ||
|
||
import 'network_info_test.mocks.dart'; | ||
|
||
@GenerateNiceMocks([MockSpec<Connectivity>()]) | ||
void main() { | ||
late NetworkInfoImplementation networkInfoImplementation; | ||
late MockConnectivity mockConnectivity; | ||
|
||
setUp(() { | ||
mockConnectivity = MockConnectivity(); | ||
networkInfoImplementation = NetworkInfoImplementation(mockConnectivity); | ||
}); | ||
|
||
group("checkConnection() tests", () { | ||
test("should forward the call to Connectivity().checkConnectivity()", | ||
() async { | ||
//arrange | ||
when(mockConnectivity.checkConnectivity()) | ||
.thenAnswer((realInvocation) async => ConnectivityResult.wifi); | ||
//act | ||
await networkInfoImplementation.checkConnection(); | ||
//assert | ||
verify(mockConnectivity.checkConnectivity()); | ||
}); | ||
|
||
group("there is a connection", () { | ||
test("should return true when there is a internet connection (wifi)", | ||
() async { | ||
//arrange | ||
when(mockConnectivity.checkConnectivity()) | ||
.thenAnswer((realInvocation) async => ConnectivityResult.wifi); | ||
//act | ||
final result = await networkInfoImplementation.checkConnection(); | ||
//assert | ||
expect(result, true); | ||
verify(mockConnectivity.checkConnectivity()); | ||
}); | ||
|
||
test("should return true when there is a internet connection (ethernet)", | ||
() async { | ||
//arrange | ||
when(mockConnectivity.checkConnectivity()) | ||
.thenAnswer((realInvocation) async => ConnectivityResult.ethernet); | ||
//act | ||
final result = await networkInfoImplementation.checkConnection(); | ||
//assert | ||
expect(result, true); | ||
verify(mockConnectivity.checkConnectivity()); | ||
}); | ||
|
||
test("should return true when there is a internet connection (mobile)", | ||
() async { | ||
//arrange | ||
when(mockConnectivity.checkConnectivity()) | ||
.thenAnswer((realInvocation) async => ConnectivityResult.mobile); | ||
//act | ||
final result = await networkInfoImplementation.checkConnection(); | ||
//assert | ||
expect(result, true); | ||
verify(mockConnectivity.checkConnectivity()); | ||
}); | ||
}); | ||
|
||
group("there is no connection", () { | ||
test( | ||
"should throw a ConnectivityException when there is no internet connection (bluetooth)", | ||
() async { | ||
//arrange | ||
when(mockConnectivity.checkConnectivity()) | ||
.thenAnswer((realInvocation) async => ConnectivityResult.bluetooth); | ||
//act | ||
final calledMethod = networkInfoImplementation.checkConnection; | ||
//assert | ||
expect( | ||
calledMethod, throwsA(const TypeMatcher<ConnectivityException>())); | ||
verify(mockConnectivity.checkConnectivity()); | ||
}); | ||
|
||
test( | ||
"should throw a ConnectivityException when there is no internet connection (none)", | ||
() async { | ||
//arrange | ||
when(mockConnectivity.checkConnectivity()) | ||
.thenAnswer((realInvocation) async => ConnectivityResult.none); | ||
//act | ||
final calledMethod = networkInfoImplementation.checkConnection; | ||
//assert | ||
expect( | ||
calledMethod, throwsA(const TypeMatcher<ConnectivityException>())); | ||
verify(mockConnectivity.checkConnectivity()); | ||
}); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.