package com.smartdevicelink.test.transport;

import junit.framework.TestCase;
import android.os.Handler;
import android.os.Message;

import com.smartdevicelink.transport.MultiplexBluetoothTransport;
import com.smartdevicelink.transport.SdlRouterService;



public class MultiplexBluetoothTransportTest extends TestCase {
	
	private static final Object REQUEST_LOCK = new Object();
	
	MultiplexBluetoothTransport bluetooth;
	boolean didCorrectThing = false, isWaitingForResponse = false;
	
	//Example handler
	Handler stateChangeHandler = new Handler(){
		int stateDesired = MultiplexBluetoothTransport.STATE_LISTEN;
		@Override
		public void handleMessage(Message msg) {
			if(!isWaitingForResponse){
				return;
			}
			switch(msg.what){
				case SdlRouterService.MESSAGE_STATE_CHANGE:
					if(msg.arg1 == stateDesired){
							didCorrectThing = true;
							break;
					}
					default:
						didCorrectThing = false;
			}
			REQUEST_LOCK.notify();
		}
		
	};
	
	public void testStateTransitions() {
		//TODO test for more than the two states
		bluetooth = MultiplexBluetoothTransport.getBluetoothSerialServerInstance();
		assertNull(bluetooth);

		bluetooth = MultiplexBluetoothTransport.getBluetoothSerialServerInstance(stateChangeHandler);
		assertEquals(bluetooth.getState(), MultiplexBluetoothTransport.STATE_NONE);
		
		bluetooth.start();
		assertEquals(bluetooth.getState(), MultiplexBluetoothTransport.STATE_LISTEN);
		
		bluetooth.stop();
		assertEquals(bluetooth.getState(), MultiplexBluetoothTransport.STATE_NONE);

	}
	
	private void notifyResponseReceived(){
		REQUEST_LOCK.notify();
	}
	
	private void waitForResponse(){
		synchronized(REQUEST_LOCK){
			try {
				REQUEST_LOCK.wait();
				assertTrue(didCorrectThing);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
@@ -49,32 +49,26 @@
 ort 
-junit.framework.TestCase
+android.os.Handler
 ;%0Aim
@@ -75,37 +75,36 @@
 port android.os.
-Handl
+Loop
 er;%0Aimport andro
@@ -241,16 +241,49 @@
 rvice;%0A%0A
+import junit.framework.TestCase;%0A
 %0A%0Apublic
@@ -536,16 +536,137 @@
 Handler 
+stateChangeHandler;%0A%09%0A%09public void testStateTransitions() %7B%0A%09%09if(Looper.myLooper() == null)%7B%0A%09%09%09Looper.prepare();%0A%09%09%7D%0A%0A%09%09
 stateCha
@@ -693,16 +693,17 @@
 dler()%7B%0A
+%09
 %09%09int st
@@ -759,16 +759,17 @@
 STEN;%0A%09%09
+%09
 @Overrid
@@ -770,16 +770,17 @@
 verride%0A
+%09
 %09%09public
@@ -817,16 +817,17 @@
 g) %7B%0A%09%09%09
+%09
 if(!isWa
@@ -845,16 +845,17 @@
 ponse)%7B%0A
+%09
 %09%09%09%09retu
@@ -861,18 +861,20 @@
 urn;%0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09switc
@@ -886,16 +886,17 @@
 .what)%7B%0A
+%09
 %09%09%09%09case
@@ -935,16 +935,17 @@
 CHANGE:%0A
+%09
 %09%09%09%09%09if(
@@ -1017,24 +1017,25 @@
 break;%0A%09%09%09%09%09
+%09
 %7D%0A%09%09%09%09%09defau
@@ -1072,18 +1072,20 @@
 lse;%0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09REQUE
@@ -1108,56 +1108,17 @@
 ;%0A%09%09
+%09
 %7D%0A
-%09%09
 %0A
+%09
 %09%7D;%0A
-%09%0A%09public void testStateTransitions() %7B
 %0A%09%09/
@@ -1435,18 +1435,16 @@
 _NONE);%0A
-%09%09
 %0A%09%09bluet
@@ -1537,18 +1537,16 @@
 ISTEN);%0A
-%09%09
 %0A%09%09bluet
@@ -1636,17 +1636,16 @@
 _NONE);%0A
-%0A
 %09%7D%0A%09%0A%09pr

Fix MultiplexBluetoothTransport unit tests