1+ package net .sharksystem .asap .android .lora ;
2+
3+ import android .bluetooth .BluetoothAdapter ;
4+ import android .bluetooth .BluetoothDevice ;
5+ import android .bluetooth .BluetoothSocket ;
6+ import android .content .Context ;
7+
8+ import org .junit .AfterClass ;
9+ import org .junit .BeforeClass ;
10+ import org .junit .FixMethodOrder ;
11+ import org .junit .Test ;
12+ import org .junit .runner .RunWith ;
13+ import org .junit .runners .MethodSorters ;
14+
15+ import java .io .BufferedReader ;
16+ import java .io .IOException ;
17+ import java .io .InputStreamReader ;
18+ import java .util .UUID ;
19+
20+ import androidx .test .ext .junit .runners .AndroidJUnit4 ;
21+ import androidx .test .platform .app .InstrumentationRegistry ;
22+
23+ import static org .junit .Assert .assertEquals ;
24+
25+ /**
26+ * Instrumented test, which will execute on an Android device.
27+ *
28+ * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
29+ */
30+ @ RunWith (AndroidJUnit4 .class )
31+ @ FixMethodOrder (MethodSorters .NAME_ASCENDING )
32+ public class RoundTripTimeTest {
33+
34+ public static String LONGEST_MESSAGE = "This is exactly 174 Characters long. This is exactly 174 Characters long. This is exactly 174 Characters long. This is exactly 174 Characters long. This is exactly 174 Charact" ;
35+
36+ public static BluetoothDevice Alice ;
37+ public static BluetoothDevice Bob ;
38+ public static BluetoothSocket AliceSocket ;
39+ public static BluetoothSocket BobSocket ;
40+
41+ @ BeforeClass
42+ public static void setup () throws IOException , InterruptedException {
43+ BluetoothAdapter btAdapter = BluetoothAdapter .getDefaultAdapter ();
44+ btAdapter .cancelDiscovery ();
45+
46+ for (BluetoothDevice btDevice : btAdapter .getBondedDevices ()) {
47+ if (btDevice .getName ().indexOf ("ASAP-LoRa-1" ) == 0 ) {
48+ RoundTripTimeTest .Alice = btDevice ;
49+ }
50+ if (btDevice .getName ().indexOf ("ASAP-LoRa-2" ) == 0 ) {
51+ RoundTripTimeTest .Bob = btDevice ;
52+ }
53+ }
54+ if (RoundTripTimeTest .Alice == null || RoundTripTimeTest .Bob == null )
55+ throw new IOException ("Please Pair BT Modules ASAP-LoRa-1 and ASAP-LoRa-2 to this device!" );
56+
57+ RoundTripTimeTest .AliceSocket = RoundTripTimeTest .Alice .createRfcommSocketToServiceRecord (UUID .fromString ("00001101-0000-1000-8000-00805F9B34FB" ));
58+ RoundTripTimeTest .BobSocket = RoundTripTimeTest .Bob .createRfcommSocketToServiceRecord (UUID .fromString ("00001101-0000-1000-8000-00805F9B34FB" ));
59+
60+ RoundTripTimeTest .AliceSocket .connect ();
61+ RoundTripTimeTest .BobSocket .connect ();
62+
63+ Thread .sleep (2000 ); //Give the BT Modules some time to stabilize
64+ }
65+
66+ @ AfterClass
67+ public static void teardown () throws InterruptedException , IOException {
68+ RoundTripTimeTest .AliceSocket .close ();
69+ RoundTripTimeTest .BobSocket .close ();
70+ Thread .sleep (2000 ); //Give the BT Modules some time to stabilize
71+ }
72+ @ Test
73+ public void usesAppContext () {
74+ // Test if we are running in App Context
75+ Context appContext = InstrumentationRegistry .getInstrumentation ().getTargetContext ();
76+ assertEquals ("net.sharksystem.asap.example" , appContext .getPackageName ());
77+ }
78+
79+ @ Test (timeout = 30000 )
80+ public void checkLongestMessageTime () throws IOException {
81+ this .AliceSocket .getOutputStream ().write (("MSSGE:1001:" +LONGEST_MESSAGE +"\n " ).getBytes ());
82+ this .AliceSocket .getOutputStream ().flush ();
83+
84+ while (true ) {
85+ if (this .BobSocket .getInputStream ().available () > 0 ) {
86+ BufferedReader br = new BufferedReader (new InputStreamReader (this .BobSocket .getInputStream ()));
87+ String deviceResponse = br .readLine ().trim ();
88+ System .out .print ("ASAP LoRaEngine Test Device Response: " );
89+ System .out .println (deviceResponse );
90+ assertEquals (("MSSGE:1000:" +LONGEST_MESSAGE ), deviceResponse );
91+ break ;
92+ }
93+ }
94+ }
95+
96+ @ Test (timeout = 380000 )
97+ public void simultaneousLongMessageTest () throws IOException {
98+ this .BobSocket .getOutputStream ().write (("MSSGE:1000:" +LONGEST_MESSAGE +"\n " ).getBytes ());
99+ this .AliceSocket .getOutputStream ().write (("MSSGE:1001:" +LONGEST_MESSAGE +"\n " ).getBytes ());
100+ this .BobSocket .getOutputStream ().flush ();
101+ this .AliceSocket .getOutputStream ().flush ();
102+
103+ while (true ) {
104+ if (this .BobSocket .getInputStream ().available () > 0 ) {
105+ BufferedReader br = new BufferedReader (new InputStreamReader (this .BobSocket .getInputStream ()));
106+ String deviceResponse = br .readLine ().trim ();
107+ System .out .print ("ASAP LoRaEngine Test Device Response: " );
108+ System .out .println (deviceResponse );
109+ assertEquals ("MSSGE:1000:" +LONGEST_MESSAGE , deviceResponse );
110+ break ;
111+ }
112+ }
113+
114+ while (true ) {
115+ if (this .AliceSocket .getInputStream ().available () > 0 ) {
116+ BufferedReader br = new BufferedReader (new InputStreamReader (this .AliceSocket .getInputStream ()));
117+ String deviceResponse = br .readLine ().trim ();
118+ System .out .print ("ASAP LoRaEngine Test Device Response: " );
119+ System .out .println (deviceResponse );
120+ assertEquals ("MSSGE:1001:" +LONGEST_MESSAGE , deviceResponse );
121+ break ;
122+ }
123+ }
124+ }
125+
126+ @ Test (timeout = 380000 )
127+ public void simultaneousLongMessageTest2 () throws IOException {
128+ simultaneousLongMessageTest ();
129+ }
130+ @ Test (timeout = 380000 )
131+ public void simultaneousLongMessageTest3 () throws IOException {
132+ simultaneousLongMessageTest ();
133+ }
134+ @ Test (timeout = 380000 )
135+ public void simultaneousLongMessageTest4 () throws IOException {
136+ simultaneousLongMessageTest ();
137+ }
138+ @ Test (timeout = 380000 )
139+ public void simultaneousLongMessageTest5 () throws IOException {
140+ simultaneousLongMessageTest ();
141+ }
142+
143+ }
0 commit comments