package com.github.xeranic.blacksmith.event;

import static org.mockito.Mockito.*;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;

public class EventSourceTest {

    interface ChangedListener {
        
        void changed(String what);
        
    }
    
    static class Changed extends Event<ChangedListener> {

        private final String what;

        public Changed(String what) {
            this.what = what;
        }

        @Override
        protected void dispatchEvent(ChangedListener listener) {
            listener.changed(what);
        }
        
    }
    
    
    private EventSource<Changed, ChangedListener> eventSource;
    
    @Before
    public void setUp() {
        eventSource = new EventSource<>();
    }
    
	@Test
	public void testOn() {
	    ChangedListener listener = mock(ChangedListener.class);
	    eventSource.on(listener);
	    verify(listener, never()).changed((String) any());
	    
        eventSource.fire(new Changed("1"));
        verify(listener).changed("1");
        
        eventSource.fire(new Changed("2"));
        verify(listener).changed("2");
	}
	
	@Test
    public void testOnce() {
        ChangedListener listener = mock(ChangedListener.class);
        eventSource.once(listener);
        verify(listener, never()).changed((String) any());
        
        eventSource.fire(new Changed("1"));
        verify(listener).changed("1");
        
        eventSource.fire(new Changed("2"));
        verify(listener, never()).changed("2");
    }
	
	@Test
	public void testPriority() {
	    ChangedListener listenerL = mock(ChangedListener.class);
	    ChangedListener listenerN = mock(ChangedListener.class);
	    ChangedListener listenerH = mock(ChangedListener.class);
        
	    eventSource.addListener(listenerN);
	    eventSource.addListener(listenerL, false, EventSource.PRIORITY_LOW);
	    eventSource.addListener(listenerH, false, EventSource.PRIORITY_HIGH);
	    
        String what = "something";
        eventSource.fire(new Changed(what));
        
        InOrder order = inOrder(listenerL, listenerN, listenerH);
        order.verify(listenerH).changed(what);
        order.verify(listenerN).changed(what);
        order.verify(listenerL).changed(what);
	}
	
}

@@ -2260,10 +2260,413 @@
 );%0A%09%7D%0A%09%0A
+%09@Test%0A%09public void testRemove() %7B%0A%09    ChangedListener listener = mock(ChangedListener.class);%0A        ListenerRegistration%3CChangedListener%3E reg = eventSource.on(listener);%0A%0A        eventSource.fire(new Changed(%221%22));%0A        verify(listener).changed(%221%22);%0A        %0A        reg.removeListener();%0A        %0A        eventSource.fire(new Changed(%222%22));%0A        verify(listener, never()).changed(%222%22);%0A%09%7D%0A%09%0A
 %7D%0A

add unit test for remove listener