11package com .rugged .application .hestia ;
22
33import android .content .Context ;
4- import android .provider .Settings ;
54import android .support .test .InstrumentationRegistry ;
65import android .support .test .runner .AndroidJUnit4 ;
76import android .util .Log ;
1110import org .junit .BeforeClass ;
1211import org .junit .Test ;
1312import org .junit .runner .RunWith ;
14-
1513import java .util .ArrayList ;
16-
14+ import hestia . UI . DeviceListFragment ;
1715import hestia .backend .Activator ;
1816import hestia .backend .ActivatorState ;
1917import hestia .backend .BackendInteractor ;
2018import hestia .backend .Device ;
21-
19+ import hestia . backend . DevicesChangeListener ;
2220import static org .junit .Assert .*;
2321
2422@ RunWith (AndroidJUnit4 .class )
2523public class BackendInteractorTest {
26- private static final int TEST_DEVICE_ID = 0 ;
27- private static final int TEST_ACTIVATOR_ID = 0 ;
24+ private static final String TEST_DEVICE_ID = "1" ;
25+
26+ private static final String TEST_ACTIVATOR_ID = "0" ;
2827 private String TAG = "ClientInteractionTest" ;
2928 private static BackendInteractor backendInteractor ;
3029
3130 @ BeforeClass
3231 public static void runBeforeTests (){
3332 backendInteractor = BackendInteractor .getInstance ();
3433 ActivatorState <Boolean > testState = new ActivatorState <Boolean >(false ,"TOGGLE" );
35- Activator testButton = new Activator (0 ,testState ,"testButton" );
34+ Activator testButton = new Activator ("0" , 0 ,testState ,"testButton" );
3635 ArrayList <Activator > arr = new ArrayList <>();
3736 arr .add (testButton );
38- Device testDevice = new Device (0 ,"testDevice" , "testing" ,arr );
37+ Device testDevice = new Device ("0" ,"testDevice" , "testing" ,arr );
3938 backendInteractor .addDevice (testDevice );
39+
4040 }
4141
4242 @ Before
4343 public void addTestDevice (){
4444 ActivatorState <Boolean > testState = new ActivatorState <Boolean >(false ,"TOGGLE" );
45- Activator testButton = new Activator (TEST_ACTIVATOR_ID ,testState ,"testButton" );
45+
46+ Activator testButton = new Activator (TEST_ACTIVATOR_ID ,0 ,testState ,"testButton" );
4647 ArrayList <Activator > arr = new ArrayList <>();
4748 arr .add (testButton );
4849 Device testDevice = new Device (TEST_DEVICE_ID ,"testDevice" , "testing" ,arr );
@@ -51,18 +52,17 @@ public void addTestDevice(){
5152
5253 @ After
5354 public void removeTestDevice (){
54- backendInteractor .deleteTestDevice (TEST_DEVICE_ID );
55+ backendInteractor .deleteTestDevice (Integer . parseInt ( TEST_DEVICE_ID ) );
5556 }
5657
57-
5858 @ Test
59- public void testPackageName (){
59+ public void packageNameTest (){
6060 Context appContext = InstrumentationRegistry .getTargetContext ();
6161 assertEquals ("com.rugged.application.hestia" , appContext .getPackageName ());
6262 }
6363
6464 @ Test
65- public void testGetDevices (){
65+ public void getDevicesTest (){
6666 StringBuilder sb = new StringBuilder ();
6767 for (Device d : backendInteractor .getDevices ()){
6868 sb .append (d .toString ());
@@ -73,26 +73,78 @@ public void testGetDevices(){
7373 /*
7474 * Test of the singleton reference, two different references should refer to the same object.
7575 */
76+
7677 @ Test
77- public void testSingleton (){
78+ public void singletonTest (){
7879 BackendInteractor copyOfInteractor = BackendInteractor .getInstance ();
7980 assertEquals (backendInteractor ,copyOfInteractor );
8081 }
8182
83+
84+ @ Test
85+ public void ipTest (){
86+ String testIp = "192.168.0.1" ;
87+ backendInteractor .setIp (testIp );
88+ assertEquals (testIp ,backendInteractor .getIp ());
89+ }
90+
8291 @ Test
83- public void testSetActivatorState (){
92+ public void setActivatorStateTest (){
8493 ArrayList <Device > testDeviceList = backendInteractor .getDevices ();
85- Device testDevice = testDeviceList .get (TEST_DEVICE_ID );
86- ActivatorState state = testDevice .getActivator ( TEST_ACTIVATOR_ID ).getState ();
94+ Device testDevice = testDeviceList .get (Integer . parseInt ( TEST_DEVICE_ID ) );
95+ ActivatorState state = testDevice .getActivators (). get ( Integer . parseInt ( TEST_ACTIVATOR_ID ) ).getState ();
8796 boolean testState = (boolean )state .getRawState ();
8897 assertEquals (testState ,false );
89- ActivatorState newState = new ActivatorState (true ,"TOGGLE" );
90- backendInteractor .setActivatorState (TEST_DEVICE_ID ,TEST_ACTIVATOR_ID ,newState );
91- testDeviceList = backendInteractor .getDevices ();
92- testDevice = testDeviceList .get (TEST_DEVICE_ID );
93- assertEquals (testDevice .getActivator (TEST_ACTIVATOR_ID ).getState ().getRawState (),true );
98+ state .setRawState (true );
99+
100+ backendInteractor .setActivatorState (testDevice ,testDevice .getActivators ().get (Integer .parseInt (TEST_ACTIVATOR_ID )),state );
101+
102+ Activator activator = backendInteractor .getDevices ().get (Integer .parseInt (TEST_DEVICE_ID )).getActivators ().get (Integer .parseInt (TEST_ACTIVATOR_ID ));
103+ assertEquals (true ,activator .getState ().getRawState ());
94104 }
95105
106+ @ Test
107+ public void deleteDeviceTest (){
108+ Device temp = backendInteractor .getDevices ().get (Integer .parseInt (TEST_DEVICE_ID ));
109+
110+ // Removing a device
111+ backendInteractor .deleteDevice (temp );
112+ backendInteractor .clearDevices ();
113+
114+ // The list should be empty, updating should leave it empty
115+ backendInteractor .updateDevices ();
116+ assertTrue (backendInteractor .getDevices ().isEmpty ());
96117
118+ // Now adding a device, trying the same device twice
119+ backendInteractor .addDevice (temp );
120+ backendInteractor .addDevice (temp );
121+ assertEquals (2 ,backendInteractor .getDevices ().size ());
97122
123+ }
124+
125+ @ Test
126+ public void setDevicesTest (){
127+ Device temp = backendInteractor .getDevices ().get (Integer .parseInt (TEST_DEVICE_ID ));
128+ ArrayList <Device > newDevices = new ArrayList <>();
129+ // Adding the same device three times
130+ newDevices .add (temp );
131+ newDevices .add (temp );
132+ newDevices .add (temp );
133+
134+ backendInteractor .setDevices (newDevices );
135+ assertEquals (3 ,backendInteractor .getDevices ().size ());
136+ }
137+
138+ @ Test
139+ public void listenerTest (){
140+ DevicesChangeListener l = new DeviceListFragment ();
141+
142+ // Testing adding a listener
143+ backendInteractor .addDevicesChangeListener (l );
144+ assertEquals (l ,backendInteractor .getListeners ().get (0 ));
145+
146+ // Testing removing a listener
147+ backendInteractor .removeDevicesChangeListener (l );
148+ assertEquals (0 ,backendInteractor .getListeners ().size ());
149+ }
98150}
0 commit comments