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.Matchers.anyString;
import static org.mockito.Mockito.*;
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() {
        when(mockBestPackageFinder.findBestPackage()).thenReturn(null);

        simpleChromeCustomTabsAvailableAppProvider.findBestPackage(mockPackageFoundCallback);

        verify(mockPackageFoundCallback).onPackageNotFound();
    }

    @Test
    public void packageNotFoundIfEmpty() {
        when(mockBestPackageFinder.findBestPackage()).thenReturn("");

        simpleChromeCustomTabsAvailableAppProvider.findBestPackage(mockPackageFoundCallback);

        verify(mockPackageFoundCallback).onPackageNotFound();
    }

    @Test
    public void ifPackageIsFoundThenPackageNotFoundWillNotBeCalled() {
        givenThatPackageIsFound();

        simpleChromeCustomTabsAvailableAppProvider.findBestPackage(mockPackageFoundCallback);

        verify(mockPackageFoundCallback, never()).onPackageNotFound();
    }

    @Test
    public void ifPackageIsNotFoundThenPackageFoundWillNotBeCalled() {
        givenThatPackageIsNotFound();

        simpleChromeCustomTabsAvailableAppProvider.findBestPackage(mockPackageFoundCallback);

        verify(mockPackageFoundCallback, never()).onPackageFound(anyString());
    }

    private void givenThatPackageIsFound() {
        when(mockBestPackageFinder.findBestPackage()).thenReturn(NON_EMPTY_PACKAGE);
    }

    private void givenThatPackageIsNotFound() {
        when(mockBestPackageFinder.findBestPackage()).thenReturn("");
    }

}

@@ -1600,82 +1600,45 @@
     
-when(mockBestPackageFinder.findBestPackage()).thenReturn(NON_EMPTY_PACKAGE
+givenThatPackageNameIsNotNullOrEmpty(
 );%0A%0A
@@ -1881,69 +1881,31 @@
     
-when(mockBestPackageFinder.findBestPackage()).thenReturn(n
+givenThatPackageIsN
 ull
+(
 );%0A%0A
@@ -2133,67 +2133,32 @@
     
-when(mockBestPackageFinder.findBestPackage()).thenReturn(%22%22
+givenThatPackageIsEmpty(
 );%0A%0A
@@ -2945,23 +2945,36 @@
 tPackage
-IsFound
+NameIsNotNullOrEmpty
 () %7B%0A   
@@ -3102,87 +3102,397 @@
 eIsN
-otFound() %7B%0A        when(mockBestPackageFinder.findBestPackage()).thenReturn(%22%22
+ull() %7B%0A        when(mockBestPackageFinder.findBestPackage()).thenReturn(null);%0A    %7D%0A%0A    private void givenThatPackageIsEmpty() %7B%0A        when(mockBestPackageFinder.findBestPackage()).thenReturn(%22%22);%0A    %7D%0A%0A    private void givenThatPackageIsFound() %7B%0A        givenThatPackageNameIsNotNullOrEmpty();%0A    %7D%0A%0A    private void givenThatPackageIsNotFound() %7B%0A        givenThatPackageIsEmpty(
 );%0A 

Refactor SimpleChromeCustomTabsAvailableAppProviderTest for legibility