package com.punchthrough.bean.sdk;

import android.bluetooth.BluetoothDevice;
import android.os.Handler;
import android.content.Context;

import com.punchthrough.bean.sdk.internal.ble.GattClient;
import com.punchthrough.bean.sdk.internal.serial.GattSerialTransportProfile.SerialListener;
import com.punchthrough.bean.sdk.internal.serial.GattSerialTransportProfile;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

import org.junit.Before;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

public class BeanTest {

    GattClient.ConnectionListener testListener;

    // Mocks
    Context mockContext;
    BluetoothDevice mockDevice;
    Handler mockHandler;
    GattClient mockGattClient;
    GattSerialTransportProfile mockGattSerialTransportProfile;

    // Class under test
    Bean bean;

    @Before
    public void setup() {

        // Instantiate mocks
        mockContext = mock(Context.class);
        mockDevice = mock(BluetoothDevice.class);
        mockHandler = mock(Handler.class);
        mockGattClient = mock(GattClient.class);
        mockGattSerialTransportProfile = mock(GattSerialTransportProfile.class);

        // Customize some behavior
        when(mockGattClient.getSerialProfile()).thenReturn(mockGattSerialTransportProfile);

        doAnswer(new Answer() {
            public Object answer(InvocationOnMock invocation) {
                Object[] args = invocation.getArguments();
                testListener = (GattClient.ConnectionListener) args[0];
                return null;
            }
        }).when(mockGattClient).setListener(any(GattClient.ConnectionListener.class));

        // Instantiate class under test
        bean = new Bean(mockDevice, mockGattClient, mockHandler);
    }

    @Test
    public void testBeanConnection() {
        // It should start disconnected
        assertThat(bean.isConnected()).isFalse();

        // Issue connection
        bean.connect(mockContext, mock(BeanListener.class));
        testListener.onConnected();

        // Connected!
        assertThat(bean.isConnected()).isTrue();

    }

}

@@ -588,16 +588,68 @@
 nswer;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 public c
@@ -712,16 +712,73 @@
 istener;
+%0A    List%3CRunnable%3E handlerRunnables = new ArrayList%3C%3E();
 %0A%0A    //
@@ -1818,24 +1818,337 @@
 r.class));%0A%0A
+        doAnswer(new Answer() %7B%0A            public Object answer(InvocationOnMock invocation) %7B%0A                Object%5B%5D args = invocation.getArguments();%0A                handlerRunnables.add((Runnable) args%5B0%5D);%0A                return null;%0A            %7D%0A        %7D).when(mockHandler).post(any(Runnable.class));%0A%0A
         // I
@@ -2309,118 +2309,61 @@
     
-// It should start disconnected%0A        assertThat(bean.isConnected()).isFalse();%0A%0A        // Issue connection
+BeanListener mockListener = mock(BeanListener.class);
 %0A   
@@ -2393,37 +2393,32 @@
 ontext, mock
-(Bean
 Listener
 .class));%0A  
@@ -2405,23 +2405,16 @@
 Listener
-.class)
 );%0A     
@@ -2444,25 +2444,24 @@
 cted();%0A
-%0A
         
 // Conne
@@ -2456,71 +2456,120 @@
     
-// Connected!%0A        assertThat(bean.isConnected()).isTrue();%0A
+for (Runnable r : handlerRunnables) %7B%0A            r.run();%0A        %7D%0A        verify(mockListener).onConnected();
 %0A   

fix bean connection unit test