Skip to content

Commit 1e5efee

Browse files
committed
refactor and add testcase
1 parent 2afaab7 commit 1e5efee

File tree

19 files changed

+343
-290
lines changed

19 files changed

+343
-290
lines changed
Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
/*
2-
* The MIT License (MIT)
2+
* Copyright (C) 2015 Mr.Simple <bboyfeiyu@gmail.com>
33
*
4-
* Copyright (c) 2014-2015 Umeng, Inc
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
57
*
6-
* Permission is hereby granted, free of charge, to any person obtaining a copy
7-
* of this software and associated documentation files (the "Software"), to deal
8-
* in the Software without restriction, including without limitation the rights
9-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10-
* copies of the Software, and to permit persons to whom the Software is
11-
* furnished to do so, subject to the following conditions:
8+
* http://www.apache.org/licenses/LICENSE-2.0
129
*
13-
* The above copyright notice and this permission notice shall be included in
14-
* all copies or substantial portions of the Software.
15-
*
16-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22-
* THE SOFTWARE.
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
2315
*/
2416

2517
package org.simple.eventbus.test;
@@ -37,7 +29,8 @@ public static Test suite() {
3729
return new TestSuiteBuilder(BusTestSuite.class)
3830
.includePackages(
3931
"org.simple.eventbus.test.EventBusTest",
40-
"org.simple.eventbus.test.EventTypeTest")
32+
"org.simple.eventbus.test.EventTypeTest",
33+
"org.simple.eventbus.test.ThreadModeTest")
4134
.build();
4235
}
4336
}

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

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
/*
2-
* The MIT License (MIT)
2+
* Copyright (C) 2015 Mr.Simple <bboyfeiyu@gmail.com>
33
*
4-
* Copyright (c) 2014-2015 Umeng, Inc
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
57
*
6-
* Permission is hereby granted, free of charge, to any person obtaining a copy
7-
* of this software and associated documentation files (the "Software"), to deal
8-
* in the Software without restriction, including without limitation the rights
9-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10-
* copies of the Software, and to permit persons to whom the Software is
11-
* furnished to do so, subject to the following conditions:
8+
* http://www.apache.org/licenses/LICENSE-2.0
129
*
13-
* The above copyright notice and this permission notice shall be included in
14-
* all copies or substantial portions of the Software.
15-
*
16-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22-
* THE SOFTWARE.
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
2315
*/
2416

2517
package org.simple.eventbus.test;
@@ -30,8 +22,8 @@
3022
import org.simple.eventbus.EventBus;
3123
import org.simple.eventbus.EventType;
3224
import org.simple.eventbus.test.mock.MockSubcriber;
33-
import org.simple.eventbus.test.mock.Person;
3425
import org.simple.eventbus.test.mock.SingleSubscriber;
26+
import org.simple.eventbus.test.mock.User;
3527

3628
/**
3729
* @author mrsimple
@@ -46,6 +38,7 @@ protected void setUp() throws Exception {
4638

4739
protected void tearDown() throws Exception {
4840
super.tearDown();
41+
EventBus.getDefault().clear();
4942
}
5043

5144
/**
@@ -59,7 +52,7 @@ public void testRepeatRegister() {
5952
}
6053

6154
// 类型为Person的有效注册函数为2个.
62-
assertEquals(2, bus.getSubscriptions(new EventType(Person.class)).size());
55+
assertEquals(2, bus.getSubscriptions(new EventType(User.class)).size());
6356
// Object类型的函数为1一个
6457
assertEquals(1, bus.getSubscriptions(new EventType(Object.class)).size());
6558
}
@@ -75,10 +68,10 @@ public void testRepeatRegisterWithTag() {
7568
}
7669

7770
// 类型为Person且tag为"test"的有效注册函数为1个.
78-
assertEquals(1, bus.getSubscriptions(new EventType(Person.class, "test")).size());
71+
assertEquals(1, bus.getSubscriptions(new EventType(User.class, "test")).size());
7972

8073
// 类型为Person且tag为"another"的有效注册函数为1个.
81-
assertEquals(1, bus.getSubscriptions(new EventType(Person.class, "another")).size());
74+
assertEquals(1, bus.getSubscriptions(new EventType(User.class, "another")).size());
8275
}
8376

8477
/**
@@ -88,14 +81,14 @@ public void testSubscribeAndPost() {
8881
MockSubcriber mockActivity = new MockSubcriber();
8982
// 正常注册与发布
9083
bus.register(mockActivity);
91-
bus.post(new Person("mr.simple"));
84+
bus.post(new User("mr.simple"));
9285

9386
// 移除对象
9487
bus.unregister(mockActivity);
9588
// 移除对象之后post不会出现问题
96-
bus.post(new Person("mr.simple"));
89+
bus.post(new User("mr.simple"));
9790
// 移除对象测试
98-
assertEquals(0, bus.getSubscriptions(new EventType(Person.class)).size());
91+
assertEquals(0, bus.getSubscriptions(new EventType(User.class)).size());
9992
assertEquals(0, bus.getSubscriptions(new EventType(Object.class)).size());
10093
}
10194

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
/*
2-
* The MIT License (MIT)
2+
* Copyright (C) 2015 Mr.Simple <bboyfeiyu@gmail.com>
33
*
4-
* Copyright (c) 2014-2015 Umeng, Inc
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
57
*
6-
* Permission is hereby granted, free of charge, to any person obtaining a copy
7-
* of this software and associated documentation files (the "Software"), to deal
8-
* in the Software without restriction, including without limitation the rights
9-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10-
* copies of the Software, and to permit persons to whom the Software is
11-
* furnished to do so, subject to the following conditions:
8+
* http://www.apache.org/licenses/LICENSE-2.0
129
*
13-
* The above copyright notice and this permission notice shall be included in
14-
* all copies or substantial portions of the Software.
15-
*
16-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22-
* THE SOFTWARE.
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
2315
*/
2416

2517
package org.simple.eventbus.test;
2618

2719
import junit.framework.TestCase;
2820

21+
import org.simple.eventbus.EventBus;
2922
import org.simple.eventbus.EventType;
3023

3124
import java.util.HashMap;
@@ -42,6 +35,8 @@ protected void setUp() throws Exception {
4235

4336
protected void tearDown() throws Exception {
4437
super.tearDown();
38+
39+
EventBus.getDefault().clear();
4540
}
4641

4742
/**
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014-2015 Umeng, Inc
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.simple.eventbus.test;
26+
27+
import android.test.AndroidTestCase;
28+
import android.util.Log;
29+
30+
import org.simple.eventbus.EventBus;
31+
import org.simple.eventbus.Subcriber;
32+
import org.simple.eventbus.ThreadMode;
33+
import org.simple.eventbus.handler.AsyncEventHandler;
34+
import org.simple.eventbus.test.mock.User;
35+
36+
public class ThreadModeTest extends AndroidTestCase {
37+
38+
private static final String MAIN_TAG = "main";
39+
private static final String POST_TAG = "post";
40+
private static final String ASYNC_TAG = "async";
41+
42+
Thread uiThread;
43+
44+
protected void setUp() throws Exception {
45+
super.setUp();
46+
uiThread = Thread.currentThread();
47+
EventBus.getDefault().register(this);
48+
}
49+
50+
protected void tearDown() throws Exception {
51+
super.tearDown();
52+
EventBus.getDefault().register(this);
53+
EventBus.getDefault().clear();
54+
}
55+
56+
@Subcriber
57+
public void willNotInvoke(User user) {
58+
assertEquals(1, 2);
59+
}
60+
61+
public void testExecuteMainThread() {
62+
EventBus.getDefault().post(new User("main-thread"), MAIN_TAG);
63+
64+
waitDispatchEvent();
65+
}
66+
67+
@Subcriber(tag = MAIN_TAG)
68+
private void executeOnUIThread(User user) {
69+
assertEquals("main-thread", user.name);
70+
// can not test main thread mode
71+
}
72+
73+
public void testExecutePostThread() {
74+
EventBus.getDefault().post(new User("post-thread"), POST_TAG);
75+
76+
waitDispatchEvent();
77+
}
78+
79+
@Subcriber(tag = POST_TAG, mode = ThreadMode.POST)
80+
private void executeOnPostThread(User user) {
81+
assertEquals("post-thread", user.name);
82+
assertEquals(uiThread, Thread.currentThread());
83+
}
84+
85+
public void testExecuteAsyncThread() {
86+
EventBus.getDefault().post(new User("async-thread"), ASYNC_TAG);
87+
waitDispatchEvent();
88+
}
89+
90+
@Subcriber(tag = ASYNC_TAG, mode = ThreadMode.ASYNC)
91+
private void executeOnAsyncThread(User user) {
92+
assertEquals("async-thread", user.name);
93+
Log.e(getName(), "### executeOnAsyncThread flag");
94+
assertEquals(AsyncEventHandler.class.getSimpleName(), Thread.currentThread().getName());
95+
}
96+
97+
private void waitDispatchEvent() {
98+
synchronized (this) {
99+
try {
100+
wait(200);
101+
} catch (InterruptedException e) {
102+
e.printStackTrace();
103+
}
104+
}
105+
}
106+
107+
}

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

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
/*
2-
* The MIT License (MIT)
2+
* Copyright (C) 2015 Mr.Simple <bboyfeiyu@gmail.com>
33
*
4-
* Copyright (c) 2014-2015 Umeng, Inc
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
57
*
6-
* Permission is hereby granted, free of charge, to any person obtaining a copy
7-
* of this software and associated documentation files (the "Software"), to deal
8-
* in the Software without restriction, including without limitation the rights
9-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10-
* copies of the Software, and to permit persons to whom the Software is
11-
* furnished to do so, subject to the following conditions:
8+
* http://www.apache.org/licenses/LICENSE-2.0
129
*
13-
* The above copyright notice and this permission notice shall be included in
14-
* all copies or substantial portions of the Software.
15-
*
16-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22-
* THE SOFTWARE.
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
2315
*/
2416

2517
package org.simple.eventbus.test.mock;
@@ -36,12 +28,12 @@ void onEventNoParam() {
3628
}
3729

3830
@Subcriber
39-
void onEventTwoParam(Person person, int id) {
31+
void onEventTwoParam(User person, int id) {
4032

4133
}
4234

4335
@Subcriber
44-
void onEvent(Person person) {
36+
void onEvent(User person) {
4537
System.out.println("invoke onEvent(Person person) in " + this.getClass().getName());
4638
System.out.println("person name = " + person.name);
4739
}
@@ -52,7 +44,7 @@ void onEvent(Person person) {
5244
* @param person
5345
*/
5446
@Subcriber
55-
void addPerson(Person person) {
47+
void addPerson(User person) {
5648
System.out.println("invoke addPerson(Person person) in " + this.getClass().getName());
5749
System.out.println("person name = " + person.name);
5850
}
@@ -63,7 +55,7 @@ void addPerson(Person person) {
6355
* @param person
6456
*/
6557
@Subcriber(tag = "test")
66-
void methodWithTag(Person person) {
58+
void methodWithTag(User person) {
6759

6860
}
6961

@@ -73,7 +65,7 @@ void methodWithTag(Person person) {
7365
* @param person
7466
*/
7567
@Subcriber(tag = "another")
76-
void methodWithAnotherTag(Person person) {
68+
void methodWithAnotherTag(User person) {
7769

7870
}
7971

0 commit comments

Comments
 (0)