Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Migrate tests
  • Loading branch information
GaryQian committed Jul 7, 2022
commit 5133ea99c89b442c06cae475d33e7160ba84bad7
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ private void isReady(MethodChannel.Result result) {
result.success(billingClient.isReady());
}

// TODO(garyq): Migrate to new subscriptions API: https://developer.android.com/google/play/billing/migrate-gpblv5
private void querySkuDetailsAsync(
final String skuType, final List<String> skusList, final MethodChannel.Result result) {
if (billingClientError(result)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import static io.flutter.plugins.inapppurchase.InAppPurchasePlugin.MethodNames.ON_DISCONNECT;
import static io.flutter.plugins.inapppurchase.InAppPurchasePlugin.MethodNames.ON_PURCHASES_UPDATED;
import static io.flutter.plugins.inapppurchase.InAppPurchasePlugin.MethodNames.QUERY_PURCHASES;
import static io.flutter.plugins.inapppurchase.InAppPurchasePlugin.MethodNames.QUERY_PURCHASES_ASYNC;
import static io.flutter.plugins.inapppurchase.InAppPurchasePlugin.MethodNames.QUERY_PURCHASE_HISTORY_ASYNC;
import static io.flutter.plugins.inapppurchase.InAppPurchasePlugin.MethodNames.QUERY_SKU_DETAILS;
import static io.flutter.plugins.inapppurchase.InAppPurchasePlugin.MethodNames.START_CONNECTION;
import static io.flutter.plugins.inapppurchase.Translator.fromBillingResult;
import static io.flutter.plugins.inapppurchase.Translator.fromPurchaseHistoryRecordList;
import static io.flutter.plugins.inapppurchase.Translator.fromPurchasesList;
import static io.flutter.plugins.inapppurchase.Translator.fromPurchasesResult;
import static io.flutter.plugins.inapppurchase.Translator.fromSkuDetailsList;
import static java.util.Arrays.asList;
import static java.util.Collections.list;
Expand Down Expand Up @@ -57,7 +57,6 @@
import com.android.billingclient.api.PriceChangeConfirmationListener;
import com.android.billingclient.api.PriceChangeFlowParams;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.Purchase.PurchasesResult;
import com.android.billingclient.api.PurchaseHistoryRecord;
import com.android.billingclient.api.PurchaseHistoryResponseListener;
import com.android.billingclient.api.SkuDetails;
Expand Down Expand Up @@ -295,7 +294,6 @@ public void launchBillingFlow_null_AccountId_do_not_crash() {
ArgumentCaptor.forClass(BillingFlowParams.class);
verify(mockBillingClient).launchBillingFlow(any(), billingFlowParamsCaptor.capture());
BillingFlowParams params = billingFlowParamsCaptor.getValue();
assertEquals(params.getSku(), skuId);

// Verify we pass the response code to result
verify(result, never()).error(any(), any(), any());
Expand Down Expand Up @@ -328,8 +326,6 @@ public void launchBillingFlow_ok_null_OldSku() {
ArgumentCaptor.forClass(BillingFlowParams.class);
verify(mockBillingClient).launchBillingFlow(any(), billingFlowParamsCaptor.capture());
BillingFlowParams params = billingFlowParamsCaptor.getValue();
assertEquals(params.getSku(), skuId);
assertNull(params.getOldSku());
// Verify we pass the response code to result
verify(result, never()).error(any(), any(), any());
verify(result, times(1)).success(fromBillingResult(billingResult));
Expand Down Expand Up @@ -381,8 +377,6 @@ public void launchBillingFlow_ok_oldSku() {
ArgumentCaptor.forClass(BillingFlowParams.class);
verify(mockBillingClient).launchBillingFlow(any(), billingFlowParamsCaptor.capture());
BillingFlowParams params = billingFlowParamsCaptor.getValue();
assertEquals(params.getSku(), skuId);
assertEquals(params.getOldSku(), oldSkuId);

// Verify we pass the response code to result
verify(result, never()).error(any(), any(), any());
Expand Down Expand Up @@ -414,7 +408,6 @@ public void launchBillingFlow_ok_AccountId() {
ArgumentCaptor.forClass(BillingFlowParams.class);
verify(mockBillingClient).launchBillingFlow(any(), billingFlowParamsCaptor.capture());
BillingFlowParams params = billingFlowParamsCaptor.getValue();
assertEquals(params.getSku(), skuId);

// Verify we pass the response code to result
verify(result, never()).error(any(), any(), any());
Expand Down Expand Up @@ -452,10 +445,6 @@ public void launchBillingFlow_ok_Proration() {
ArgumentCaptor.forClass(BillingFlowParams.class);
verify(mockBillingClient).launchBillingFlow(any(), billingFlowParamsCaptor.capture());
BillingFlowParams params = billingFlowParamsCaptor.getValue();
assertEquals(params.getSku(), skuId);
assertEquals(params.getOldSku(), oldSkuId);
assertEquals(params.getOldSkuPurchaseToken(), purchaseToken);
assertEquals(params.getReplaceSkusProrationMode(), prorationMode);

// Verify we pass the response code to result
verify(result, never()).error(any(), any(), any());
Expand Down Expand Up @@ -527,7 +516,6 @@ public void launchBillingFlow_ok_Full() {
ArgumentCaptor.forClass(BillingFlowParams.class);
verify(mockBillingClient).launchBillingFlow(any(), billingFlowParamsCaptor.capture());
BillingFlowParams params = billingFlowParamsCaptor.getValue();
assertEquals(params.getSku(), skuId);

// Verify we pass the response code to result
verify(result, never()).error(any(), any(), any());
Expand Down Expand Up @@ -592,47 +580,6 @@ public void launchBillingFlow_oldSkuNotFound() {
verify(result, never()).success(any());
}

@Test
public void queryPurchasesAsync() {
establishConnectedBillingClient(null, null);
PurchasesResult purchasesResult = mock(PurchasesResult.class);
Purchase purchase = buildPurchase("foo");
when(purchasesResult.getPurchasesList()).thenReturn(asList(purchase));
BillingResult billingResult =
BillingResult.newBuilder()
.setResponseCode(100)
.setDebugMessage("dummy debug message")
.build();
when(purchasesResult.getBillingResult()).thenReturn(billingResult);
when(mockBillingClient.queryPurchasesAsync(
any(QueryPurchasesParams.class),
any(PurchaseHistoryResponseListener.class)
)).thenAnswer(
new Answer<Object>() {
Object answer(InvocationOnMock invocation) {
BillingResult.Builder builder = BillingResult.newBuilder();
builder.setDebugMessage("debug message");
builder.setResponseCode(10);
((Callback<Response>) invocation.getArguments()[1]).onPurchaseHistoryResponse(
builder.build(),
List.of(PurchaseHistoryRecord("{}", "signature"))
);
return null;
}
}
);

HashMap<String, Object> arguments = new HashMap<>();
arguments.put("skuType", SkuType.INAPP);
methodChannelHandler.onMethodCall(new MethodCall(QUERY_PURCHASES_ASYNC, arguments), result);

// Verify we pass the response to result
ArgumentCaptor<HashMap<String, Object>> resultCaptor = ArgumentCaptor.forClass(HashMap.class);
verify(result, never()).error(any(), any(), any());
verify(result, times(1)).success(resultCaptor.capture());
assertEquals(fromPurchasesResult(purchasesResult), resultCaptor.getValue());
}

@Test
public void queryPurchases_clientDisconnected() {
// Prepare the launch call after disconnecting the client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingResult;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.Purchase.PurchasesResult;
import com.android.billingclient.api.PurchaseHistoryRecord;
import com.android.billingclient.api.SkuDetails;
import java.util.Arrays;
Expand Down Expand Up @@ -138,37 +137,6 @@ public void fromPurchasesList_null() {
assertEquals(Collections.emptyList(), Translator.fromPurchasesList(null));
}

@Test
public void fromPurchasesResult() throws JSONException {
PurchasesResult result = mock(PurchasesResult.class);
final String purchase2Json =
"{\"orderId\":\"foo2\",\"packageName\":\"bar\",\"productId\":\"consumable\",\"purchaseTime\":11111111,\"purchaseState\":0,\"purchaseToken\":\"baz\",\"developerPayload\":\"dummy payload\",\"isAcknowledged\":\"true\"}";
final String signature = "signature";
final List<Purchase> expectedPurchases =
Arrays.asList(
new Purchase(PURCHASE_EXAMPLE_JSON, signature), new Purchase(purchase2Json, signature));
when(result.getPurchasesList()).thenReturn(expectedPurchases);
when(result.getResponseCode()).thenReturn(BillingClient.BillingResponseCode.OK);
BillingResult newBillingResult =
BillingResult.newBuilder()
.setDebugMessage("dummy debug message")
.setResponseCode(BillingClient.BillingResponseCode.OK)
.build();
when(result.getBillingResult()).thenReturn(newBillingResult);
final HashMap<String, Object> serialized = Translator.fromPurchasesResult(result);

assertEquals(BillingClient.BillingResponseCode.OK, serialized.get("responseCode"));
List<Map<String, Object>> serializedPurchases =
(List<Map<String, Object>>) serialized.get("purchasesList");
assertEquals(expectedPurchases.size(), serializedPurchases.size());
assertSerialized(expectedPurchases.get(0), serializedPurchases.get(0));
assertSerialized(expectedPurchases.get(1), serializedPurchases.get(1));

Map<String, Object> billingResultMap = (Map<String, Object>) serialized.get("billingResult");
assertEquals(billingResultMap.get("responseCode"), newBillingResult.getResponseCode());
assertEquals(billingResultMap.get("debugMessage"), newBillingResult.getDebugMessage());
}

@Test
public void fromBillingResult() throws JSONException {
BillingResult newBillingResult =
Expand Down Expand Up @@ -232,7 +200,8 @@ private void assertSerialized(Purchase expected, Map<String, Object> serialized)
assertEquals(expected.getPurchaseToken(), serialized.get("purchaseToken"));
assertEquals(expected.getSignature(), serialized.get("signature"));
assertEquals(expected.getOriginalJson(), serialized.get("originalJson"));
assertEquals(expected.getSku(), serialized.get("sku"));
assertEquals(expected.getSkus().get(0), serialized.get("sku"));
assertEquals(expected.getSkus(), serialized.get("skus"));
assertEquals(expected.getDeveloperPayload(), serialized.get("developerPayload"));
assertEquals(expected.isAcknowledged(), serialized.get("isAcknowledged"));
assertEquals(expected.getPurchaseState(), serialized.get("purchaseState"));
Expand All @@ -251,7 +220,8 @@ private void assertSerialized(PurchaseHistoryRecord expected, Map<String, Object
assertEquals(expected.getPurchaseToken(), serialized.get("purchaseToken"));
assertEquals(expected.getSignature(), serialized.get("signature"));
assertEquals(expected.getOriginalJson(), serialized.get("originalJson"));
assertEquals(expected.getSku(), serialized.get("sku"));
assertEquals(expected.getSkus().get(0), serialized.get("sku"));
assertEquals(expected.getSkus(), serialized.get("skus"));
assertEquals(expected.getDeveloperPayload(), serialized.get("developerPayload"));
}
}
Expand Down