summaryrefslogtreecommitdiffstats
path: root/test/jrummikub/util/Event2Test.java
blob: c3d9c2a63f582f97bcaa403412b67300432c38f0 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
package jrummikub.util;

import static org.junit.Assert.*;

import org.junit.Test;

public class Event2Test {

  int fired, fired2, fired3, fired4;

  @Test
  public void singleListener() {
    fired = 0;
    fired2 = 0;
    Event2<Integer, Integer> testEvent = new Event2<Integer, Integer>();
    testEvent.add(new IListener2<Integer, Integer>() {

      @Override
      public void fire(Integer n, Integer m) {
        fired += n;
        fired2 += m;
      }
    });
    assertEquals(fired, 0);
    assertEquals(fired2, 0);
    testEvent.fire(10, 5);
    assertEquals(fired, 10);
    assertEquals(fired2, 5);
    testEvent.fire(20, 45);
    assertEquals(fired, 30);
    assertEquals(fired2, 50);
  }

  @Test
  public void twoListeners() {
    fired = 0;
    fired2 = 0;
    fired3 = 0;
    fired4 = 0;
    Event2<Integer, Integer> testEvent = new Event2<Integer, Integer>();
    testEvent.add(new IListener2<Integer, Integer>() {

      @Override
      public void fire(Integer n, Integer m) {
        fired += n;
        fired2 += m;
      }
    });
    testEvent.add(new IListener2<Integer, Integer>() {

      @Override
      public void fire(Integer n, Integer m) {
        fired3 -= n;
        fired4 -= m;
      }
    });
    assertEquals(fired, 0);
    assertEquals(fired2, 0);
    assertEquals(fired3, 0);
    assertEquals(fired4, 0);
    testEvent.fire(5, 10);
    assertEquals(fired, 5);
    assertEquals(fired2, 10);
    assertEquals(fired3, -5);
    assertEquals(fired4, -10);
  }

  @Test
  public void removeListener() {
    fired = 0;
    fired2 = 0;
    Event2<Integer, Integer> testEvent = new Event2<Integer, Integer>();
    testEvent.add(new IListener2<Integer, Integer>() {

      @Override
      public void fire(Integer n, Integer m) {
        fired += n;
        fired2 += m;
      }
    });
    IListener2<Integer, Integer> rem = new IListener2<Integer, Integer>() {

      @Override
      public void fire(Integer n, Integer m) {
        fail();
      }
    };
    assertEquals(fired, 0);
    assertEquals(fired2, 0);
    testEvent.add(rem);
    testEvent.remove(rem);
    testEvent.fire(10, 20);
    assertEquals(fired, 10);
    assertEquals(fired2, 20);
  }
}