11package org .droidplanner .services .android .impl ;
22
33import android .content .Context ;
4- import android .os . Bundle ;
4+ import android .content . Intent ;
55import android .os .Handler ;
6+ import android .text .TextUtils ;
7+ import android .util .Log ;
68
79import com .MAVLink .MAVLinkPacket ;
810import com .MAVLink .Messages .MAVLinkMessage ;
911import com .MAVLink .common .msg_command_long ;
1012import com .MAVLink .enums .MAV_CMD ;
1113import com .o3dr .android .client .BuildConfig ;
1214import com .o3dr .services .android .lib .drone .connection .ConnectionParameter ;
15+ import com .o3dr .services .android .lib .drone .connection .ConnectionType ;
1316import com .o3dr .services .android .lib .gcs .link .LinkConnectionStatus ;
1417
1518import org .junit .Assert ;
2932import org .robolectric .RuntimeEnvironment ;
3033import org .robolectric .annotation .Config ;
3134
35+ import static android .support .v4 .app .ActivityCompat .startActivity ;
36+
3237
3338/**
3439 * Created by djmedina on 3/5/15.
3843@ Config (constants = BuildConfig .class , sdk = 18 )
3944public class BasicTest {
4045
46+ private static final String TAG = BasicTest .class .getSimpleName ();
47+
4148 private MavLinkDrone drone ;
4249 private MockMAVLinkClient mavClient ;
4350
51+ private boolean isUdpPingEnabled = false ;
52+
4453 private final Handler dpHandler = new Handler ();
4554
4655 private final DataLink .DataLinkListener inputStreamListener = new DataLink .DataLinkListener () {
@@ -57,7 +66,7 @@ public void onConnectionStatus(LinkConnectionStatus connectionStatus) {
5766 public void setUp () throws Exception {
5867 final Context context = RuntimeEnvironment .application .getApplicationContext ();
5968
60- ConnectionParameter connParams = new ConnectionParameter ( 0 , new Bundle () );
69+ ConnectionParameter connParams = retrieveConnectionParameters ( );
6170 mavClient = new MockMAVLinkClient (context , inputStreamListener , connParams );
6271
6372 drone = new ArduCopter ("test:" + FirmwareType .ARDU_COPTER .getType (), context , mavClient , dpHandler , new AndroidApWarningParser (), new LogMessageListener () {
@@ -95,4 +104,59 @@ public void testArm() {
95104 Assert .assertEquals (MAV_CMD .MAV_CMD_COMPONENT_ARM_DISARM , longMsg .command );
96105 Assert .assertEquals (1f , longMsg .param1 , 0.001 );
97106 }
107+
108+ private ConnectionParameter retrieveConnectionParameters () {
109+ final @ ConnectionType .Type int connectionType = ConnectionType .TYPE_USB ;
110+
111+ ConnectionParameter connParams ;
112+ switch (connectionType ) {
113+ case ConnectionType .TYPE_USB :
114+ connParams = ConnectionParameter .newUsbConnection (57600 , null , 0L );
115+ break ;
116+
117+ case ConnectionType .TYPE_UDP :
118+ if (isUdpPingEnabled ) {
119+ connParams = ConnectionParameter .newUdpWithPingConnection (
120+ 14550 ,
121+ "127.0.0.1" ,
122+ 14550 ,
123+ "Hello" .getBytes (),
124+ ConnectionType .DEFAULT_UDP_PING_PERIOD ,
125+ null ,
126+ 0L );
127+ }
128+ else {
129+ connParams = ConnectionParameter .newUdpConnection (14550 ,
130+ null , 0l );
131+ }
132+ break ;
133+
134+ case ConnectionType .TYPE_TCP :
135+ connParams = ConnectionParameter .newTcpConnection ("192.168.40.100" ,
136+ 5763 , null , 0L );
137+ break ;
138+
139+ case ConnectionType .TYPE_BLUETOOTH :
140+ String btAddress = null ;
141+ if (TextUtils .isEmpty (btAddress )) {
142+ connParams = null ;
143+ // TODO: Add behaviour to obtain Bluetooth Address
144+ // startActivity(new Intent(getApplicationContext(),
145+ // BluetoothDevicesActivity.class)
146+ // .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
147+
148+ } else {
149+ connParams = ConnectionParameter .newBluetoothConnection (btAddress ,
150+ null , 0L );
151+ }
152+ break ;
153+
154+ default :
155+ Log .e (TAG , "Unrecognized connection type: " + connectionType );
156+ connParams = null ;
157+ break ;
158+ }
159+
160+ return connParams ;
161+ }
98162}
0 commit comments