/unittest/machineTests/MachineLoaderTest.java

https://github.com/adamlwatson/ReplicatorG · Java · 275 lines · 166 code · 65 blank · 44 comment · 12 complexity · 2d3d55286f9857953f90f22d30c5906f MD5 · raw file

  1. package machineTests;
  2. import static org.junit.Assert.*;
  3. import java.util.concurrent.LinkedBlockingQueue;
  4. import java.util.concurrent.TimeUnit;
  5. import org.junit.After;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import replicatorg.app.Base;
  9. import replicatorg.machine.MachineListener;
  10. import replicatorg.machine.MachineLoader;
  11. import replicatorg.machine.MachineProgressEvent;
  12. import replicatorg.machine.MachineState;
  13. import replicatorg.machine.MachineStateChangeEvent;
  14. import replicatorg.machine.MachineToolStatusEvent;
  15. public class MachineLoaderTest {
  16. // Simple class to collect Machine events so they can be read back later.
  17. public class TestMachineListener implements MachineListener {
  18. // We have to receive these kinds of messages.
  19. // TODO: Make this abstract.
  20. LinkedBlockingQueue<MachineStateChangeEvent> machineStateChangeEventQueue;
  21. // LinkedBlockingQueue<MachineProgressEvent> machineProgressEventQueue;
  22. // LinkedBlockingQueue<MachineToolStatusEvent> machineToolStatusEventQueue;
  23. TestMachineListener() {
  24. machineStateChangeEventQueue = new LinkedBlockingQueue<MachineStateChangeEvent>();
  25. }
  26. @Override
  27. public void machineStateChanged(MachineStateChangeEvent evt) {
  28. machineStateChangeEventQueue.add(evt);
  29. }
  30. @Override
  31. public void machineProgress(MachineProgressEvent event) {
  32. // TODO Auto-generated method stub
  33. }
  34. @Override
  35. public void toolStatusChanged(MachineToolStatusEvent event) {
  36. // TODO Auto-generated method stub
  37. }
  38. synchronized MachineStateChangeEvent getMachineStateChangedEvent(int timeout) {
  39. try {
  40. return machineStateChangeEventQueue.poll(timeout, TimeUnit.MILLISECONDS) ;
  41. } catch (InterruptedException e) {
  42. }
  43. return null;
  44. }
  45. }
  46. MachineLoader loader;
  47. TestMachineListener listener;
  48. /** Amount of time we should wait for a serial message to complete **/
  49. final int commTimeout = 5000;
  50. @Before
  51. public void setUp() throws Exception {
  52. loader = new MachineLoader();
  53. listener = new TestMachineListener();
  54. loader.addMachineListener(listener);
  55. }
  56. @After
  57. public void tearDown() throws Exception {
  58. loader.disconnect();
  59. loader = null;
  60. }
  61. @Test
  62. public void testGetMachine() {
  63. // Without initializing the machine loader, this should return null.
  64. assertNull(loader.getMachine());
  65. }
  66. @Test
  67. public void testIsLoaded() {
  68. // Without initializing the machine loader, this should return false.
  69. assertFalse(loader.isLoaded());
  70. }
  71. @Test
  72. public void testIsConnected() {
  73. // Without initializing the machine loader, this should return false.
  74. assertFalse(loader.isConnected());
  75. }
  76. @Test
  77. public void testGetDriver() {
  78. // Without initializing the machine loader, this should return null.
  79. assertNull(loader.getDriver());
  80. }
  81. @Test
  82. public void testLoad() {
  83. // Try loading a garbage machine
  84. assertFalse(loader.load("fake machine"));
  85. // Now try loading a known good machine
  86. assertTrue(loader.load("Cupcake Basic"));
  87. // Test that the isLoaded() function actually works
  88. assertTrue(loader.isLoaded());
  89. // Now, try loading another garbage machine on top of the known good one.
  90. assertFalse(loader.load("fake machine"));
  91. // Finally, load the good one again to make sure it can recover.
  92. assertTrue(loader.load("Cupcake Basic"));
  93. }
  94. @Test
  95. public void testUnload() {
  96. // See if we can bring up and then dispose of a machine
  97. assertTrue(loader.load("Cupcake Basic"));
  98. loader.unload();
  99. assertFalse(loader.isLoaded());
  100. }
  101. @Test
  102. public void testConnectionInvalidPort() {
  103. MachineStateChangeEvent event;
  104. // Try connecting to a machine with a garbage port.
  105. // This should give us a couple of messages: CONNECTING ERROR
  106. // Make sure that we get them both back in order, within a reasonable time.
  107. // Finally, unload the machine to receive NOT_ATTACHED.
  108. assertTrue(loader.load("Cupcake Basic"));
  109. loader.connect("");
  110. event = listener.getMachineStateChangedEvent(commTimeout);
  111. assertNotNull(event);
  112. Base.logger.info(event.getState().getState().toString());
  113. assertTrue(event.getState().getState() == MachineState.State.CONNECTING);
  114. event = listener.getMachineStateChangedEvent(commTimeout);
  115. assertNotNull(event);
  116. assertTrue(event.getState().getState() == MachineState.State.NOT_ATTACHED);
  117. // Finally, make sure that the query interface agrees we aren't connected
  118. assertFalse(loader.isConnected());
  119. // Unload the machine,
  120. loader.unload();
  121. // Make sure that it actually unloaded
  122. assertFalse(loader.isLoaded());
  123. // And make sure we don't get any more errors from it.
  124. event = listener.getMachineStateChangedEvent(commTimeout);
  125. assertNull(event);
  126. }
  127. // TODO: This requires a Thing-O-Matic on a specific serial port.
  128. // We don't have any mocks :-(
  129. @Test
  130. public void testConnectionKnownGoodPort() {
  131. MachineStateChangeEvent event;
  132. // Try connecting to a machine with a known good port.
  133. // This should give us a couple of messages: CONNECTING READY
  134. // Make sure that we get them both back in order, within a reasonable time.
  135. // Finally, unload the machine to receive NOT_ATTACHED.
  136. for(int i = 0; i < 1; i++) {
  137. Base.logger.info("i = " + i );
  138. assertTrue(loader.load("Thingomatic w/ HBP and Stepstruder MK6"));
  139. loader.connect("/dev/ttyUSB0");
  140. event = listener.getMachineStateChangedEvent(commTimeout);
  141. assertNotNull(event);
  142. Base.logger.info(event.getState().getState().toString());
  143. assertTrue(event.getState().getState() == MachineState.State.CONNECTING);
  144. event = listener.getMachineStateChangedEvent(commTimeout);
  145. assertNotNull(event);
  146. assertTrue(event.getState().getState() == MachineState.State.READY);
  147. // Finally, make sure that the query interface agrees we aren't connected
  148. assertTrue(loader.isConnected());
  149. // Unload the machine,
  150. loader.unload();
  151. event = listener.getMachineStateChangedEvent(commTimeout);
  152. assertNotNull(event);
  153. assertTrue(event.getState().getState() == MachineState.State.NOT_ATTACHED);
  154. }
  155. }
  156. // TODO: This requires a Thing-O-Matic on a specific serial port.
  157. // We don't have any mocks :-(
  158. @Test
  159. public void testDisconnect() {
  160. MachineStateChangeEvent event;
  161. assertTrue(loader.load("Thingomatic w/ HBP and Stepstruder MK6"));
  162. loader.connect("/dev/ttyUSB0");
  163. event = listener.getMachineStateChangedEvent(commTimeout);
  164. assertNotNull(event);
  165. Base.logger.info(event.getState().getState().toString());
  166. assertTrue(event.getState().getState() == MachineState.State.CONNECTING);
  167. event = listener.getMachineStateChangedEvent(commTimeout);
  168. assertNotNull(event);
  169. assertTrue(event.getState().getState() == MachineState.State.READY);
  170. // Finally, make sure that the query interface agrees we aren't connected
  171. assertTrue(loader.isConnected());
  172. // Now, disconnect the machine.
  173. loader.disconnect();
  174. event = listener.getMachineStateChangedEvent(commTimeout);
  175. assertNotNull(event);
  176. assertTrue(event.getState().getState() == MachineState.State.NOT_ATTACHED);
  177. assertFalse(loader.isConnected());
  178. // Unload the machine. Since we're not attached, there shouldn't be another message.
  179. loader.unload();
  180. event = listener.getMachineStateChangedEvent(500);
  181. assertNull(event);
  182. }
  183. // TODO: This requires a Thing-O-Matic on a specific serial port.
  184. // We don't have any mocks :-(
  185. @Test
  186. public void testUserDisconnect() {
  187. MachineStateChangeEvent event;
  188. assertTrue(loader.load("Thingomatic w/ HBP and Stepstruder MK6"));
  189. loader.connect("/dev/ttyUSB0");
  190. event = listener.getMachineStateChangedEvent(commTimeout);
  191. assertNotNull(event);
  192. Base.logger.info(event.getState().getState().toString());
  193. assertTrue(event.getState().getState() == MachineState.State.CONNECTING);
  194. event = listener.getMachineStateChangedEvent(commTimeout);
  195. assertNotNull(event);
  196. assertTrue(event.getState().getState() == MachineState.State.READY);
  197. // Finally, make sure that the query interface agrees we aren't connected
  198. assertTrue(loader.isConnected());
  199. Base.logger.severe("Unplug the USB port!");
  200. // Now, wait for the user to disconnect the machine
  201. event = listener.getMachineStateChangedEvent(commTimeout);
  202. assertNotNull(event);
  203. assertTrue(event.getState().getState() == MachineState.State.NOT_ATTACHED);
  204. assertFalse(loader.isConnected());
  205. // Unload the machine. Since we're not attached, there shouldn't be another message.
  206. loader.unload();
  207. event = listener.getMachineStateChangedEvent(500);
  208. assertNull(event);
  209. }
  210. }