summaryrefslogtreecommitdiff
path: root/sdl_android/src/androidTest/java/com/smartdevicelink/SdlConnection/SdlConnectionTest.java
blob: 7d9e2cfcda5272d9554ead6aaca11fd20ed9c60d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.smartdevicelink.SdlConnection;

import android.test.AndroidTestCase;

import com.smartdevicelink.test.SdlUnitTestContants;
import com.smartdevicelink.transport.BTTransportConfig;
import com.smartdevicelink.transport.BaseTransportConfig;
import com.smartdevicelink.transport.MultiplexTransportConfig;
import com.smartdevicelink.transport.RouterServiceValidator;
import com.smartdevicelink.transport.USBTransportConfig;
import com.smartdevicelink.transport.enums.TransportType;

public class SdlConnectionTest extends AndroidTestCase {
	
	private static final String TAG = "SdlConnection Tests";
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		
	}

	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	public void testForceConnectEvent(){
		SdlConnection.enableLegacyMode(false, null);
		assertNull(SdlConnectionTestClass.cachedMultiConfig);
		
		RouterServiceValidator rsvp = new RouterServiceValidator(this.mContext);
		rsvp.setFlags(RouterServiceValidator.FLAG_DEBUG_PERFORM_ALL_CHECKS);
		MultiplexTransportConfig config = new MultiplexTransportConfig(this.mContext,SdlUnitTestContants.TEST_APP_ID);
		SdlConnectionTestClass connection = new SdlConnectionTestClass(config,rsvp);
		assertNotNull(SdlConnectionTestClass.cachedMultiConfig);
		
		assertTrue(SdlConnectionTestClass.isLegacyModeEnabled());
		
		assertEquals(TransportType.BLUETOOTH, connection.getCurrentTransportType());
		
		assertNotNull(SdlConnectionTestClass.cachedMultiConfig);
		
		connection.forceHardwareConnectEvent(TransportType.MULTIPLEX);
		assertFalse(connection.connected);

		SdlConnectionTestClass.cachedMultiConfig.setSecurityLevel(MultiplexTransportConfig.FLAG_MULTI_SECURITY_OFF);
		
		assertNotNull(SdlConnectionTestClass.cachedMultiConfig.getService());

		assertEquals(TransportType.MULTIPLEX, connection.getCurrentTransportType());
		
		// Test for handling of null service
		MultiplexTransportConfig null_service_config = new MultiplexTransportConfig(this.mContext,SdlUnitTestContants.TEST_APP_ID);
		null_service_config.setService(null);
		SdlConnectionTestClass null_service_connection = new SdlConnectionTestClass(null_service_config, null);
		Exception exception = null;
		try{
			null_service_connection.forceHardwareConnectEvent(TransportType.MULTIPLEX);
		}catch(NullPointerException e){
			exception = e;
		}
		
		assertNull(exception); // Failed, unhandled NPE
	}
	
	public void testMultiplexConstructor(){
		SdlConnection.enableLegacyMode(false, null);
		RouterServiceValidator rsvp = new RouterServiceValidator(this.mContext);
		rsvp.setFlags(RouterServiceValidator.FLAG_DEBUG_NONE);
		MultiplexTransportConfig config = new MultiplexTransportConfig(this.mContext,SdlUnitTestContants.TEST_APP_ID);
		SdlConnection connection = new SdlConnection(config,rsvp);
		assertEquals(TransportType.MULTIPLEX, connection.getCurrentTransportType());
	}
	
	public void testMultiplexConstructorInsalledFrom(){
		SdlConnection.enableLegacyMode(false, null);
		RouterServiceValidator rsvp = new RouterServiceValidator(this.mContext);
		rsvp.setFlags(RouterServiceValidator.FLAG_DEBUG_INSTALLED_FROM_CHECK);
		MultiplexTransportConfig config = new MultiplexTransportConfig(this.mContext,SdlUnitTestContants.TEST_APP_ID);
		SdlConnection connection = new SdlConnection(config,rsvp);
		boolean didValidate = rsvp.validate();
		if(didValidate){
			assertEquals(TransportType.MULTIPLEX, connection.getCurrentTransportType());
		}else{
			assertEquals(TransportType.BLUETOOTH, connection.getCurrentTransportType());

		}
	}
	
	public void testMultiplexConstructorTrustedPackage(){
		SdlConnection.enableLegacyMode(false, null);
		RouterServiceValidator rsvp = new RouterServiceValidator(this.mContext);
		rsvp.setFlags(RouterServiceValidator.FLAG_DEBUG_PACKAGE_CHECK);
		MultiplexTransportConfig config = new MultiplexTransportConfig(this.mContext,SdlUnitTestContants.TEST_APP_ID);
		SdlConnection connection = new SdlConnection(config,rsvp);
		boolean didValidate = rsvp.validate();
		if(didValidate){
			assertEquals(TransportType.MULTIPLEX, connection.getCurrentTransportType());
		}else{
			assertEquals(TransportType.BLUETOOTH, connection.getCurrentTransportType());

		}
	}
	
	public void testMultiplexConstructorTrustedPackageAndVersion(){
		SdlConnection.enableLegacyMode(false, null);
		RouterServiceValidator rsvp = new RouterServiceValidator(this.mContext);
		rsvp.setFlags(RouterServiceValidator.FLAG_DEBUG_VERSION_CHECK);
		MultiplexTransportConfig config = new MultiplexTransportConfig(this.mContext,SdlUnitTestContants.TEST_APP_ID);
		SdlConnection connection = new SdlConnection(config,rsvp);
		boolean didValidate = rsvp.validate();
		if(didValidate){
			assertEquals(TransportType.MULTIPLEX, connection.getCurrentTransportType());
		}else{
			assertEquals(TransportType.BLUETOOTH, connection.getCurrentTransportType());

		}	}
	
	public void testMultiplexConstructorAllFlags(){
		SdlConnection.enableLegacyMode(false, null);
		RouterServiceValidator rsvp = new RouterServiceValidator(this.mContext);
		rsvp.setFlags(RouterServiceValidator.FLAG_DEBUG_PERFORM_ALL_CHECKS);
		MultiplexTransportConfig config = new MultiplexTransportConfig(this.mContext,SdlUnitTestContants.TEST_APP_ID);
		SdlConnection connection = new SdlConnection(config,rsvp);
		boolean didValidate = rsvp.validate();
		if(didValidate){
			assertEquals(TransportType.MULTIPLEX, connection.getCurrentTransportType());
		}else{
			assertEquals(TransportType.BLUETOOTH, connection.getCurrentTransportType());

		}	}
	
	public void testBluetoothConstructor(){
		SdlConnection.enableLegacyMode(false, null);
		BTTransportConfig btConfig = new BTTransportConfig(true);
		SdlConnection connection = new SdlConnection(btConfig);
		assertEquals(TransportType.BLUETOOTH, connection.getCurrentTransportType());
	}

	public void testUsbConstructor(){
		SdlConnection.enableLegacyMode(false, null);
		USBTransportConfig btConfig = new USBTransportConfig(mContext);
		SdlConnection connection = new SdlConnection(btConfig,null);
		assertFalse(SdlConnection.isLegacyModeEnabled());
		assertEquals(TransportType.USB, connection.getCurrentTransportType());

		SdlConnection connection2 = new SdlConnection(btConfig);
		assertEquals(TransportType.USB, connection2.getCurrentTransportType());
	}
	
	protected class SdlConnectionTestClass extends SdlConnection{
		protected boolean connected = false;
		public SdlConnectionTestClass(BaseTransportConfig transportConfig) {
			super(transportConfig);
		}

		protected SdlConnectionTestClass(BaseTransportConfig transportConfig,RouterServiceValidator rsvp){
			super(transportConfig,rsvp);
		}
		
		@Override
		public void onTransportConnected() {
			super.onTransportConnected();
			connected = true;
		}

		@Override
		public void onTransportDisconnected(String info) {
			connected = false;
			//Grab a currently running router service
			RouterServiceValidator rsvp2 = new RouterServiceValidator(mContext);
			rsvp2.setFlags(RouterServiceValidator.FLAG_DEBUG_NONE);
			assertTrue(rsvp2.validate());
			assertNotNull(rsvp2.getService());
			SdlConnectionTestClass.cachedMultiConfig.setService(rsvp2.getService());
			super.onTransportDisconnected(info);
		}

		@Override
		public void onTransportError(String info, Exception e) {
			connected = false;
			super.onTransportError(info, e);
		}
		
		
		
	}
	
}