|
| 1 | +// Copyright 2026, the Chromium project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:firebase_core/firebase_core.dart'; |
| 6 | +import 'package:firebase_remote_config_platform_interface/src/method_channel/utils/error_code.dart'; |
| 7 | +import 'package:firebase_remote_config_platform_interface/src/method_channel/utils/exception.dart'; |
| 8 | +import 'package:flutter/services.dart'; |
| 9 | +import 'package:flutter_test/flutter_test.dart'; |
| 10 | + |
| 11 | +void main() { |
| 12 | + group('refineRemoteConfigErrorCode', () { |
| 13 | + test('classifies a missing data connection as a network error', () { |
| 14 | + // Android, through `FirebaseRemoteConfigClientException`. |
| 15 | + expect( |
| 16 | + refineRemoteConfigErrorCode( |
| 17 | + 'internal', |
| 18 | + 'A data connection is not currently allowed.', |
| 19 | + ), |
| 20 | + 'network-error', |
| 21 | + ); |
| 22 | + expect( |
| 23 | + refineRemoteConfigErrorCode( |
| 24 | + 'internal', |
| 25 | + 'Failed to get installations token. A data connection is not ' |
| 26 | + 'currently allowed.', |
| 27 | + ), |
| 28 | + 'network-error', |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + test('classifies other connectivity failures as a network error', () { |
| 33 | + for (final String message in <String>[ |
| 34 | + 'Fetch failed: Unable to resolve host "firebaseremoteconfig.googleapis.com"', |
| 35 | + 'The request timed out.', |
| 36 | + 'The Internet connection appears to be offline.', |
| 37 | + 'Network error (such as timeout or unreachable host) has occurred.', |
| 38 | + ]) { |
| 39 | + expect( |
| 40 | + refineRemoteConfigErrorCode('internal', message), |
| 41 | + 'network-error', |
| 42 | + reason: message, |
| 43 | + ); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + test('classifies a cancelled fetch', () { |
| 48 | + expect(refineRemoteConfigErrorCode('internal', 'cancelled'), 'cancelled'); |
| 49 | + expect(refineRemoteConfigErrorCode('unknown', 'Canceled'), 'cancelled'); |
| 50 | + }); |
| 51 | + |
| 52 | + test('classifies a backend status code', () { |
| 53 | + expect( |
| 54 | + refineRemoteConfigErrorCode( |
| 55 | + 'internal', |
| 56 | + 'Internal Error. Status code: 503', |
| 57 | + ), |
| 58 | + 'remote-config-server-error', |
| 59 | + ); |
| 60 | + expect( |
| 61 | + refineRemoteConfigErrorCode( |
| 62 | + 'internal', |
| 63 | + 'Internal Error. Status code: 403', |
| 64 | + ), |
| 65 | + 'forbidden', |
| 66 | + ); |
| 67 | + expect( |
| 68 | + refineRemoteConfigErrorCode( |
| 69 | + 'internal', |
| 70 | + 'Internal Error. Status code: 429', |
| 71 | + ), |
| 72 | + 'throttled', |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + test('leaves an informative status code alone', () { |
| 77 | + expect( |
| 78 | + refineRemoteConfigErrorCode('internal', 'Status code: 200'), |
| 79 | + 'internal', |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + test('leaves codes the native SDKs classified alone', () { |
| 84 | + expect( |
| 85 | + refineRemoteConfigErrorCode( |
| 86 | + 'throttled', |
| 87 | + 'frequency of requests exceeds throttled limits', |
| 88 | + ), |
| 89 | + 'throttled', |
| 90 | + ); |
| 91 | + expect( |
| 92 | + refineRemoteConfigErrorCode( |
| 93 | + 'forbidden', |
| 94 | + 'A data connection is not currently allowed.', |
| 95 | + ), |
| 96 | + 'forbidden', |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + test('leaves a message it cannot classify alone', () { |
| 101 | + expect( |
| 102 | + refineRemoteConfigErrorCode( |
| 103 | + 'internal', |
| 104 | + 'internal remote config fetch error', |
| 105 | + ), |
| 106 | + 'internal', |
| 107 | + ); |
| 108 | + expect(refineRemoteConfigErrorCode('internal', null), 'internal'); |
| 109 | + expect(refineRemoteConfigErrorCode(null, 'cancelled'), 'unknown'); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + group('convertPlatformException', () { |
| 114 | + test('throws a FirebaseException with the refined code', () { |
| 115 | + expect( |
| 116 | + () => convertPlatformException( |
| 117 | + PlatformException( |
| 118 | + code: 'firebase_remote_config', |
| 119 | + message: 'A data connection is not currently allowed.', |
| 120 | + details: <String, Object?>{ |
| 121 | + 'code': 'internal', |
| 122 | + 'message': 'A data connection is not currently allowed.', |
| 123 | + }, |
| 124 | + ), |
| 125 | + StackTrace.empty, |
| 126 | + ), |
| 127 | + throwsA( |
| 128 | + isA<FirebaseException>() |
| 129 | + .having((e) => e.plugin, 'plugin', 'firebase_remote_config') |
| 130 | + .having((e) => e.code, 'code', 'network-error') |
| 131 | + .having( |
| 132 | + (e) => e.message, |
| 133 | + 'message', |
| 134 | + 'A data connection is not currently allowed.', |
| 135 | + ), |
| 136 | + ), |
| 137 | + ); |
| 138 | + }); |
| 139 | + |
| 140 | + test('keeps the code the native SDK reported', () { |
| 141 | + expect( |
| 142 | + () => convertPlatformException( |
| 143 | + PlatformException( |
| 144 | + code: 'firebase_remote_config', |
| 145 | + message: 'frequency of requests exceeds throttled limits', |
| 146 | + details: <String, Object?>{ |
| 147 | + 'code': 'throttled', |
| 148 | + 'message': 'frequency of requests exceeds throttled limits', |
| 149 | + }, |
| 150 | + ), |
| 151 | + StackTrace.empty, |
| 152 | + ), |
| 153 | + throwsA( |
| 154 | + isA<FirebaseException>().having((e) => e.code, 'code', 'throttled'), |
| 155 | + ), |
| 156 | + ); |
| 157 | + }); |
| 158 | + |
| 159 | + test('rethrows anything that is not a PlatformException', () { |
| 160 | + final error = StateError('nope'); |
| 161 | + |
| 162 | + expect( |
| 163 | + () => convertPlatformException(error, StackTrace.empty), |
| 164 | + throwsA(same(error)), |
| 165 | + ); |
| 166 | + }); |
| 167 | + }); |
| 168 | +} |
0 commit comments