Skip to content

Commit acfb1a9

Browse files
committed
event bus beta
1 parent def8764 commit acfb1a9

24 files changed

+787
-288
lines changed

src/org/simple/eventbus/SubscribeMethod.java renamed to Simple_Event_Test/src/org/simple/eventbus/test/BusTestSuite.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,22 @@
2222
* THE SOFTWARE.
2323
*/
2424

25-
package org.simple.eventbus;
25+
package org.simple.eventbus.test;
2626

27-
import java.lang.reflect.Method;
27+
import android.test.suitebuilder.TestSuiteBuilder;
28+
29+
import junit.framework.Test;
30+
import junit.framework.TestSuite;
2831

2932
/**
30-
* 订阅的函数
31-
*
3233
* @author mrsimple
3334
*/
34-
public class SubscribeMethod {
35-
/**
36-
* 订阅者
37-
*/
38-
Method targetMethod;
39-
/**
40-
* 事件类型
41-
*/
42-
Class<?> eventType;
43-
/**
44-
*
45-
*/
46-
ThreadMode threadMode;
47-
48-
public SubscribeMethod(Method md, Class<?> eventType, ThreadMode mode) {
49-
this.targetMethod = md;
50-
this.eventType = eventType;
51-
this.threadMode = mode;
35+
public class BusTestSuite extends TestSuite {
36+
public static Test suite() {
37+
return new TestSuiteBuilder(BusTestSuite.class)
38+
.includePackages(
39+
"org.simple.eventbus.test.EventBusTest",
40+
"org.simple.eventbus.test.SubscriberHolderTest")
41+
.build();
5242
}
5343
}

Simple_Event_Test/src/org/simple/eventbus/test/EventBusTest.java

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@
2525
package org.simple.eventbus.test;
2626

2727
import android.test.AndroidTestCase;
28+
import android.util.Log;
2829

2930
import org.simple.eventbus.EventBus;
31+
import org.simple.eventbus.EventType;
3032
import org.simple.eventbus.test.mock.MockActivity;
3133
import org.simple.eventbus.test.mock.Person;
34+
import org.simple.eventbus.test.mock.SingleSubscriber;
3235

36+
/**
37+
* @author mrsimple
38+
*/
3339
public class EventBusTest extends AndroidTestCase {
3440

3541
EventBus bus = new EventBus();
@@ -45,18 +51,76 @@ protected void tearDown() throws Exception {
4551
/**
4652
*
4753
*/
48-
public void testSubscribe() {
54+
public void testRepeatRegister() {
4955
MockActivity mockActivity = new MockActivity();
50-
bus.register(mockActivity);
51-
bus.register(mockActivity);
52-
bus.register(mockActivity);
53-
bus.register(mockActivity);
56+
for (int i = 0; i < 10; i++) {
57+
// 测试重复注册一个对象
58+
bus.register(mockActivity);
59+
}
60+
61+
// 类型为Person的有效注册函数为2个.
62+
assertEquals(2, bus.getSubscriptions(new EventType(Person.class)).size());
63+
// Object类型的函数为1一个
64+
assertEquals(1, bus.getSubscriptions(new EventType(Object.class)).size());
65+
}
66+
67+
/**
68+
*
69+
*/
70+
public void testRepeatRegisterWithTag() {
71+
MockActivity mockActivity = new MockActivity();
72+
for (int i = 0; i < 10; i++) {
73+
// 测试重复注册一个对象
74+
bus.register(mockActivity);
75+
}
5476

77+
// 类型为Person且tag为"test"的有效注册函数为1个.
78+
assertEquals(1, bus.getSubscriptions(new EventType(Person.class, "test")).size());
79+
80+
// 类型为Person且tag为"another"的有效注册函数为1个.
81+
assertEquals(1, bus.getSubscriptions(new EventType(Person.class, "another")).size());
82+
}
83+
84+
/**
85+
*
86+
*/
87+
public void testSubscribeAndPost() {
88+
MockActivity mockActivity = new MockActivity();
89+
// 正常注册与发布
90+
bus.register(mockActivity);
5591
bus.post(new Person("mr.simple"));
56-
bus.unregister(mockActivity);
5792

93+
// 移除对象
94+
bus.unregister(mockActivity);
95+
// 移除对象之后post不会出现问题
5896
bus.post(new Person("mr.simple"));
97+
// 移除对象测试
98+
assertEquals(0, bus.getSubscriptions(new EventType(Person.class)).size());
99+
assertEquals(0, bus.getSubscriptions(new EventType(Object.class)).size());
100+
}
101+
102+
public void testRegisterNull() {
103+
bus.register(null);
104+
}
105+
106+
public void testUnRegisterNull() {
107+
bus.unregister(null);
108+
}
109+
110+
/**
111+
*
112+
*/
113+
public void testPerformence() {
114+
long start = System.nanoTime();
115+
for (int i = 0; i < 1000; i++) {
116+
SingleSubscriber subscriber = new SingleSubscriber();
117+
bus.register(subscriber);
118+
}
119+
long end = System.nanoTime();
120+
Log.d(getName(), "### register 1000 subscriber, time = " + (end - start) + " ns, "
121+
+ (end - start) / 1 * 1e6 + " ms");
59122

123+
assertEquals(1000, bus.getSubscriptions(new EventType(Object.class)).size());
60124
}
61125

62126
}

Simple_Event_Test/src/org/simple/eventbus/test/SubscriberHolderTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626

2727
import junit.framework.TestCase;
2828

29-
import org.simple.eventbus.Event;
30-
29+
import org.simple.eventbus.EventType;
3130
import java.util.HashMap;
3231
import java.util.Map;
3332

33+
/**
34+
* @author mrsimple
35+
*/
3436
public class SubscriberHolderTest extends TestCase {
3537

3638
protected void setUp() throws Exception {
@@ -45,11 +47,11 @@ protected void tearDown() throws Exception {
4547
* 检测SubscriberHolder作为map的key的唯一性
4648
*/
4749
public void testKeysInMap() {
48-
Map<Event, String> map = new HashMap<Event, String>();
50+
Map<EventType, String> map = new HashMap<EventType, String>();
4951

5052
String tag = "default";
5153
for (int i = 0; i < 10; i++) {
52-
map.put(new Event(String.class, tag), tag + i);
54+
map.put(new EventType(String.class, tag), tag + i);
5355
}
5456

5557
assertEquals(1, map.size());

Simple_Event_Test/src/org/simple/eventbus/test/mock/MockActivity.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,64 @@
2626

2727
import org.simple.eventbus.Subcriber;
2828

29+
/**
30+
* @author mrsimple
31+
*/
2932
public class MockActivity {
33+
34+
@Subcriber
35+
void onEventNoParam() {
36+
}
37+
38+
@Subcriber
39+
void onEventTwoParam(Person person, int id) {
40+
41+
}
42+
3043
@Subcriber
3144
void onEvent(Person person) {
3245
System.out.println("invoke onEvent(Person person) in " + this.getClass().getName());
3346
System.out.println("person name = " + person.name);
3447
}
48+
49+
/**
50+
* 参数相同,函数名不同
51+
*
52+
* @param person
53+
*/
54+
@Subcriber
55+
void addPerson(Person person) {
56+
System.out.println("invoke addPerson(Person person) in " + this.getClass().getName());
57+
System.out.println("person name = " + person.name);
58+
}
59+
60+
/**
61+
* test tag
62+
*
63+
* @param person
64+
*/
65+
@Subcriber(tag = "test")
66+
void methodWithTag(Person person) {
67+
68+
}
69+
70+
/**
71+
* another tag
72+
*
73+
* @param person
74+
*/
75+
@Subcriber(tag = "another")
76+
void methodWithAnotherTag(Person person) {
77+
78+
}
79+
80+
/**
81+
* 同名函数,但是参数不同
82+
*
83+
* @param object
84+
*/
85+
@Subcriber
86+
void onEvent(Object object) {
87+
System.out.println("invoke onEvent(Person person) in " + this.getClass().getName());
88+
}
3589
}

Simple_Event_Test/src/org/simple/eventbus/test/mock/MockFragment.java renamed to Simple_Event_Test/src/org/simple/eventbus/test/mock/SingleSubscriber.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,9 @@
2929
/**
3030
* @author mrsimple
3131
*/
32-
public class MockFragment {
32+
public class SingleSubscriber {
3333
@Subcriber
3434
void onEvent(Object event) {
3535
System.out.println("invoke onEvent(Object event) in " + this.getClass().getName());
3636
}
37-
38-
@Subcriber
39-
void onEvent(Person person) {
40-
System.out.println("invoke onEvent(Person person) in " + this.getClass().getName());
41-
System.out.println("person name = " + person.name);
42-
}
43-
44-
@Subcriber
45-
void hello(Object event) {
46-
System.out.println("invoke hello in " + this.getClass().getName());
47-
}
4837
}

Simple_eventbus_demo/res/layout/activity_main.xml

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:background="#cccccc"
6+
android:padding="2dp" >
7+
8+
</ListView>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:id="@+id/fragment_container"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:orientation="horizontal"
7+
android:padding="3dp" >
8+
9+
<fragment
10+
android:id="@+id/fragment_menu"
11+
android:name="org.simple.eventbus.demo.fragment.MenuFragment"
12+
android:layout_width="0dp"
13+
android:layout_height="match_parent"
14+
android:layout_weight="1" />
15+
16+
<fragment
17+
android:id="@+id/fragment_list"
18+
android:name="org.simple.eventbus.demo.fragment.ConstactFragment"
19+
android:layout_width="0dp"
20+
android:layout_height="match_parent"
21+
android:layout_weight="3" />
22+
23+
</LinearLayout>

Simple_eventbus_demo/res/layout/second_fragment.xml renamed to Simple_eventbus_demo/res/layout/menu_fragment.xml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,31 @@
22
xmlns:tools="http://schemas.android.com/tools"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5-
android:background="#cccccc"
65
android:orientation="vertical"
76
android:padding="3dp" >
87

9-
<Button
10-
android:id="@+id/my_button_2"
11-
android:layout_width="match_parent"
12-
android:layout_height="wrap_content"
13-
android:text="回到到第一页" />
14-
158
<Button
169
android:id="@+id/my_post_button"
1710
android:layout_width="match_parent"
1811
android:layout_height="wrap_content"
19-
android:text="发布消息" />
12+
android:text="添加用户" />
2013

2114
<Button
22-
android:id="@+id/my_post_event_tag_button"
15+
android:id="@+id/my_remove_button"
2316
android:layout_width="match_parent"
2417
android:layout_height="wrap_content"
25-
android:text="发布消息 ( tag == hello )" />
18+
android:text="移除用户 ( tag == remove )" />
2619

2720
<Button
2821
android:id="@+id/my_post_async_event_button"
2922
android:layout_width="match_parent"
3023
android:layout_height="wrap_content"
3124
android:text="发布消息 ( mode == async )" />
3225

26+
<TextView
27+
android:id="@+id/click_tv"
28+
android:layout_width="match_parent"
29+
android:layout_height="wrap_content"
30+
android:layout_marginTop="20dp" />
31+
3332
</LinearLayout>

Simple_eventbus_demo/res/layout/second_activity.xml

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)