@@ -73,6 +73,8 @@ public class AutoRecordEventClientTest {
7373 private ClickstreamContext clickstreamContext ;
7474 private AutoRecordEventClient client ;
7575 private LifecycleRegistry lifecycle ;
76+ private LifecycleOwner lifecycleOwner ;
77+ private ClickstreamManager clickstreamManager ;
7678
7779 /**
7880 * prepare AutoRecordEventClient and context.
@@ -83,22 +85,91 @@ public void setup() {
8385 dbUtil = new ClickstreamDBUtil (context );
8486 AWSClickstreamPluginConfiguration .Builder configurationBuilder = AWSClickstreamPluginConfiguration .builder ();
8587 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" )
8789 .withSendEventsInterval (10000 )
8890 .withTrackScreenViewEvents (true )
8991 .withTrackUserEngagementEvents (true );
9092 AWSClickstreamPluginConfiguration clickstreamPluginConfiguration = configurationBuilder .build ();
91- ClickstreamManager clickstreamManager =
92- ClickstreamManagerFactory .create (context , clickstreamPluginConfiguration );
93+ clickstreamManager = ClickstreamManagerFactory .create (context , clickstreamPluginConfiguration );
9394 client = clickstreamManager .getAutoRecordEventClient ();
9495 clickstreamContext = clickstreamManager .getClickstreamContext ();
9596 callbacks = new ActivityLifecycleManager (clickstreamManager );
9697
9798 ActivityLifecycleManager lifecycleManager = new ActivityLifecycleManager (clickstreamManager );
98- lifecycle = new LifecycleRegistry (mock (LifecycleOwner .class ));
99+ lifecycleOwner = mock (LifecycleOwner .class );
100+ lifecycle = new LifecycleRegistry (lifecycleOwner );
99101 lifecycleManager .startLifecycleTracking (ApplicationProvider .getApplicationContext (), lifecycle );
100102 }
101103
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+
102173 /**
103174 * test record user engagement event after view screen more than 1 second.
104175 *
@@ -147,6 +218,7 @@ public void testEventsHaveSameSessionId() throws Exception {
147218 callbacks .onActivityCreated (activity , bundle );
148219 callbacks .onActivityStarted (activity );
149220 callbacks .onActivityResumed (activity );
221+ assertNotNull (lifecycleOwner );
150222 lifecycle .handleLifecycleEvent (Lifecycle .Event .ON_STOP );
151223 try (Cursor cursor = dbUtil .queryAllEvents ()) {
152224 List <String > eventList = new ArrayList <>();
@@ -864,7 +936,7 @@ public void testOSVersionForUpdate() throws Exception {
864936 @ Test
865937 public void testHandleFirstOpen () throws Exception {
866938 client .handleAppStart ();
867- assertEquals (2 , dbUtil .getTotalNumber ());
939+ assertEquals (3 , dbUtil .getTotalNumber ());
868940 Cursor cursor = dbUtil .queryAllEvents ();
869941 cursor .moveToFirst ();
870942 String eventString = cursor .getString (2 );
@@ -889,7 +961,7 @@ public void testHandleFirstOpenMultiTimes() throws Exception {
889961 client .handleAppStart ();
890962 client .handleAppStart ();
891963 client .handleAppStart ();
892- assertEquals (4 , dbUtil .getTotalNumber ());
964+ assertEquals (3 , dbUtil .getTotalNumber ());
893965 Cursor cursor = dbUtil .queryAllEvents ();
894966 cursor .moveToFirst ();
895967 String eventString = cursor .getString (2 );
@@ -906,14 +978,14 @@ public void testHandleFirstOpenMultiTimes() throws Exception {
906978 */
907979 @ Test
908980 public void testHandleAppStart () throws Exception {
909- client .handleAppStart ();
910981 Activity activity1 = mock (Activity .class );
911982 Bundle bundle = mock (Bundle .class );
912983 callbacks .onActivityCreated (activity1 , bundle );
913984 callbacks .onActivityStarted (activity1 );
914985 callbacks .onActivityResumed (activity1 );
986+ client .handleAppEnd ();
915987 client .handleAppStart ();
916- assertEquals (4 , dbUtil .getTotalNumber ());
988+ assertEquals (6 , dbUtil .getTotalNumber ());
917989 try (Cursor cursor = dbUtil .queryAllEvents ()) {
918990 List <JSONObject > eventList = new ArrayList <>();
919991 while (cursor .moveToNext ()) {
@@ -928,10 +1000,11 @@ public void testHandleAppStart() throws Exception {
9281000 assertFalse (appStart1 .has (ReservedAttribute .SCREEN_NAME ));
9291001 assertFalse (appStart1 .has (Event .ReservedAttribute .SCREEN_ID ));
9301002
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" );
9351008 assertFalse (appStart2 .getBoolean (Event .ReservedAttribute .IS_FIRST_TIME ));
9361009 assertTrue (appStart2 .has (ReservedAttribute .SCREEN_NAME ));
9371010 assertTrue (appStart2 .has (ReservedAttribute .SCREEN_UNIQUE_ID ));
0 commit comments