1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.chabala.brick.controllab;
20
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnit;
29 import org.mockito.junit.MockitoRule;
30
31 import static org.chabala.brick.controllab.Protocol.STOP_DEPRESSED;
32 import static org.chabala.brick.controllab.Protocol.STOP_RELEASED;
33 import static org.hamcrest.Matchers.*;
34 import static org.junit.Assert.assertThat;
35 import static org.junit.Assume.assumeThat;
36 import static org.mockito.Mockito.*;
37
38
39
40
41 public class StopButtonTest {
42
43 @Rule
44 public MockitoRule rule = MockitoJUnit.rule();
45
46 @Rule
47 public ExpectedException thrown = ExpectedException.none();
48
49 @Mock
50 private InputManager inputManager;
51
52 @Mock
53 private StopButtonListener stopButtonListener;
54
55 private StopButton stopButton;
56 private ByteConsumer stopButtonCallback;
57
58 @Before
59 public void setUp() {
60 ArgumentCaptor<ByteConsumer> captor = ArgumentCaptor.forClass(ByteConsumer.class);
61 stopButton = new StopButton(inputManager);
62 verify(inputManager).setStopButtonCallback(captor.capture());
63 stopButtonCallback = captor.getValue();
64 }
65
66 @After
67 public void tearDown() {
68 stopButton = null;
69 stopButtonCallback = null;
70 }
71
72 @Test
73 public void testStopButtonIsInitiallyNotStopped() {
74 assertThat(stopButton.isStopDepressed(), is(false));
75 }
76
77 @Test
78 public void testNoEventsWhenNotStoppedAndStopNotPressed() {
79 assumeThat(stopButton.isStopDepressed(), is(false));
80 stopButton.addListener(stopButtonListener);
81 stopButtonCallback.accept(STOP_RELEASED);
82 stopButton.removeListener(stopButtonListener);
83
84 assertThat(stopButton.isStopDepressed(), is(false));
85 verify(stopButtonListener, never()).stopButtonPressed(any());
86 verify(stopButtonListener, never()).stopButtonReleased(any());
87 }
88
89 @Test
90 public void testStopPressedEventWhenNotStoppedAndStopPressed() {
91 assumeThat(stopButton.isStopDepressed(), is(false));
92 stopButton.addListener(stopButtonListener);
93 stopButtonCallback.accept(STOP_DEPRESSED);
94 stopButton.removeListener(stopButtonListener);
95
96 assertThat(stopButton.isStopDepressed(), is(true));
97 ArgumentCaptor<StopButtonEvent> captor = ArgumentCaptor.forClass(StopButtonEvent.class);
98 verify(stopButtonListener, times(1)).stopButtonPressed(captor.capture());
99 StopButtonEvent stopButtonEvent = captor.getValue();
100 assertThat(stopButtonEvent.getRawValue(), is(STOP_DEPRESSED));
101 assertThat(stopButtonEvent + "", containsString("0x10"));
102 verify(stopButtonListener, never()).stopButtonReleased(any());
103 }
104
105 @Test
106 public void testNoEventsWhenStoppedAndStopNotReleased() {
107 assumeThat(stopButton.isStopDepressed(), is(false));
108 stopButtonCallback.accept(STOP_DEPRESSED);
109 assumeThat(stopButton.isStopDepressed(), is(true));
110
111 stopButton.addListener(stopButtonListener);
112 stopButtonCallback.accept(STOP_DEPRESSED);
113 stopButton.removeListener(stopButtonListener);
114
115 assertThat(stopButton.isStopDepressed(), is(true));
116 verify(stopButtonListener, never()).stopButtonPressed(any());
117 verify(stopButtonListener, never()).stopButtonReleased(any());
118 }
119
120 @Test
121 public void testStopReleasedEventWhenStoppedAndStopReleased() {
122 assumeThat(stopButton.isStopDepressed(), is(false));
123 stopButtonCallback.accept(STOP_DEPRESSED);
124 assumeThat(stopButton.isStopDepressed(), is(true));
125
126 stopButton.addListener(stopButtonListener);
127 stopButtonCallback.accept(STOP_RELEASED);
128 stopButton.removeListener(stopButtonListener);
129
130 assertThat(stopButton.isStopDepressed(), is(false));
131 verify(stopButtonListener, never()).stopButtonPressed(any());
132 ArgumentCaptor<StopButtonEvent> captor = ArgumentCaptor.forClass(StopButtonEvent.class);
133 verify(stopButtonListener, times(1)).stopButtonReleased(captor.capture());
134 StopButtonEvent stopButtonEvent = captor.getValue();
135 assertThat(stopButtonEvent.getRawValue(), is(STOP_RELEASED));
136 }
137 }