@@ -676,7 +676,7 @@ protected void onStart() {
676676 Branch .init ().addFacebookPartnerParameterWithName ("ph" , getHashedValue ("6516006060" ));
677677 Log .d ("BranchSDK_Tester" , "initSession" );
678678
679- // initSessionsWithTests();
679+ initSessionsWithTests ();
680680
681681 // Branch integration validation: Validate Branch integration with your app
682682 // NOTE : The below method will run few checks for verifying correctness of the Branch integration.
@@ -698,36 +698,107 @@ private void initSessionsWithTests() {
698698 private void userAgentTests (boolean userAgentSync , int n ) {
699699 Log .i ("BranchSDK_Tester" , "Beginning stress tests" );
700700
701- for (int i = 0 ; i < n ; i ++) {
702- BranchEvent event = new BranchEvent ("Event " + i );
703- event .logEvent (this );
704- }
705-
706- Branch .sessionBuilder (this ).withCallback (new Branch .BranchUniversalReferralInitListener () {
707- @ Override
708- public void onInitFinished (BranchUniversalObject branchUniversalObject , LinkProperties linkProperties , BranchError error ) {
709- if (error != null ) {
710- Log .d ("BranchSDK_Tester" , "branch init failed. Caused by -" + error .getMessage ());
711- } else {
712- Log .d ("BranchSDK_Tester" , "branch init complete!" );
713- if (branchUniversalObject != null ) {
714- Log .d ("BranchSDK_Tester" , "title " + branchUniversalObject .getTitle ());
715- Log .d ("BranchSDK_Tester" , "CanonicalIdentifier " + branchUniversalObject .getCanonicalIdentifier ());
716- Log .d ("BranchSDK_Tester" , "metadata " + branchUniversalObject .getContentMetadata ().convertToJson ());
717- }
718-
719- if (linkProperties != null ) {
720- Log .d ("BranchSDK_Tester" , "Channel " + linkProperties .getChannel ());
721- Log .d ("BranchSDK_Tester" , "control params " + linkProperties .getControlParams ());
722- }
723- }
724-
701+ // Initialize session first, then create events in callback
702+ initializeSessionWithEventTests (n );
703+ }
725704
726- // QA purpose only
727- // TrackingControlTestRoutines.runTrackingControlTest(MainActivity.this);
728- // BUOTestRoutines.TestBUOFunctionalities(MainActivity.this);
705+ /**
706+ * Initializes Branch session and creates test events after successful initialization.
707+ * Follows SRP - single responsibility for session initialization with event creation.
708+ *
709+ * @param eventCount Number of test events to create after session initialization
710+ */
711+ private void initializeSessionWithEventTests (int eventCount ) {
712+ Branch .sessionBuilder (this ).withCallback (new BranchSessionInitializationHandler (eventCount ))
713+ .withData (this .getIntent ().getData ())
714+ .init ();
715+ }
716+
717+ /**
718+ * Handler for Branch session initialization with event creation capability.
719+ * Follows SRP and DIP principles - separated concerns and depends on abstractions.
720+ */
721+ private class BranchSessionInitializationHandler implements Branch .BranchUniversalReferralInitListener {
722+ private final int eventCount ;
723+
724+ public BranchSessionInitializationHandler (int eventCount ) {
725+ this .eventCount = eventCount ;
726+ }
727+
728+ @ Override
729+ public void onInitFinished (BranchUniversalObject branchUniversalObject , LinkProperties linkProperties , BranchError error ) {
730+ if (error != null ) {
731+ handleSessionInitializationError (error );
732+ return ;
733+ }
734+
735+ handleSessionInitializationSuccess (branchUniversalObject , linkProperties );
736+ createTestEvents ();
737+ }
738+
739+ /**
740+ * Handles successful session initialization.
741+ * Follows SRP - single responsibility for handling success scenario.
742+ */
743+ private void handleSessionInitializationSuccess (BranchUniversalObject branchUniversalObject , LinkProperties linkProperties ) {
744+ Log .d ("BranchSDK_Tester" , "branch init complete!" );
745+
746+ if (branchUniversalObject != null ) {
747+ logBranchUniversalObjectDetails (branchUniversalObject );
729748 }
730- }).withData (this .getIntent ().getData ()).init ();
749+
750+ if (linkProperties != null ) {
751+ logLinkPropertiesDetails (linkProperties );
752+ }
753+ }
754+
755+ /**
756+ * Handles session initialization errors.
757+ * Follows SRP - single responsibility for error handling.
758+ */
759+ private void handleSessionInitializationError (BranchError error ) {
760+ Log .d ("BranchSDK_Tester" , "branch init failed. Caused by -" + error .getMessage ());
761+ }
762+
763+ /**
764+ * Creates and logs test events after session is successfully initialized.
765+ * Follows SRP - single responsibility for event creation.
766+ */
767+ private void createTestEvents () {
768+ Log .i ("BranchSDK_Tester" , "Creating " + eventCount + " test events after session initialization" );
769+
770+ for (int i = 0 ; i < eventCount ; i ++) {
771+ createAndLogTestEvent (i );
772+ }
773+ }
774+
775+ /**
776+ * Creates and logs a single test event.
777+ * Follows SRP - single responsibility for individual event creation.
778+ */
779+ private void createAndLogTestEvent (int eventIndex ) {
780+ BranchEvent event = new BranchEvent ("Event " + eventIndex );
781+ event .logEvent (MainActivity .this );
782+ }
783+
784+ /**
785+ * Logs BranchUniversalObject details.
786+ * Follows SRP - single responsibility for logging object details.
787+ */
788+ private void logBranchUniversalObjectDetails (BranchUniversalObject branchUniversalObject ) {
789+ Log .d ("BranchSDK_Tester" , "title " + branchUniversalObject .getTitle ());
790+ Log .d ("BranchSDK_Tester" , "CanonicalIdentifier " + branchUniversalObject .getCanonicalIdentifier ());
791+ Log .d ("BranchSDK_Tester" , "metadata " + branchUniversalObject .getContentMetadata ().convertToJson ());
792+ }
793+
794+ /**
795+ * Logs LinkProperties details.
796+ * Follows SRP - single responsibility for logging link properties.
797+ */
798+ private void logLinkPropertiesDetails (LinkProperties linkProperties ) {
799+ Log .d ("BranchSDK_Tester" , "Channel " + linkProperties .getChannel ());
800+ Log .d ("BranchSDK_Tester" , "control params " + linkProperties .getControlParams ());
801+ }
731802 }
732803
733804 @ Override
0 commit comments