2222import static java .util .stream .Collectors .toList ;
2323import static org .junit .Assert .assertEquals ;
2424import static org .junit .Assert .assertNull ;
25+ import static org .junit .Assert .fail ;
2526import static org .mockito .ArgumentMatchers .any ;
2627import static org .mockito .ArgumentMatchers .contains ;
2728import static org .mockito .ArgumentMatchers .eq ;
6061import java .util .HashMap ;
6162import java .util .List ;
6263import java .util .Map ;
64+ import org .json .JSONException ;
6365import org .junit .Before ;
6466import org .junit .Test ;
6567import org .mockito .ArgumentCaptor ;
@@ -79,7 +81,7 @@ public class MethodCallHandlerTest {
7981
8082 @ Before
8183 public void setUp () {
82- MockitoAnnotations .initMocks (this );
84+ MockitoAnnotations .openMocks (this );
8385 factory =
8486 (@ NonNull Context context ,
8587 @ NonNull MethodChannel channel ,
@@ -261,14 +263,18 @@ public void querySkuDetailsAsync_clientDisconnected() {
261263 verify (result , never ()).success (any ());
262264 }
263265
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.
264269 @ Test
265- public void launchBillingFlow_ok_null_AccountId () {
270+ public void launchBillingFlow_null_AccountId_do_not_crash () {
266271 // Fetch the sku details first and then prepare the launch billing flow call
267272 String skuId = "foo" ;
268273 queryForSkus (singletonList (skuId ));
269274 HashMap <String , Object > arguments = new HashMap <>();
270275 arguments .put ("sku" , skuId );
271276 arguments .put ("accountId" , null );
277+ arguments .put ("obfuscatedProfileId" , null );
272278 MethodCall launchCall = new MethodCall (LAUNCH_BILLING_FLOW , arguments );
273279
274280 // Launch the billing flow
@@ -286,7 +292,6 @@ public void launchBillingFlow_ok_null_AccountId() {
286292 verify (mockBillingClient ).launchBillingFlow (any (), billingFlowParamsCaptor .capture ());
287293 BillingFlowParams params = billingFlowParamsCaptor .getValue ();
288294 assertEquals (params .getSku (), skuId );
289- assertNull (params .getAccountId ());
290295
291296 // Verify we pass the response code to result
292297 verify (result , never ()).error (any (), any (), any ());
@@ -320,7 +325,6 @@ public void launchBillingFlow_ok_null_OldSku() {
320325 verify (mockBillingClient ).launchBillingFlow (any (), billingFlowParamsCaptor .capture ());
321326 BillingFlowParams params = billingFlowParamsCaptor .getValue ();
322327 assertEquals (params .getSku (), skuId );
323- assertEquals (params .getAccountId (), accountId );
324328 assertNull (params .getOldSku ());
325329 // Verify we pass the response code to result
326330 verify (result , never ()).error (any (), any (), any ());
@@ -374,7 +378,6 @@ public void launchBillingFlow_ok_oldSku() {
374378 verify (mockBillingClient ).launchBillingFlow (any (), billingFlowParamsCaptor .capture ());
375379 BillingFlowParams params = billingFlowParamsCaptor .getValue ();
376380 assertEquals (params .getSku (), skuId );
377- assertEquals (params .getAccountId (), accountId );
378381 assertEquals (params .getOldSku (), oldSkuId );
379382
380383 // Verify we pass the response code to result
@@ -408,7 +411,6 @@ public void launchBillingFlow_ok_AccountId() {
408411 verify (mockBillingClient ).launchBillingFlow (any (), billingFlowParamsCaptor .capture ());
409412 BillingFlowParams params = billingFlowParamsCaptor .getValue ();
410413 assertEquals (params .getSku (), skuId );
411- assertEquals (params .getAccountId (), accountId );
412414
413415 // Verify we pass the response code to result
414416 verify (result , never ()).error (any (), any (), any ());
@@ -420,13 +422,15 @@ public void launchBillingFlow_ok_Proration() {
420422 // Fetch the sku details first and query the method call
421423 String skuId = "foo" ;
422424 String oldSkuId = "oldFoo" ;
425+ String purchaseToken = "purchaseTokenFoo" ;
423426 String accountId = "account" ;
424427 int prorationMode = BillingFlowParams .ProrationMode .IMMEDIATE_AND_CHARGE_PRORATED_PRICE ;
425428 queryForSkus (unmodifiableList (asList (skuId , oldSkuId )));
426429 HashMap <String , Object > arguments = new HashMap <>();
427430 arguments .put ("sku" , skuId );
428431 arguments .put ("accountId" , accountId );
429432 arguments .put ("oldSku" , oldSkuId );
433+ arguments .put ("purchaseToken" , purchaseToken );
430434 arguments .put ("prorationMode" , prorationMode );
431435 MethodCall launchCall = new MethodCall (LAUNCH_BILLING_FLOW , arguments );
432436
@@ -445,8 +449,8 @@ public void launchBillingFlow_ok_Proration() {
445449 verify (mockBillingClient ).launchBillingFlow (any (), billingFlowParamsCaptor .capture ());
446450 BillingFlowParams params = billingFlowParamsCaptor .getValue ();
447451 assertEquals (params .getSku (), skuId );
448- assertEquals (params .getAccountId (), accountId );
449452 assertEquals (params .getOldSku (), oldSkuId );
453+ assertEquals (params .getOldSkuPurchaseToken (), purchaseToken );
450454 assertEquals (params .getReplaceSkusProrationMode (), prorationMode );
451455
452456 // Verify we pass the response code to result
@@ -668,11 +672,7 @@ public void consumeAsync() {
668672
669673 methodChannelHandler .onMethodCall (new MethodCall (CONSUME_PURCHASE_ASYNC , arguments ), result );
670674
671- ConsumeParams params =
672- ConsumeParams .newBuilder ()
673- .setDeveloperPayload ("mockPayload" )
674- .setPurchaseToken ("mockToken" )
675- .build ();
675+ ConsumeParams params = ConsumeParams .newBuilder ().setPurchaseToken ("mockToken" ).build ();
676676
677677 // Verify we pass the data to result
678678 verify (mockBillingClient ).consumeAsync (refEq (params ), listenerCaptor .capture ());
@@ -703,10 +703,7 @@ public void acknowledgePurchase() {
703703 methodChannelHandler .onMethodCall (new MethodCall (ACKNOWLEDGE_PURCHASE , arguments ), result );
704704
705705 AcknowledgePurchaseParams params =
706- AcknowledgePurchaseParams .newBuilder ()
707- .setDeveloperPayload ("mockPayload" )
708- .setPurchaseToken ("mockToken" )
709- .build ();
706+ AcknowledgePurchaseParams .newBuilder ().setPurchaseToken ("mockToken" ).build ();
710707
711708 // Verify we pass the data to result
712709 verify (mockBillingClient ).acknowledgePurchase (refEq (params ), listenerCaptor .capture ());
@@ -774,6 +771,7 @@ private void queryForSkus(List<String> skusList) {
774771 verify (mockBillingClient ).querySkuDetailsAsync (any (), listenerCaptor .capture ());
775772 List <SkuDetails > skuDetailsResponse =
776773 skusList .stream ().map (this ::buildSkuDetails ).collect (toList ());
774+
777775 BillingResult billingResult =
778776 BillingResult .newBuilder ()
779777 .setResponseCode (100 )
@@ -783,8 +781,16 @@ private void queryForSkus(List<String> skusList) {
783781 }
784782
785783 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+ }
788794 return details ;
789795 }
790796
0 commit comments