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 java.util.EnumMap;
014import java.util.EnumSet;
015
016import org.junit.Assert;
017import org.junit.Test;
018
019import com.ardor3d.input.ButtonState;
020import com.ardor3d.input.ControllerState;
021import com.ardor3d.input.InputState;
022import com.ardor3d.input.Key;
023import com.ardor3d.input.KeyEvent;
024import com.ardor3d.input.KeyboardState;
025import com.ardor3d.input.MouseButton;
026import com.ardor3d.input.MouseState;
027
028public class TestStandardConditions {
029    final KeyboardState ks = new KeyboardState(EnumSet.noneOf(Key.class), KeyEvent.NOTHING);
030    final MouseState ms = new MouseState(0, 0, 0, 0, 0,
031            MouseButton.makeMap(ButtonState.UP, ButtonState.UP, ButtonState.UP), null);
032    final ControllerState cs = new ControllerState();
033    InputState is1, is2, is3, is4, is5;
034
035    KeyboardState aDown = new KeyboardState(EnumSet.of(Key.A), KeyEvent.NOTHING);
036    KeyboardState bDown = new KeyboardState(EnumSet.of(Key.B), KeyEvent.NOTHING);
037
038    EnumMap<MouseButton, ButtonState> bothUp = MouseButton.makeMap(ButtonState.UP, ButtonState.UP, ButtonState.UP);
039    EnumMap<MouseButton, ButtonState> upDown = MouseButton.makeMap(ButtonState.UP, ButtonState.DOWN, ButtonState.UP);
040    EnumMap<MouseButton, ButtonState> downUp = MouseButton.makeMap(ButtonState.DOWN, ButtonState.UP, ButtonState.UP);
041    EnumMap<MouseButton, ButtonState> bothDown = MouseButton.makeMap(ButtonState.DOWN, ButtonState.DOWN,
042            ButtonState.UP);
043
044    @Test
045    public void testKeyHeld1() throws Exception {
046        final KeyHeldCondition kh = new KeyHeldCondition(Key.A);
047
048        is1 = new InputState(ks, ms, cs);
049        is2 = new InputState(aDown, ms, cs);
050        is3 = new InputState(bDown, ms, cs);
051
052        Assert.assertFalse("not down", kh.test(new TwoInputStates(is1, is1)));
053        Assert.assertTrue("down", kh.test(new TwoInputStates(is1, is2)));
054        Assert.assertFalse("not down", kh.test(new TwoInputStates(is1, is3)));
055        Assert.assertFalse("not down", kh.test(new TwoInputStates(is2, is3)));
056        Assert.assertTrue("not down", kh.test(new TwoInputStates(is2, is2)));
057
058        Assert.assertFalse("empty1", kh.test(new TwoInputStates(InputState.EMPTY, InputState.EMPTY)));
059        Assert.assertFalse("empty2", kh.test(new TwoInputStates(is1, InputState.EMPTY)));
060        Assert.assertFalse("empty3", kh.test(new TwoInputStates(InputState.EMPTY, is1)));
061        Assert.assertTrue("empty4", kh.test(new TwoInputStates(InputState.EMPTY, is2)));
062    }
063
064    @Test(expected = NullPointerException.class)
065    public void testKeyHeld2() throws Exception {
066        new KeyHeldCondition(null);
067    }
068
069    @Test
070    public void testKeyPressed() throws Exception {
071        final KeyPressedCondition kh = new KeyPressedCondition(Key.A);
072
073        is1 = new InputState(ks, ms, cs);
074        is2 = new InputState(aDown, ms, cs);
075        is3 = new InputState(bDown, ms, cs);
076
077        Assert.assertFalse("not down", kh.test(new TwoInputStates(is1, is1)));
078        Assert.assertTrue("down", kh.test(new TwoInputStates(is1, is2)));
079        Assert.assertFalse("not down", kh.test(new TwoInputStates(is1, is3)));
080        Assert.assertFalse("not down", kh.test(new TwoInputStates(is2, is3)));
081        Assert.assertFalse("not down", kh.test(new TwoInputStates(is2, is2)));
082
083        Assert.assertFalse("empty1", kh.test(new TwoInputStates(InputState.EMPTY, InputState.EMPTY)));
084        Assert.assertFalse("empty2", kh.test(new TwoInputStates(is1, InputState.EMPTY)));
085        Assert.assertFalse("empty3", kh.test(new TwoInputStates(InputState.EMPTY, is1)));
086        Assert.assertTrue("empty4", kh.test(new TwoInputStates(InputState.EMPTY, is2)));
087    }
088
089    @Test(expected = NullPointerException.class)
090    public void testKeyPressedNull() throws Exception {
091        new KeyPressedCondition(null);
092    }
093
094    @Test
095    public void testKeyReleased() throws Exception {
096        final KeyReleasedCondition kh = new KeyReleasedCondition(Key.A);
097
098        is1 = new InputState(ks, ms, cs);
099        is2 = new InputState(aDown, ms, cs);
100        is3 = new InputState(bDown, ms, cs);
101
102        Assert.assertFalse("not down", kh.test(new TwoInputStates(is1, is1)));
103        Assert.assertFalse("not down", kh.test(new TwoInputStates(is1, is2)));
104        Assert.assertFalse("not down", kh.test(new TwoInputStates(is1, is3)));
105        Assert.assertTrue("not down", kh.test(new TwoInputStates(is2, is3)));
106        Assert.assertFalse("not down", kh.test(new TwoInputStates(is2, is2)));
107
108        Assert.assertFalse("empty1", kh.test(new TwoInputStates(InputState.EMPTY, InputState.EMPTY)));
109        Assert.assertFalse("empty2", kh.test(new TwoInputStates(is1, InputState.EMPTY)));
110        Assert.assertFalse("empty3", kh.test(new TwoInputStates(InputState.EMPTY, is1)));
111        Assert.assertFalse("empty4", kh.test(new TwoInputStates(InputState.EMPTY, is2)));
112    }
113
114    @Test(expected = NullPointerException.class)
115    public void testKeyReleasedNull() throws Exception {
116        new KeyReleasedCondition(null);
117    }
118
119    @Test
120    public void testMouseMove() throws Exception {
121        final MouseMovedCondition mm = TriggerConditions.mouseMoved();
122
123        final MouseState ms2 = new MouseState(1, 0, 1, 0, 0, bothUp, null);
124        final MouseState ms3 = new MouseState(1, 0, 0, 0, 0, bothDown, null);
125        final MouseState ms4 = new MouseState(3, 1, 2, 1, 0, bothDown, null);
126        final MouseState ms5 = new MouseState(3, 0, 0, -1, 0, bothDown, null);
127
128        is1 = new InputState(ks, ms, cs);
129        is2 = new InputState(ks, ms2, cs);
130        is3 = new InputState(ks, ms3, cs);
131        is4 = new InputState(ks, ms4, cs);
132        is5 = new InputState(ks, ms5, cs);
133
134        Assert.assertFalse("mm1", mm.test(new TwoInputStates(is1, is1)));
135        Assert.assertTrue("mm2", mm.test(new TwoInputStates(is1, is2)));
136        Assert.assertFalse("mm3", mm.test(new TwoInputStates(is2, is3)));
137        Assert.assertTrue("mm4", mm.test(new TwoInputStates(is3, is4)));
138        Assert.assertTrue("mm5", mm.test(new TwoInputStates(is4, is5)));
139        Assert.assertFalse("mm6", mm.test(new TwoInputStates(is2, is2)));
140
141        Assert.assertFalse("empty1", mm.test(new TwoInputStates(InputState.EMPTY, InputState.EMPTY)));
142        Assert.assertFalse("empty2", mm.test(new TwoInputStates(is1, InputState.EMPTY)));
143        Assert.assertFalse("empty3", mm.test(new TwoInputStates(InputState.EMPTY, is1)));
144        Assert.assertTrue("empty4", mm.test(new TwoInputStates(InputState.EMPTY, is2)));
145    }
146
147    @Test
148    public void testMouseButton1() throws Exception {
149        final MouseButtonCondition mm = TriggerConditions.leftButtonDown();
150
151        final MouseState ms2 = new MouseState(1, 0, 1, 0, 0, bothUp, null);
152        final MouseState ms3 = new MouseState(1, 0, 0, 0, 0, bothDown, null);
153        final MouseState ms4 = new MouseState(3, 1, 2, 1, 0, upDown, null);
154        final MouseState ms5 = new MouseState(3, 0, 0, -1, 0, downUp, null);
155
156        is1 = new InputState(ks, ms, cs);
157        is2 = new InputState(ks, ms2, cs);
158        is3 = new InputState(ks, ms3, cs);
159        is4 = new InputState(ks, ms4, cs);
160        is5 = new InputState(ks, ms5, cs);
161
162        Assert.assertFalse("mm1", mm.test(new TwoInputStates(is1, is1)));
163        Assert.assertFalse("mm2", mm.test(new TwoInputStates(is1, is2)));
164        Assert.assertTrue("mm3", mm.test(new TwoInputStates(is2, is3)));
165        Assert.assertFalse("mm4", mm.test(new TwoInputStates(is3, is4)));
166        Assert.assertTrue("mm5", mm.test(new TwoInputStates(is4, is5)));
167
168        Assert.assertFalse("empty1", mm.test(new TwoInputStates(InputState.EMPTY, InputState.EMPTY)));
169        Assert.assertFalse("empty2", mm.test(new TwoInputStates(is1, InputState.EMPTY)));
170        Assert.assertFalse("empty3", mm.test(new TwoInputStates(InputState.EMPTY, is1)));
171        Assert.assertTrue("empty4", mm.test(new TwoInputStates(InputState.EMPTY, is3)));
172    }
173
174    @Test
175    public void testMouseButton2() throws Exception {
176        final MouseButtonCondition mm = TriggerConditions.rightButtonDown();
177
178        final MouseState ms2 = new MouseState(1, 0, 1, 0, 0, bothUp, null);
179        final MouseState ms3 = new MouseState(1, 0, 0, 0, 0, bothDown, null);
180        final MouseState ms4 = new MouseState(3, 1, 2, 1, 0, upDown, null);
181        final MouseState ms5 = new MouseState(3, 0, 0, -1, 0, downUp, null);
182
183        is1 = new InputState(ks, ms, cs);
184        is2 = new InputState(ks, ms2, cs);
185        is3 = new InputState(ks, ms3, cs);
186        is4 = new InputState(ks, ms4, cs);
187        is5 = new InputState(ks, ms5, cs);
188
189        Assert.assertFalse("mm1", mm.test(new TwoInputStates(is1, is1)));
190        Assert.assertFalse("mm2", mm.test(new TwoInputStates(is1, is2)));
191        Assert.assertTrue("mm3", mm.test(new TwoInputStates(is2, is3)));
192        Assert.assertTrue("mm4", mm.test(new TwoInputStates(is3, is4)));
193        Assert.assertFalse("mm5", mm.test(new TwoInputStates(is4, is5)));
194
195        Assert.assertFalse("empty1", mm.test(new TwoInputStates(InputState.EMPTY, InputState.EMPTY)));
196        Assert.assertFalse("empty2", mm.test(new TwoInputStates(is1, InputState.EMPTY)));
197        Assert.assertFalse("empty3", mm.test(new TwoInputStates(InputState.EMPTY, is1)));
198        Assert.assertTrue("empty4", mm.test(new TwoInputStates(InputState.EMPTY, is3)));
199    }
200}