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
update tests and changelog.
  • Loading branch information
juliocbcotta committed Aug 27, 2019
commit 65a6a1a5f730a967fe83ec57290b9ec8859f18f9
2 changes: 1 addition & 1 deletion packages/in_app_purchase/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 0.2.1+1

* Android: Do not register if Activity is null.
* Android: Require Activity only to use launchBillingFlow method.

## 0.2.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static void registerWith(Registrar registrar) {
}

public InAppPurchasePlugin(Registrar registrar, MethodChannel channel) {
this.applicationContext = registrar.context().getApplicationContext();
this.applicationContext = registrar.context();
this.registrar = registrar;
this.channel = channel;
}
Expand Down Expand Up @@ -113,11 +113,12 @@ public void onMethodCall(MethodCall call, Result result) {
}

@VisibleForTesting
/*package*/ InAppPurchasePlugin(@Nullable BillingClient billingClient, MethodChannel channel) {
/*package*/ InAppPurchasePlugin(
Registrar registrar, @Nullable BillingClient billingClient, MethodChannel channel) {
this.billingClient = billingClient;
this.channel = channel;
this.applicationContext = null;
this.registrar = null;
this.applicationContext = registrar.context();
this.registrar = registrar;
}

private void startConnection(final int handle, final Result result) {
Expand Down Expand Up @@ -204,9 +205,11 @@ private void launchBillingFlow(String sku, @Nullable String accountId, Result re

if (activity == null) {
result.error(
"UNAVAILABLE",
"Details for sku " + sku + " are not available. This method requires to be run with the app in foreground.",
null);
"ACTIVITY_UNAVAILABLE",
"Details for sku "
+ sku
+ " are not available. This method requires to be run with the app in foreground.",
null);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static org.mockito.Mockito.when;

import android.app.Activity;
import android.content.Context;
import androidx.annotation.Nullable;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClient.BillingResponse;
Expand Down Expand Up @@ -66,25 +67,13 @@ public class InAppPurchasePluginTest {
@Mock PluginRegistry.Registrar registrar;
@Mock Activity activity;
@Mock BinaryMessenger messenger;
@Mock Context context;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
plugin = new InAppPurchasePlugin(mockBillingClient, mockMethodChannel);
}

@Test
public void doNotRegisterWithoutActivityAvailable() {
InAppPurchasePlugin.registerWith(registrar);
verify(registrar, never()).messenger();
}

@Test
public void registerWithoutActivityAvailable() {
when(registrar.messenger()).thenReturn(messenger);
when(registrar.activity()).thenReturn(activity);
InAppPurchasePlugin.registerWith(registrar);
verify(registrar).messenger();
when(registrar.context()).thenReturn(context);
plugin = new InAppPurchasePlugin(registrar, mockBillingClient, mockMethodChannel);
}

@Test
Expand Down Expand Up @@ -239,6 +228,7 @@ public void querySkuDetailsAsync_clientDisconnected() {

@Test
public void launchBillingFlow_ok_nullAccountId() {
when(registrar.activity()).thenReturn(activity);
// Fetch the sku details first and then prepare the launch billing flow call
String skuId = "foo";
queryForSkus(singletonList(skuId));
Expand All @@ -265,8 +255,27 @@ public void launchBillingFlow_ok_nullAccountId() {
verify(result, times(1)).success(responseCode);
}

@Test
public void launchBillingFlow_ok_null_Activity() {
// Fetch the sku details first and then prepare the launch billing flow call
String skuId = "foo";
String accountId = "account";
queryForSkus(singletonList(skuId));
HashMap<String, Object> arguments = new HashMap<>();
arguments.put("sku", skuId);
arguments.put("accountId", accountId);
MethodCall launchCall = new MethodCall(LAUNCH_BILLING_FLOW, arguments);

plugin.onMethodCall(launchCall, result);

// Verify we pass the response code to result
verify(result).error(contains("ACTIVITY_UNAVAILABLE"), contains("foreground"), any());
verify(result, never()).success(any());
}

@Test
public void launchBillingFlow_ok_AccountId() {
when(registrar.activity()).thenReturn(activity);
// Fetch the sku details first and query the method call
String skuId = "foo";
String accountId = "account";
Expand Down