package com.novoda.simplechromecustomtabs.connection;

import android.app.Activity;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.robolectric.Robolectric;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;

public class SimpleChromeCustomTabsConnectionTest {

    @Mock
    private Binder mockBinder;
    @Mock
    private Activity mockActivity;
    @Mock
    private ConnectedClient mockConnectedClient;

    private SimpleChromeCustomTabsConnection simpleChromeCustomTabsConnection;

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

        when(mockActivity.getApplicationContext()).thenReturn(Robolectric.application);
        when(mockConnectedClient.newSession()).thenReturn(mock(Session.class));

        simpleChromeCustomTabsConnection = new SimpleChromeCustomTabsConnection(mockBinder);
    }

    @Test
    public void connectToBindsApplicationContextToService() {
        simpleChromeCustomTabsConnection.connectTo(mockActivity);

        verify(mockBinder).bindCustomTabsServiceTo(mockActivity.getApplicationContext(), simpleChromeCustomTabsConnection);
    }

    @Test
    public void disconnectFromUnbindsApplicationContextFromService() {
        simpleChromeCustomTabsConnection.disconnectFrom(mockActivity);

        verify(mockBinder).unbindCustomTabsService(mockActivity.getApplicationContext());
    }

    @Test
    public void warmsUpConnectedClientOnServiceConnected() {
        givenAConnectedClient();

        simpleChromeCustomTabsConnection.onServiceConnected(mockConnectedClient);

        verify(mockConnectedClient).warmup();
    }

    @Test
    public void doesNotWarmUpDisconnectedClientOnServiceConnected() {
        givenADisconnectedClient();

        simpleChromeCustomTabsConnection.onServiceConnected(mockConnectedClient);

        verify(mockConnectedClient, never()).warmup();
    }

    @Test
    public void createsNewSessionWhenClientIsStillConnected() {
        givenAConnectedClient();

        simpleChromeCustomTabsConnection.onServiceConnected(mockConnectedClient);

        verify(mockConnectedClient).newSession();
    }

    @Test
    public void doesNotCreateNewSessionWhenClientIsDisconnected() {
        givenADisconnectedClient();

        simpleChromeCustomTabsConnection.onServiceConnected(mockConnectedClient);
        assertThat(simpleChromeCustomTabsConnection.getSession()).isEqualTo(Session.NULL_SESSION);

        verify(mockConnectedClient, never()).newSession();
    }

    @Test
    public void disconnectsConnectedClientOnServiceDisconnected() {
        givenAConnectedClient();

        simpleChromeCustomTabsConnection.onServiceConnected(mockConnectedClient);
        simpleChromeCustomTabsConnection.onServiceDisconnected();

        verify(mockConnectedClient).disconnect();
    }

    @Test
    public void doesNotDisconnectDisconnectedConnectedClientOnServiceDisconnected() {
        givenADisconnectedClient();

        simpleChromeCustomTabsConnection.onServiceConnected(mockConnectedClient);
        simpleChromeCustomTabsConnection.onServiceDisconnected();

        verify(mockConnectedClient, never()).disconnect();
    }

    private void givenAConnectedClient() {
        when(mockConnectedClient.stillConnected()).thenReturn(true);
    }

    private void givenADisconnectedClient() {
        when(mockConnectedClient.stillConnected()).thenReturn(false);
    }

}

@@ -76,16 +76,40 @@
 ctivity;
+%0Aimport android.net.Uri;
 %0A%0Aimport
@@ -175,16 +175,44 @@
 o.Mock;%0A
+import org.mockito.Mockito;%0A
 import o
@@ -448,16 +448,61 @@
 Test %7B%0A%0A
+    private static Uri ANY_URI = Uri.EMPTY;%0A%0A
     @Moc
@@ -581,32 +581,32 @@
 vity;%0A    @Mock%0A
-
     private Conn
@@ -637,16 +637,59 @@
 dClient;
+%0A    @Mock%0A    private Session mockSession;
 %0A%0A    pr
@@ -3387,32 +3387,868 @@
 nnect();%0A    %7D%0A%0A
+    @Test%0A    public void warmsUpFutureUrlForConnectedClientOnServiceConnected() %7B%0A        givenAConnectedClient();%0A%0A        simpleChromeCustomTabsConnection.onServiceConnected(mockConnectedClient);%0A%0A        verify(mockSession).mayLaunch(any(Uri.class));%0A    %7D%0A%0A    @Test%0A    public void warmsUpFutureUrlForConnectedClientOnMayLaunchUrl() %7B%0A        givenAConnectedClient();%0A        simpleChromeCustomTabsConnection.onServiceConnected(mockConnectedClient);%0A        Mockito.reset(mockSession);%0A%0A        simpleChromeCustomTabsConnection.mayLaunch(ANY_URI);%0A%0A        verify(mockSession).mayLaunch(ANY_URI);%0A    %7D%0A%0A    @Test%0A    public void doesNotWarmsUpFutureUrlForDisconnectedClient() %7B%0A        givenADisconnectedClient();%0A%0A        simpleChromeCustomTabsConnection.mayLaunch(ANY_URI);%0A%0A        verifyZeroInteractions(mockSession);%0A    %7D%0A%0A
     private void
@@ -4339,24 +4339,96 @@
 turn(true);%0A
+        when(mockConnectedClient.newSession()).thenReturn(mockSession);%0A
     %7D%0A%0A    p

Write test for SimpleChromeCustomTabsConnection related to mayLaunch