summaryrefslogtreecommitdiffstats
path: root/test/jrummikub/util/Event1Test.java
blob: 3f8839d400f98d24a559b6904dc6aaa29f2223aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package jrummikub.util;

import static org.junit.Assert.*;

import org.junit.Test;

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);
  }
}