Search in sources :

Example 1 with InputDeviceKeyListener

use of com.b3dgs.lionengine.InputDeviceKeyListener in project lionengine by b3dgs.

the class KeyboardAwtListenerTest method testKeyboardListener.

/**
 * Test key listener.
 */
@Test
public void testKeyboardListener() {
    final AtomicBoolean reachedPressed = new AtomicBoolean(false);
    final AtomicBoolean reachedReleased = new AtomicBoolean(false);
    final KeyboardAwtListener listener = new KeyboardAwtListener(new InputDeviceKeyListener() {

        @Override
        public void keyPressed(int keyCode, char keyChar) {
            reachedPressed.set(true);
        }

        @Override
        public void keyReleased(int keyCode, char keyChar) {
            reachedReleased.set(true);
        }
    });
    listener.keyTyped(KeyboardAwtTest.createEvent(KeyboardAwt.UP));
    listener.keyPressed(KeyboardAwtTest.createEvent(KeyboardAwt.UP));
    listener.keyReleased(KeyboardAwtTest.createEvent(KeyboardAwt.UP));
    Assert.assertTrue(reachedPressed.get());
    Assert.assertTrue(reachedReleased.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InputDeviceKeyListener(com.b3dgs.lionengine.InputDeviceKeyListener) Test(org.junit.Test)

Example 2 with InputDeviceKeyListener

use of com.b3dgs.lionengine.InputDeviceKeyListener in project lionengine by b3dgs.

the class KeyboardAwtListenerTest method testKeyboardListener.

/**
 * Test key listener.
 */
@Test
void testKeyboardListener() {
    final AtomicBoolean reachedPressed = new AtomicBoolean(false);
    final AtomicBoolean reachedReleased = new AtomicBoolean(false);
    final KeyboardAwtListener listener = new KeyboardAwtListener(new InputDeviceKeyListener() {

        @Override
        public void keyPressed(int keyCode, char keyChar) {
            reachedPressed.set(true);
        }

        @Override
        public void keyReleased(int keyCode, char keyChar) {
            reachedReleased.set(true);
        }
    });
    listener.keyTyped(KeyboardAwtTest.createEvent(KeyboardAwt.UP));
    listener.keyPressed(KeyboardAwtTest.createEvent(KeyboardAwt.UP));
    listener.keyReleased(KeyboardAwtTest.createEvent(KeyboardAwt.UP));
    assertTrue(reachedPressed.get());
    assertTrue(reachedReleased.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InputDeviceKeyListener(com.b3dgs.lionengine.InputDeviceKeyListener) Test(org.junit.jupiter.api.Test)

Example 3 with InputDeviceKeyListener

use of com.b3dgs.lionengine.InputDeviceKeyListener in project lionengine by b3dgs.

the class ScreenAwtTest method testScreen.

/**
 * Test screen.
 *
 * @param config The config to test with.
 */
private void testScreen(Config config) {
    final Screen screen = Graphics.createScreen(config);
    final InputDeviceKeyListener listener = new InputDeviceKeyListener() {

        @Override
        public void keyReleased(int keyCode, char keyChar) {
        // Mock
        }

        @Override
        public void keyPressed(int keyCode, char keyChar) {
        // Mock
        }
    };
    final AtomicBoolean lost = new AtomicBoolean();
    final AtomicBoolean gained = new AtomicBoolean();
    final AtomicBoolean closed = new AtomicBoolean();
    final ScreenListener screenListener = new ScreenListener() {

        @Override
        public void notifyFocusLost() {
            lost.set(true);
        }

        @Override
        public void notifyFocusGained() {
            gained.set(true);
        }

        @Override
        public void notifyClosed() {
            closed.set(true);
        }
    };
    Assert.assertFalse(screen.isReady());
    screen.addListener(screenListener);
    screen.start();
    screen.awaitReady();
    screen.addKeyListener(listener);
    screen.preUpdate();
    screen.update();
    screen.showCursor();
    screen.hideCursor();
    screen.requestFocus();
    screen.onSourceChanged(UtilTests.RESOLUTION_320_240);
    Assert.assertNotNull(screen.getConfig());
    Assert.assertNotNull(screen.getGraphic());
    Assert.assertTrue(screen.getReadyTimeOut() > -1L);
    Assert.assertTrue(screen.getX() > -1);
    Assert.assertTrue(screen.getY() > -1);
    Assert.assertTrue(screen.isReady());
    while (config.isWindowed() && !gained.get()) {
        continue;
    }
    screen.setIcon("void");
    screen.setIcon("image.png");
    if (!config.hasApplet()) {
        final javax.swing.JFrame frame = (javax.swing.JFrame) UtilReflection.getField(screen, "frame");
        frame.dispatchEvent(new java.awt.event.WindowEvent(frame, java.awt.event.WindowEvent.WINDOW_CLOSING));
        while (config.isWindowed() && !gained.get()) {
            continue;
        }
    }
    screen.dispose();
    Assert.assertEquals(0, screen.getX());
    Assert.assertEquals(0, screen.getY());
    screen.removeListener(screenListener);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScreenListener(com.b3dgs.lionengine.graphic.ScreenListener) Screen(com.b3dgs.lionengine.graphic.Screen) InputDeviceKeyListener(com.b3dgs.lionengine.InputDeviceKeyListener)

Example 4 with InputDeviceKeyListener

use of com.b3dgs.lionengine.InputDeviceKeyListener in project lionengine by b3dgs.

the class ScreenAwtTest method testScreen.

/**
 * Test screen.
 *
 * @param config The config to test with.
 */
private void testScreen(Config config) {
    final Screen screen = Graphics.createScreen(config);
    final InputDeviceKeyListener listener = new InputDeviceKeyListener() {

        @Override
        public void keyReleased(int keyCode, char keyChar) {
        // Mock
        }

        @Override
        public void keyPressed(int keyCode, char keyChar) {
        // Mock
        }
    };
    final AtomicBoolean lost = new AtomicBoolean();
    final AtomicBoolean gained = new AtomicBoolean();
    final AtomicBoolean closed = new AtomicBoolean();
    final ScreenListener screenListener = new ScreenListener() {

        @Override
        public void notifyFocusLost() {
            lost.set(true);
        }

        @Override
        public void notifyFocusGained() {
            gained.set(true);
        }

        @Override
        public void notifyClosed() {
            closed.set(true);
        }
    };
    assertFalse(screen.isReady());
    screen.addListener(screenListener);
    screen.start();
    screen.awaitReady();
    screen.addKeyListener(listener);
    screen.preUpdate();
    screen.update();
    screen.showCursor();
    screen.hideCursor();
    screen.requestFocus();
    screen.onSourceChanged(UtilTests.RESOLUTION_320_240);
    assertNotNull(screen.getConfig());
    assertNotNull(screen.getGraphic());
    assertTrue(screen.getReadyTimeOut() > -1L);
    assertTrue(screen.getX() > -1);
    assertTrue(screen.getY() > -1);
    assertTrue(screen.isReady());
    assertTimeout(10_000L, () -> {
        while (config.isWindowed() && !gained.get()) {
            continue;
        }
    });
    assertThrows(() -> screen.setIcons(Arrays.asList(Medias.create("void"))), "[void] Cannot open the media !");
    screen.setIcons(Arrays.asList(Medias.create("image.png")));
    final javax.swing.JFrame frame = (javax.swing.JFrame) UtilReflection.getField(screen, "frame");
    frame.dispatchEvent(new java.awt.event.WindowEvent(frame, java.awt.event.WindowEvent.WINDOW_CLOSING));
    assertTimeout(10_000L, () -> {
        while (config.isWindowed() && !gained.get()) {
            continue;
        }
    });
    screen.dispose();
    Verbose.info("*********************************** EXPECTED VERBOSE ***********************************");
    assertEquals(0, screen.getX());
    assertEquals(0, screen.getY());
    Verbose.info("****************************************************************************************");
    screen.removeListener(screenListener);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScreenListener(com.b3dgs.lionengine.graphic.ScreenListener) Screen(com.b3dgs.lionengine.graphic.Screen) InputDeviceKeyListener(com.b3dgs.lionengine.InputDeviceKeyListener)

Example 5 with InputDeviceKeyListener

use of com.b3dgs.lionengine.InputDeviceKeyListener in project lionengine by b3dgs.

the class ScreenHeadlessTest method testScreen.

/**
 * Test screen.
 *
 * @param config The config to test with.
 */
private void testScreen(Config config) {
    final Screen screen = Graphics.createScreen(config);
    final InputDeviceKeyListener listener = new InputDeviceKeyListener() {

        @Override
        public void keyReleased(int keyCode, char keyChar) {
        // Mock
        }

        @Override
        public void keyPressed(int keyCode, char keyChar) {
        // Mock
        }
    };
    final AtomicBoolean lost = new AtomicBoolean();
    final AtomicBoolean gained = new AtomicBoolean();
    final AtomicBoolean closed = new AtomicBoolean();
    final ScreenListener screenListener = new ScreenListener() {

        @Override
        public void notifyFocusLost() {
            lost.set(true);
        }

        @Override
        public void notifyFocusGained() {
            gained.set(true);
        }

        @Override
        public void notifyClosed() {
            closed.set(true);
        }
    };
    assertFalse(screen.isReady());
    screen.addListener(screenListener);
    screen.start();
    screen.awaitReady();
    screen.addKeyListener(listener);
    screen.preUpdate();
    screen.update();
    screen.showCursor();
    screen.hideCursor();
    screen.requestFocus();
    screen.onSourceChanged(UtilTests.RESOLUTION_320_240);
    assertNotNull(screen.getConfig());
    assertNotNull(screen.getGraphic());
    assertTrue(screen.getReadyTimeOut() > -1L);
    assertTrue(screen.getX() > -1);
    assertTrue(screen.getY() > -1);
    assertTrue(screen.isReady());
    while (config.isWindowed() && !gained.get()) {
        continue;
    }
    screen.setIcons(Arrays.asList(Medias.create("void")));
    screen.setIcons(Arrays.asList(Medias.create("image.png")));
    screen.dispose();
    assertEquals(0, screen.getX());
    assertEquals(0, screen.getY());
    screen.removeListener(screenListener);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScreenListener(com.b3dgs.lionengine.graphic.ScreenListener) Screen(com.b3dgs.lionengine.graphic.Screen) InputDeviceKeyListener(com.b3dgs.lionengine.InputDeviceKeyListener)

Aggregations

InputDeviceKeyListener (com.b3dgs.lionengine.InputDeviceKeyListener)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Screen (com.b3dgs.lionengine.graphic.Screen)3 ScreenListener (com.b3dgs.lionengine.graphic.ScreenListener)3 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1