11package com .hcaptcha .sdk .journeylitics ;
22
3+ import android .app .Application ;
4+
35import com .fasterxml .jackson .databind .JsonNode ;
46import com .fasterxml .jackson .databind .ObjectMapper ;
57import org .junit .Assert ;
68import org .junit .Test ;
9+ import org .mockito .Mockito ;
710
11+ import java .lang .reflect .Field ;
812import java .util .AbstractMap ;
913import java .util .ArrayList ;
1014import java .util .HashMap ;
1115import java .util .List ;
1216import java .util .Map ;
17+ import java .util .concurrent .atomic .AtomicBoolean ;
1318
1419public class JourneyliticsTest {
20+ private static final String VIEW_BUTTON = "Button" ;
1521 private final List <JLEvent > captured = new ArrayList <>();
1622
23+ private static void resetJourneyliticsState () throws Exception {
24+ final Field startedField = Journeylitics .class .getDeclaredField ("STARTED" );
25+ startedField .setAccessible (true );
26+ ((AtomicBoolean ) startedField .get (null )).set (false );
27+
28+ final Field appField = Journeylitics .class .getDeclaredField ("sApp" );
29+ appField .setAccessible (true );
30+ appField .set (null , null );
31+
32+ final Field configField = Journeylitics .class .getDeclaredField ("sConfig" );
33+ configField .setAccessible (true );
34+ configField .set (null , JLConfig .DEFAULT );
35+
36+ final Field sinksField = Journeylitics .class .getDeclaredField ("SINKS" );
37+ sinksField .setAccessible (true );
38+ ((List <?>) sinksField .get (null )).clear ();
39+
40+ final Field instrumentedField = Journeylitics .class .getDeclaredField ("INSTRUMENTED" );
41+ instrumentedField .setAccessible (true );
42+ ((Map <?, ?>) instrumentedField .get (null )).clear ();
43+ }
44+
1745 @ Test
1846 public void sink_emits_event () {
1947 // This test verifies that the sink pipeline works correctly
@@ -27,14 +55,14 @@ public void emit(JLEvent event) {
2755 final Map <String , Object > meta = MetaMapHelper .createMetaMap (
2856 new AbstractMap .SimpleEntry <>(FieldKey .ID , "test-button" )
2957 );
30- sink .emit (new JLEvent (EventKind .click , "Button" , new HashMap <>(meta )));
58+ sink .emit (new JLEvent (EventKind .click , VIEW_BUTTON , new HashMap <>(meta )));
3159 Assert .assertTrue (captured .size () == before + 1 );
3260 }
3361
3462 @ Test
3563 public void metadata_serializes_as_string () throws Exception {
3664 final ObjectMapper mapper = new ObjectMapper ();
37- final JLEvent event = new JLEvent (1234567890L , EventKind .click , "Button" , "meta-string" );
65+ final JLEvent event = new JLEvent (1234567890L , EventKind .click , VIEW_BUTTON , "meta-string" );
3866 final JsonNode node = mapper .readTree (mapper .writeValueAsString (event ));
3967 Assert .assertEquals ("meta-string" , node .get ("m" ).asText ());
4068 }
@@ -44,9 +72,45 @@ public void metadata_serializes_as_object() throws Exception {
4472 final ObjectMapper mapper = new ObjectMapper ();
4573 final Map <String , Object > meta = new HashMap <>();
4674 meta .put ("id" , "submit-btn" );
47- final JLEvent event = new JLEvent (1234567890L , EventKind .click , "Button" , meta );
75+ final JLEvent event = new JLEvent (1234567890L , EventKind .click , VIEW_BUTTON , meta );
4876 final JsonNode node = mapper .readTree (mapper .writeValueAsString (event ));
4977 Assert .assertEquals ("submit-btn" , node .get ("m" ).get ("id" ).asText ());
5078 }
51- }
5279
80+ @ Test
81+ public void addSink_afterStart_receivesEvents () throws Exception {
82+ resetJourneyliticsState ();
83+ final Application app = Mockito .mock (Application .class );
84+ Mockito .when (app .getApplicationContext ()).thenReturn (app );
85+ Journeylitics .start (app , new JLConfig ());
86+
87+ final List <JLEvent > events = new ArrayList <>();
88+ final JLSink sink = events ::add ;
89+ Journeylitics .addSink (sink );
90+
91+ final Map <String , Object > meta = MetaMapHelper .createMetaMap (
92+ new AbstractMap .SimpleEntry <>(FieldKey .ID , "test-button" )
93+ );
94+ Journeylitics .emit (EventKind .click , VIEW_BUTTON , meta );
95+ Assert .assertEquals (1 , events .size ());
96+ }
97+
98+ @ Test
99+ public void removeSink_stopsEvents () throws Exception {
100+ resetJourneyliticsState ();
101+ final Application app = Mockito .mock (Application .class );
102+ Mockito .when (app .getApplicationContext ()).thenReturn (app );
103+ Journeylitics .start (app , new JLConfig ());
104+
105+ final List <JLEvent > events = new ArrayList <>();
106+ final JLSink sink = events ::add ;
107+ Journeylitics .addSink (sink );
108+
109+ Journeylitics .emit (EventKind .click , VIEW_BUTTON , new HashMap <>());
110+ Assert .assertEquals (1 , events .size ());
111+
112+ Journeylitics .removeSink (sink );
113+ Journeylitics .emit (EventKind .click , VIEW_BUTTON , new HashMap <>());
114+ Assert .assertEquals (1 , events .size ());
115+ }
116+ }
0 commit comments