Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,29 @@ private Task<Map<String, Object>> aggregateQuery(Map<String, Object> arguments)
return taskCompletionSource.getTask();
}

private Task<Void> setIndexConfiguration(Map<String, Object> arguments) {
TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();

cachedThreadPool.execute(
() -> {
try {
FirebaseFirestore firestore =
(FirebaseFirestore) Objects.requireNonNull(arguments.get("firestore"));

Tasks.await(
firestore.setIndexConfiguration(
(String) Objects.requireNonNull(arguments.get("indexConfiguration"))));

taskCompletionSource.setResult(null);

} catch (Exception e) {
taskCompletionSource.setException(e);
}
});

return taskCompletionSource.getTask();
}

@Override
public void onMethodCall(MethodCall call, @NonNull final MethodChannel.Result result) {
Task<?> methodCallTask;
Expand Down Expand Up @@ -592,6 +615,9 @@ public void onMethodCall(MethodCall call, @NonNull final MethodChannel.Result re
case "AggregateQuery#count":
methodCallTask = aggregateQuery(call.arguments());
break;
case "Firestore#setIndexConfiguration":
methodCallTask = setIndexConfiguration(call.arguments());
break;
default:
result.notImplemented();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,98 +18,106 @@ void runInstanceTests() {
firestore = FirebaseFirestore.instance;
});

test('snapshotsInSync()', () async {
DocumentReference<Map<String, dynamic>> documentReference =
firestore.doc('flutter-tests/insync');

// Ensure deleted
await documentReference.delete();

StreamController controller = StreamController();
StreamSubscription insync;
StreamSubscription snapshots;

int inSyncCount = 0;

insync = firestore.snapshotsInSync().listen((_) {
controller.add('insync=$inSyncCount');
inSyncCount++;
});

snapshots = documentReference.snapshots().listen((ds) {
controller.add('snapshot-exists=${ds.exists}');
});

// Allow the snapshots to trigger...
await Future.delayed(const Duration(seconds: 1));

await documentReference.set({'foo': 'bar'});

await expectLater(
controller.stream,
emitsInOrder([
'insync=0', // No other snapshots
'snapshot-exists=false',
'insync=1',
'snapshot-exists=true',
'insync=2',
]),
);

await controller.close();
await insync.cancel();
await snapshots.cancel();
});

test('enableNetwork()', () async {
if (kIsWeb) return;

// Write some data while online
await firestore.enableNetwork();
DocumentReference<Map<String, dynamic>> documentReference =
firestore.doc('flutter-tests/enable-network');
await documentReference.set({'foo': 'bar'});

// Disable the network
await firestore.disableNetwork();

StreamController controller = StreamController();

// Set some data while offline
// ignore: unawaited_futures
documentReference.set({'foo': 'baz'}).then((_) async {
// Only when back online will this trigger
controller.add(true);
});
test(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this file so that web is skipped for each individual e2e test rather than for the entire file hence why it looks like a lot of changes.

'snapshotsInSync()',
() async {
DocumentReference<Map<String, dynamic>> documentReference =
firestore.doc('flutter-tests/insync');

// Ensure deleted
await documentReference.delete();

StreamController controller = StreamController();
StreamSubscription insync;
StreamSubscription snapshots;

int inSyncCount = 0;

insync = firestore.snapshotsInSync().listen((_) {
controller.add('insync=$inSyncCount');
inSyncCount++;
});

snapshots = documentReference.snapshots().listen((ds) {
controller.add('snapshot-exists=${ds.exists}');
});

// Allow the snapshots to trigger...
await Future.delayed(const Duration(seconds: 1));

await documentReference.set({'foo': 'bar'});

await expectLater(
controller.stream,
emitsInOrder([
'insync=0', // No other snapshots
'snapshot-exists=false',
'insync=1',
'snapshot-exists=true',
'insync=2',
]),
);

await controller.close();
await insync.cancel();
await snapshots.cancel();
},
skip: kIsWeb,
);

// Go back online
await firestore.enableNetwork();
test(
'enableNetwork()',
() async {
// Write some data while online
await firestore.enableNetwork();
DocumentReference<Map<String, dynamic>> documentReference =
firestore.doc('flutter-tests/enable-network');
await documentReference.set({'foo': 'bar'});

await expectLater(controller.stream, emits(true));
await controller.close();
});
// Disable the network
await firestore.disableNetwork();

test('disableNetwork()', () async {
if (kIsWeb) return;
StreamController controller = StreamController();

// Write some data while online
await firestore.enableNetwork();
DocumentReference<Map<String, dynamic>> documentReference =
firestore.doc('flutter-tests/disable-network');
await documentReference.set({'foo': 'bar'});
// Set some data while offline
// ignore: unawaited_futures
documentReference.set({'foo': 'baz'}).then((_) async {
// Only when back online will this trigger
controller.add(true);
});

// Disable the network
await firestore.disableNetwork();
// Go back online
await firestore.enableNetwork();

// Get data from cache
DocumentSnapshot<Map<String, dynamic>> documentSnapshot =
await documentReference.get();
expect(documentSnapshot.metadata.isFromCache, isTrue);
expect(documentSnapshot.data()!['foo'], equals('bar'));
await expectLater(controller.stream, emits(true));
await controller.close();
},
skip: kIsWeb,
);

// Go back online once test complete
await firestore.enableNetwork();
});
test(
'disableNetwork()',
() async {
// Write some data while online
await firestore.enableNetwork();
DocumentReference<Map<String, dynamic>> documentReference =
firestore.doc('flutter-tests/disable-network');
await documentReference.set({'foo': 'bar'});

// Disable the network
await firestore.disableNetwork();

// Get data from cache
DocumentSnapshot<Map<String, dynamic>> documentSnapshot =
await documentReference.get();
expect(documentSnapshot.metadata.isFromCache, isTrue);
expect(documentSnapshot.data()!['foo'], equals('bar'));

// Go back online once test complete
await firestore.enableNetwork();
},
skip: kIsWeb,
);

test(
'waitForPendingWrites()',
Expand All @@ -119,24 +127,108 @@ void runInstanceTests() {
skip: kIsWeb,
);

test('terminate() / clearPersistence()', () async {
// Since the firestore instance has already been used,
// calling `clearPersistence` will throw a native error.
// We first check it does throw as expected, then terminate
// the instance, and then check whether clearing succeeds.
try {
test(
'terminate() / clearPersistence()',
() async {
// Since the firestore instance has already been used,
// calling `clearPersistence` will throw a native error.
// We first check it does throw as expected, then terminate
// the instance, and then check whether clearing succeeds.
try {
await firestore.clearPersistence();
fail('Should have thrown');
} on FirebaseException catch (e) {
expect(e.code, equals('failed-precondition'));
} catch (e) {
fail('$e');
}

await firestore.terminate();
await firestore.clearPersistence();
fail('Should have thrown');
} on FirebaseException catch (e) {
expect(e.code, equals('failed-precondition'));
} catch (e) {
fail('$e');
}

await firestore.terminate();
await firestore.clearPersistence();
},
skip: kIsWeb,
);

test('setIndexConfiguration()', () async {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the integration test for setIndexConfiguration().

Index index1 = Index(
collectionGroup: 'bar',
queryScope: QueryScope.collectionGroup,
fields: [
IndexField(
fieldPath: 'fieldPath',
order: Order.ascending,
arrayConfig: ArrayConfig.contains,
)
],
);

Index index2 = Index(
collectionGroup: 'baz',
queryScope: QueryScope.collection,
fields: [
IndexField(
fieldPath: 'foo',
arrayConfig: ArrayConfig.contains,
),
IndexField(
fieldPath: 'bar',
order: Order.descending,
arrayConfig: ArrayConfig.contains,
),
IndexField(
fieldPath: 'baz',
order: Order.descending,
arrayConfig: ArrayConfig.contains,
),
],
);

FieldOverrides fieldOverride1 = FieldOverrides(
fieldPath: 'fieldPath',
indexes: [
FieldOverrideIndex(
queryScope: 'foo',
order: Order.ascending,
arrayConfig: ArrayConfig.contains,
),
FieldOverrideIndex(
queryScope: 'bar',
order: Order.descending,
arrayConfig: ArrayConfig.contains,
),
FieldOverrideIndex(
queryScope: 'baz',
order: Order.descending,
),
],
collectionGroup: 'bar',
);
FieldOverrides fieldOverride2 = FieldOverrides(
fieldPath: 'anotherField',
indexes: [
FieldOverrideIndex(
queryScope: 'foo',
order: Order.ascending,
arrayConfig: ArrayConfig.contains,
),
FieldOverrideIndex(
queryScope: 'bar',
order: Order.descending,
arrayConfig: ArrayConfig.contains,
),
FieldOverrideIndex(
queryScope: 'baz',
order: Order.descending,
),
],
collectionGroup: 'collectiongroup',
);

await firestore.setIndexConfiguration(
indexes: [index1, index2],
fieldOverrides: [fieldOverride1, fieldOverride2],
);
});
},
skip: kIsWeb,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)flutter
[self setupLoadBundleListener:call.arguments withMethodCallResult:methodCallResult];
} else if ([@"AggregateQuery#count" isEqualToString:call.method]) {
[self aggregateQuery:call.arguments withMethodCallResult:methodCallResult];
} else if ([@"Firestore#setIndexConfiguration" isEqualToString:call.method]) {
[self setIndexConfiguration:call.arguments withMethodCallResult:methodCallResult];
} else {
methodCallResult.success(FlutterMethodNotImplemented);
}
Expand Down Expand Up @@ -256,6 +258,21 @@ - (NSString *_Nonnull)flutterChannelName {

#pragma mark - Firestore API

- (void)setIndexConfiguration:(id)arguments
withMethodCallResult:(FLTFirebaseMethodCallResult *)result {
FIRFirestore *firestore = arguments[@"firestore"];
NSString *indexConfiguration = arguments[@"indexConfiguration"];

[firestore setIndexConfigurationFromJSON:indexConfiguration
completion:^(NSError *_Nullable error) {
if (error != nil) {
result.error(nil, nil, nil, error);
} else {
result.success(nil);
}
}];
}

- (void)setupSnapshotsInSyncListener:(id)arguments
withMethodCallResult:(FLTFirebaseMethodCallResult *)result {
result.success([self
Expand Down
Loading