Skip to content

Commit b25321c

Browse files
committed
Unlink Module and UserData for testability.
Improve tests. Refatoring.
1 parent 9552354 commit b25321c

File tree

51 files changed

+651
-467
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+651
-467
lines changed

app/src/androidTest/java/tranquvis/simplesmsremote/CommandManagement/Commands/CommandGrantPhoneRemotelyTest.java

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,23 @@
33
import static tranquvis.simplesmsremote.CommandManagement.Commands.CommandGrantPhoneRemotely.PARAM_MODULE_NAMES;
44
import static tranquvis.simplesmsremote.CommandManagement.Commands.CommandGrantPhoneRemotely.PARAM_PASSWORD;
55

6+
import org.junit.Assert;
67
import org.junit.Before;
78
import org.junit.Test;
89

9-
import java.lang.reflect.Constructor;
10-
import java.util.ArrayList;
11-
12-
import tranquvis.simplesmsremote.CommandManagement.Modules.Module;
13-
import tranquvis.simplesmsremote.CommandManagement.Modules.ModuleGrantPhoneRemotely;
10+
import tranquvis.simplesmsremote.CommandManagement.Modules.Instances;
1411
import tranquvis.simplesmsremote.Data.DataManager;
1512
import tranquvis.simplesmsremote.Data.GrantModuleSettingsData;
1613
import tranquvis.simplesmsremote.Data.ModuleUserData;
17-
import tranquvis.simplesmsremote.Data.PhoneAllowlistModuleUserData;
18-
import tranquvis.simplesmsremote.Data.UserData;
19-
import tranquvis.simplesmsremote.Utils.UnitTestUtils;
14+
import tranquvis.simplesmsremote.TestDataManager;
2015

2116
public class CommandGrantPhoneRemotelyTest extends CommandTest {
2217
private static final String password = "pwd";
2318

2419
@Override
2520
@Before
2621
public void setUp() throws Exception {
27-
Module module = new ModuleGrantPhoneRemotely();
28-
GrantModuleSettingsData settings = new GrantModuleSettingsData(password);
29-
ModuleUserData userData = new ModuleUserData(module.getId(), settings);
30-
DataManager.getUserData().setControlModule(userData);
31-
command = new CommandGrantPhoneRemotely(module);
32-
22+
command = Instances.GRANT_PHONE_REMOTELY.commandGrantPhoneRemotely;
3323
super.setUp();
3424
}
3525

@@ -46,8 +36,58 @@ public void testPattern() throws Exception {
4636

4737
@Override
4838
@Test
49-
public void testExecution() throws Exception {
50-
assertThat("grant pwd all").matches().executes();
51-
assertThat("grant pwd audio wifi-hotspot").matches().executes();
39+
public void testExecution() throws Exception { }
40+
41+
@Test
42+
public void testExecutionGrantAll() throws Exception {
43+
String phone = "123";
44+
45+
TestDataManager dataManager = new TestDataManager();
46+
dataManager.enableModule(command.getModule(), new GrantModuleSettingsData(password));
47+
dataManager.enableModule(Instances.AUDIO);
48+
dataManager.enableModule(Instances.WIFI_HOTSPOT);
49+
dataManager.enableModule(Instances.BATTERY);
50+
51+
assertThat("grant pwd all").matches().fromPhone(phone).executes(dataManager);
52+
53+
Assert.assertTrue(dataManager.isPhoneGranted(Instances.AUDIO, phone));
54+
Assert.assertTrue(dataManager.isPhoneGranted(Instances.WIFI_HOTSPOT, phone));
55+
Assert.assertTrue(dataManager.isPhoneGranted(Instances.BATTERY, phone));
56+
}
57+
58+
@Test
59+
public void testExecutionGrantSpecific() throws Exception {
60+
String phone = "123";
61+
62+
TestDataManager dataManager = new TestDataManager();
63+
dataManager.enableModule(command.getModule(), new GrantModuleSettingsData(password));
64+
dataManager.enableModule(Instances.AUDIO);
65+
dataManager.enableModule(Instances.WIFI_HOTSPOT);
66+
dataManager.enableModule(Instances.BATTERY);
67+
68+
assertThat("grant pwd audio wifi-hotspot").matches().fromPhone(phone)
69+
.executes(dataManager);
70+
71+
Assert.assertTrue(dataManager.isPhoneGranted(Instances.AUDIO, phone));
72+
Assert.assertTrue(dataManager.isPhoneGranted(Instances.WIFI_HOTSPOT, phone));
73+
Assert.assertFalse(dataManager.isPhoneGranted(Instances.BATTERY, phone));
74+
}
75+
76+
@Test
77+
public void testExecutionWrongPassword() throws Exception {
78+
String phone = "123";
79+
80+
TestDataManager dataManager = new TestDataManager();
81+
dataManager.enableModule(command.getModule(), new GrantModuleSettingsData(password));
82+
dataManager.enableModule(Instances.AUDIO);
83+
dataManager.enableModule(Instances.WIFI_HOTSPOT);
84+
dataManager.enableModule(Instances.BATTERY);
85+
86+
assertThat("grant pwd-wrong audio wifi-hotspot").matches().fromPhone(phone)
87+
.executesWithError(dataManager);
88+
89+
Assert.assertFalse(dataManager.isPhoneGranted(Instances.AUDIO, phone));
90+
Assert.assertFalse(dataManager.isPhoneGranted(Instances.WIFI_HOTSPOT, phone));
91+
Assert.assertFalse(dataManager.isPhoneGranted(Instances.BATTERY, phone));
5292
}
5393
}

app/src/androidTest/java/tranquvis/simplesmsremote/CommandManagement/Commands/CommandSetDisplayOffTimeoutTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ public class CommandSetDisplayOffTimeoutTest extends CommandTest {
1717
@Override
1818
@Test
1919
public void testPattern() throws Exception {
20+
Locale.setDefault(Locale.ENGLISH);
21+
2022
assertThat("\n set screen Off timeout to 0 S").matches()
2123
.has(PARAM_TIMEOUT_VALUE, 0d)
2224
.has(PARAM_TIMEOUT_UNIT, Unit.SECONDS);
23-
24-
assertThat(String.format(Locale.ENGLISH, "set display off timeout to %,.4fms", 10000d)).matches()
25+
assertThat(String.format("set display off timeout to %,.4fms", 10000d)).matches()
2526
.has(PARAM_TIMEOUT_VALUE, 10000d)
2627
.has(PARAM_TIMEOUT_UNIT, Unit.MILLISECONDS);
2728
assertThat("set display off timeout to 10s").matches()
2829
.has(PARAM_TIMEOUT_VALUE, 10d)
2930
.has(PARAM_TIMEOUT_UNIT, Unit.SECONDS);
30-
3131
assertThat(format("set display off timeout to %.4f min", 21.4d)).matches()
3232
.has(PARAM_TIMEOUT_VALUE, 21.4d)
3333
.has(PARAM_TIMEOUT_UNIT, Unit.MINUTES);

app/src/androidTest/java/tranquvis/simplesmsremote/CommandManagement/Commands/CommandTakePictureTest.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
import org.junit.Test;
55

66
import tranquvis.simplesmsremote.CommandManagement.Modules.Instances;
7+
import tranquvis.simplesmsremote.Data.CameraModuleSettingsData;
78
import tranquvis.simplesmsremote.Data.CaptureSettings;
9+
import tranquvis.simplesmsremote.Data.AppDataManager;
810
import tranquvis.simplesmsremote.Data.DataManager;
11+
import tranquvis.simplesmsremote.Data.ModuleUserData;
12+
import tranquvis.simplesmsremote.TestDataManager;
913
import tranquvis.simplesmsremote.Utils.Device.CameraUtils;
1014

1115
import static tranquvis.simplesmsremote.CommandManagement.Commands.CommandTakePicture.PARAM_OPTIONS_WRAPPER;
@@ -23,7 +27,6 @@ public class CommandTakePictureTest extends CommandTest {
2327
public void setUp() throws Exception {
2428
command = Instances.CAMERA.commandTakePicture;
2529
super.setUp();
26-
DataManager.LoadUserData(appContext);
2730
}
2831

2932
@Override
@@ -93,16 +96,26 @@ public void testPattern() throws Exception {
9396
@Override
9497
@Test
9598
public void testExecution() throws Exception {
96-
assertThat("take picture").matches().executes();
97-
assertThat("take picture with camera 1, autofocus, flash").matches().executes();
98-
assertThat("take picture with front, no autofocus, no flash").matches().executes();
99+
DataManager dm = newDataManager();
100+
assertThat("take picture").matches().executes(dm);
101+
assertThat("take picture with camera 1, autofocus, flash").matches().executes(dm);
102+
assertThat("take picture with front, no autofocus, no flash").matches().executes(dm);
99103
}
100104

101105
public void testCustomExecutionWith(String options) throws Exception {
102106
String input = "take picture";
103107
if (options != null)
104108
input += " with " + options;
105109

106-
assertThat(input).matches().executes();
110+
DataManager dm = newDataManager();
111+
assertThat(input).matches().executes(dm);
112+
}
113+
114+
private TestDataManager newDataManager() throws Exception {
115+
TestDataManager dataManager = new TestDataManager();
116+
dataManager.enableModule(command.getModule(),
117+
CameraModuleSettingsData.CreateDefaultSettings(
118+
CameraUtils.GetAllCameras(appContext)));
119+
return dataManager;
107120
}
108121
}

app/src/androidTest/java/tranquvis/simplesmsremote/CommandManagement/Commands/CommandTest.java

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@
99

1010
import java.lang.reflect.Constructor;
1111
import java.util.Locale;
12-
import java.util.Objects;
1312

1413
import tranquvis.simplesmsremote.AppContextTest;
1514
import tranquvis.simplesmsremote.CommandManagement.CommandExecResult;
1615
import tranquvis.simplesmsremote.CommandManagement.CommandInstance;
1716
import tranquvis.simplesmsremote.CommandManagement.Modules.Module;
1817
import tranquvis.simplesmsremote.CommandManagement.Params.CommandParam;
18+
import tranquvis.simplesmsremote.Data.DataManager;
19+
import tranquvis.simplesmsremote.TestDataManager;
1920
import tranquvis.simplesmsremote.Utils.Regex.MatcherTreeNode;
2021
import tranquvis.simplesmsremote.Utils.StringUtils;
2122
import tranquvis.simplesmsremote.Utils.UnitTestUtils;
2223

2324
import static org.junit.Assert.assertEquals;
2425
import static org.junit.Assert.assertFalse;
26+
import static org.junit.Assert.assertNotNull;
2527
import static org.junit.Assert.assertTrue;
2628

2729
/**
@@ -82,15 +84,22 @@ public static class CommandTester {
8284
private String sendingPhone;
8385
private CommandInstance ci;
8486

85-
private CommandTester(String input, Command command, String sendingPhone, Context context)
86-
throws Exception {
87+
private CommandTester(String input, Command command, String sendingPhone, Context context) {
8788
this.input = input;
8889
ci = CommandInstance.CreateFromCommand(input);
8990
this.command = command;
9091
this.sendingPhone = sendingPhone;
9192
this.context = context;
9293
}
9394

95+
/**
96+
* Set the sendingPhone, which is passed to Command.execute .
97+
*/
98+
public CommandTester fromPhone(String sendingPhone) {
99+
this.sendingPhone = sendingPhone;
100+
return this;
101+
}
102+
94103
/**
95104
* Assert that the input matches a specific command.
96105
*
@@ -161,8 +170,8 @@ public CommandTester doesNotMatch() throws Exception {
161170
*
162171
* @param param the parameter to check
163172
*/
164-
public CommandTester has(CommandParam param) throws Exception {
165-
assertTrue(ci.getParam(param) != null);
173+
public <T> CommandTester has(CommandParam<T> param) throws Exception {
174+
assertNotNull(ci.getParam(param));
166175
return this;
167176
}
168177

@@ -172,7 +181,7 @@ public CommandTester has(CommandParam param) throws Exception {
172181
*
173182
* @param param the parameter to check
174183
*/
175-
public CommandTester hasNot(CommandParam param) throws Exception {
184+
public <T> CommandTester hasNot(CommandParam<T> param) throws Exception {
176185
assertFalse(ci.isParamAssigned(param));
177186
return this;
178187
}
@@ -195,14 +204,13 @@ public <T> CommandTester has(CommandParam<T> param, T value) throws Exception {
195204
* Assert that a command executes successful with the given input.
196205
* Assert before that the input matches the command.
197206
*
198-
* @param command the command
199207
* @return the result of the execution
200208
* @throws Exception
201209
*/
202-
public CommandExecResult executes(Command command) throws Exception {
210+
public CommandExecResult executes(Command command, DataManager dataManager) throws Exception {
203211
CommandExecResult result = new CommandExecResult(ci);
204212
result.setSuccess(true);
205-
command.execute(context, ci, sendingPhone, result);
213+
command.execute(context, ci, sendingPhone, result, dataManager);
206214
String assertionMsg = String.format(
207215
"Expect that command execution result is success (customResultMessage=\"%s\")",
208216
result.getCustomResultMessage()
@@ -211,6 +219,18 @@ public CommandExecResult executes(Command command) throws Exception {
211219
return result;
212220
}
213221

222+
/**
223+
* Assert that the default command of the related unit test
224+
* executes successful with the given input.
225+
* Assert before that the input matches the default command.
226+
*
227+
* @return the result of the execution
228+
* @throws Exception
229+
*/
230+
public CommandExecResult executes(DataManager dataManager) throws Exception {
231+
return executes(command, dataManager);
232+
}
233+
214234
/**
215235
* Assert that the default command of the related unit test
216236
* executes successful with the given input.
@@ -220,7 +240,7 @@ public CommandExecResult executes(Command command) throws Exception {
220240
* @throws Exception
221241
*/
222242
public CommandExecResult executes() throws Exception {
223-
return executes(command);
243+
return executes(command, getDataManager());
224244
}
225245

226246
/**
@@ -229,13 +249,12 @@ public CommandExecResult executes() throws Exception {
229249
*
230250
* @param command the command
231251
* @return the result of the execution
232-
* @throws Exception
233252
*/
234-
public CommandExecResult executesWithError(Command command) throws Exception {
253+
public CommandExecResult executesWithError(Command command, DataManager dataManager) {
235254
CommandExecResult result = new CommandExecResult(ci);
236255
result.setSuccess(false);
237256
try {
238-
command.execute(context, ci, result);
257+
command.execute(context, ci, sendingPhone, result, dataManager);
239258
} catch (Exception ex) {
240259
return result;
241260
}
@@ -251,8 +270,24 @@ public CommandExecResult executesWithError(Command command) throws Exception {
251270
* @return the result of the execution
252271
* @throws Exception
253272
*/
254-
public CommandExecResult executesWithError() throws Exception {
255-
return executesWithError(command);
273+
public CommandExecResult executesWithError(DataManager dataManager) {
274+
return executesWithError(command, dataManager);
275+
}
276+
277+
/**
278+
* Assert that the default command of the related unit test
279+
* executes with error with the given input.
280+
* Assert before that the input matches the default command.
281+
*
282+
* @return the result of the execution
283+
* @throws Exception
284+
*/
285+
public CommandExecResult executesWithError() {
286+
return executesWithError(command, getDataManager());
287+
}
288+
289+
public DataManager getDataManager() {
290+
return new TestDataManager();
256291
}
257292
}
258293
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
package tranquvis.simplesmsremote.CommandManagement.Modules;
22

3+
import tranquvis.simplesmsremote.CommandManagement.Commands.CommandGrantPhoneRemotely;
4+
import tranquvis.simplesmsremote.CommandManagement.Commands.CommandGrantPhoneRemotelyTest;
5+
36
public class ModuleGrantPhoneRemotelyTest extends ModuleTest {
7+
@Override
8+
public void testCommands() throws Exception {
9+
super.testCommands();
10+
CommandGrantPhoneRemotelyTest commandTest =
11+
getUnitTestFrom(getCommand(CommandGrantPhoneRemotely.class));
12+
commandTest.testExecutionGrantSpecific();
13+
commandTest.testExecutionWrongPassword();
14+
commandTest.testExecutionGrantAll();
15+
}
416
}

app/src/androidTest/java/tranquvis/simplesmsremote/CommandManagement/Modules/ModuleTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
* Commands are tested automatically in all inherited classes.
2424
*/
2525
public abstract class ModuleTest extends AppContextTest {
26-
private static List<String> successfulModuleTests = new ArrayList<>();
27-
private static List<String> failedModuleTests = new ArrayList<>();
26+
private static final List<String> successfulModuleTests = new ArrayList<>();
27+
private static final List<String> failedModuleTests = new ArrayList<>();
2828
protected Module module;
2929
protected List<Command> commands;
3030
protected boolean assertCompatibility = true;

app/src/androidTest/java/tranquvis/simplesmsremote/MyNotificationManagerTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ public void testNotifySmsCommandsReceived() throws Exception {
4040
smsCommandMessage.addCommandInstance(ci1);
4141
smsCommandMessage.addCommandInstance(ci2);
4242

43-
CommandExecResult result1 = ci1.executeCommand(appContext, smsCommandMessage);
44-
CommandExecResult result2 = ci2.executeCommand(appContext, smsCommandMessage);
43+
TestDataManager dataManager = new TestDataManager();
44+
45+
CommandExecResult result1 = ci1.executeCommand(appContext, smsCommandMessage, dataManager);
46+
CommandExecResult result2 = ci2.executeCommand(appContext, smsCommandMessage, dataManager);
4547

4648
List<CommandExecResult> executionResults = new ArrayList<>();
4749
executionResults.add(result1);

0 commit comments

Comments
 (0)