package org.westmalle.wayland.core;

import org.freedesktop.wayland.server.Display;
import org.freedesktop.wayland.server.WlKeyboardResource;
import org.freedesktop.wayland.shared.WlKeyboardKeyState;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.westmalle.wayland.nativ.NativeFileFactory;
import org.westmalle.wayland.nativ.libc.Libc;

import java.util.HashSet;
import java.util.Set;

import static org.mockito.Mockito.mock;

@RunWith(MockitoJUnitRunner.class)
public class KeyboardDeviceTest {

    @Mock
    private Display           display;
    @Mock
    private NativeFileFactory nativeFileFactory;
    @Mock
    private Libc              libc;
    @Mock
    private Compositor        compositor;
    @InjectMocks
    private KeyboardDevice    keyboardDevice;

    @Test
    public void testKey() throws Exception {
        //given
        final WlKeyboardResource      wlKeyboardResource0 = mock(WlKeyboardResource.class);
        final WlKeyboardResource      wlKeyboardResource1 = mock(WlKeyboardResource.class);
        final Set<WlKeyboardResource> wlKeyboardResources = new HashSet<>();
        wlKeyboardResources.add(wlKeyboardResource0);
        wlKeyboardResources.add(wlKeyboardResource1);

        //TODO set keyboard focus

        final int                key                = 123;
        final WlKeyboardKeyState wlKeyboardKeyState = WlKeyboardKeyState.PRESSED;

        //when
        this.keyboardDevice.key(wlKeyboardResources,
                                key,
                                wlKeyboardKeyState);

        //then

        throw new UnsupportedOperationException();
    }

    @Test
    public void testSetFocus() throws Exception {
        throw new UnsupportedOperationException();
    }

    @Test
    public void testUpdateKeymap() throws Exception {
        throw new UnsupportedOperationException();
    }
}
@@ -30,16 +30,62 @@
 .core;%0A%0A
+import org.freedesktop.wayland.server.Client;%0A
 import o
@@ -181,16 +181,73 @@
 source;%0A
+import org.freedesktop.wayland.server.WlSurfaceResource;%0A
 import o
@@ -578,78 +578,273 @@
 til.
-HashSet;%0Aimport java.util.Set;%0A%0Aimport static org.mockito.Mockito.mock
+Collections;%0Aimport java.util.HashSet;%0Aimport java.util.Optional;%0Aimport java.util.Set;%0A%0Aimport static com.google.common.truth.Truth.assertThat;%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.verify;%0Aimport static org.mockito.Mockito.when
 ;%0A%0A@
@@ -1253,16 +1253,119 @@
 //given%0A
+        final Client client0 = mock(Client.class);%0A        final Client client1 = mock(Client.class);%0A%0A
         
@@ -1738,33 +1738,433 @@
     
-//TODO set keyboard focus
+when(wlKeyboardResource0.getClient()).thenReturn(client0);%0A        when(wlKeyboardResource1.getClient()).thenReturn(client1);%0A%0A        final WlSurfaceResource wlSurfaceResource = mock(WlSurfaceResource.class);%0A        when(wlSurfaceResource.getClient()).thenReturn(client0);%0A        this.keyboardDevice.setFocus(Collections.singleton(wlKeyboardResource0),%0A                                     Optional.of(wlSurfaceResource));
 %0A%0A  
@@ -2213,16 +2213,24 @@
         
+        
 = 123;%0A 
@@ -2279,16 +2279,24 @@
 KeyState
+Pressed 
  = WlKey
@@ -2318,16 +2318,664 @@
 RESSED;%0A
+        final WlKeyboardKeyState wlKeyboardKeyStateReleased = WlKeyboardKeyState.RELEASED;%0A%0A        final int serial0 = 1278;%0A        final int serial1 = 1279;%0A        final int serial2 = 1280;%0A        when(this.display.nextSerial()).thenReturn(serial0,%0A                                                   serial1,%0A                                                   serial2);%0A        final int time0 = 27646;%0A        final int time1 = 29253;%0A        final int time2 = 30898;%0A        when(this.compositor.getTime()).thenReturn(time0,%0A                                                   time1,%0A                                                   time2);
 %0A       
@@ -3122,16 +3122,23 @@
 KeyState
+Pressed
 );%0A%0A    
@@ -3148,25 +3148,24 @@
  //then%0A
-%0A
         
 throw ne
@@ -3160,51 +3160,819 @@
     
-throw new UnsupportedOperationException(
+assertThat((Iterable%3CInteger%3E) this.keyboardDevice.getPressedKeys()).contains(key);%0A        verify(wlKeyboardResource0).key(serial0,%0A                                        time0,%0A                                        key,%0A                                        wlKeyboardKeyStatePressed.getValue());%0A%0A        //and when%0A        this.keyboardDevice.key(wlKeyboardResources,%0A                                key,%0A                                wlKeyboardKeyStateReleased);%0A%0A        //then%0A        assertThat((Iterable%3CInteger%3E) this.keyboardDevice.getPressedKeys()).doesNotContain(key);%0A        verify(wlKeyboardResource0).key(serial1,%0A                                        time1,%0A                                        key,%0A                                        wlKeyboardKeyStateReleased.getValue()
 );%0A
+%0A
     

implement key event unit test