package pl.mkrystek.mkbot.pl.mkrystek.mkbot;

import static org.mockito.Mockito.inOrder;
import static java.awt.event.KeyEvent.*;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import java.awt.*;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class KeyboardTest {

    @Mock
    private Robot robot;

    @InjectMocks
    private Keyboard keyboard;

    InOrder order;

    @Before
    public void setUp() {
        order = inOrder(robot);
    }

    @Test
    public void shouldPressProperKeys() {
        //given
        String sampleMessage = "AbC";

        //when
        keyboard.type(sampleMessage);

        //then
        verifySequencePressed(VK_SHIFT, VK_A);
        verifySequencePressed(VK_B);
        verifySequencePressed(VK_SHIFT, VK_C);
        verifyNoMoreInteractions(robot);
    }

    private void verifySequencePressed(int... sequence) {
        verifySequence(sequence);
    }

    private void verifySequence(int... keyCodes) {
        verifySequence(keyCodes, 0, keyCodes.length);
    }

    private void verifySequence(int[] keyCodes, int offset, int length) {
        if (length == 0) {
            return;
        }

        order.verify(robot).keyPress(keyCodes[offset]);
        verifySequence(keyCodes, offset + 1, length - 1);
        order.verify(robot).keyRelease(keyCodes[offset]);
    }
}
package pl.mkrystek.mkbot.pl.mkrystek.mkbot;

import static org.mockito.Mockito.inOrder;
import static java.awt.event.KeyEvent.*;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import java.awt.*;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class KeyboardTest {

    @Mock
    private Robot robot;

    @InjectMocks
    private Keyboard keyboard;

    InOrder order;

    @Before
    public void setUp() {
        order = inOrder(robot);
    }

    @Test
    public void testTypingSmallLetters() {
        //given
        String messageWithSmallLetters = "abcdefghijklmnopqrstuvwxyz";

        //when
        keyboard.type(messageWithSmallLetters);

        //then
        for (int i = VK_A; i <= VK_Z; i++) {
            verifyCharacterTyped(i);
        }
        verifyNoMoreInteractions(robot);
    }

    @Test
    public void testTypingBigLetters() {
        //given
        String messageWithBigLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        //when
        keyboard.type(messageWithBigLetters);

        //then
        for (int i = VK_A; i <= VK_Z; i++) {
            verifyCharacterTypedWithShift(i);
        }
        verifyNoMoreInteractions(robot);
    }

    @Test
    public void testTypingNumbers() {
        //given
        String messageWithNumbers = "0123456789";

        //when
        keyboard.type(messageWithNumbers);

        //then
        for (int i = VK_0; i <= VK_9; i++) {
            verifyCharacterTyped(i);
        }
        verifyNoMoreInteractions(robot);
    }

    @Test
    public void testTypingSpecialCharactersUnderNumbers() {
        //given
        String messageWithSpecialCharacters = ")!@#$%^&*(";

        //when
        keyboard.type(messageWithSpecialCharacters);

        //then
        for (int i = VK_0; i <= VK_9; i++) {
            verifyCharacterTypedWithShift(i);
        }
        verifyNoMoreInteractions(robot);
    }

    private void verifyCharacterTypedWithShift(int keyCode) {
        verifyCharacterTyped(VK_SHIFT, keyCode);
    }

    private void verifyCharacterTyped(int... keyCodes) {
        for (int keyCode : keyCodes) {
            order.verify(robot).keyPress(keyCode);
        }

        for (int i = keyCodes.length - 1; i >= 0; i--) {
            order.verify(robot).keyRelease(keyCodes[i]);
        }
    }
}
Improve keyboard test to cover more characters