Skip to content

Commit 1edf729

Browse files
committed
Improve testing
Eliminate duplicated code with helper function
1 parent e662b84 commit 1edf729

File tree

4 files changed

+221
-104
lines changed

4 files changed

+221
-104
lines changed

packages/flutter/lib/parse_server_sdk_flutter.dart

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,38 +116,32 @@ class Parse extends sdk.Parse
116116
final StreamController<void> _appResumedStreamController =
117117
StreamController<void>();
118118

119-
@override
120-
Future<sdk.ParseConnectivityResult> checkConnectivity() async {
121-
List<ConnectivityResult> list = await Connectivity().checkConnectivity();
122-
123-
// Priority mapping: wifi > ethernet > mobile > none
124-
if (list.contains(ConnectivityResult.wifi)) {
119+
/// Maps connectivity_plus results to ParseConnectivityResult.
120+
///
121+
/// Priority: wifi > ethernet > mobile > none
122+
/// This ensures ethernet is treated as an online connection type.
123+
sdk.ParseConnectivityResult _mapConnectivity(
124+
List<ConnectivityResult> results) {
125+
if (results.contains(ConnectivityResult.wifi)) {
125126
return sdk.ParseConnectivityResult.wifi;
126-
} else if (list.contains(ConnectivityResult.ethernet)) {
127+
} else if (results.contains(ConnectivityResult.ethernet)) {
127128
return sdk.ParseConnectivityResult.ethernet;
128-
} else if (list.contains(ConnectivityResult.mobile)) {
129+
} else if (results.contains(ConnectivityResult.mobile)) {
129130
return sdk.ParseConnectivityResult.mobile;
130131
} else {
131132
return sdk.ParseConnectivityResult.none;
132133
}
133134
}
134135

136+
@override
137+
Future<sdk.ParseConnectivityResult> checkConnectivity() async {
138+
List<ConnectivityResult> list = await Connectivity().checkConnectivity();
139+
return _mapConnectivity(list);
140+
}
141+
135142
@override
136143
Stream<sdk.ParseConnectivityResult> get connectivityStream {
137-
return Connectivity().onConnectivityChanged.map((
138-
List<ConnectivityResult> event,
139-
) {
140-
// Priority mapping: wifi > ethernet > mobile > none
141-
if (event.contains(ConnectivityResult.wifi)) {
142-
return sdk.ParseConnectivityResult.wifi;
143-
} else if (event.contains(ConnectivityResult.ethernet)) {
144-
return sdk.ParseConnectivityResult.ethernet;
145-
} else if (event.contains(ConnectivityResult.mobile)) {
146-
return sdk.ParseConnectivityResult.mobile;
147-
} else {
148-
return sdk.ParseConnectivityResult.none;
149-
}
150-
});
144+
return Connectivity().onConnectivityChanged.map(_mapConnectivity);
151145
}
152146

153147
@override

packages/flutter/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ dev_dependencies:
4848
sdk: flutter
4949

5050
flutter_lints: ">=4.0.0 <7.0.0"
51+
connectivity_plus_platform_interface: ^2.0.0
5152
path_provider_platform_interface: ^2.1.2
5253
plugin_platform_interface: ^2.1.8
5354

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
import 'dart:async';
2+
3+
import 'package:connectivity_plus/connectivity_plus.dart';
4+
import 'package:connectivity_plus_platform_interface/connectivity_plus_platform_interface.dart';
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:parse_server_sdk_flutter/parse_server_sdk_flutter.dart';
7+
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
8+
9+
/// Mock implementation of ConnectivityPlatform for testing
10+
class MockConnectivityPlatform extends Fake
11+
with MockPlatformInterfaceMixin
12+
implements ConnectivityPlatform {
13+
List<ConnectivityResult> _connectivity = [ConnectivityResult.none];
14+
final StreamController<List<ConnectivityResult>> _controller =
15+
StreamController<List<ConnectivityResult>>.broadcast();
16+
17+
void setConnectivity(List<ConnectivityResult> connectivity) {
18+
_connectivity = connectivity;
19+
_controller.add(connectivity);
20+
}
21+
22+
@override
23+
Future<List<ConnectivityResult>> checkConnectivity() async {
24+
return _connectivity;
25+
}
26+
27+
@override
28+
Stream<List<ConnectivityResult>> get onConnectivityChanged =>
29+
_controller.stream;
30+
31+
void dispose() {
32+
_controller.close();
33+
}
34+
}
35+
36+
void main() {
37+
TestWidgetsFlutterBinding.ensureInitialized();
38+
39+
group('Parse.checkConnectivity() implementation', () {
40+
late MockConnectivityPlatform mockPlatform;
41+
42+
setUp(() {
43+
mockPlatform = MockConnectivityPlatform();
44+
ConnectivityPlatform.instance = mockPlatform;
45+
});
46+
47+
tearDown(() {
48+
mockPlatform.dispose();
49+
});
50+
51+
test('wifi connection returns ParseConnectivityResult.wifi', () async {
52+
mockPlatform.setConnectivity([ConnectivityResult.wifi]);
53+
54+
final result = await Parse().checkConnectivity();
55+
56+
expect(result, ParseConnectivityResult.wifi);
57+
});
58+
59+
test('ethernet connection returns ParseConnectivityResult.ethernet',
60+
() async {
61+
mockPlatform.setConnectivity([ConnectivityResult.ethernet]);
62+
63+
final result = await Parse().checkConnectivity();
64+
65+
expect(result, ParseConnectivityResult.ethernet);
66+
});
67+
68+
test('mobile connection returns ParseConnectivityResult.mobile', () async {
69+
mockPlatform.setConnectivity([ConnectivityResult.mobile]);
70+
71+
final result = await Parse().checkConnectivity();
72+
73+
expect(result, ParseConnectivityResult.mobile);
74+
});
75+
76+
test('no connection returns ParseConnectivityResult.none', () async {
77+
mockPlatform.setConnectivity([ConnectivityResult.none]);
78+
79+
final result = await Parse().checkConnectivity();
80+
81+
expect(result, ParseConnectivityResult.none);
82+
});
83+
84+
test('wifi takes priority over ethernet', () async {
85+
mockPlatform
86+
.setConnectivity([ConnectivityResult.wifi, ConnectivityResult.ethernet]);
87+
88+
final result = await Parse().checkConnectivity();
89+
90+
expect(result, ParseConnectivityResult.wifi);
91+
});
92+
93+
test('ethernet takes priority over mobile (issue #1042 fix)', () async {
94+
mockPlatform.setConnectivity(
95+
[ConnectivityResult.ethernet, ConnectivityResult.mobile]);
96+
97+
final result = await Parse().checkConnectivity();
98+
99+
expect(result, ParseConnectivityResult.ethernet);
100+
});
101+
102+
test('unsupported connection types fall back to none', () async {
103+
mockPlatform.setConnectivity([ConnectivityResult.bluetooth]);
104+
105+
final result = await Parse().checkConnectivity();
106+
107+
expect(result, ParseConnectivityResult.none);
108+
});
109+
});
110+
111+
group('Parse.connectivityStream implementation', () {
112+
late MockConnectivityPlatform mockPlatform;
113+
114+
setUp(() {
115+
mockPlatform = MockConnectivityPlatform();
116+
ConnectivityPlatform.instance = mockPlatform;
117+
});
118+
119+
tearDown(() {
120+
mockPlatform.dispose();
121+
});
122+
123+
test('wifi event emits ParseConnectivityResult.wifi', () async {
124+
final completer = Completer<ParseConnectivityResult>();
125+
final subscription = Parse().connectivityStream.listen((result) {
126+
if (!completer.isCompleted) {
127+
completer.complete(result);
128+
}
129+
});
130+
131+
mockPlatform.setConnectivity([ConnectivityResult.wifi]);
132+
133+
final result = await completer.future;
134+
expect(result, ParseConnectivityResult.wifi);
135+
136+
await subscription.cancel();
137+
});
138+
139+
test('ethernet event emits ParseConnectivityResult.ethernet', () async {
140+
final completer = Completer<ParseConnectivityResult>();
141+
final subscription = Parse().connectivityStream.listen((result) {
142+
if (!completer.isCompleted) {
143+
completer.complete(result);
144+
}
145+
});
146+
147+
mockPlatform.setConnectivity([ConnectivityResult.ethernet]);
148+
149+
final result = await completer.future;
150+
expect(result, ParseConnectivityResult.ethernet);
151+
152+
await subscription.cancel();
153+
});
154+
155+
test('mobile event emits ParseConnectivityResult.mobile', () async {
156+
final completer = Completer<ParseConnectivityResult>();
157+
final subscription = Parse().connectivityStream.listen((result) {
158+
if (!completer.isCompleted) {
159+
completer.complete(result);
160+
}
161+
});
162+
163+
mockPlatform.setConnectivity([ConnectivityResult.mobile]);
164+
165+
final result = await completer.future;
166+
expect(result, ParseConnectivityResult.mobile);
167+
168+
await subscription.cancel();
169+
});
170+
171+
test('none event emits ParseConnectivityResult.none', () async {
172+
final completer = Completer<ParseConnectivityResult>();
173+
final subscription = Parse().connectivityStream.listen((result) {
174+
if (!completer.isCompleted) {
175+
completer.complete(result);
176+
}
177+
});
178+
179+
mockPlatform.setConnectivity([ConnectivityResult.none]);
180+
181+
final result = await completer.future;
182+
expect(result, ParseConnectivityResult.none);
183+
184+
await subscription.cancel();
185+
});
186+
187+
test('stream respects priority: ethernet over mobile', () async {
188+
final completer = Completer<ParseConnectivityResult>();
189+
final subscription = Parse().connectivityStream.listen((result) {
190+
if (!completer.isCompleted) {
191+
completer.complete(result);
192+
}
193+
});
194+
195+
mockPlatform.setConnectivity(
196+
[ConnectivityResult.ethernet, ConnectivityResult.mobile]);
197+
198+
final result = await completer.future;
199+
expect(result, ParseConnectivityResult.ethernet);
200+
201+
await subscription.cancel();
202+
});
203+
});
204+
}

packages/flutter/test/parse_connectivity_mapping_test.dart

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)