Skip to content

Commit bb3545f

Browse files
committed
PR Comments
1 parent 41a990c commit bb3545f

File tree

9 files changed

+56
-45
lines changed

9 files changed

+56
-45
lines changed

packages/analytics/amplify_analytics_pinpoint/example/integration_test/auto_session_tracking_test.dart

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,51 +26,55 @@ void main() {
2626
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
2727

2828
group('auto session tracking', () {
29-
final mockLifecycleObserver = MockLifecycleProvider();
29+
final mockLifecycleProvider = MockLifecycleProvider();
3030

3131
late Stream<TestEvent> eventsStream;
3232

3333
setUp(() async {
34-
eventsStream = await configureAnalytics(
35-
appLifecycleProvider: mockLifecycleObserver,
34+
await configureAnalytics(
35+
appLifecycleProvider: mockLifecycleProvider,
3636
);
37+
eventsStream = await subscribeToEvents();
3738
});
3839

3940
testWidgets(
4041
'manual trigger of onBackground/onForeground triggers session '
4142
'start/end events ',
4243
(_) async {
43-
mockLifecycleObserver.triggerOnBackgroundListener();
44+
mockLifecycleProvider.triggerOnBackgroundListener();
4445

4546
TestSession? sessionStop;
4647

4748
// Verify new session has newer values than old session
4849
final streamSubscription = eventsStream.listen(
4950
expectAsync1(
5051
count: 2,
51-
(event) {
52+
(event) async {
5253
if (sessionStop == null) {
5354
expect(event.eventType, zSessionStopEventType);
5455
sessionStop = event.session;
55-
return;
56+
mockLifecycleProvider.triggerOnForegroundListener();
57+
await Amplify.Analytics.flushEvents();
58+
} else {
59+
expect(event.eventType, zSessionStartEventType);
60+
final sessionStart = event.session;
61+
expect(
62+
sessionStop!.sessionId,
63+
isNot(sessionStart.sessionId),
64+
);
65+
expect(
66+
sessionStart.startTimestamp
67+
.isAfter(sessionStop!.stopTimestamp!),
68+
isTrue,
69+
reason: 'onForeground was called after onBackground',
70+
);
5671
}
57-
expect(event.eventType, zSessionStartEventType);
58-
final sessionStart = event.session;
59-
expect(
60-
sessionStop!.sessionId,
61-
isNot(sessionStart.sessionId),
62-
);
63-
expect(
64-
sessionStart.startTimestamp
65-
.isAfter(sessionStop!.stopTimestamp!),
66-
isTrue,
67-
reason: 'onForeground was called after onBackground',
68-
);
6972
},
7073
),
7174
);
7275
addTearDown(streamSubscription.cancel);
7376

77+
/*
7478
await expectLater(
7579
eventsStream,
7680
emits(
@@ -82,7 +86,7 @@ void main() {
8286
),
8387
);
8488
85-
mockLifecycleObserver.triggerOnForegroundListener();
89+
mockLifecycleProvider.triggerOnForegroundListener();
8690
8791
await Amplify.Analytics.flushEvents();
8892
@@ -96,6 +100,8 @@ void main() {
96100
),
97101
),
98102
);
103+
104+
*/
99105
},
100106
timeout: const Timeout(Duration(minutes: 3)),
101107
);

packages/analytics/amplify_analytics_pinpoint/example/integration_test/enable_disable_test.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ void main() {
2525
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
2626

2727
group('enable/disable', () {
28-
final mockLifecycleObserver = MockLifecycleProvider();
28+
final mockLifecycleProvider = MockLifecycleProvider();
2929

3030
late Stream<TestEvent> eventsStream;
3131

3232
setUp(() async {
33-
eventsStream = await configureAnalytics(
34-
appLifecycleProvider: mockLifecycleObserver,
33+
await configureAnalytics(
34+
appLifecycleProvider: mockLifecycleProvider,
3535
);
36+
eventsStream = await subscribeToEvents();
3637
});
3738

3839
testWidgets(
@@ -51,8 +52,9 @@ void main() {
5152

5253
await Amplify.Analytics.recordEvent(event: customEvent);
5354

54-
mockLifecycleObserver.triggerOnBackgroundListener();
55-
mockLifecycleObserver.triggerOnForegroundListener();
55+
// Ensure app background/foreground does not auto send event
56+
mockLifecycleProvider.triggerOnBackgroundListener();
57+
mockLifecycleProvider.triggerOnForegroundListener();
5658

5759
// Give time for events to propagate if they were sent to remote server
5860
// to ensure the failure is not triggered

packages/analytics/amplify_analytics_pinpoint/example/integration_test/events_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ void main() {
2828
late Stream<TestEvent> eventsStream;
2929

3030
setUp(() async {
31-
eventsStream = await configureAnalytics();
31+
await configureAnalytics();
32+
eventsStream = await subscribeToEvents();
3233
});
3334

3435
testWidgets(

packages/analytics/amplify_analytics_pinpoint/example/integration_test/identify_user_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ void main() {
3030
late Stream<TestEvent> eventsStream;
3131

3232
setUpAll(() async {
33-
eventsStream = await configureAnalytics();
33+
await configureAnalytics();
34+
eventsStream = await subscribeToEvents();
3435
});
3536

3637
testWidgets(

packages/analytics/amplify_analytics_pinpoint/example/integration_test/utils/mock_key_value_store.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ import 'package:amplify_flutter/amplify_flutter.dart';
2121
///
2222
/// Should be static to mimic the behavior of iOS/Android which persist keys
2323
/// between relaunches (e.g. between test groups).
24-
final SecureStorageInterface mockKeyValueStore = _MockKeyValueStore()
24+
final SecureStorageInterface mockEndpointInfoStore = _MockEndpointInfoStore()
2525
..write(
2626
key: AmplifyAnalyticsPinpointDart.endpointIdStorageKey,
2727
value: mockEndpointId,
2828
);
2929
final String mockEndpointId = uuid();
3030

31-
class _MockKeyValueStore extends SecureStorageInterface {
31+
class _MockEndpointInfoStore extends SecureStorageInterface {
3232
final _storage = <String, String>{};
3333

3434
@override

packages/analytics/amplify_analytics_pinpoint/example/integration_test/utils/setup_utils.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import 'package:flutter_test/flutter_test.dart';
2727
import 'mock_key_value_store.dart';
2828
import 'test_event.dart';
2929

30-
Future<Stream<TestEvent>> configureAnalytics({
30+
Future<void> configureAnalytics({
3131
AppLifecycleProvider? appLifecycleProvider,
3232
}) async {
3333
await Amplify.addPlugins([
@@ -41,7 +41,7 @@ Future<Stream<TestEvent>> configureAnalytics({
4141
),
4242
AmplifyAnalyticsPinpoint(
4343
appLifecycleProvider: appLifecycleProvider,
44-
keyValueStore: mockKeyValueStore,
44+
endpointInfoStore: mockEndpointInfoStore,
4545
),
4646
AmplifyAPI(),
4747
]);
@@ -57,7 +57,9 @@ Future<Stream<TestEvent>> configureAnalytics({
5757
}
5858
await Amplify.reset();
5959
});
60+
}
6061

62+
Future<Stream<TestEvent>> subscribeToEvents() async {
6163
final subscriptionEstablished = Completer<void>.sync();
6264

6365
final eventsStream = Amplify.API

packages/analytics/amplify_analytics_pinpoint/lib/src/analytics_plugin_impl.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import 'package:meta/meta.dart';
2525
class AmplifyAnalyticsPinpoint extends AmplifyAnalyticsPinpointDart {
2626
/// {@macro amplify_analytics_pinpoint.analytics_plugin_impl}
2727
AmplifyAnalyticsPinpoint({
28-
@visibleForTesting super.keyValueStore,
28+
@visibleForTesting super.endpointInfoStore,
2929
@visibleForTesting CachedEventsPathProvider? pathProvider,
3030
@visibleForTesting AppLifecycleProvider? appLifecycleProvider,
3131
@visibleForTesting DeviceContextInfoProvider? deviceContextInfoProvider,

packages/analytics/amplify_analytics_pinpoint_dart/lib/src/analytics_plugin_impl.dart

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ const zSessionStopEventType = '_session.stop';
4848
class AmplifyAnalyticsPinpointDart extends AnalyticsPluginInterface {
4949
/// {@macro amplify_analytics_pinpoint_dart.amplify_analytics_pinpoint_dart}
5050
AmplifyAnalyticsPinpointDart({
51-
SecureStorageInterface? keyValueStore,
51+
SecureStorageInterface? endpointInfoStore,
5252
CachedEventsPathProvider? pathProvider,
5353
AppLifecycleProvider? appLifecycleProvider,
5454
DeviceContextInfoProvider? deviceContextInfoProvider,
5555
required Connect dbConnectFunction,
56-
}) : _keyValueStore = keyValueStore ??
56+
}) : _endpointInfoStore = endpointInfoStore ??
5757
AmplifySecureStorageWorker(
5858
config: AmplifySecureStorageConfig(
5959
scope: 'analyticsPinpoint',
@@ -77,9 +77,8 @@ class AmplifyAnalyticsPinpointDart extends AnalyticsPluginInterface {
7777
var _isConfigured = false;
7878
var _analyticsEnabled = false;
7979

80-
@visibleForTesting
81-
8280
/// Storage key for the static Pinpoint endpoint id
81+
@visibleForTesting
8382
static const String endpointIdStorageKey = 'UniqueId';
8483
static const String _endpointGlobalAttrsKey = 'EndpointGlobalAttributesKey';
8584
static const String _endpointGlobalMetricsKey = 'EndpointGlobalMetricsKey';
@@ -94,7 +93,7 @@ class AmplifyAnalyticsPinpointDart extends AnalyticsPluginInterface {
9493
late final EventStorageAdapter _eventStorageAdapter;
9594
late final StoppableTimer _autoEventSubmitter;
9695

97-
final SecureStorageInterface _keyValueStore;
96+
final SecureStorageInterface _endpointInfoStore;
9897

9998
/// External Flutter Provider implementations
10099
final CachedEventsPathProvider? _pathProvider;
@@ -151,10 +150,10 @@ class AmplifyAnalyticsPinpointDart extends AnalyticsPluginInterface {
151150

152151
// Retrieve Unique ID
153152
final savedFixedEndpointId =
154-
await _keyValueStore.read(key: endpointIdStorageKey);
153+
await _endpointInfoStore.read(key: endpointIdStorageKey);
155154
final fixedEndpointId = savedFixedEndpointId ?? const Uuid().v1();
156155
if (savedFixedEndpointId == null) {
157-
await _keyValueStore.write(
156+
await _endpointInfoStore.write(
158157
key: endpointIdStorageKey,
159158
value: fixedEndpointId,
160159
);
@@ -179,22 +178,22 @@ class AmplifyAnalyticsPinpointDart extends AnalyticsPluginInterface {
179178

180179
/// Retrieve stored GlobalAttributes
181180
final cachedAttributes =
182-
await _keyValueStore.read(key: _endpointGlobalAttrsKey);
181+
await _endpointInfoStore.read(key: _endpointGlobalAttrsKey);
183182
final globalAttributes = cachedAttributes == null
184183
? <String, String>{}
185184
: (jsonDecode(cachedAttributes) as Map<String, Object?>)
186185
.cast<String, String>();
187186

188187
/// Retrieve stored GlobalMetrics
189188
final cachedMetrics =
190-
await _keyValueStore.read(key: _endpointGlobalMetricsKey);
189+
await _endpointInfoStore.read(key: _endpointGlobalMetricsKey);
191190
final globalMetrics = cachedMetrics == null
192191
? <String, double>{}
193192
: (jsonDecode(cachedMetrics) as Map<String, Object?>)
194193
.cast<String, double>();
195194

196195
_endpointGlobalFieldsManager = EndpointGlobalFieldsManager(
197-
_keyValueStore,
196+
_endpointInfoStore,
198197
globalAttributes,
199198
globalMetrics,
200199
);

packages/analytics/amplify_analytics_pinpoint_dart/lib/src/impl/analytics_client/endpoint_client/endpoint_global_fields_manager.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ import 'package:amplify_secure_storage_dart/amplify_secure_storage_dart.dart';
2929
class EndpointGlobalFieldsManager {
3030
/// {@macro amplify_analytics_pinpoint_dart.endpoint_global_fields_manager}
3131
EndpointGlobalFieldsManager(
32-
this._keyValueStore,
32+
this._endpointInfoStore,
3333
this._globalAttributes,
3434
this._globalMetrics,
3535
);
3636

37-
final SecureStorageInterface _keyValueStore;
37+
final SecureStorageInterface _endpointInfoStore;
3838
final Map<String, String> _globalAttributes;
3939
final Map<String, double> _globalMetrics;
4040

@@ -118,7 +118,7 @@ class EndpointGlobalFieldsManager {
118118
}
119119

120120
Future<void> _saveAttributes() async {
121-
await _keyValueStore.write(
121+
await _endpointInfoStore.write(
122122
key: _endpointGlobalAttrsKey,
123123
value: jsonEncode(_globalAttributes),
124124
);
@@ -154,7 +154,7 @@ class EndpointGlobalFieldsManager {
154154
}
155155

156156
Future<void> _saveMetrics() async {
157-
await _keyValueStore.write(
157+
await _endpointInfoStore.write(
158158
key: _endpointGlobalMetricsKey,
159159
value: jsonEncode(_globalMetrics),
160160
);

0 commit comments

Comments
 (0)