001/**
002 * Copyright (c) 2008-2014 Ardor Labs, Inc.
003 *
004 * This file is part of Ardor3D.
005 *
006 * Ardor3D is free software: you can redistribute it and/or modify it
007 * under the terms of its license which may be found in the accompanying
008 * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
009 */
010
011package com.ardor3d.input.logical;
012
013import static org.easymock.EasyMock.createMock;
014import static org.easymock.EasyMock.expect;
015import static org.easymock.EasyMock.isA;
016import static org.easymock.EasyMock.replay;
017import static org.easymock.EasyMock.verify;
018
019import java.util.EnumSet;
020import java.util.LinkedList;
021import java.util.function.Predicate;
022
023import org.junit.After;
024import org.junit.Before;
025import org.junit.Test;
026
027import com.ardor3d.framework.Canvas;
028import com.ardor3d.input.ButtonState;
029import com.ardor3d.input.ControllerState;
030import com.ardor3d.input.InputState;
031import com.ardor3d.input.Key;
032import com.ardor3d.input.KeyEvent;
033import com.ardor3d.input.KeyboardState;
034import com.ardor3d.input.MouseButton;
035import com.ardor3d.input.MouseState;
036import com.ardor3d.input.PhysicalLayer;
037
038/**
039 * Tests for the LogicalLayer
040 */
041public class TestLogicalLayer {
042    LogicalLayer ll;
043    PhysicalLayer pl;
044
045    TriggerAction ta1, ta2;
046
047    Predicate<TwoInputStates> p1;
048    Predicate<TwoInputStates> p2;
049
050    KeyboardState ks;
051    MouseState ms;
052    ControllerState cs;
053
054    Canvas canvas;
055
056    Object[] mocks;
057
058    @SuppressWarnings({ "unchecked" })
059    @Before
060    public void setup() throws Exception {
061        pl = createMock("Physicallayer", PhysicalLayer.class);
062
063        ta1 = createMock("TA1", TriggerAction.class);
064        ta2 = createMock("TA2", TriggerAction.class);
065
066        p1 = createMock("P1", Predicate.class);
067        p2 = createMock("P2", Predicate.class);
068
069        canvas = createMock("canvas", Canvas.class);
070
071        ll = new LogicalLayer();
072
073        ll.registerInput(canvas, pl);
074
075        ks = new KeyboardState(EnumSet.noneOf(Key.class), KeyEvent.NOTHING);
076        ms = new MouseState(0, 0, 0, 0, 0, MouseButton.makeMap(ButtonState.UP, ButtonState.UP, ButtonState.UP), null);
077        cs = new ControllerState();
078
079        mocks = new Object[] { pl, ta1, ta2, p1, p2, canvas };
080    }
081
082    @After
083    public void verifyMocks() throws Exception {
084        verify(mocks);
085    }
086
087    @Test
088    public void testTriggers1() throws Exception {
089        final InputState state1 = new InputState(ks, ms, cs);
090        final InputState state2 = new InputState(ks, ms, cs);
091
092        final double tpf = 14;
093
094        final LinkedList<InputState> states1 = new LinkedList<>();
095        final LinkedList<InputState> states2 = new LinkedList<>();
096
097        states1.add(state1);
098        states2.add(state2);
099
100        pl.readState();
101        pl.readState();
102        expect(pl.drainAvailableStates()).andReturn(states1);
103        expect(pl.drainAvailableStates()).andReturn(states2);
104        expect(p1.test(isA(TwoInputStates.class))).andReturn(false);
105        expect(p1.test(isA(TwoInputStates.class))).andReturn(true);
106        ta1.perform(canvas, new TwoInputStates(state1, state2), tpf);
107
108        replay(mocks);
109
110        ll.registerTrigger(new InputTrigger(p1, ta1));
111
112        ll.checkTriggers(tpf);
113        ll.checkTriggers(tpf);
114    }
115
116    @Test
117    public void testTriggers2() throws Exception {
118        final InputState state1 = new InputState(ks, ms, cs);
119        final InputState state2 = new InputState(ks, ms, cs);
120
121        final double tpf = 14;
122
123        final LinkedList<InputState> states1 = new LinkedList<>();
124        final LinkedList<InputState> states2 = new LinkedList<>();
125
126        states1.add(state1);
127        states2.add(state2);
128
129        pl.readState();
130        pl.readState();
131        expect(pl.drainAvailableStates()).andReturn(states1);
132        expect(pl.drainAvailableStates()).andReturn(states2);
133        expect(p1.test(isA(TwoInputStates.class))).andReturn(false);
134        expect(p1.test(isA(TwoInputStates.class))).andReturn(true);
135        expect(p2.test(isA(TwoInputStates.class))).andReturn(true);
136        expect(p2.test(isA(TwoInputStates.class))).andReturn(true);
137        ta1.perform(canvas, new TwoInputStates(state1, state2), tpf);
138        ta2.perform(canvas, new TwoInputStates(InputState.EMPTY, state1), tpf);
139        ta2.perform(canvas, new TwoInputStates(state1, state2), tpf);
140
141        replay(mocks);
142
143        ll.registerTrigger(new InputTrigger(p1, ta1));
144        ll.registerTrigger(new InputTrigger(p2, ta2));
145
146        ll.checkTriggers(tpf);
147        ll.checkTriggers(tpf);
148    }
149
150    @Test
151    public void testTriggers3() throws Exception {
152        final InputState state1 = new InputState(ks, ms, cs);
153        final InputState state2 = new InputState(ks, ms, cs);
154
155        final double tpf = 14;
156
157        final LinkedList<InputState> states1 = new LinkedList<>();
158        final LinkedList<InputState> states2 = new LinkedList<>();
159
160        states1.add(state1);
161        states2.add(state2);
162
163        pl.readState();
164        pl.readState();
165        expect(pl.drainAvailableStates()).andReturn(states1);
166        expect(pl.drainAvailableStates()).andReturn(states2);
167        expect(p1.test(isA(TwoInputStates.class))).andReturn(false);
168        expect(p1.test(isA(TwoInputStates.class))).andReturn(true);
169        expect(p2.test(isA(TwoInputStates.class))).andReturn(true);
170        ta1.perform(canvas, new TwoInputStates(state1, state2), tpf);
171        ta2.perform(canvas, new TwoInputStates(InputState.EMPTY, state1), tpf);
172
173        replay(mocks);
174
175        final InputTrigger trigger2 = new InputTrigger(p2, ta2);
176
177        ll.registerTrigger(new InputTrigger(p1, ta1));
178        ll.registerTrigger(trigger2);
179
180        ll.checkTriggers(tpf);
181        ll.deregisterTrigger(trigger2);
182        ll.checkTriggers(tpf);
183    }
184
185    @Test
186    public void testTriggers4() throws Exception {
187        final InputState state1 = new InputState(ks, ms, cs);
188
189        final double tpf = 14;
190
191        final LinkedList<InputState> states1 = new LinkedList<>();
192        final LinkedList<InputState> states2 = new LinkedList<>();
193
194        states1.add(state1);
195
196        pl.readState();
197        pl.readState();
198        expect(pl.drainAvailableStates()).andReturn(states1);
199        expect(pl.drainAvailableStates()).andReturn(states2);
200        expect(p1.test(isA(TwoInputStates.class))).andReturn(false);
201        expect(p1.test(isA(TwoInputStates.class))).andReturn(true);
202        expect(p2.test(isA(TwoInputStates.class))).andReturn(true);
203        expect(p2.test(isA(TwoInputStates.class))).andReturn(true);
204        ta1.perform(canvas, new TwoInputStates(state1, state1), tpf);
205        ta2.perform(canvas, new TwoInputStates(InputState.EMPTY, state1), tpf);
206        ta2.perform(canvas, new TwoInputStates(state1, state1), tpf);
207
208        replay(mocks);
209
210        final InputTrigger trigger2 = new InputTrigger(p2, ta2);
211
212        ll.registerTrigger(new InputTrigger(p1, ta1));
213        ll.registerTrigger(trigger2);
214
215        ll.checkTriggers(tpf);
216        ll.checkTriggers(tpf);
217    }
218
219    @Test
220    public void testLostFocus() throws Exception {
221        final InputState state1 = new InputState(ks, ms, cs);
222        final InputState state2 = new InputState(ks, ms, cs);
223
224        final double tpf = 14;
225
226        final LinkedList<InputState> states1 = new LinkedList<>();
227        final LinkedList<InputState> states2 = new LinkedList<>();
228
229        states1.add(state1);
230        states2.add(InputState.LOST_FOCUS);
231        states2.add(state2);
232
233        pl.readState();
234        pl.readState();
235        expect(pl.drainAvailableStates()).andReturn(states1);
236        expect(pl.drainAvailableStates()).andReturn(states2);
237        expect(p1.test(isA(TwoInputStates.class))).andReturn(true);
238        expect(p1.test(isA(TwoInputStates.class))).andReturn(false);
239        ta1.perform(canvas, new TwoInputStates(InputState.EMPTY, state1), tpf);
240
241        replay(mocks);
242
243        ll.registerTrigger(new InputTrigger(p1, ta1));
244
245        ll.checkTriggers(tpf);
246        ll.checkTriggers(tpf);
247
248    }
249}