package com.eggheadgames.inapppayments;

import android.content.Context;

import com.billing.PurchaseServiceListener;
import com.billing.SubscriptionServiceListener;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RunWith(MockitoJUnitRunner.class)
public class BillingTest {

    @Mock
    Context context;

    @Test
    public void onProductOwnedEvent_eachRegisteredListenerShouldBeTriggered() throws Exception {
        IAPManager.build(context, IAPManager.BUILD_TARGET_GOOGLE, new ArrayList<String>());

        PurchaseServiceListener firstListener = Mockito.spy(PurchaseServiceListener.class);
        IAPManager.addPurchaseListener(firstListener);

        PurchaseServiceListener secondListener = Mockito.spy(PurchaseServiceListener.class);
        IAPManager.addPurchaseListener(secondListener);

        IAPManager.getBillingService().productOwned(TestConstants.TEST_SKU, false);

        Mockito.verify(firstListener, Mockito.times(1)).onProductPurchased(TestConstants.TEST_SKU);
        Mockito.verify(secondListener, Mockito.times(1)).onProductPurchased(TestConstants.TEST_SKU);
    }

    @Test
    public void onProductDetailsFetched_eachRegisteredListenerShouldBeTriggered() {
        IAPManager.build(context, IAPManager.BUILD_TARGET_GOOGLE, new ArrayList<String>());

        PurchaseServiceListener firstListener = Mockito.spy(PurchaseServiceListener.class);
        IAPManager.addPurchaseListener(firstListener);

        PurchaseServiceListener secondListener = Mockito.spy(PurchaseServiceListener.class);
        IAPManager.addPurchaseListener(secondListener);

        Map<String, String> products = new HashMap<>();
        products.put(TestConstants.TEST_SKU, TestConstants.TEST_PRODUCT);
        products.put(TestConstants.TEST_SKU_1, TestConstants.TEST_PRODUCT);

        IAPManager.getBillingService().updatePrices(products);

        Mockito.verify(firstListener, Mockito.times(1)).onPricesUpdated(products);
        Mockito.verify(secondListener, Mockito.times(1)).onPricesUpdated(products);
    }

    @Test
    public void onIapManagerInteraction_onlyRegisteredListenersShouldBeTriggered() {
        IAPManager.build(context, IAPManager.BUILD_TARGET_GOOGLE, new ArrayList<String>());

        PurchaseServiceListener firstListener = Mockito.spy(PurchaseServiceListener.class);
        IAPManager.addPurchaseListener(firstListener);

        PurchaseServiceListener secondListener = Mockito.spy(PurchaseServiceListener.class);
        IAPManager.addPurchaseListener(secondListener);

        IAPManager.removePurchaseListener(firstListener);

        IAPManager.getBillingService().productOwned(TestConstants.TEST_SKU, false);

        Mockito.verify(firstListener, Mockito.never()).onProductPurchased(Mockito.anyString());
        Mockito.verify(secondListener, Mockito.times(1)).onProductPurchased(TestConstants.TEST_SKU);

    }

    @Test
    public void onProductPurchase_purchaseCallbackShouldBeTriggered() {
        IAPManager.build(context, IAPManager.BUILD_TARGET_GOOGLE, new ArrayList<String>());

        PurchaseServiceListener listener = Mockito.spy(PurchaseServiceListener.class);
        IAPManager.addPurchaseListener(listener);

        IAPManager.getBillingService().productOwned(TestConstants.TEST_SKU, false);

        Mockito.verify(listener, Mockito.times(1)).onProductPurchased(TestConstants.TEST_SKU);
    }

    @Test
    public void onProductRestore_restoreCallbackShouldBeTriggered() {
        IAPManager.build(context, IAPManager.BUILD_TARGET_GOOGLE, new ArrayList<String>());

        PurchaseServiceListener listener = Mockito.spy(PurchaseServiceListener.class);
        IAPManager.addPurchaseListener(listener);

        IAPManager.getBillingService().productOwned(TestConstants.TEST_SKU, true);

        Mockito.verify(listener, Mockito.times(1)).onProductRestored(TestConstants.TEST_SKU);
    }

    @Test
    public void onSubscriptionPurchase_purchaseCallbackShouldBeTriggered() {
        IAPManager.build(context, IAPManager.BUILD_TARGET_GOOGLE, new ArrayList<String>());

        SubscriptionServiceListener listener = Mockito.spy(SubscriptionServiceListener.class);
        IAPManager.addSubscriptionListener(listener);

        IAPManager.getBillingService().subscriptionOwned(TestConstants.TEST_SKU, false);

        Mockito.verify(listener, Mockito.times(1)).onSubscriptionPurchased(TestConstants.TEST_SKU);
    }

    @Test
    public void onSubscriptionRestore_restoreCallbackShouldBeTriggered() {
        IAPManager.build(context, IAPManager.BUILD_TARGET_GOOGLE, new ArrayList<String>());

        SubscriptionServiceListener listener = Mockito.spy(SubscriptionServiceListener.class);
        IAPManager.addSubscriptionListener(listener);

        IAPManager.getBillingService().subscriptionOwned(TestConstants.TEST_SKU, true);

        Mockito.verify(listener, Mockito.times(1)).onSubscriptionRestored(TestConstants.TEST_SKU);
    }

}
@@ -653,32 +653,57 @@
 D_TARGET_GOOGLE,
+ new ArrayList%3CString%3E(),
  new ArrayList%3CS
@@ -705,32 +705,32 @@
 ist%3CString%3E());%0A
-
 %0A        Purchas
@@ -1456,32 +1456,57 @@
 D_TARGET_GOOGLE,
+ new ArrayList%3CString%3E(),
  new ArrayList%3CS
@@ -2412,32 +2412,57 @@
 D_TARGET_GOOGLE,
+ new ArrayList%3CString%3E(),
  new ArrayList%3CS
@@ -3259,32 +3259,57 @@
 D_TARGET_GOOGLE,
+ new ArrayList%3CString%3E(),
  new ArrayList%3CS
@@ -3782,32 +3782,57 @@
 D_TARGET_GOOGLE,
+ new ArrayList%3CString%3E(),
  new ArrayList%3CS
@@ -4310,32 +4310,57 @@
 D_TARGET_GOOGLE,
+ new ArrayList%3CString%3E(),
  new ArrayList%3CS
@@ -4795,32 +4795,32 @@
 BeTriggered() %7B%0A
-
         IAPManag
@@ -4860,32 +4860,57 @@
 D_TARGET_GOOGLE,
+ new ArrayList%3CString%3E(),
  new ArrayList%3CS

Fix Billing tests