View Javadoc
1   /*
2    * Copyright © 2016 Greg Chabala
3    *
4    * This file is part of brick-control-lab.
5    *
6    * brick-control-lab is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   *
11   * brick-control-lab is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with brick-control-lab.  If not, see http://www.gnu.org/licenses/.
18   */
19  package org.chabala.brick.controllab;
20  
21  import org.chabala.brick.controllab.sensor.SensorListener;
22  import org.junit.After;
23  import org.junit.Before;
24  import org.junit.Rule;
25  import org.junit.Test;
26  import org.junit.rules.ExpectedException;
27  import org.mockito.Mock;
28  import org.mockito.junit.MockitoJUnit;
29  import org.mockito.junit.MockitoRule;
30  
31  import static org.hamcrest.Matchers.*;
32  import static org.junit.Assert.*;
33  import static org.mockito.Mockito.*;
34  
35  /**
36   * Testing {@link Input}.
37   */
38  public class InputTest {
39  
40      @Rule
41      public MockitoRule rule = MockitoJUnit.rule();
42  
43      @Rule
44      public ExpectedException thrown = ExpectedException.none();
45  
46      @Mock
47      private InputManager inputManager;
48  
49      @Mock
50      private SensorListener listener;
51  
52      private InputId inputId;
53  
54      private Input input;
55  
56      private RandomEnum randomEnum = new RandomEnum();
57  
58      @Before
59      public void setUp() {
60          inputId = randomEnum.get(InputId.class);
61          input = new Input(inputManager, inputId);
62      }
63  
64      @After
65      public void tearDown() {
66          input = null;
67          inputId = null;
68      }
69  
70      @Test
71      public void testAddListener() {
72          input.addListener(listener);
73          verify(inputManager, times(1)).addSensorListener(inputId, listener);
74      }
75  
76      @Test
77      public void testRemoveListener() {
78          input.removeListener(listener);
79          verify(inputManager, times(1)).removeSensorListener(inputId, listener);
80      }
81  
82      @Test
83      public void testToString() {
84          assertThat(input + "", containsString("inputId=" + inputId.name()));
85      }
86  }