yzt
2023-09-27 726603df43447f8cfedfeaae4267209adbd01699
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { Event } from "../../Source/Cesium.js";
 
describe("Core/Event", function () {
  var event;
  var spyListener;
  beforeEach(function () {
    event = new Event();
    spyListener = jasmine.createSpy("listener");
  });
 
  it("works with no scope", function () {
    var someValue = 123;
 
    event.addEventListener(spyListener);
    event.raiseEvent(someValue);
 
    expect(spyListener).toHaveBeenCalledWith(someValue);
 
    spyListener.calls.reset();
 
    event.removeEventListener(spyListener);
    event.raiseEvent(someValue);
 
    expect(spyListener).not.toHaveBeenCalled();
  });
 
  it("works with scope", function () {
    var someValue = 123;
    var scope = {};
 
    event.addEventListener(spyListener, scope);
    event.raiseEvent(someValue);
 
    expect(spyListener).toHaveBeenCalledWith(someValue);
    expect(spyListener.calls.first().object).toBe(scope);
 
    spyListener.calls.reset();
 
    event.removeEventListener(spyListener, scope);
    event.raiseEvent(someValue);
 
    expect(spyListener).not.toHaveBeenCalled();
  });
 
  it("can remove from within a callback", function () {
    var doNothing = function (evt) {};
 
    var removeEventCb = function (evt) {
      event.removeEventListener(removeEventCb);
    };
 
    var doNothing2 = function (evt) {};
 
    event.addEventListener(doNothing);
    event.addEventListener(removeEventCb);
    event.addEventListener(doNothing2);
    event.raiseEvent();
    expect(event.numberOfListeners).toEqual(2);
 
    event.removeEventListener(doNothing);
    event.removeEventListener(doNothing2);
    expect(event.numberOfListeners).toEqual(0);
  });
 
  it("can remove multiple listeners within a callback", function () {
    var removeEvent0 = event.addEventListener(function () {
      removeEvent0();
    });
    event.addEventListener(function () {});
    var removeEvent2 = event.addEventListener(function () {
      removeEvent2();
    });
    event.addEventListener(function () {});
    var removeEvent4 = event.addEventListener(function () {
      removeEvent4();
    });
    event.addEventListener(function () {});
    var removeEvent6 = event.addEventListener(function () {
      removeEvent6();
    });
    event.addEventListener(function () {});
    var removeEvent8 = event.addEventListener(function () {
      removeEvent8();
    });
    event.addEventListener(function () {});
 
    expect(event.numberOfListeners).toEqual(10);
    event.raiseEvent();
    expect(event.numberOfListeners).toEqual(5);
    event.raiseEvent();
    expect(event.numberOfListeners).toEqual(5);
  });
 
  it("addEventListener and removeEventListener works with same function of different scopes", function () {
    var Scope = function () {
      this.timesCalled = 0;
    };
 
    Scope.prototype.myCallback = function () {
      this.timesCalled++;
    };
 
    var scope1 = new Scope();
    var scope2 = new Scope();
 
    event.addEventListener(Scope.prototype.myCallback, scope1);
    event.addEventListener(Scope.prototype.myCallback, scope2);
    event.raiseEvent();
 
    expect(scope1.timesCalled).toEqual(1);
    expect(scope2.timesCalled).toEqual(1);
 
    event.removeEventListener(Scope.prototype.myCallback, scope1);
    expect(event.numberOfListeners).toEqual(1);
    event.raiseEvent();
 
    expect(scope1.timesCalled).toEqual(1);
    expect(scope2.timesCalled).toEqual(2);
 
    event.removeEventListener(Scope.prototype.myCallback, scope2);
    expect(event.numberOfListeners).toEqual(0);
  });
 
  it("numberOfListeners returns the correct number", function () {
    var callback1 = function () {};
 
    var callback2 = function () {};
 
    expect(event.numberOfListeners).toEqual(0);
 
    event.addEventListener(callback1);
    expect(event.numberOfListeners).toEqual(1);
 
    event.addEventListener(callback2);
    expect(event.numberOfListeners).toEqual(2);
 
    event.removeEventListener(callback2);
    expect(event.numberOfListeners).toEqual(1);
  });
 
  it("removeEventListener indicates if the listener is registered with the event", function () {
    var callback = function () {};
 
    event.addEventListener(callback);
    expect(event.numberOfListeners).toEqual(1);
 
    expect(event.removeEventListener(callback)).toEqual(true);
    expect(event.numberOfListeners).toEqual(0);
 
    expect(event.removeEventListener(callback)).toEqual(false);
  });
 
  it("removeEventListener does not remove a registered listener of a different scope", function () {
    var myFunc = function () {};
    var scope = {};
    event.addEventListener(myFunc, scope);
    expect(event.removeEventListener(myFunc)).toEqual(false);
  });
 
  it("works with no listeners", function () {
    event.raiseEvent(123);
  });
 
  it("addEventListener returns a function allowing removal", function () {
    var someValue = 123;
 
    var remove = event.addEventListener(spyListener);
    event.raiseEvent(someValue);
 
    expect(spyListener).toHaveBeenCalledWith(someValue);
    spyListener.calls.reset();
 
    remove();
    event.raiseEvent(someValue);
 
    expect(spyListener).not.toHaveBeenCalled();
  });
 
  it("addEventListener with scope returns a function allowing removal", function () {
    var someValue = 123;
    var scope = {};
 
    var remove = event.addEventListener(spyListener, scope);
    event.raiseEvent(someValue);
 
    expect(spyListener).toHaveBeenCalledWith(someValue);
    spyListener.calls.reset();
 
    remove();
    event.raiseEvent(someValue);
 
    expect(spyListener).not.toHaveBeenCalled();
  });
 
  it("addEventListener throws with undefined listener", function () {
    expect(function () {
      event.addEventListener(undefined);
    }).toThrowDeveloperError();
  });
 
  it("addEventListener throws with null listener", function () {
    expect(function () {
      event.addEventListener(null);
    }).toThrowDeveloperError();
  });
 
  it("addEventListener throws with non-function listener", function () {
    expect(function () {
      event.addEventListener({});
    }).toThrowDeveloperError();
  });
 
  it("removeEventListener throws with undefined listener", function () {
    expect(function () {
      event.removeEventListener(undefined);
    }).toThrowDeveloperError();
  });
 
  it("removeEventListener throws with null listener", function () {
    expect(function () {
      event.removeEventListener(null);
    }).toThrowDeveloperError();
  });
});