@@ -73,6 +73,8 @@ public class AutoRecordEventClientTest {
73
73
private ClickstreamContext clickstreamContext ;
74
74
private AutoRecordEventClient client ;
75
75
private LifecycleRegistry lifecycle ;
76
+ private LifecycleOwner lifecycleOwner ;
77
+ private ClickstreamManager clickstreamManager ;
76
78
77
79
/**
78
80
* prepare AutoRecordEventClient and context.
@@ -83,22 +85,91 @@ public void setup() {
83
85
dbUtil = new ClickstreamDBUtil (context );
84
86
AWSClickstreamPluginConfiguration .Builder configurationBuilder = AWSClickstreamPluginConfiguration .builder ();
85
87
configurationBuilder .withAppId ("demo-app" )
86
- .withEndpoint ("http://cs-se-serve-1qtj719j88vwn-1291141553.ap-southeast-1.elb.amazonaws .com/collect" )
88
+ .withEndpoint ("http://example .com/collect" )
87
89
.withSendEventsInterval (10000 )
88
90
.withTrackScreenViewEvents (true )
89
91
.withTrackUserEngagementEvents (true );
90
92
AWSClickstreamPluginConfiguration clickstreamPluginConfiguration = configurationBuilder .build ();
91
- ClickstreamManager clickstreamManager =
92
- ClickstreamManagerFactory .create (context , clickstreamPluginConfiguration );
93
+ clickstreamManager = ClickstreamManagerFactory .create (context , clickstreamPluginConfiguration );
93
94
client = clickstreamManager .getAutoRecordEventClient ();
94
95
clickstreamContext = clickstreamManager .getClickstreamContext ();
95
96
callbacks = new ActivityLifecycleManager (clickstreamManager );
96
97
97
98
ActivityLifecycleManager lifecycleManager = new ActivityLifecycleManager (clickstreamManager );
98
- lifecycle = new LifecycleRegistry (mock (LifecycleOwner .class ));
99
+ lifecycleOwner = mock (LifecycleOwner .class );
100
+ lifecycle = new LifecycleRegistry (lifecycleOwner );
99
101
lifecycleManager .startLifecycleTracking (ApplicationProvider .getApplicationContext (), lifecycle );
100
102
}
101
103
104
+ /**
105
+ * test events after SDK initialization.
106
+ *
107
+ * @throws Exception exception.
108
+ */
109
+ @ Test
110
+ public void testSDKInitializationEvents () throws Exception {
111
+ try (Cursor cursor = dbUtil .queryAllEvents ()) {
112
+ List <String > eventList = new ArrayList <>();
113
+ while (cursor .moveToNext ()) {
114
+ String eventString = cursor .getString (2 );
115
+ JSONObject jsonObject = new JSONObject (eventString );
116
+ String eventName = jsonObject .getString ("event_type" );
117
+ eventList .add (eventName );
118
+ }
119
+ assertEquals (3 , eventList .size ());
120
+ assertEquals (Event .PresetEvent .FIRST_OPEN , eventList .get (0 ));
121
+ assertEquals (Event .PresetEvent .APP_START , eventList .get (1 ));
122
+ assertEquals (Event .PresetEvent .SESSION_START , eventList .get (2 ));
123
+ }
124
+ }
125
+
126
+ /**
127
+ * test SDK initialize with stored session.
128
+ *
129
+ * @throws Exception exception.
130
+ */
131
+ @ Test
132
+ public void testSDKInitializeWithStoredSession () throws Exception {
133
+ ReflectUtil .invokeMethod (clickstreamManager , "handleSessionStart" );
134
+ try (Cursor cursor = dbUtil .queryAllEvents ()) {
135
+ List <String > eventList = new ArrayList <>();
136
+ while (cursor .moveToNext ()) {
137
+ String eventString = cursor .getString (2 );
138
+ JSONObject jsonObject = new JSONObject (eventString );
139
+ String eventName = jsonObject .getString ("event_type" );
140
+ eventList .add (eventName );
141
+ }
142
+ assertEquals (3 , eventList .size ());
143
+ assertEquals (Event .PresetEvent .FIRST_OPEN , eventList .get (0 ));
144
+ assertEquals (Event .PresetEvent .APP_START , eventList .get (1 ));
145
+ assertEquals (Event .PresetEvent .SESSION_START , eventList .get (2 ));
146
+ }
147
+ }
148
+
149
+ /**
150
+ * test SDK lazy initialization.
151
+ *
152
+ * @throws Exception exception.
153
+ */
154
+ @ Test
155
+ public void testLazyInitialization () throws Exception {
156
+ lifecycle .handleLifecycleEvent (Lifecycle .Event .ON_START );
157
+ try (Cursor cursor = dbUtil .queryAllEvents ()) {
158
+ List <String > eventList = new ArrayList <>();
159
+ while (cursor .moveToNext ()) {
160
+ String eventString = cursor .getString (2 );
161
+ JSONObject jsonObject = new JSONObject (eventString );
162
+ String eventName = jsonObject .getString ("event_type" );
163
+ eventList .add (eventName );
164
+ }
165
+ assertEquals (3 , eventList .size ());
166
+ assertEquals (Event .PresetEvent .FIRST_OPEN , eventList .get (0 ));
167
+ assertEquals (Event .PresetEvent .APP_START , eventList .get (1 ));
168
+ assertEquals (Event .PresetEvent .SESSION_START , eventList .get (2 ));
169
+ }
170
+ }
171
+
172
+
102
173
/**
103
174
* test record user engagement event after view screen more than 1 second.
104
175
*
@@ -147,6 +218,7 @@ public void testEventsHaveSameSessionId() throws Exception {
147
218
callbacks .onActivityCreated (activity , bundle );
148
219
callbacks .onActivityStarted (activity );
149
220
callbacks .onActivityResumed (activity );
221
+ assertNotNull (lifecycleOwner );
150
222
lifecycle .handleLifecycleEvent (Lifecycle .Event .ON_STOP );
151
223
try (Cursor cursor = dbUtil .queryAllEvents ()) {
152
224
List <String > eventList = new ArrayList <>();
@@ -864,7 +936,7 @@ public void testOSVersionForUpdate() throws Exception {
864
936
@ Test
865
937
public void testHandleFirstOpen () throws Exception {
866
938
client .handleAppStart ();
867
- assertEquals (2 , dbUtil .getTotalNumber ());
939
+ assertEquals (3 , dbUtil .getTotalNumber ());
868
940
Cursor cursor = dbUtil .queryAllEvents ();
869
941
cursor .moveToFirst ();
870
942
String eventString = cursor .getString (2 );
@@ -889,7 +961,7 @@ public void testHandleFirstOpenMultiTimes() throws Exception {
889
961
client .handleAppStart ();
890
962
client .handleAppStart ();
891
963
client .handleAppStart ();
892
- assertEquals (4 , dbUtil .getTotalNumber ());
964
+ assertEquals (3 , dbUtil .getTotalNumber ());
893
965
Cursor cursor = dbUtil .queryAllEvents ();
894
966
cursor .moveToFirst ();
895
967
String eventString = cursor .getString (2 );
@@ -906,14 +978,14 @@ public void testHandleFirstOpenMultiTimes() throws Exception {
906
978
*/
907
979
@ Test
908
980
public void testHandleAppStart () throws Exception {
909
- client .handleAppStart ();
910
981
Activity activity1 = mock (Activity .class );
911
982
Bundle bundle = mock (Bundle .class );
912
983
callbacks .onActivityCreated (activity1 , bundle );
913
984
callbacks .onActivityStarted (activity1 );
914
985
callbacks .onActivityResumed (activity1 );
986
+ client .handleAppEnd ();
915
987
client .handleAppStart ();
916
- assertEquals (4 , dbUtil .getTotalNumber ());
988
+ assertEquals (6 , dbUtil .getTotalNumber ());
917
989
try (Cursor cursor = dbUtil .queryAllEvents ()) {
918
990
List <JSONObject > eventList = new ArrayList <>();
919
991
while (cursor .moveToNext ()) {
@@ -928,10 +1000,11 @@ public void testHandleAppStart() throws Exception {
928
1000
assertFalse (appStart1 .has (ReservedAttribute .SCREEN_NAME ));
929
1001
assertFalse (appStart1 .has (Event .ReservedAttribute .SCREEN_ID ));
930
1002
931
- assertEquals (Event .PresetEvent .SCREEN_VIEW , eventList .get (2 ).getString ("event_type" ));
932
-
933
- assertEquals (Event .PresetEvent .APP_START , eventList .get (3 ).getString ("event_type" ));
934
- JSONObject appStart2 = eventList .get (3 ).getJSONObject ("attributes" );
1003
+ assertEquals (Event .PresetEvent .SESSION_START , eventList .get (2 ).getString ("event_type" ));
1004
+ assertEquals (Event .PresetEvent .SCREEN_VIEW , eventList .get (3 ).getString ("event_type" ));
1005
+ assertEquals (Event .PresetEvent .APP_END , eventList .get (4 ).getString ("event_type" ));
1006
+ assertEquals (Event .PresetEvent .APP_START , eventList .get (5 ).getString ("event_type" ));
1007
+ JSONObject appStart2 = eventList .get (5 ).getJSONObject ("attributes" );
935
1008
assertFalse (appStart2 .getBoolean (Event .ReservedAttribute .IS_FIRST_TIME ));
936
1009
assertTrue (appStart2 .has (ReservedAttribute .SCREEN_NAME ));
937
1010
assertTrue (appStart2 .has (ReservedAttribute .SCREEN_UNIQUE_ID ));
0 commit comments