-
Notifications
You must be signed in to change notification settings - Fork 390
Expand file tree
/
Copy pathEventBusTest.java
More file actions
216 lines (182 loc) · 6.98 KB
/
EventBusTest.java
File metadata and controls
216 lines (182 loc) · 6.98 KB
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
/*
* Copyright (C) 2015 Mr.Simple <bboyfeiyu@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.simple.eventbus.test;
import android.test.AndroidTestCase;
import android.util.Log;
import org.simple.eventbus.EventBus;
import org.simple.eventbus.EventType;
import org.simple.eventbus.Subscription;
import org.simple.eventbus.test.mock.MockSubcriber;
import org.simple.eventbus.test.mock.SingleSubscriber;
import org.simple.eventbus.test.mock.StickySubscriber;
import org.simple.eventbus.test.mock.User;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* @author mrsimple
*/
public class EventBusTest extends AndroidTestCase {
EventBus bus = EventBus.getDefault();
Map<EventType, CopyOnWriteArrayList<Subscription>> mSubcriberMap = bus.getSubscriberMap();
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
EventBus.getDefault().clear();
}
/**
* 获取某个事件的观察者列表
*
* @param event 事件类型
* @return
*/
@SuppressWarnings("unchecked")
public Collection<Subscription> getSubscriptions(EventType event) {
List<Subscription> result = mSubcriberMap.get(event);
return result != null ? result : Collections.EMPTY_LIST;
}
/**
* 获取已经注册的事件类型列表
*
* @return
*/
@SuppressWarnings("unchecked")
public Collection<EventType> getEventTypes() {
Collection<EventType> result = mSubcriberMap.keySet();
return result != null ? result : Collections.EMPTY_LIST;
}
/**
*
*/
public void testRepeatRegister() {
MockSubcriber mockActivity = new MockSubcriber();
for (int i = 0; i < 10; i++) {
// 测试重复注册一个对象
bus.register(mockActivity);
}
// 类型为Person的有效注册函数为2个.
assertEquals(2, getSubscriptions(new EventType(User.class)).size());
// Object类型的函数为1一个
assertEquals(1, getSubscriptions(new EventType(Object.class)).size());
// NULL类型的函数为2一个
assertEquals(2, getSubscriptions(new EventType(null)).size());
// NULL类型tag为tag_null的函数为1一个
assertEquals(1, getSubscriptions(new EventType(null, "tag_null")).size());
}
/**
*
*/
public void testRepeatRegisterWithTag() {
MockSubcriber mockActivity = new MockSubcriber();
for (int i = 0; i < 10; i++) {
// 测试重复注册一个对象
bus.register(mockActivity);
}
// 类型为Person且tag为"test"的有效注册函数为1个.
assertEquals(1, getSubscriptions(new EventType(User.class, "test")).size());
// 类型为Person且tag为"another"的有效注册函数为1个.
assertEquals(1, getSubscriptions(new EventType(User.class, "another")).size());
// 类型为NULL且tag为"tag_null"的有效注册函数为1个.
assertEquals(1, getSubscriptions(new EventType(null, "tag_null")).size());
}
/**
*
*/
public void testSubscribeAndPost() {
MockSubcriber mockActivity = new MockSubcriber();
// 正常注册与发布
bus.register(mockActivity);
bus.post(new User("mr.simple"));
bus.post(User.class);
bus.post("tag_null");
// 移除对象
bus.unregister(mockActivity);
// 移除对象之后post不会出现问题
bus.post(new User("mr.simple"));
// 移除对象测试
assertEquals(0, getSubscriptions(new EventType(User.class)).size());
assertEquals(0, getSubscriptions(new EventType(Object.class)).size());
}
public void testRegisterNull() {
bus.register(null);
}
public void testUnRegisterNull() {
bus.unregister(null);
}
public void testStickyEvents() {
assertEquals(0, bus.getStickyEvents().size());
bus.postSticky("hello", EventType.DEFAULT_TAG);
assertEquals(1, bus.getStickyEvents().size());
final String tag = "sticky";
bus.postSticky("world", tag);
assertEquals(2, bus.getStickyEvents().size());
bus.postSticky(String.class);
assertEquals(3, bus.getStickyEvents().size());
bus.postSticky(tag);
assertEquals(4, bus.getStickyEvents().size());
bus.postSticky(tag, String.class);
assertEquals(5, bus.getStickyEvents().size());
bus.removeStickyEvent(String.class);
assertEquals(3, bus.getStickyEvents().size());
bus.removeStickyEvent(String.class, tag);
assertEquals(1, bus.getStickyEvents().size());
bus.removeStickyEvent(null, tag);
assertEquals(0, bus.getStickyEvents().size());
}
public void testStickySubscriber() {
StickySubscriber subscriber = new StickySubscriber();
assertEquals(null, subscriber.mStickyName);
assertEquals(null, subscriber.mStickyTag);
// 未注册时发布事件,那么对象接收不到事件
bus.post("hello", EventType.DEFAULT_TAG);
// 普通注册形式
bus.register(subscriber);
assertEquals(null, subscriber.mStickyName);
assertEquals(null, subscriber.mStickyTag);
bus.unregister(subscriber);
// post sticky事件
bus.postSticky("simple", EventType.DEFAULT_TAG);
// 以sticky的形式注册
bus.registerSticky(subscriber);
// 接收到数据
assertEquals("simple", subscriber.mStickyName);
assertEquals(null, subscriber.mStickyTag);
bus.unregister(subscriber);
// post sticky事件, 含有tag, 这里需要注意
bus.postSticky("simple", "sticky");
bus.registerSticky(subscriber);
assertEquals("sticky", subscriber.mStickyTag);
bus.unregister(subscriber);
}
/**
*
*/
public void testPerformence() {
long start = System.nanoTime();
for (int i = 0; i < 1000; i++) {
SingleSubscriber subscriber = new SingleSubscriber();
bus.register(subscriber);
}
long end = System.nanoTime();
Log.d(getName(), "### register 1000 subscriber, time = " + (end - start) + " ns, "
+ (end - start) / 1 * 1e6 + " ms");
assertEquals(1000, getSubscriptions(new EventType(Object.class)).size());
}
}