src/test/java/de/uapcore/sudoku/MainMenuTest.java

Tue, 28 Jul 2020 14:05:04 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Jul 2020 14:05:04 +0200
changeset 21
11cb2d9413d4
parent 20
a95423d37588
permissions
-rw-r--r--

DocumentHandlerTest tests if modified state is reset

package de.uapcore.sudoku;

import org.junit.jupiter.api.Test;

import javax.swing.*;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class MainMenuTest {

    private final ActionHandler actionHandler = new ActionHandler(new Field());

    private void testListeners(JMenuItem m) {
        final var listeners = m.getActionListeners();
        assertEquals(1, listeners.length);
        assertEquals(actionHandler, listeners[0]);
    }

    @Test
    void testStructure() {
        final var menu = new MainMenu(actionHandler);

        assertEquals(2, menu.getMenuCount());

        final var datei = menu.getMenu(0);
        assertEquals('D', datei.getMnemonic());
        assertEquals(9, datei.getItemCount());
        {
            final var item = datei.getItem(0);
            assertEquals("Neu", item.getText());
            assertEquals('N', item.getMnemonic());
            assertEquals(1, item.getRegisteredKeyStrokes().length);
            assertEquals(KeyStroke.getKeyStroke("control N"), item.getRegisteredKeyStrokes()[0]);
            testListeners(item);
        }
        {
            final var item = datei.getItem(1);
            assertEquals("Öffnen", item.getText());
            assertEquals('F', item.getMnemonic());
            assertEquals(1, item.getRegisteredKeyStrokes().length);
            assertEquals(KeyStroke.getKeyStroke("control O"), item.getRegisteredKeyStrokes()[0]);
            testListeners(item);
        }
        {
            final var item = datei.getItem(2);
            assertEquals("Speichern", item.getText());
            assertEquals('S', item.getMnemonic());
            assertEquals(1, item.getRegisteredKeyStrokes().length);
            assertEquals(KeyStroke.getKeyStroke("control S"), item.getRegisteredKeyStrokes()[0]);
            testListeners(item);
        }
        {
            final var item = datei.getItem(3);
            assertEquals("Speichern als...", item.getText());
            assertEquals('A', item.getMnemonic());
            testListeners(item);
        }

        assertNull(datei.getItem(4));
        {
            final var item = datei.getItem(5);
            assertEquals("Prüfen", item.getText());
            assertEquals('P', item.getMnemonic());
            assertEquals(1, item.getRegisteredKeyStrokes().length);
            assertEquals(KeyStroke.getKeyStroke("control P"), item.getRegisteredKeyStrokes()[0]);
            testListeners(item);
        }
        {
            final var item = datei.getItem(6);
            assertEquals("Lösen", item.getText());
            assertEquals('L', item.getMnemonic());
            assertEquals(1, item.getRegisteredKeyStrokes().length);
            assertEquals(KeyStroke.getKeyStroke("control L"), item.getRegisteredKeyStrokes()[0]);
            testListeners(item);
        }
        assertNull(datei.getItem(7));
        {
            final var item = datei.getItem(8);
            assertEquals("Beenden", item.getText());
            assertEquals('E', item.getMnemonic());
            testListeners(item);
        }

        final var info = menu.getMenu(1);
        assertEquals("Info", info.getText());
        assertEquals('I', info.getMnemonic());

        assertEquals(1, info.getItemCount());
        final var about = info.getItem(0);
        assertEquals("Über...", about.getText());
        assertEquals('B', about.getMnemonic());
        assertEquals(1, about.getRegisteredKeyStrokes().length);
        assertEquals(KeyStroke.getKeyStroke("F1"), about.getRegisteredKeyStrokes()[0]);
        testListeners(about);
    }
}

mercurial