Skip to content

Commit b575b6a

Browse files
committed
Communication tests
1 parent bdc95d4 commit b575b6a

File tree

3 files changed

+240
-75
lines changed

3 files changed

+240
-75
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package net.sharksystem.asap.android.LoRaEngine;
2+
3+
4+
import android.content.Context;
5+
import android.support.test.InstrumentationRegistry;
6+
import android.support.test.runner.AndroidJUnit4;
7+
8+
import org.junit.FixMethodOrder;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.junit.runners.MethodSorters;
12+
13+
import java.nio.charset.StandardCharsets;
14+
import java.util.Base64;
15+
16+
import static org.junit.Assert.assertArrayEquals;
17+
import static org.junit.Assert.assertEquals;
18+
19+
/**
20+
* Instrumented test, which will execute on an Android device.
21+
*
22+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
23+
*/
24+
@RunWith(AndroidJUnit4.class)
25+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
26+
public class Base64Test {
27+
28+
@Test
29+
public void usesAppContext() {
30+
// Test if we are running in App Context
31+
Context appContext = InstrumentationRegistry.getTargetContext();
32+
assertEquals("net.sharksystem.asap.example", appContext.getPackageName());
33+
}
34+
35+
@Test
36+
public void charsetEncodeDecode() {
37+
byte[] message = "Testmessage123!&".getBytes();
38+
String base64message = new String(Base64.getEncoder().encode(message), StandardCharsets.UTF_8);
39+
byte[] decodedMessage = Base64.getMimeDecoder().decode(base64message.getBytes(StandardCharsets.UTF_8));
40+
assertArrayEquals(message, decodedMessage);
41+
assertEquals("Testmessage123!&", new String(decodedMessage));
42+
}
43+
44+
@Test
45+
public void simpleEncodeDecode() {
46+
byte[] message = "Testmessage123!&".getBytes();
47+
String base64message = Base64.getEncoder().encodeToString(message);
48+
assertEquals("VGVzdG1lc3NhZ2UxMjMhJg==", base64message);
49+
byte[] decodedMessage = Base64.getMimeDecoder().decode(base64message);
50+
assertArrayEquals(message, decodedMessage);
51+
assertEquals("Testmessage123!&", new String(decodedMessage));
52+
}
53+
}

app/src/androidTest/java/net/sharksystem/asap/android/LoRaEngine/BasicCommunicationTest.java

Lines changed: 18 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static void setup() throws IOException, InterruptedException {
6060
BasicCommunicationTest.AliceSocket.connect();
6161
BasicCommunicationTest.BobSocket.connect();
6262

63-
Thread.sleep(1000); //Give the BT Modules some time to stabilize
63+
Thread.sleep(2000); //Give the BT Modules some time to stabilize
6464
}
6565

6666
@Test
@@ -70,33 +70,25 @@ public void usesAppContext() {
7070
assertEquals("net.sharksystem.asap.example", appContext.getPackageName());
7171
}
7272

73-
@Test(timeout=20000)
73+
@Test(timeout = 30000)
7474
public void deviceDiscoveryTest() throws IOException {
7575
this.AliceSocket.getOutputStream().write("DSCVR\n".getBytes());
7676

77-
while(true){
78-
if(this.BobSocket.getInputStream().available() > 0) {
77+
while (true) {
78+
if (this.BobSocket.getInputStream().available() > 0) {
7979
BufferedReader br = new BufferedReader(new InputStreamReader(this.BobSocket.getInputStream()));
80-
StringBuilder sb = new StringBuilder(this.BobSocket.getInputStream().available());
81-
do {
82-
sb.append(br.readLine()).append("\n");
83-
} while(br.ready());
84-
String deviceResponse = sb.toString().trim();
80+
String deviceResponse = br.readLine().trim();
8581
System.out.print("ASAP LoRaEngine Test Device Response: ");
8682
System.out.println(deviceResponse);
8783
assertEquals("DVDCR:1000", deviceResponse);
8884
break;
8985
}
9086
}
9187

92-
while(true){
93-
if(this.AliceSocket.getInputStream().available() > 0) {
88+
while (true) {
89+
if (this.AliceSocket.getInputStream().available() > 0) {
9490
BufferedReader br = new BufferedReader(new InputStreamReader(this.AliceSocket.getInputStream()));
95-
StringBuilder sb = new StringBuilder(this.AliceSocket.getInputStream().available());
96-
do {
97-
sb.append(br.readLine()).append("\n");
98-
} while(br.ready());
99-
String deviceResponse = sb.toString().trim();
91+
String deviceResponse = br.readLine().trim();
10092
System.out.print("ASAP LoRaEngine Test Device Response: ");
10193
System.out.println(deviceResponse);
10294
assertEquals("DVDCR:1001", deviceResponse);
@@ -105,82 +97,33 @@ public void deviceDiscoveryTest() throws IOException {
10597
}
10698
}
10799

108-
@Test(timeout=10000)
100+
@Test(timeout = 30000)
109101
public void simpleAliceToBobMessageTest() throws IOException {
110102
this.AliceSocket.getOutputStream().write("MSSGE@1001:Hello World!\n".getBytes());
111103

112-
while(true){
113-
if(this.BobSocket.getInputStream().available() > 0) {
104+
while (true) {
105+
if (this.BobSocket.getInputStream().available() > 0) {
114106
BufferedReader br = new BufferedReader(new InputStreamReader(this.BobSocket.getInputStream()));
115-
StringBuilder sb = new StringBuilder(this.BobSocket.getInputStream().available());
116-
do {
117-
sb.append(br.readLine()).append("\n");
118-
} while(br.ready());
119-
String deviceResponse = sb.toString().trim();
107+
String deviceResponse = br.readLine().trim();
120108
System.out.print("ASAP LoRaEngine Test Device Response: ");
121109
System.out.println(deviceResponse);
122-
assertEquals("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"1000\",\"message\":\"Hello World!\"}", deviceResponse);
110+
assertEquals("MSSGE@1000:Hello World!", deviceResponse);
123111
break;
124112
}
125113
}
126114
}
127115

128-
@Test(timeout=10000)
116+
@Test(timeout = 30000)
129117
public void simpleBobToAliceMessageTest() throws IOException {
130118
this.BobSocket.getOutputStream().write("MSSGE@1000:Hello World!".getBytes());
131119

132-
while(true){
133-
if(this.AliceSocket.getInputStream().available() > 0) {
120+
while (true) {
121+
if (this.AliceSocket.getInputStream().available() > 0) {
134122
BufferedReader br = new BufferedReader(new InputStreamReader(this.AliceSocket.getInputStream()));
135-
StringBuilder sb = new StringBuilder(this.AliceSocket.getInputStream().available());
136-
do {
137-
sb.append(br.readLine()).append("\n");
138-
} while(br.ready());
139-
String deviceResponse = sb.toString().trim();
123+
String deviceResponse = br.readLine().trim();
140124
System.out.print("ASAP LoRaEngine Test Device Response: ");
141125
System.out.println(deviceResponse);
142-
assertEquals("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"1001\",\"message\":\"Hello World!\"}", deviceResponse);
143-
break;
144-
}
145-
}
146-
}
147-
148-
@Test(timeout=20000)
149-
public void simultaneousMessageTest() throws IOException {
150-
this.BobSocket.getOutputStream().write("MSSGE@1000:Hello World!\n".getBytes());
151-
try {
152-
Thread.sleep(500);
153-
} catch (InterruptedException e) {
154-
e.printStackTrace();
155-
}
156-
this.AliceSocket.getOutputStream().write("MSSGE@1001:Hello World!\n".getBytes());
157-
158-
while(true){
159-
if(this.BobSocket.getInputStream().available() > 0) {
160-
BufferedReader br = new BufferedReader(new InputStreamReader(this.BobSocket.getInputStream()));
161-
StringBuilder sb = new StringBuilder(this.BobSocket.getInputStream().available());
162-
do {
163-
sb.append(br.readLine()).append("\n");
164-
} while(br.ready());
165-
String deviceResponse = sb.toString().trim();
166-
System.out.print("ASAP LoRaEngine Test Device Response: ");
167-
System.out.println(deviceResponse);
168-
assertEquals("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"1000\",\"message\":\"Hello World!\"}", deviceResponse);
169-
break;
170-
}
171-
}
172-
173-
while(true){
174-
if(this.AliceSocket.getInputStream().available() > 0) {
175-
BufferedReader br = new BufferedReader(new InputStreamReader(this.AliceSocket.getInputStream()));
176-
StringBuilder sb = new StringBuilder(this.AliceSocket.getInputStream().available());
177-
do {
178-
sb.append(br.readLine()).append("\n");
179-
} while(br.ready());
180-
String deviceResponse = sb.toString().trim();
181-
System.out.print("ASAP LoRaEngine Test Device Response: ");
182-
System.out.println(deviceResponse);
183-
assertEquals("{\"COMMAND\":\".ASAPLoRaMessage\",\"address\":\"1001\",\"message\":\"Hello World!\"}", deviceResponse);
126+
assertEquals("MSSGE@1001:Hello World!", deviceResponse);
184127
break;
185128
}
186129
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package net.sharksystem.asap.android.LoRaEngine;
2+
3+
import android.bluetooth.BluetoothAdapter;
4+
import android.bluetooth.BluetoothDevice;
5+
import android.bluetooth.BluetoothSocket;
6+
import android.content.Context;
7+
import android.support.test.InstrumentationRegistry;
8+
import android.support.test.runner.AndroidJUnit4;
9+
10+
import org.junit.BeforeClass;
11+
import org.junit.FixMethodOrder;
12+
import org.junit.Test;
13+
import org.junit.runner.RunWith;
14+
import org.junit.runners.MethodSorters;
15+
16+
import java.io.BufferedReader;
17+
import java.io.IOException;
18+
import java.io.InputStreamReader;
19+
import java.util.UUID;
20+
21+
import static org.junit.Assert.assertEquals;
22+
23+
/**
24+
* Instrumented test, which will execute on an Android device.
25+
*
26+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
27+
*/
28+
@RunWith(AndroidJUnit4.class)
29+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
30+
public class SimultaneousCommunicationTest {
31+
32+
public static BluetoothDevice Alice;
33+
public static BluetoothDevice Bob;
34+
public static BluetoothSocket AliceSocket;
35+
public static BluetoothSocket BobSocket;
36+
37+
@BeforeClass
38+
public static void setup() throws IOException, InterruptedException {
39+
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
40+
btAdapter.cancelDiscovery();
41+
42+
for (BluetoothDevice btDevice : btAdapter.getBondedDevices()) {
43+
if (btDevice.getName().indexOf("ASAP-LoRa-1") == 0) {
44+
SimultaneousCommunicationTest.Alice = btDevice;
45+
}
46+
if (btDevice.getName().indexOf("ASAP-LoRa-2") == 0) {
47+
SimultaneousCommunicationTest.Bob = btDevice;
48+
}
49+
}
50+
if (SimultaneousCommunicationTest.Alice == null || SimultaneousCommunicationTest.Bob == null)
51+
throw new IOException("Please Pair BT Modules ASAP-LoRa-1 and ASAP-LoRa-2 to this device!");
52+
53+
SimultaneousCommunicationTest.AliceSocket = SimultaneousCommunicationTest.Alice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
54+
SimultaneousCommunicationTest.BobSocket = SimultaneousCommunicationTest.Bob.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
55+
56+
SimultaneousCommunicationTest.AliceSocket.connect();
57+
SimultaneousCommunicationTest.BobSocket.connect();
58+
59+
Thread.sleep(2000); //Give the BT Modules some time to stabilize
60+
}
61+
62+
@Test
63+
public void usesAppContext() {
64+
// Test if we are running in App Context
65+
Context appContext = InstrumentationRegistry.getTargetContext();
66+
assertEquals("net.sharksystem.asap.example", appContext.getPackageName());
67+
}
68+
69+
@Test(timeout = 60000)
70+
public void simultaneousMessageTest() throws IOException {
71+
this.BobSocket.getOutputStream().write("MSSGE@1000:Simultan Hello Alice!\n".getBytes());
72+
this.AliceSocket.getOutputStream().write("MSSGE@1001:Simultan Hello Bob!\n".getBytes());
73+
74+
while (true) {
75+
if (this.BobSocket.getInputStream().available() > 0) {
76+
BufferedReader br = new BufferedReader(new InputStreamReader(this.BobSocket.getInputStream()));
77+
String deviceResponse = br.readLine().trim();
78+
System.out.print("ASAP LoRaEngine Test Device Response: ");
79+
System.out.println(deviceResponse);
80+
assertEquals("MSSGE@1000:Simultan Hello Bob!", deviceResponse);
81+
break;
82+
}
83+
}
84+
85+
while (true) {
86+
if (this.AliceSocket.getInputStream().available() > 0) {
87+
BufferedReader br = new BufferedReader(new InputStreamReader(this.AliceSocket.getInputStream()));
88+
String deviceResponse = br.readLine().trim();
89+
System.out.print("ASAP LoRaEngine Test Device Response: ");
90+
System.out.println(deviceResponse);
91+
assertEquals("MSSGE@1001:Simultan Hello Alice!", deviceResponse);
92+
break;
93+
}
94+
}
95+
}
96+
/*
97+
@Test(timeout = 240000)
98+
public void tenSimultaneousMessageTest() throws IOException {
99+
int rounds = 10;
100+
for (int i = 0; i < rounds; i++) {
101+
this.BobSocket.getOutputStream().write("MSSGE@1000:Simultan Hello Alice!\n".getBytes());
102+
this.AliceSocket.getOutputStream().write("MSSGE@1001:Simultan Hello Bob!\n".getBytes());
103+
}
104+
105+
int AliceCounter = 0;
106+
int BobCounter = 0;
107+
while (true) {
108+
if (this.AliceSocket.getInputStream().available() > 0) {
109+
BufferedReader br = new BufferedReader(new InputStreamReader(this.AliceSocket.getInputStream()));
110+
String deviceResponse = br.readLine().trim();
111+
System.out.print("ASAP LoRaEngine Test Device Response: ");
112+
System.out.println(deviceResponse);
113+
assertEquals("MSSGE@1001:Simultan Hello Alice!", deviceResponse);
114+
AliceCounter++;
115+
System.out.print("Found that much correct answers from Bob: ");
116+
System.out.println(AliceCounter);
117+
}
118+
if (this.BobSocket.getInputStream().available() > 0) {
119+
BufferedReader br = new BufferedReader(new InputStreamReader(this.BobSocket.getInputStream()));
120+
String deviceResponse = br.readLine().trim();
121+
System.out.print("ASAP LoRaEngine Test Device Response: ");
122+
System.out.println(deviceResponse);
123+
assertEquals("MSSGE@1000:Simultan Hello Bob!", deviceResponse);
124+
BobCounter++;
125+
System.out.print("Found that much correct answers from Alice: ");
126+
System.out.println(BobCounter);
127+
}
128+
if (AliceCounter == rounds && BobCounter == rounds) break;
129+
}
130+
assertEquals(rounds, AliceCounter);
131+
assertEquals(rounds, BobCounter);
132+
}
133+
*/
134+
@Test(timeout = 240000)
135+
public void tenSimultaneousOrderedMessageTest() throws IOException {
136+
int rounds = 10;
137+
for (int i = 0; i < rounds; i++) {
138+
this.BobSocket.getOutputStream().write(("MSSGE@1000:Hello Alice Nr. " + String.valueOf(i) + "\n").getBytes());
139+
this.AliceSocket.getOutputStream().write(("MSSGE@1001:Hello Bob Nr. " + String.valueOf(i) + "\n").getBytes());
140+
}
141+
142+
int AliceCounter = 0;
143+
int BobCounter = 0;
144+
while (AliceCounter < rounds || BobCounter < rounds) {
145+
if (this.AliceSocket.getInputStream().available() > 0) {
146+
BufferedReader br = new BufferedReader(new InputStreamReader(this.AliceSocket.getInputStream()));
147+
String deviceResponse = br.readLine().trim();
148+
System.out.print("ASAP LoRaEngine Test Device Response: ");
149+
System.out.println(deviceResponse);
150+
assertEquals("MSSGE@1001:Hello Alice Nr. " + String.valueOf(AliceCounter), deviceResponse);
151+
AliceCounter++;
152+
System.out.print("Found that much correct answers from Bob: ");
153+
System.out.println(AliceCounter);
154+
}
155+
if (this.BobSocket.getInputStream().available() > 0) {
156+
BufferedReader br = new BufferedReader(new InputStreamReader(this.BobSocket.getInputStream()));
157+
String deviceResponse = br.readLine().trim();
158+
System.out.print("ASAP LoRaEngine Test Device Response: ");
159+
System.out.println(deviceResponse);
160+
assertEquals("MSSGE@1000:Hello Bob Nr. " + String.valueOf(BobCounter), deviceResponse);
161+
BobCounter++;
162+
System.out.print("Found that much correct answers from Alice: ");
163+
System.out.println(BobCounter);
164+
}
165+
}
166+
assertEquals(rounds, AliceCounter);
167+
assertEquals(rounds, BobCounter);
168+
}
169+
}

0 commit comments

Comments
 (0)