package com.novoda.simplechromecustomtabs.provider;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.robolectric.RobolectricTestRunner;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

@RunWith(RobolectricTestRunner.class)
public class SimpleChromeCustomTabsAvailableAppProviderTest {

    private static final String NON_EMPTY_PACKAGE = "non.empty.package";

    @Mock
    private BestPackageFinder mockBestPackageFinder;
    @Mock
    private SimpleChromeCustomTabsAvailableAppProvider.PackageFoundCallback mockPackageFoundCallback;

    private SimpleChromeCustomTabsAvailableAppProvider simpleChromeCustomTabsAvailableAppProvider;

    @Before
    public void setUp() {
        initMocks(this);

        simpleChromeCustomTabsAvailableAppProvider = new SimpleChromeCustomTabsAvailableAppProvider(createObservable(), mockBestPackageFinder);

    }

    private Observable<String> createObservable() {
        return Observable.<String>empty()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(AndroidSchedulers.mainThread());
    }

    @Test
    public void findBestPackageDelegatesToPackageFinder() {
        simpleChromeCustomTabsAvailableAppProvider.findBestPackage(mockPackageFoundCallback);

        verify(mockBestPackageFinder).findBestPackage();
    }

    @Test
    public void packageIsFoundIfNotNullOrEmpty() {
        when(mockBestPackageFinder.findBestPackage()).thenReturn(NON_EMPTY_PACKAGE);

        simpleChromeCustomTabsAvailableAppProvider.findBestPackage(mockPackageFoundCallback);

        verify(mockPackageFoundCallback).onPackageFound(NON_EMPTY_PACKAGE);
    }

    @Test
    public void packageIsNotFoundIfNull() {
        simpleChromeCustomTabsAvailableAppProvider.findBestPackage(mockPackageFoundCallback);

        when(mockBestPackageFinder.findBestPackage()).thenReturn(null);

        verify(mockPackageFoundCallback).onPackageNotFound();
    }

    @Test
    public void packageNotFoundIfEmpty() {
        simpleChromeCustomTabsAvailableAppProvider.findBestPackage(mockPackageFoundCallback);

        when(mockBestPackageFinder.findBestPackage()).thenReturn("");

        verify(mockPackageFoundCallback).onPackageNotFound();
    }

}

@@ -301,21 +301,25 @@
 to.M
-ockito.verify
+atchers.anyString
 ;%0Aim
@@ -350,20 +350,17 @@
 Mockito.
-when
+*
 ;%0Aimport
@@ -1069,17 +1069,16 @@
 inder);%0A
-%0A
     %7D%0A%0A 
@@ -1902,24 +1902,97 @@
 dIfNull() %7B%0A
+        when(mockBestPackageFinder.findBestPackage()).thenReturn(null);%0A%0A
         simp
@@ -2086,357 +2086,858 @@
     
-when(mockBestPackageFinder.findBestPackage()).thenReturn(null);%0A%0A        verify(mockPackageFoundCallback).onPackageNotFound();%0A    %7D%0A%0A    @Test%0A    public void packageNotFoundIfEmpty() %7B%0A        simpleChromeCustomTabsAvailableAppProvider.findBestPackage(mockPackageFoundCallback);%0A%0A        when(mockBestPackageFinder.findBestPackage()).thenReturn(%22%22
+verify(mockPackageFoundCallback).onPackageNotFound();%0A    %7D%0A%0A    @Test%0A    public void packageNotFoundIfEmpty() %7B%0A        when(mockBestPackageFinder.findBestPackage()).thenReturn(%22%22);%0A%0A        simpleChromeCustomTabsAvailableAppProvider.findBestPackage(mockPackageFoundCallback);%0A%0A        verify(mockPackageFoundCallback).onPackageNotFound();%0A    %7D%0A%0A    @Test%0A    public void ifPackageIsFoundThenPackageNotFoundWillNotBeCalled() %7B%0A        givenThatPackageIsFound();%0A%0A        simpleChromeCustomTabsAvailableAppProvider.findBestPackage(mockPackageFoundCallback);%0A%0A        verify(mockPackageFoundCallback, never()).onPackageNotFound();%0A    %7D%0A%0A    @Test%0A    public void ifPackageIsNotFoundThenPackageFoundWillNotBeCalled() %7B%0A        givenThatPackageIsNotFound();%0A%0A        simpleChromeCustomTabsAvailableAppProvider.findBestPackage(mockPackageFoundCallback
 );%0A%0A
@@ -2967,32 +2967,41 @@
 ageFoundCallback
+, never()
 ).onPackageNotFo
@@ -2991,33 +2991,303 @@
 )).onPackage
-Not
 Found(
+anyString());%0A    %7D%0A%0A    private void givenThatPackageIsFound() %7B%0A        when(mockBestPackageFinder.findBestPackage()).thenReturn(NON_EMPTY_PACKAGE);%0A    %7D%0A%0A    private void givenThatPackageIsNotFound() %7B%0A        when(mockBestPackageFinder.findBestPackage()).thenReturn(%22%22
 );%0A    %7D%0A%0A%7D%0A

Include unit test to ensure that when a package is not found then the wrong callback won't be called