|
| 1 | +package io.flutter.plugins.inapppurchase; |
| 2 | + |
| 3 | +import static org.mockito.ArgumentMatchers.any; |
| 4 | +import static org.mockito.Mockito.doNothing; |
| 5 | +import static org.mockito.Mockito.mock; |
| 6 | +import static org.mockito.Mockito.never; |
| 7 | +import static org.mockito.Mockito.times; |
| 8 | +import static org.mockito.Mockito.verify; |
| 9 | +import static org.mockito.Mockito.when; |
| 10 | + |
| 11 | +import com.android.billingclient.api.BillingClient; |
| 12 | +import com.android.billingclient.api.BillingClientStateListener; |
| 13 | +import io.flutter.plugin.common.MethodCall; |
| 14 | +import io.flutter.plugin.common.MethodChannel; |
| 15 | +import io.flutter.plugin.common.MethodChannel.Result; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.Test; |
| 20 | +import org.mockito.ArgumentCaptor; |
| 21 | +import org.mockito.Mock; |
| 22 | +import org.mockito.MockitoAnnotations; |
| 23 | +import org.mockito.Spy; |
| 24 | + |
| 25 | +public class InAppPurchasePluginTest { |
| 26 | + InAppPurchasePlugin plugin; |
| 27 | + @Mock BillingClient mockBillingClient; |
| 28 | + @Mock MethodChannel mockMethodChannel; |
| 29 | + @Spy Result result; |
| 30 | + |
| 31 | + @Before |
| 32 | + public void setUp() { |
| 33 | + MockitoAnnotations.initMocks(this); |
| 34 | + plugin = new InAppPurchasePlugin(mockBillingClient, mockMethodChannel); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void invalidMethod() { |
| 39 | + MethodCall call = new MethodCall("invalid", null); |
| 40 | + plugin.onMethodCall(call, result); |
| 41 | + verify(result, times(1)).notImplemented(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void isReady_true() { |
| 46 | + MethodCall call = new MethodCall("BillingClient#isReady()", null); |
| 47 | + when(mockBillingClient.isReady()).thenReturn(true); |
| 48 | + plugin.onMethodCall(call, result); |
| 49 | + verify(result).success(true); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void isReady_false() { |
| 54 | + MethodCall call = new MethodCall("BillingClient#isReady()", null); |
| 55 | + when(mockBillingClient.isReady()).thenReturn(false); |
| 56 | + plugin.onMethodCall(call, result); |
| 57 | + verify(result).success(false); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void startConnection() { |
| 62 | + Map<String, Integer> arguments = new HashMap<>(); |
| 63 | + arguments.put("handle", 1); |
| 64 | + MethodCall call = |
| 65 | + new MethodCall("BillingClient#startConnection(BillingClientStateListener)", arguments); |
| 66 | + ArgumentCaptor<BillingClientStateListener> captor = |
| 67 | + ArgumentCaptor.forClass(BillingClientStateListener.class); |
| 68 | + doNothing().when(mockBillingClient).startConnection(captor.capture()); |
| 69 | + |
| 70 | + plugin.onMethodCall(call, result); |
| 71 | + verify(result, never()).success(any()); |
| 72 | + captor.getValue().onBillingSetupFinished(100); |
| 73 | + |
| 74 | + verify(result, times(1)).success(100); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void endConnection() { |
| 79 | + // Set up a connected BillingClient instance |
| 80 | + final int disconnectCallbackHandle = 22; |
| 81 | + Map<String, Integer> arguments = new HashMap<>(); |
| 82 | + arguments.put("handle", disconnectCallbackHandle); |
| 83 | + MethodCall connectCall = |
| 84 | + new MethodCall("BillingClient#startConnection(BillingClientStateListener)", arguments); |
| 85 | + ArgumentCaptor<BillingClientStateListener> captor = |
| 86 | + ArgumentCaptor.forClass(BillingClientStateListener.class); |
| 87 | + doNothing().when(mockBillingClient).startConnection(captor.capture()); |
| 88 | + plugin.onMethodCall(connectCall, mock(Result.class)); |
| 89 | + final BillingClientStateListener stateListener = captor.getValue(); |
| 90 | + |
| 91 | + // Disconnect the connected client |
| 92 | + MethodCall disconnectCall = new MethodCall("BillingClient#endConnection()", null); |
| 93 | + plugin.onMethodCall(disconnectCall, result); |
| 94 | + |
| 95 | + // Verify that the client is disconnected and that the OnDisconnect callback has been triggered |
| 96 | + verify(result, times(1)).success(any()); |
| 97 | + verify(mockBillingClient, times(1)).endConnection(); |
| 98 | + stateListener.onBillingServiceDisconnected(); |
| 99 | + Map<String, Integer> expectedInvocation = new HashMap<>(); |
| 100 | + expectedInvocation.put("handle", disconnectCallbackHandle); |
| 101 | + verify(mockMethodChannel, times(1)) |
| 102 | + .invokeMethod( |
| 103 | + "BillingClientStateListener#onBillingServiceDisconnected()", expectedInvocation); |
| 104 | + } |
| 105 | +} |
0 commit comments