From ac910103c5b75571f61e58930235f8c16dc9e077 Mon Sep 17 00:00:00 2001 From: Jedsada Tiwongvorakul Date: Sat, 29 Apr 2017 18:20:25 +0700 Subject: [PATCH] :dragon: test post and subscribe Rxbus2. --- .../dagger2_mvp_example/MyApplication.java | 18 ++++++++++++++- .../dagger2_mvp_example/ui/MainActivity.java | 20 ++++++++++++++-- .../dagger2_mvp_example/ui/MainInterface.java | 5 ++++ .../dagger2_mvp_example/ui/MainPresenter.java | 23 ++++++++++++++++++- .../ui/base/BaseActivity.java | 2 -- .../ui/base/RxBusProvider.java | 15 ------------ .../ui/event/TestBusEvent.java | 23 +++++++++++++++++++ .../ui/MainPresenterTest.java | 4 ++++ 8 files changed, 89 insertions(+), 21 deletions(-) delete mode 100644 app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/base/RxBusProvider.java create mode 100644 app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/event/TestBusEvent.java diff --git a/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/MyApplication.java b/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/MyApplication.java index 73db8b5..023114c 100644 --- a/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/MyApplication.java +++ b/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/MyApplication.java @@ -1,6 +1,9 @@ package com.wisdomlanna.www.dagger2_mvp_example; +import android.annotation.SuppressLint; import android.app.Application; +import android.os.Build; +import android.os.StrictMode; import com.wisdomlanna.www.dagger2_mvp_example.module.AndroidModule; import com.wisdomlanna.www.dagger2_mvp_example.module.ApiModule; @@ -14,12 +17,25 @@ public class MyApplication extends Application { private ApplicationComponent component; + @SuppressLint("ObsoleteSdkInt") @Override public void onCreate() { super.onCreate(); ButterKnife.setDebug(BuildConfig.DEBUG); initDependencyInjection(); - if (BuildConfig.DEBUG) Timber.plant(new Timber.DebugTree()); + if (BuildConfig.DEBUG) { + Timber.plant(new Timber.DebugTree()); + StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() + .detectAll() + .penaltyLog() + .build()); + StrictMode.ThreadPolicy.Builder threadPolicyBuilder = new StrictMode.ThreadPolicy.Builder() + .detectAll() + .penaltyLog(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) + threadPolicyBuilder.penaltyDeathOnNetwork(); + StrictMode.setThreadPolicy(threadPolicyBuilder.build()); + } } private void initDependencyInjection() { diff --git a/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/MainActivity.java b/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/MainActivity.java index 89f4cc4..38c8689 100644 --- a/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/MainActivity.java +++ b/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/MainActivity.java @@ -5,11 +5,13 @@ import android.os.Bundle; import android.widget.TextView; +import com.google.gson.Gson; import com.wisdomlanna.www.dagger2_mvp_example.ApplicationComponent; import com.wisdomlanna.www.dagger2_mvp_example.R; import com.wisdomlanna.www.dagger2_mvp_example.R2; import com.wisdomlanna.www.dagger2_mvp_example.api.dao.UserInfoDao; import com.wisdomlanna.www.dagger2_mvp_example.ui.base.BaseActivity; +import com.wisdomlanna.www.dagger2_mvp_example.ui.event.TestBusEvent; import javax.inject.Inject; @@ -25,6 +27,8 @@ public class MainActivity extends BaseActivity implements MainInt @Inject SharedPreferences sharedPreferences; + @Inject + Gson gson; private int result; @@ -37,10 +41,20 @@ public void showResultPlus(int result) { @Override public void showResultUserInfoGitHubApi(UserInfoDao dao) { - Timber.d("result userName : %s", dao.getName()); + Timber.d("github result userName : %s", dao.getName()); tvUsername.setText(dao.getName()); } + @Override + public void showResultBusTag(int result) { + Timber.d("result bus tag : %d", result); + } + + @Override + public void showResultBusTestBusEvent(TestBusEvent event) { + Timber.d("result bus TestBusEvent : %s", gson.toJson(event)); + } + @Override protected int layoutToInflate() { return R.layout.activity_main; @@ -54,11 +68,13 @@ protected void doInjection(ApplicationComponent component) { @Override protected void startActivity() { getPresenter().loadUserInfo("pondthaitay"); + getPresenter().onViewStart(); } @Override protected void stopActivity() { Timber.d("stop activity"); + getPresenter().onViewStop(); } @Override @@ -69,7 +85,7 @@ protected void bindView() { @Override protected void setupInstance() { sharedPreferences.edit().putString("kk", "Jedsada").apply(); - Timber.d(sharedPreferences.getString("kk", "")); + Timber.d("result shared preferences : %s", sharedPreferences.getString("kk", "")); } @Override diff --git a/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/MainInterface.java b/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/MainInterface.java index f588a77..304cf12 100644 --- a/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/MainInterface.java +++ b/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/MainInterface.java @@ -2,6 +2,7 @@ import com.wisdomlanna.www.dagger2_mvp_example.api.dao.UserInfoDao; import com.wisdomlanna.www.dagger2_mvp_example.ui.base.BaseInterface; +import com.wisdomlanna.www.dagger2_mvp_example.ui.event.TestBusEvent; class MainInterface { @@ -9,6 +10,10 @@ interface View extends BaseInterface.View { void showResultPlus(int result); void showResultUserInfoGitHubApi(UserInfoDao dao); + + void showResultBusTag(int result); + + void showResultBusTestBusEvent(TestBusEvent event); } interface Presenter { diff --git a/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/MainPresenter.java b/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/MainPresenter.java index 6f22e2a..e891d53 100644 --- a/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/MainPresenter.java +++ b/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/MainPresenter.java @@ -1,11 +1,15 @@ package com.wisdomlanna.www.dagger2_mvp_example.ui; import com.hwangjr.rxbus.RxBus; +import com.hwangjr.rxbus.annotation.Subscribe; +import com.hwangjr.rxbus.annotation.Tag; +import com.hwangjr.rxbus.thread.EventThread; import com.wisdomlanna.www.dagger2_mvp_example.R; import com.wisdomlanna.www.dagger2_mvp_example.api.BaseSubscriber; import com.wisdomlanna.www.dagger2_mvp_example.api.dao.UserInfoDao; import com.wisdomlanna.www.dagger2_mvp_example.api.service.GitHubApi; import com.wisdomlanna.www.dagger2_mvp_example.ui.base.BasePresenter; +import com.wisdomlanna.www.dagger2_mvp_example.ui.event.TestBusEvent; import javax.inject.Inject; @@ -76,11 +80,28 @@ public void onViewDestroy() { @Override public void onViewStart() { - if (getView() != null) RxBus.get().register(this); + if (getView() != null) { + RxBus.get().register(this); + TestBusEvent event = new TestBusEvent(); + event.setName("Jedsada Tiwongvorakul"); + event.setAge(22); + RxBus.get().post("tag_test", 555); + RxBus.get().post(event); + } } @Override public void onViewStop() { if (getView() != null) RxBus.get().unregister(this); } + + @Subscribe(thread = EventThread.MAIN_THREAD, tags = {@Tag("tag_test")}) + public void busTestTag(Integer result) { + if (getView() != null) getView().showResultBusTag(result); + } + + @Subscribe(thread = EventThread.MAIN_THREAD) + public void busTestObj(TestBusEvent event) { + if (getView() != null) getView().showResultBusTestBusEvent(event); + } } \ No newline at end of file diff --git a/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/base/BaseActivity.java b/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/base/BaseActivity.java index a432497..b914c22 100644 --- a/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/base/BaseActivity.java +++ b/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/base/BaseActivity.java @@ -116,14 +116,12 @@ public void unAuthorizedApi() { protected void onStart() { super.onStart(); startActivity(); - getPresenter().onViewStart(); } @Override protected void onStop() { super.onStop(); stopActivity(); - getPresenter().onViewStop(); } @Override diff --git a/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/base/RxBusProvider.java b/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/base/RxBusProvider.java deleted file mode 100644 index 31577d4..0000000 --- a/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/base/RxBusProvider.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.wisdomlanna.www.dagger2_mvp_example.ui.base; - -import com.hwangjr.rxbus.Bus; - -public class RxBusProvider { - - private static Bus sBus; - - public static synchronized Bus get() { - if (sBus == null) { - sBus = new Bus(); - } - return sBus; - } -} diff --git a/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/event/TestBusEvent.java b/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/event/TestBusEvent.java new file mode 100644 index 0000000..4dff057 --- /dev/null +++ b/app/src/main/java/com/wisdomlanna/www/dagger2_mvp_example/ui/event/TestBusEvent.java @@ -0,0 +1,23 @@ +package com.wisdomlanna.www.dagger2_mvp_example.ui.event; + +public class TestBusEvent { + + private String name; + private int age; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} diff --git a/app/src/test/java/com/wisdomlanna/www/dagger2_mvp_example/ui/MainPresenterTest.java b/app/src/test/java/com/wisdomlanna/www/dagger2_mvp_example/ui/MainPresenterTest.java index 448a390..283eceb 100644 --- a/app/src/test/java/com/wisdomlanna/www/dagger2_mvp_example/ui/MainPresenterTest.java +++ b/app/src/test/java/com/wisdomlanna/www/dagger2_mvp_example/ui/MainPresenterTest.java @@ -7,6 +7,7 @@ import com.wisdomlanna.www.dagger2_mvp_example.R; import com.wisdomlanna.www.dagger2_mvp_example.api.dao.UserInfoDao; import com.wisdomlanna.www.dagger2_mvp_example.api.service.GitHubApi; +import com.wisdomlanna.www.dagger2_mvp_example.ui.event.TestBusEvent; import com.wisdomlanna.www.dagger2_mvp_example.utils.JsonMockUtility; import com.wisdomlanna.www.dagger2_mvp_example.utils.RxSchedulersOverrideRule; @@ -28,6 +29,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; +import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.times; @@ -157,6 +159,8 @@ public void testViewCreate() throws Exception { public void testRxBusRegister() throws Exception { presenter.onViewStart(); verify(RxBus.get(), times(1)).register(presenter); + verify(RxBus.get(), times(1)).post("tag_test", 555); + verify(RxBus.get(), times(1)).post(any(TestBusEvent.class)); } @Test