Skip to content

Commit 19568b2

Browse files
test: add DefaultSupportRepository tests
1 parent c699913 commit 19568b2

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package com.d4rk.androidtutorials.java.data.repository;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertSame;
6+
import static org.mockito.ArgumentMatchers.any;
7+
import static org.mockito.Mockito.eq;
8+
import static org.mockito.Mockito.mock;
9+
import static org.mockito.Mockito.times;
10+
import static org.mockito.Mockito.verify;
11+
import static org.mockito.Mockito.when;
12+
13+
import android.app.Activity;
14+
import android.content.Context;
15+
16+
import com.android.billingclient.api.BillingClient;
17+
import com.android.billingclient.api.BillingClientStateListener;
18+
import com.android.billingclient.api.BillingFlowParams;
19+
import com.android.billingclient.api.BillingResult;
20+
import com.android.billingclient.api.ProductDetails;
21+
import com.android.billingclient.api.ProductDetailsResponseListener;
22+
import com.android.billingclient.api.QueryProductDetailsParams;
23+
import com.android.billingclient.api.QueryProductDetailsResult;
24+
import com.google.android.gms.ads.AdRequest;
25+
import com.d4rk.androidtutorials.java.ads.AdUtils;
26+
27+
import org.junit.Test;
28+
import org.mockito.ArgumentCaptor;
29+
import org.mockito.MockedStatic;
30+
31+
import java.lang.reflect.Field;
32+
import java.util.Collections;
33+
import java.util.List;
34+
import java.util.Map;
35+
36+
public class DefaultSupportRepositoryTest {
37+
38+
@Test
39+
public void initBillingClient_connectsAndTriggersOnConnected() {
40+
Context context = mock(Context.class);
41+
when(context.getApplicationContext()).thenReturn(context);
42+
DefaultSupportRepository repository = new DefaultSupportRepository(context);
43+
44+
Runnable onConnected = mock(Runnable.class);
45+
46+
BillingClient billingClient = mock(BillingClient.class);
47+
BillingClient.Builder builder = mock(BillingClient.Builder.class);
48+
ArgumentCaptor<BillingClientStateListener> listenerCaptor = ArgumentCaptor.forClass(BillingClientStateListener.class);
49+
50+
try (MockedStatic<BillingClient> billingStatic = org.mockito.Mockito.mockStatic(BillingClient.class)) {
51+
billingStatic.when(() -> BillingClient.newBuilder(context)).thenReturn(builder);
52+
when(builder.setListener(any())).thenReturn(builder);
53+
when(builder.enablePendingPurchases(any())).thenReturn(builder);
54+
when(builder.enableAutoServiceReconnection()).thenReturn(builder);
55+
when(builder.build()).thenReturn(billingClient);
56+
57+
repository.initBillingClient(onConnected);
58+
59+
verify(billingClient).startConnection(listenerCaptor.capture());
60+
61+
BillingClientStateListener stateListener = listenerCaptor.getValue();
62+
BillingResult result = BillingResult.newBuilder()
63+
.setResponseCode(BillingClient.BillingResponseCode.OK)
64+
.build();
65+
stateListener.onBillingSetupFinished(result);
66+
67+
verify(onConnected).run();
68+
}
69+
}
70+
71+
@Test
72+
public void queryProductDetails_populatesMapAndInvokesListener() throws Exception {
73+
Context context = mock(Context.class);
74+
when(context.getApplicationContext()).thenReturn(context);
75+
DefaultSupportRepository repository = new DefaultSupportRepository(context);
76+
77+
BillingClient billingClient = mock(BillingClient.class);
78+
when(billingClient.isReady()).thenReturn(true);
79+
80+
Field billingField = DefaultSupportRepository.class.getDeclaredField("billingClient");
81+
billingField.setAccessible(true);
82+
billingField.set(repository, billingClient);
83+
84+
SupportRepository.OnProductDetailsListener listener = mock(SupportRepository.OnProductDetailsListener.class);
85+
ProductDetails details = mock(ProductDetails.class);
86+
when(details.getProductId()).thenReturn("id1");
87+
88+
QueryProductDetailsResult queryResult = mock(QueryProductDetailsResult.class);
89+
when(queryResult.getProductDetailsList()).thenReturn(Collections.singletonList(details));
90+
91+
org.mockito.Mockito.doAnswer(invocation -> {
92+
ProductDetailsResponseListener responseListener = invocation.getArgument(1);
93+
BillingResult billingResult = BillingResult.newBuilder()
94+
.setResponseCode(BillingClient.BillingResponseCode.OK)
95+
.build();
96+
responseListener.onProductDetailsResponse(billingResult, queryResult);
97+
return null;
98+
}).when(billingClient).queryProductDetailsAsync(any(QueryProductDetailsParams.class), any(ProductDetailsResponseListener.class));
99+
100+
repository.queryProductDetails(Collections.singletonList("id1"), listener);
101+
102+
ArgumentCaptor<List<ProductDetails>> listCaptor = ArgumentCaptor.forClass(List.class);
103+
verify(listener).onProductDetailsRetrieved(listCaptor.capture());
104+
assertEquals(1, listCaptor.getValue().size());
105+
assertSame(details, listCaptor.getValue().get(0));
106+
107+
Field mapField = DefaultSupportRepository.class.getDeclaredField("productDetailsMap");
108+
mapField.setAccessible(true);
109+
Map<String, ProductDetails> map = (Map<String, ProductDetails>) mapField.get(repository);
110+
assertSame(details, map.get("id1"));
111+
}
112+
113+
@Test
114+
public void initiatePurchase_buildsFlowParamsAndCallsLaunchBillingFlow() throws Exception {
115+
Context context = mock(Context.class);
116+
when(context.getApplicationContext()).thenReturn(context);
117+
DefaultSupportRepository repository = new DefaultSupportRepository(context);
118+
119+
BillingClient billingClient = mock(BillingClient.class);
120+
Field billingField = DefaultSupportRepository.class.getDeclaredField("billingClient");
121+
billingField.setAccessible(true);
122+
billingField.set(repository, billingClient);
123+
124+
ProductDetails details = mock(ProductDetails.class);
125+
when(details.getProductId()).thenReturn("id1");
126+
ProductDetails.OneTimePurchaseOfferDetails offerDetails = mock(ProductDetails.OneTimePurchaseOfferDetails.class);
127+
when(offerDetails.getOfferToken()).thenReturn("token");
128+
when(details.getOneTimePurchaseOfferDetails()).thenReturn(offerDetails);
129+
130+
Field mapField = DefaultSupportRepository.class.getDeclaredField("productDetailsMap");
131+
mapField.setAccessible(true);
132+
Map<String, ProductDetails> map = (Map<String, ProductDetails>) mapField.get(repository);
133+
map.put("id1", details);
134+
135+
SupportRepository.BillingFlowLauncher launcher = repository.initiatePurchase("id1");
136+
Activity activity = mock(Activity.class);
137+
assertNotNull(launcher);
138+
launcher.launch(activity);
139+
140+
ArgumentCaptor<BillingFlowParams> paramsCaptor = ArgumentCaptor.forClass(BillingFlowParams.class);
141+
verify(billingClient).launchBillingFlow(eq(activity), paramsCaptor.capture());
142+
BillingFlowParams params = paramsCaptor.getValue();
143+
List<BillingFlowParams.ProductDetailsParams> list = params.getProductDetailsParamsList();
144+
assertEquals(1, list.size());
145+
BillingFlowParams.ProductDetailsParams pdp = list.get(0);
146+
assertSame(details, pdp.getProductDetails());
147+
assertEquals("token", pdp.getOfferToken());
148+
}
149+
150+
@Test
151+
public void initMobileAds_returnsAdRequestAndCallsInitialize() {
152+
Context context = mock(Context.class);
153+
when(context.getApplicationContext()).thenReturn(context);
154+
DefaultSupportRepository repository = new DefaultSupportRepository(context);
155+
156+
try (MockedStatic<AdUtils> adUtils = org.mockito.Mockito.mockStatic(AdUtils.class)) {
157+
AdRequest request = repository.initMobileAds();
158+
assertNotNull(request);
159+
adUtils.verify(() -> AdUtils.initialize(context), times(1));
160+
}
161+
}
162+
}

0 commit comments

Comments
 (0)