1+ package io .branch .referral ;
2+
3+ import android .graphics .Bitmap ;
4+ import android .graphics .Color ;
5+
6+ import androidx .test .ext .junit .runners .AndroidJUnit4 ;
7+
8+ import org .json .JSONException ;
9+ import org .json .JSONObject ;
10+ import org .junit .Assert ;
11+ import org .junit .Before ;
12+ import org .junit .Test ;
13+ import org .junit .runner .RunWith ;
14+
15+ import java .io .IOException ;
16+ import java .util .concurrent .CountDownLatch ;
17+
18+ import io .branch .indexing .BranchUniversalObject ;
19+ import io .branch .referral .QRCode .BranchQRCode ;
20+ import io .branch .referral .util .LinkProperties ;
21+
22+ @ RunWith (AndroidJUnit4 .class )
23+ public class BranchQRCodeTests extends BranchTest {
24+
25+ @ Before
26+ public void initializeValues (){
27+ initBranchInstance ();
28+ }
29+
30+ @ Test
31+ public void testQRCodeWithSettings () {
32+ initSessionResumeActivity (null , new Runnable () {
33+ @ Override
34+ public void run () {
35+ BranchQRCode qrCode = new BranchQRCode ()
36+ .setCodeColor ("#a4c639" )
37+ .setBackgroundColor (Color .WHITE )
38+ .setMargin (1 )
39+ .setWidth (512 )
40+ .setImageFormat (BranchQRCode .BranchImageFormat .PNG )
41+ .setCenterLogo ("https://cdn.branch.io/branch-assets/1598575682753-og_image.png" );
42+
43+ BranchUniversalObject buo = new BranchUniversalObject ()
44+ .setCanonicalIdentifier ("test/123" )
45+ .setTitle ("My Test Title" )
46+ .setContentDescription ("My Test Description" )
47+ .setContentImageUrl ("https://lorempixel.com/400/400" );
48+
49+ LinkProperties lp = new LinkProperties ()
50+ .setChannel ("facebook" )
51+ .setFeature ("sharing" )
52+ .setCampaign ("test 123 launch" )
53+ .setStage ("test" );
54+
55+ final CountDownLatch lock = new CountDownLatch (1 );
56+
57+ try {
58+ qrCode .getQRCodeAsData (getTestContext (), buo , lp , new BranchQRCode .BranchQRCodeDataHandler () {
59+ @ Override
60+ public void onSuccess (byte [] qrCodeData ) {
61+ Assert .assertNotNull (qrCodeData );
62+ lock .countDown ();
63+ }
64+
65+ @ Override
66+ public void onFailure (Exception e ) {
67+ e .printStackTrace ();
68+ Assert .fail ();
69+ }
70+ });
71+ } catch (IOException e ) {
72+ e .printStackTrace ();
73+ Assert .fail ();
74+ }
75+ }
76+ });
77+ }
78+
79+ @ Test
80+ public void testQRCodeWithNoSettings () {
81+ initSessionResumeActivity (null , new Runnable () {
82+ @ Override
83+ public void run () {
84+ BranchQRCode qrCode = new BranchQRCode ();
85+
86+ BranchUniversalObject buo = new BranchUniversalObject ()
87+ .setCanonicalIdentifier ("test/123" )
88+ .setTitle ("My Test Title" )
89+ .setContentDescription ("My Test Description" )
90+ .setContentImageUrl ("https://lorempixel.com/400/400" );
91+
92+ LinkProperties lp = new LinkProperties ()
93+ .setChannel ("facebook" )
94+ .setFeature ("sharing" )
95+ .setCampaign ("test 123 launch" )
96+ .setStage ("test" );
97+
98+ final CountDownLatch lock = new CountDownLatch (1 );
99+
100+ try {
101+ qrCode .getQRCodeAsData (getTestContext (), buo , lp , new BranchQRCode .BranchQRCodeDataHandler () {
102+ @ Override
103+ public void onSuccess (byte [] qrCodeData ) {
104+ Assert .assertNotNull (qrCodeData );
105+ lock .countDown ();
106+ }
107+
108+ @ Override
109+ public void onFailure (Exception e ) {
110+ e .printStackTrace ();
111+ Assert .fail ();
112+ }
113+ });
114+ } catch (IOException e ) {
115+ e .printStackTrace ();
116+ Assert .fail ();
117+ }
118+ }
119+ });
120+ }
121+
122+ @ Test
123+ public void testQRCodeAsImage () {
124+ initSessionResumeActivity (null , new Runnable () {
125+ @ Override
126+ public void run () {
127+ BranchQRCode qrCode = new BranchQRCode ();
128+ BranchUniversalObject buo = new BranchUniversalObject ();
129+ LinkProperties lp = new LinkProperties ();
130+
131+ final CountDownLatch lock = new CountDownLatch (1 );
132+
133+ try {
134+ qrCode .getQRCodeAsImage (branch .getCurrentActivity (), buo , lp , new BranchQRCode .BranchQRCodeImageHandler () {
135+ @ Override
136+ public void onSuccess (Bitmap qrCodeImage ) {
137+ Assert .assertNotNull (qrCodeImage );
138+ lock .countDown ();
139+ }
140+
141+ @ Override
142+ public void onFailure (Exception e ) {
143+ e .printStackTrace ();
144+ Assert .fail ();
145+ }
146+ });
147+ } catch (IOException e ) {
148+ e .printStackTrace ();
149+ Assert .fail ();
150+ }
151+ }
152+ });
153+ }
154+
155+ @ Test
156+ public void testQRCodeCache () {
157+ initSessionResumeActivity (null , new Runnable () {
158+ @ Override
159+ public void run () {
160+ final BranchQRCode qrCode = new BranchQRCode ();
161+ BranchUniversalObject buo = new BranchUniversalObject ();
162+ LinkProperties lp = new LinkProperties ();
163+
164+ final CountDownLatch lock = new CountDownLatch (1 );
165+ try {
166+
167+ qrCode .getQRCodeAsData (getTestContext (), buo , lp , new BranchQRCode .BranchQRCodeDataHandler () {
168+ @ Override
169+ public void onSuccess (byte [] qrCodeData ) {
170+ try {
171+ JSONObject expectedCachedParams = new JSONObject ("{\" feature\" :\" Share\" ,\" stage\" :\" \" ,\" data\" :{\" $publicly_indexable\" :true,\" $locally_indexable\" :true},\" channel\" :\" \" ,\" qr_code_settings\" :{\" image_format\" :\" PNG\" },\" campaign\" :\" \" ,\" branch_key\" :\" key_live_testing_only\" ,\" tags\" :[]}" );
172+ byte [] cachedQRCodeData = BranchQRCodeCache .getInstance ().checkQRCodeCache (expectedCachedParams );
173+
174+ Assert .assertEquals (qrCodeData , cachedQRCodeData );
175+ lock .countDown ();
176+ } catch (JSONException e ) {
177+ e .printStackTrace ();
178+ }
179+ }
180+
181+ @ Override
182+ public void onFailure (Exception e ) {
183+ e .printStackTrace ();
184+ Assert .fail ();
185+ }
186+ });
187+ } catch (IOException e ) {
188+ e .printStackTrace ();
189+ Assert .fail ();
190+ }
191+ }
192+ });
193+
194+
195+ }
196+
197+
198+ }
0 commit comments