|
| 1 | +import 'package:connectivity_plus/connectivity_plus.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:parse_server_sdk_flutter/parse_server_sdk_flutter.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + TestWidgetsFlutterBinding.ensureInitialized(); |
| 7 | + |
| 8 | + group('Connectivity mapping logic', () { |
| 9 | + test('should map connectivity_plus results to ParseConnectivityResult', |
| 10 | + () { |
| 11 | + // This test documents the expected mapping behavior |
| 12 | + // The actual Parse class implementation follows this mapping: |
| 13 | + |
| 14 | + // Test enum structure |
| 15 | + expect(ParseConnectivityResult.values.length, 4); |
| 16 | + expect(ParseConnectivityResult.values, |
| 17 | + containsAll([ |
| 18 | + ParseConnectivityResult.wifi, |
| 19 | + ParseConnectivityResult.ethernet, |
| 20 | + ParseConnectivityResult.mobile, |
| 21 | + ParseConnectivityResult.none, |
| 22 | + ])); |
| 23 | + }); |
| 24 | + |
| 25 | + test('ParseConnectivityResult should have correct ordering for priority', |
| 26 | + () { |
| 27 | + // Verify enum ordering (wifi has highest priority in the if-else chain) |
| 28 | + expect(ParseConnectivityResult.wifi.index, 0); |
| 29 | + expect(ParseConnectivityResult.ethernet.index, 1); |
| 30 | + expect(ParseConnectivityResult.mobile.index, 2); |
| 31 | + expect(ParseConnectivityResult.none.index, 3); |
| 32 | + }); |
| 33 | + |
| 34 | + test('should identify online vs offline states correctly', () { |
| 35 | + // Online states |
| 36 | + expect(ParseConnectivityResult.wifi != ParseConnectivityResult.none, |
| 37 | + true); |
| 38 | + expect( |
| 39 | + ParseConnectivityResult.ethernet != ParseConnectivityResult.none, |
| 40 | + true); |
| 41 | + expect(ParseConnectivityResult.mobile != ParseConnectivityResult.none, |
| 42 | + true); |
| 43 | + |
| 44 | + // Offline state |
| 45 | + expect(ParseConnectivityResult.none == ParseConnectivityResult.none, |
| 46 | + true); |
| 47 | + }); |
| 48 | + |
| 49 | + test('mapping logic follows priority: wifi > ethernet > mobile > none', |
| 50 | + () { |
| 51 | + // This documents the if-else chain priority in checkConnectivity() |
| 52 | + // If a list contains wifi, it returns wifi |
| 53 | + // Else if it contains ethernet, it returns ethernet |
| 54 | + // Else if it contains mobile, it returns mobile |
| 55 | + // Else it returns none |
| 56 | + |
| 57 | + // Simulating the mapping logic |
| 58 | + ParseConnectivityResult mapResult(List<ConnectivityResult> list) { |
| 59 | + if (list.contains(ConnectivityResult.wifi)) { |
| 60 | + return ParseConnectivityResult.wifi; |
| 61 | + } else if (list.contains(ConnectivityResult.ethernet)) { |
| 62 | + return ParseConnectivityResult.ethernet; |
| 63 | + } else if (list.contains(ConnectivityResult.mobile)) { |
| 64 | + return ParseConnectivityResult.mobile; |
| 65 | + } else { |
| 66 | + return ParseConnectivityResult.none; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Test single connection types |
| 71 | + expect(mapResult([ConnectivityResult.wifi]), |
| 72 | + ParseConnectivityResult.wifi); |
| 73 | + expect(mapResult([ConnectivityResult.ethernet]), |
| 74 | + ParseConnectivityResult.ethernet); |
| 75 | + expect(mapResult([ConnectivityResult.mobile]), |
| 76 | + ParseConnectivityResult.mobile); |
| 77 | + expect(mapResult([ConnectivityResult.none]), |
| 78 | + ParseConnectivityResult.none); |
| 79 | + |
| 80 | + // Test priority when multiple connections exist |
| 81 | + expect(mapResult([ConnectivityResult.wifi, ConnectivityResult.ethernet]), |
| 82 | + ParseConnectivityResult.wifi); |
| 83 | + expect(mapResult([ConnectivityResult.wifi, ConnectivityResult.mobile]), |
| 84 | + ParseConnectivityResult.wifi); |
| 85 | + expect( |
| 86 | + mapResult([ConnectivityResult.ethernet, ConnectivityResult.mobile]), |
| 87 | + ParseConnectivityResult.ethernet); |
| 88 | + |
| 89 | + // Test that ethernet takes priority over mobile (important for issue #1042) |
| 90 | + expect( |
| 91 | + mapResult([ConnectivityResult.mobile, ConnectivityResult.ethernet]), |
| 92 | + ParseConnectivityResult.ethernet); |
| 93 | + |
| 94 | + // Test fallback behavior for unsupported types |
| 95 | + expect(mapResult([ConnectivityResult.bluetooth]), |
| 96 | + ParseConnectivityResult.none); |
| 97 | + expect(mapResult([ConnectivityResult.vpn]), ParseConnectivityResult.none); |
| 98 | + expect( |
| 99 | + mapResult([ConnectivityResult.other]), ParseConnectivityResult.none); |
| 100 | + |
| 101 | + // Test mixed with unsupported types |
| 102 | + expect( |
| 103 | + mapResult([ConnectivityResult.ethernet, ConnectivityResult.vpn]), |
| 104 | + ParseConnectivityResult.ethernet); |
| 105 | + }); |
| 106 | + |
| 107 | + test('ethernet should be treated as online connection type', () { |
| 108 | + // Critical test for issue #1042 fix |
| 109 | + // Ethernet must be treated as an online state, not as "none" |
| 110 | + |
| 111 | + final isOnline = |
| 112 | + ParseConnectivityResult.ethernet != ParseConnectivityResult.none; |
| 113 | + expect(isOnline, true, |
| 114 | + reason: |
| 115 | + 'Ethernet should be treated as online for LiveQuery connectivity'); |
| 116 | + }); |
| 117 | + |
| 118 | + test('all online states should be distinguishable from none', () { |
| 119 | + final onlineStates = [ |
| 120 | + ParseConnectivityResult.wifi, |
| 121 | + ParseConnectivityResult.ethernet, |
| 122 | + ParseConnectivityResult.mobile, |
| 123 | + ]; |
| 124 | + |
| 125 | + for (final state in onlineStates) { |
| 126 | + expect(state != ParseConnectivityResult.none, true, |
| 127 | + reason: '$state should be distinguishable from none'); |
| 128 | + } |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + group('ConnectivityResult enum compatibility', () { |
| 133 | + test('should handle all connectivity_plus enum values', () { |
| 134 | + // Ensure we're aware of all possible values from connectivity_plus |
| 135 | + final allConnectivityResults = [ |
| 136 | + ConnectivityResult.wifi, |
| 137 | + ConnectivityResult.ethernet, |
| 138 | + ConnectivityResult.mobile, |
| 139 | + ConnectivityResult.none, |
| 140 | + ConnectivityResult.bluetooth, |
| 141 | + ConnectivityResult.vpn, |
| 142 | + ConnectivityResult.other, |
| 143 | + ]; |
| 144 | + |
| 145 | + // Verify all values exist (will fail if connectivity_plus adds new values) |
| 146 | + expect(allConnectivityResults.length, 7); |
| 147 | + }); |
| 148 | + |
| 149 | + test('ParseConnectivityResult should support main connection types', () { |
| 150 | + // The Parse SDK should support the main internet connection types |
| 151 | + final supportedTypes = [ |
| 152 | + ParseConnectivityResult.wifi, |
| 153 | + ParseConnectivityResult.ethernet, |
| 154 | + ParseConnectivityResult.mobile, |
| 155 | + ]; |
| 156 | + |
| 157 | + expect(supportedTypes.length, 3, |
| 158 | + reason: 'Should support 3 main online connection types'); |
| 159 | + }); |
| 160 | + }); |
| 161 | +} |
0 commit comments