Skip to content

Commit

Permalink
Handle organizationUserId parameter in SyncReady event
Browse files Browse the repository at this point in the history
  • Loading branch information
pmerlet-at-didomi committed Dec 6, 2024
1 parent 2cc5813 commit 8d53066
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class DidomiEventStreamHandler : EventChannel.StreamHandler, DidomiEventListener
override fun syncReady(event: SyncReadyEvent) {
syncReadyEventIndex++
syncReadyEventReferences.put(syncReadyEventIndex, event)
sendEvent("onSyncReady", mapOf("statusApplied" to event.statusApplied, "syncReadyEventIndex" to syncReadyEventIndex))
sendEvent("onSyncReady", mapOf("organizationUserId" to event.organizationUserId, "statusApplied" to event.statusApplied, "syncReadyEventIndex" to syncReadyEventIndex))
}

override fun syncDone(event: SyncDoneEvent) {
Expand Down
35 changes: 19 additions & 16 deletions example/integration_test/set_user_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void main() {
final reset = find.byKey(Key("reset"));
final listKey = Key("components_list");

String? syncUserId;
String? syncDoneUserId;
bool isReady = false;
bool syncError = false;
bool consentChanged = false;
Expand All @@ -45,7 +45,7 @@ void main() {
isReady = true;
};
listener.onConsentChanged = () {
syncUserId = null;
syncDoneUserId = null;
syncError = false;
consentChanged = true;
syncReadyEvent = null;
Expand All @@ -55,7 +55,7 @@ void main() {
};
// ignore: deprecated_member_use
listener.onSyncDone = (String userId) {
syncUserId = userId != "null" ? userId : null;
syncDoneUserId = userId != "null" ? userId : null;
};
listener.onSyncError = (String error) {
syncError = true;
Expand All @@ -67,18 +67,19 @@ void main() {
Future waitForSync(WidgetTester tester) async {
// Wait for sync result
await tester.runAsync(() async {
while (syncUserId == null && !syncError) {
while (syncReadyEvent == null && !syncError) {
await Future.delayed(Duration(milliseconds: 100));
}
});
}

// Assert sync event is triggered correctly.
Future<void> assertSyncEvent(WidgetTester tester) async {
Future<void> assertSyncReadyEvent(WidgetTester tester) async {
// First time the sync event is triggered. Status is applied and API Event triggered only once.
assert(syncReadyEvent?.statusApplied == true);
assert((await syncReadyEvent?.syncAcknowledged()) == true);
assert((await syncReadyEvent?.syncAcknowledged()) == false);
assert(syncReadyEvent?.organizationUserId == userId);

// We reinitialize the SDK to re-trigger the sync event.
await tester.tap(initializeBtnFinder);
Expand All @@ -89,26 +90,28 @@ void main() {
assert(syncReadyEvent?.statusApplied == false);
assert((await syncReadyEvent?.syncAcknowledged()) == false);
assert((await syncReadyEvent?.syncAcknowledged()) == false);
assert(syncReadyEvent?.organizationUserId == userId);
}

// Reset all variables used for assertion.
void resetExpectedSyncValues() {
syncUserId = null;
syncDoneUserId = null;
syncError = false;
consentChanged = false;
syncReadyEvent = null;
}

// Assert that all the expected sync variables are populated.
void assertExpectedSyncValuesArePopulated() {
assert(syncUserId == userId);
assert(syncDoneUserId == userId);
assert(syncError == false);
assert(syncReadyEvent != null);
assert(syncReadyEvent?.organizationUserId == userId);
}

// Assert that all the expected sync variables are empty.
void assertExpectedSyncValuesAreEmpty() {
assert(syncUserId == null);
assert(syncDoneUserId == null);
assert(syncError == false);
assert(syncReadyEvent == null);
}
Expand All @@ -126,7 +129,7 @@ void main() {
app.main();
await tester.pumpAndSettle();

assert(syncUserId == null);
assert(syncDoneUserId == null);
assert(syncError == false);
assert(syncReadyEvent == null);

Expand Down Expand Up @@ -179,7 +182,7 @@ void main() {
await waitForSync(tester);

// Encryption parameters are not valid
assert(syncUserId == null);
assert(syncDoneUserId == null);
assert(syncError == true);
assert(syncReadyEvent == null);
});
Expand Down Expand Up @@ -209,10 +212,10 @@ void main() {

await waitForSync(tester);

assert(syncUserId == userId);
assert(syncDoneUserId == userId);
assert(syncError == false);

await assertSyncEvent(tester);
await assertSyncReadyEvent(tester);
});

testWidgets("Click setUser with id and underage false", (WidgetTester tester) async {
Expand Down Expand Up @@ -254,10 +257,10 @@ void main() {

await waitForSync(tester);

assert(syncUserId == userId);
assert(syncDoneUserId == userId);
assert(syncError == false);

await assertSyncEvent(tester);
await assertSyncReadyEvent(tester);
});

testWidgets("Click setUser with id and underage true", (WidgetTester tester) async {
Expand Down Expand Up @@ -299,10 +302,10 @@ void main() {

await waitForSync(tester);

assert(syncUserId == userId);
assert(syncDoneUserId == userId);
assert(syncError == false);

await assertSyncEvent(tester);
await assertSyncReadyEvent(tester);
});

testWidgets("Click setUser with encryption", (WidgetTester tester) async {
Expand Down
2 changes: 1 addition & 1 deletion ios/Classes/DidomiEventStreamHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class DidomiEventStreamHandler : NSObject, FlutterStreamHandler {
}
strongSelf.syncReadyEventIndex += 1
strongSelf.syncReadyEventReferences[strongSelf.syncReadyEventIndex] = event
strongSelf.sendEvent(eventType: "onSyncReady", arguments: ["statusApplied": event.statusApplied, "syncReadyEventIndex": strongSelf.syncReadyEventIndex])
strongSelf.sendEvent(eventType: "onSyncReady", arguments: ["organizationUserId": event.organizationUserId, "statusApplied": event.statusApplied, "syncReadyEventIndex": strongSelf.syncReadyEventIndex])
}
eventListener.onSyncDone = { [weak self] event, organizationUserId in
self?.sendEvent(eventType: "onSyncDone", arguments: ["organizationUserId": organizationUserId])
Expand Down
1 change: 1 addition & 0 deletions lib/events/events_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ class EventsHandler {
case "onSyncReady":
for (var listener in listeners) {
final SyncReadyEvent newEvent = SyncReadyEvent(
event["organizationUserId"],
event["statusApplied"],
() async {
// This method allows us to execute the syncAcknowledged callback from the flutter side.
Expand Down
4 changes: 3 additions & 1 deletion lib/events/sync_ready_event.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
class SyncReadyEvent {
// The organization user ID corresponding to the event
String organizationUserId;
// Boolean that indicates whether remote user status has been applied locally.
bool statusApplied;
// Function used to send a Sync Acknowledged API Event. Returns **true** if the API Event was sent, **false** otherwise.
Future<bool> Function() syncAcknowledged = () async => false;

SyncReadyEvent(this.statusApplied, this.syncAcknowledged);
SyncReadyEvent(this.organizationUserId, this.statusApplied, this.syncAcknowledged);
}

0 comments on commit 8d53066

Please sign in to comment.