-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[In_App_Purchase] Improve testability #2016
Changes from 9 commits
52cd0a0
da055d5
bcf733a
ed97762
469a7ac
cdaea15
65a6a1a
c673a21
ab48f81
daa9a07
e026980
739e199
a07f6e2
6eabb6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package io.flutter.plugins.inapppurchase; | ||
|
|
||
| import android.content.Context; | ||
| import com.android.billingclient.api.BillingClient; | ||
| import io.flutter.plugin.common.MethodChannel; | ||
|
|
||
| public class BillingClientFactory { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this works and it's definitely an improvement over the current code. But there's some subjective style nits I have with this approach.
It adds a little extra overhead, but I'd prefer that this were an interface that the test had it's own implementation instead. interface BillingClientFactory {
BillingClient createBillingClient(Context context, MethodChannel channel);
}
// Instantiated in InAppPurchasePlugin#registerWith
class BillingClientFactoryImpl implements BillingClientFactory {
public BillingClient createBillingClient(Context context, MethodChannel channel) {
return BillingClient.newBuilder(context)
.setListener(new PluginPurchaseListener(channel))
.build();
}
}And then the tests could have a real implementation of this that returned the mock: @Before
public void setUp() {
MockitoAnnotations.initMocks(this);
BillingClientFactory factory = (Context context, MethodChannel channel) -> mockBillingClient;
plugin = new InAppPurchasePlugin(factory, registrar, mockMethodChannel);
}What do you think?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think all your suggestions are fair. |
||
|
|
||
| public BillingClient createBillingClient(Context context, MethodChannel channel) { | ||
| return BillingClient.newBuilder(context) | ||
| .setListener(new PluginPurchaseListener(channel)) | ||
| .build(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package io.flutter.plugins.inapppurchase; | ||
|
|
||
| import static io.flutter.plugins.inapppurchase.Translator.fromPurchasesList; | ||
|
|
||
| import androidx.annotation.Nullable; | ||
| import com.android.billingclient.api.Purchase; | ||
| import com.android.billingclient.api.PurchasesUpdatedListener; | ||
| import io.flutter.plugin.common.MethodChannel; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| class PluginPurchaseListener implements PurchasesUpdatedListener { | ||
| private final MethodChannel channel; | ||
|
|
||
| PluginPurchaseListener(MethodChannel channel) { | ||
| this.channel = channel; | ||
| } | ||
|
|
||
| @Override | ||
| public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) { | ||
| final Map<String, Object> callbackArgs = new HashMap<>(); | ||
| callbackArgs.put("responseCode", responseCode); | ||
| callbackArgs.put("purchasesList", fromPurchasesList(purchases)); | ||
| channel.invokeMethod(InAppPurchasePlugin.MethodNames.ON_PURCHASES_UPDATED, callbackArgs); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.