ensureLiveFirestoreForTest function
void
ensureLiveFirestoreForTest()
Bridges Firestore pigeon calls to the real Firestore REST API during
flutter test. Native plugins are unavailable in the VM test binding.
Implementation
void ensureLiveFirestoreForTest() {
if (_firestoreBridgeInstalled) return;
TestWidgetsFlutterBinding.ensureInitialized();
final messenger =
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
final codec = FirebaseFirestoreHostApi.codec;
void register(
String method,
Future<List<Object?>> Function(List<Object?> args) handler,
) {
messenger.setMockDecodedMessageHandler<Object?>(
BasicMessageChannel<Object?>(
'dev.flutter.pigeon.cloud_firestore_platform_interface.FirebaseFirestoreHostApi.$method',
codec,
),
(Object? message) async {
final args = (message! as List<Object?>);
try {
return await handler(args);
} on PlatformException catch (error) {
return <Object?>[error.code, error.message, error.details];
} catch (error) {
return <Object?>['error', error.toString(), null];
}
},
);
}
Future<List<Object?>> noop(List<Object?> args) async => <Object?>[];
register('setLoggingEnabled', (_) async => <Object?>[]);
register('enableNetwork', noop);
register('disableNetwork', noop);
register('clearPersistence', noop);
register('terminate', noop);
register('waitForPendingWrites', noop);
register(
'snapshotsInSyncSetup',
(_) async => <Object?>[
'ensemble_test_runner/firestore/snapshots-in-sync',
]);
register('persistenceCacheIndexManagerRequest', noop);
register('documentReferenceSet', (args) async {
final app = _decodeApp(args[0]);
final request = _decodeDocumentRequest(args[1]);
await _LiveFirestoreRestClient().documentSet(app, request);
return <Object?>[];
});
register('documentReferenceUpdate', (args) async {
final app = _decodeApp(args[0]);
final request = _decodeDocumentRequest(args[1]);
await _LiveFirestoreRestClient().documentUpdate(app, request);
return <Object?>[];
});
register('documentReferenceDelete', (args) async {
final app = _decodeApp(args[0]);
final request = _decodeDocumentRequest(args[1]);
await _LiveFirestoreRestClient().documentDelete(app, request);
return <Object?>[];
});
register('documentReferenceGet', (args) async {
final app = _decodeApp(args[0]);
final request = _decodeDocumentRequest(args[1]);
final snapshot = await _LiveFirestoreRestClient().documentGet(app, request);
return <Object?>[snapshot];
});
register('queryGet', (args) async {
final app = _decodeApp(args[0]);
final path = args[1]! as String;
final isCollectionGroup = args[2]! as bool;
final parameters = args[3]! as PigeonQueryParameters;
final options = args[4]! as PigeonGetOptions;
final snapshot = await _LiveFirestoreRestClient().queryGet(
app,
path: path,
isCollectionGroup: isCollectionGroup,
parameters: parameters,
options: options,
);
return <Object?>[snapshot];
});
register('documentReferenceSnapshot', (args) async {
final request = _decodeDocumentRequest(args[1]);
final listenerId =
'ensemble_test_runner/firestore/document/${_snapshotListenerCounter++}/${request.path}';
_mockFirestoreEventChannel('document', listenerId);
return <Object?>[listenerId];
});
register('querySnapshot', (args) async {
final path = args[1]! as String;
final listenerId =
'ensemble_test_runner/firestore/query/${_snapshotListenerCounter++}/$path';
_mockFirestoreEventChannel('query', listenerId);
return <Object?>[listenerId];
});
_firestoreBridgeInstalled = true;
}