22
22
import static java .util .stream .Collectors .toList ;
23
23
import static org .junit .Assert .assertEquals ;
24
24
import static org .junit .Assert .assertNull ;
25
+ import static org .junit .Assert .fail ;
25
26
import static org .mockito .ArgumentMatchers .any ;
26
27
import static org .mockito .ArgumentMatchers .contains ;
27
28
import static org .mockito .ArgumentMatchers .eq ;
60
61
import java .util .HashMap ;
61
62
import java .util .List ;
62
63
import java .util .Map ;
64
+ import org .json .JSONException ;
63
65
import org .junit .Before ;
64
66
import org .junit .Test ;
65
67
import org .mockito .ArgumentCaptor ;
@@ -79,7 +81,7 @@ public class MethodCallHandlerTest {
79
81
80
82
@ Before
81
83
public void setUp () {
82
- MockitoAnnotations .initMocks (this );
84
+ MockitoAnnotations .openMocks (this );
83
85
factory =
84
86
(@ NonNull Context context ,
85
87
@ NonNull MethodChannel channel ,
@@ -261,14 +263,18 @@ public void querySkuDetailsAsync_clientDisconnected() {
261
263
verify (result , never ()).success (any ());
262
264
}
263
265
266
+ // Test launchBillingFlow not crash if `accountId` is `null`
267
+ // Ideally, we should check if the `accountId` is null in the parameter; however,
268
+ // since PBL 3.0, the `accountId` variable is not public.
264
269
@ Test
265
- public void launchBillingFlow_ok_null_AccountId () {
270
+ public void launchBillingFlow_null_AccountId_do_not_crash () {
266
271
// Fetch the sku details first and then prepare the launch billing flow call
267
272
String skuId = "foo" ;
268
273
queryForSkus (singletonList (skuId ));
269
274
HashMap <String , Object > arguments = new HashMap <>();
270
275
arguments .put ("sku" , skuId );
271
276
arguments .put ("accountId" , null );
277
+ arguments .put ("obfuscatedProfileId" , null );
272
278
MethodCall launchCall = new MethodCall (LAUNCH_BILLING_FLOW , arguments );
273
279
274
280
// Launch the billing flow
@@ -286,7 +292,6 @@ public void launchBillingFlow_ok_null_AccountId() {
286
292
verify (mockBillingClient ).launchBillingFlow (any (), billingFlowParamsCaptor .capture ());
287
293
BillingFlowParams params = billingFlowParamsCaptor .getValue ();
288
294
assertEquals (params .getSku (), skuId );
289
- assertNull (params .getAccountId ());
290
295
291
296
// Verify we pass the response code to result
292
297
verify (result , never ()).error (any (), any (), any ());
@@ -320,7 +325,6 @@ public void launchBillingFlow_ok_null_OldSku() {
320
325
verify (mockBillingClient ).launchBillingFlow (any (), billingFlowParamsCaptor .capture ());
321
326
BillingFlowParams params = billingFlowParamsCaptor .getValue ();
322
327
assertEquals (params .getSku (), skuId );
323
- assertEquals (params .getAccountId (), accountId );
324
328
assertNull (params .getOldSku ());
325
329
// Verify we pass the response code to result
326
330
verify (result , never ()).error (any (), any (), any ());
@@ -374,7 +378,6 @@ public void launchBillingFlow_ok_oldSku() {
374
378
verify (mockBillingClient ).launchBillingFlow (any (), billingFlowParamsCaptor .capture ());
375
379
BillingFlowParams params = billingFlowParamsCaptor .getValue ();
376
380
assertEquals (params .getSku (), skuId );
377
- assertEquals (params .getAccountId (), accountId );
378
381
assertEquals (params .getOldSku (), oldSkuId );
379
382
380
383
// Verify we pass the response code to result
@@ -408,7 +411,6 @@ public void launchBillingFlow_ok_AccountId() {
408
411
verify (mockBillingClient ).launchBillingFlow (any (), billingFlowParamsCaptor .capture ());
409
412
BillingFlowParams params = billingFlowParamsCaptor .getValue ();
410
413
assertEquals (params .getSku (), skuId );
411
- assertEquals (params .getAccountId (), accountId );
412
414
413
415
// Verify we pass the response code to result
414
416
verify (result , never ()).error (any (), any (), any ());
@@ -420,13 +422,15 @@ public void launchBillingFlow_ok_Proration() {
420
422
// Fetch the sku details first and query the method call
421
423
String skuId = "foo" ;
422
424
String oldSkuId = "oldFoo" ;
425
+ String purchaseToken = "purchaseTokenFoo" ;
423
426
String accountId = "account" ;
424
427
int prorationMode = BillingFlowParams .ProrationMode .IMMEDIATE_AND_CHARGE_PRORATED_PRICE ;
425
428
queryForSkus (unmodifiableList (asList (skuId , oldSkuId )));
426
429
HashMap <String , Object > arguments = new HashMap <>();
427
430
arguments .put ("sku" , skuId );
428
431
arguments .put ("accountId" , accountId );
429
432
arguments .put ("oldSku" , oldSkuId );
433
+ arguments .put ("purchaseToken" , purchaseToken );
430
434
arguments .put ("prorationMode" , prorationMode );
431
435
MethodCall launchCall = new MethodCall (LAUNCH_BILLING_FLOW , arguments );
432
436
@@ -445,8 +449,8 @@ public void launchBillingFlow_ok_Proration() {
445
449
verify (mockBillingClient ).launchBillingFlow (any (), billingFlowParamsCaptor .capture ());
446
450
BillingFlowParams params = billingFlowParamsCaptor .getValue ();
447
451
assertEquals (params .getSku (), skuId );
448
- assertEquals (params .getAccountId (), accountId );
449
452
assertEquals (params .getOldSku (), oldSkuId );
453
+ assertEquals (params .getOldSkuPurchaseToken (), purchaseToken );
450
454
assertEquals (params .getReplaceSkusProrationMode (), prorationMode );
451
455
452
456
// Verify we pass the response code to result
@@ -668,11 +672,7 @@ public void consumeAsync() {
668
672
669
673
methodChannelHandler .onMethodCall (new MethodCall (CONSUME_PURCHASE_ASYNC , arguments ), result );
670
674
671
- ConsumeParams params =
672
- ConsumeParams .newBuilder ()
673
- .setDeveloperPayload ("mockPayload" )
674
- .setPurchaseToken ("mockToken" )
675
- .build ();
675
+ ConsumeParams params = ConsumeParams .newBuilder ().setPurchaseToken ("mockToken" ).build ();
676
676
677
677
// Verify we pass the data to result
678
678
verify (mockBillingClient ).consumeAsync (refEq (params ), listenerCaptor .capture ());
@@ -703,10 +703,7 @@ public void acknowledgePurchase() {
703
703
methodChannelHandler .onMethodCall (new MethodCall (ACKNOWLEDGE_PURCHASE , arguments ), result );
704
704
705
705
AcknowledgePurchaseParams params =
706
- AcknowledgePurchaseParams .newBuilder ()
707
- .setDeveloperPayload ("mockPayload" )
708
- .setPurchaseToken ("mockToken" )
709
- .build ();
706
+ AcknowledgePurchaseParams .newBuilder ().setPurchaseToken ("mockToken" ).build ();
710
707
711
708
// Verify we pass the data to result
712
709
verify (mockBillingClient ).acknowledgePurchase (refEq (params ), listenerCaptor .capture ());
@@ -774,6 +771,7 @@ private void queryForSkus(List<String> skusList) {
774
771
verify (mockBillingClient ).querySkuDetailsAsync (any (), listenerCaptor .capture ());
775
772
List <SkuDetails > skuDetailsResponse =
776
773
skusList .stream ().map (this ::buildSkuDetails ).collect (toList ());
774
+
777
775
BillingResult billingResult =
778
776
BillingResult .newBuilder ()
779
777
.setResponseCode (100 )
@@ -783,8 +781,16 @@ private void queryForSkus(List<String> skusList) {
783
781
}
784
782
785
783
private SkuDetails buildSkuDetails (String id ) {
786
- SkuDetails details = mock (SkuDetails .class );
787
- when (details .getSku ()).thenReturn (id );
784
+ String json =
785
+ String .format (
786
+ "{\" packageName\" : \" dummyPackageName\" ,\" productId\" :\" %s\" ,\" type\" :\" inapp\" ,\" price\" :\" $0.99\" ,\" price_amount_micros\" :990000,\" price_currency_code\" :\" USD\" ,\" title\" :\" Example title\" ,\" description\" :\" Example description.\" ,\" original_price\" :\" $0.99\" ,\" original_price_micros\" :990000}" ,
787
+ id );
788
+ SkuDetails details = null ;
789
+ try {
790
+ details = new SkuDetails (json );
791
+ } catch (JSONException e ) {
792
+ fail ("buildSkuDetails failed with JSONException " + e .toString ());
793
+ }
788
794
return details ;
789
795
}
790
796
0 commit comments