//Copyright 2015 Erik De Rijcke
//
//Licensed under the Apache License,Version2.0(the"License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing,software
//distributed under the License is distributed on an"AS IS"BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
package org.westmalle.wayland.core;

import org.freedesktop.wayland.server.Display;
import org.freedesktop.wayland.server.EventLoop;
import org.freedesktop.wayland.server.EventSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.LinkedList;
import java.util.List;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest(EventSource.class)
public class CompositorTest {

    @Mock
    private Display  display;
    @Mock
    private Renderer renderer;
    @Mock
    private Platform platform;

    @InjectMocks
    private Compositor compositor;

    @Test
    public void testRequestRender() throws Exception {
        //given
        final EventLoop eventLoop = mock(EventLoop.class);
        when(this.display.getEventLoop()).thenReturn(eventLoop);
        final List<EventLoop.IdleHandler> idleHandlers = new LinkedList<>();
        when(eventLoop.addIdle(any())).thenAnswer(invocation -> {
            final Object                arg0        = invocation.getArguments()[0];
            final EventLoop.IdleHandler idleHandler = (EventLoop.IdleHandler) arg0;
            idleHandlers.add(idleHandler);
            return mock(EventSource.class);
        });

        //when
        this.compositor.requestRender();
        idleHandlers.get(0)
                    .handle();
        //then
        final InOrder inOrder0 = inOrder(this.platform,
                                         this.renderer);
        //FIXME
//        inOrder0.verify(this.platform)
//                .accept(this.renderer);

        final InOrder inOrder1 = inOrder(this.platform,
                                         this.renderer);
//        inOrder1.verify(this.platform)
//                .accept(renderer);

        verify(this.display).flushClients();
    }

    @Test
    public void testRequestRenderPreviousRenderBusy() throws Exception {
        //given
        final EventLoop eventLoop = mock(EventLoop.class);
        when(this.display.getEventLoop()).thenReturn(eventLoop);

        final List<EventLoop.IdleHandler> idleHandlers = new LinkedList<>();
        when(eventLoop.addIdle(any())).thenAnswer(invocation -> {
            final Object                arg0        = invocation.getArguments()[0];
            final EventLoop.IdleHandler idleHandler = (EventLoop.IdleHandler) arg0;
            idleHandlers.add(idleHandler);
            return mock(EventSource.class);
        });

        //when
        this.compositor.requestRender();
        this.compositor.requestRender();
        //then
        assertThat(idleHandlers).hasSize(1);
        //and when
        idleHandlers.get(0)
                    .handle();
        this.compositor.requestRender();
        //then
        assertThat(idleHandlers).hasSize(2);
    }
}
@@ -603,104 +603,8 @@
 e;%0A%0A
-import org.freedesktop.wayland.server.Display;%0Aimport org.freedesktop.wayland.server.EventLoop;%0A
 impo
@@ -710,36 +710,8 @@
 th;%0A
-import org.mockito.InOrder;%0A
 impo
@@ -900,26 +900,22 @@
 va.util.
-LinkedList
+Arrays
 ;%0Aimport
@@ -935,147 +935,35 @@
 st;%0A
-%0A
 import 
-static com.google.common.truth.Truth.assertThat;%0Aimport static org.mockito.Matchers.any;%0Aimport static org.mockito.Mockito.inOrder;
+java.util.Optional;%0A
 %0Aimp
@@ -1184,48 +1184,8 @@
  %7B%0A%0A
-    @Mock%0A    private Display  display;%0A
     
@@ -1409,618 +1409,219 @@
     
-final EventLoop eventLoop = mock(EventLoop.class);%0A        when(this.display.getEventLoop()).thenReturn(eventLoop);%0A        final List%3CEventLoop.IdleHandler%3E idleHandlers = new LinkedList%3C%3E();%0A        when(eventLoop.addIdle(any())).thenAnswer(invocation -%3E %7B%0A            final Object                arg0        = invocation.getArguments()%5B0%5D;%0A            final EventLoop.IdleHandler idleHandler = (EventLoop.IdleHandler) arg0;%0A            idleHandlers.add(idleHandler);%0A            return mock(EventSource.class);%0A        %7D);%0A%0A        //when%0A        this.compositor.requestRender();%0A        idleHandlers.get(
+Connector connector0 = mock(Connector.class);%0A        Connector connector1 = mock(Connector.class);%0A        final List%3C? extends Optional%3C? extends Connector%3E%3E connectors = Arrays.asList(Optional.of(connector
 0)
+,
 %0A   
@@ -1637,35 +1637,24 @@
         
-.handle();%0A
         
 //then%0A 
@@ -1645,31 +1645,24 @@
         
-//then%0A
         
 final In
@@ -1657,1365 +1657,380 @@
     
-final InOrder inOrder0 = inOrder(this.platform,%0A                                         this.renderer);%0A        //FIXME%0A//        inOrder0.verify(this.platform)%0A//                .accept(this.renderer);%0A%0A        final InOrder inOrder1 = inOrder(this.platform,%0A                                         this.renderer);%0A//        inOrder1.verify(this.platform)%0A//                .accept(renderer);%0A%0A        verify(this.display).flushClients();%0A
+                                                   Optional.empty(),%0A                                                                         
     
-%7D%0A%0A
     
-@Test%0A    public void testRequestRenderPreviousRenderBusy() throws Exception %7B%0A        //given%0A        final EventLoop eventLoop = mock(EventLoop.class);%0A        when(this.display.getEventLoop()).thenReturn(eventLoop);%0A%0A        final List%3CEventLoop.IdleHandler%3E idleHandlers = new LinkedList%3C%3E();%0A        when(eventLoop.addIdle(any())).thenAnswer(invocation -%3E %7B%0A            final Object                arg0        = invocation.getArguments()%5B0%5D;%0A            final EventLoop.IdleHandler idleHandler = (EventLoop.IdleHandler) arg0;%0A            idleHandlers.add(idleHandler);%0A            return mock(EventSource.class);%0A        %7D);%0A%0A        //when%0A        this.compositor.requestRender();%0A        this.compositor.requestRender();%0A        //then%0A        assertThat(idleHandlers).hasSize(1);%0A        //and when%0A        idleHandlers.get(0)%0A                    .handle();%0A        this.compositor.requestR
+      Optional.of(connector1));%0A        when(platform.getConnectors()).thenReturn((List) connectors);%0A%0A        //when%0A        this.compositor.requestRender();%0A%0A        //then%0A        verify(connector0).accept(this.r
 ender
-(
+er
 );%0A 
@@ -2040,57 +2040,47 @@
     
-//then%0A        assertThat(idleHa
+verify(connector1).accept(this.re
 nd
-l
 er
-s).hasSize(2
+er
 );%0A 

fix compositor unit test