|
| 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 | +} |
0 commit comments