package jrummikub.util;

import static org.junit.Assert.*;

import org.junit.Test;

/**
 * Test class for {@link Event1}
 */
public class Event1Test {

	int fired, fired2;

	/** */
	@Test
	public void singleListener() {
		fired = 0;
		Event1<Integer> testEvent = new Event1<Integer>();
		testEvent.add(new IListener1<Integer>() {

			@Override
			public void handle(Integer n) {
				fired += n;

			}
		});
		assertEquals(fired, 0);
		testEvent.emit(10);
		assertEquals(fired, 10);
		testEvent.emit(20);
		assertEquals(fired, 30);
	}

	/** */
	@Test
	public void twoListeners() {
		fired = 0;
		fired2 = 0;
		Event1<Integer> testEvent = new Event1<Integer>();
		testEvent.add(new IListener1<Integer>() {

			@Override
			public void handle(Integer n) {
				fired += n;

			}
		});
		testEvent.add(new IListener1<Integer>() {

			@Override
			public void handle(Integer n) {
				fired2 -= n;

			}
		});
		assertEquals(fired, 0);
		assertEquals(fired2, 0);
		testEvent.emit(5);
		assertEquals(fired, 5);
		assertEquals(fired2, -5);

	}

	/** */
	@Test
	public void removeListener() {
		fired = 0;
		Event1<Integer> testEvent = new Event1<Integer>();
		testEvent.add(new IListener1<Integer>() {

			@Override
			public void handle(Integer n) {
				fired += n;

			}
		});
		IListener1<Integer> rem = new IListener1<Integer>() {

			@Override
			public void handle(Integer n) {
				fail();
			}
		};
		testEvent.add(rem);
		testEvent.remove(rem);
		testEvent.emit(10);
		assertEquals(fired, 10);
	}

	/** */
	@Test
	public void removeListenerByConnection() {
		fired = 0;
		Event1<Integer> testEvent = new Event1<Integer>();
		testEvent.add(new IListener1<Integer>() {

			@Override
			public void handle(Integer n) {
				fired += n;

			}
		});
		IListener1<Integer> rem = new IListener1<Integer>() {

			@Override
			public void handle(Integer n) {
				fail();
			}
		};
		Connection connection = testEvent.add(rem);
		connection.remove();
		testEvent.emit(10);
		assertEquals(fired, 10);
	}
}
