diff --git a/serenity-app/build.gradle b/serenity-app/build.gradle index 0c8633b47..ff8d6695a 100644 --- a/serenity-app/build.gradle +++ b/serenity-app/build.gradle @@ -110,7 +110,7 @@ android { unitTests.returnDefaultValues = true unitTests.all { maxParallelForks = 2 - forkEvery = 50 + forkEvery = 75 minHeapSize = "512m" maxHeapSize = "1024m" } diff --git a/serenity-app/src/main/java/us/nineworlds/serenity/ui/listeners/SettingsMenuDrawerOnItemClickedListener.java b/serenity-app/src/main/java/us/nineworlds/serenity/ui/listeners/SettingsMenuDrawerOnItemClickedListener.java index 0e402f03a..bbb684a09 100644 --- a/serenity-app/src/main/java/us/nineworlds/serenity/ui/listeners/SettingsMenuDrawerOnItemClickedListener.java +++ b/serenity-app/src/main/java/us/nineworlds/serenity/ui/listeners/SettingsMenuDrawerOnItemClickedListener.java @@ -23,10 +23,12 @@ package us.nineworlds.serenity.ui.listeners; +import android.app.Activity; +import android.content.Context; +import android.content.ContextWrapper; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; -import us.nineworlds.serenity.ui.activity.SerenityDrawerLayoutActivity; import us.nineworlds.serenity.ui.preferences.SerenityPreferenceActivity; import us.nineworlds.serenity.widgets.DrawerLayout; @@ -38,10 +40,24 @@ public SettingsMenuDrawerOnItemClickedListener(DrawerLayout drawer) { } @Override public void onClick(View view) { - SerenityDrawerLayoutActivity activity = (SerenityDrawerLayoutActivity) view.getContext(); + Activity activity = getActivity(view.getContext()); + if (activity == null) { + return; + } Intent i = new Intent(activity, SerenityPreferenceActivity.class); activity.startActivity(i); menuDrawer.closeDrawers(); } + + protected Activity getActivity(Context contextWrapper) { + Context context = contextWrapper; + while (context instanceof ContextWrapper) { + if (context instanceof Activity) { + return (Activity) context; + } + context = ((ContextWrapper) context).getBaseContext(); + } + return null; + } } diff --git a/serenity-app/src/test/java/us/nineworlds/serenity/ui/listeners/SettingsMenuDrawerOnItemClickedListenerTest.java b/serenity-app/src/test/java/us/nineworlds/serenity/ui/listeners/SettingsMenuDrawerOnItemClickedListenerTest.java index 75d421c82..a323b1cb2 100644 --- a/serenity-app/src/test/java/us/nineworlds/serenity/ui/listeners/SettingsMenuDrawerOnItemClickedListenerTest.java +++ b/serenity-app/src/test/java/us/nineworlds/serenity/ui/listeners/SettingsMenuDrawerOnItemClickedListenerTest.java @@ -24,15 +24,14 @@ package us.nineworlds.serenity.ui.listeners; import android.content.Intent; +import android.content.MutableContextWrapper; import android.view.View; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; - import us.nineworlds.serenity.BuildConfig; import us.nineworlds.serenity.ui.activity.SerenityDrawerLayoutActivity; import us.nineworlds.serenity.widgets.DrawerLayout; @@ -43,54 +42,51 @@ import static org.mockito.Mockito.verify; import static org.mockito.MockitoAnnotations.initMocks; -// UnitTestCodeMash2015 -// Mockito with out building Activity. -// -// Using Robolectric.buildActivity(Activity.class).create().get() adds about 300ms everytime it is called. - @RunWith(RobolectricTestRunner.class) @Config(constants = BuildConfig.class) public class SettingsMenuDrawerOnItemClickedListenerTest { - @Mock - protected DrawerLayout mockDrawerLayout; + @Mock DrawerLayout mockDrawerLayout; + @Mock View mockView; + @Mock SerenityDrawerLayoutActivity mockSerenityDrawerLayoutActivity; + + SettingsMenuDrawerOnItemClickedListener onItemClickedListener; + + @Before public void setUp() { + initMocks(this); + onItemClickedListener = new SettingsMenuDrawerOnItemClickedListener(mockDrawerLayout); + } - @Mock - protected View mockView; + @Test public void onClickStartsSettingActivity() { + doNothing().when(mockSerenityDrawerLayoutActivity).startActivity(any(Intent.class)); + doReturn(mockSerenityDrawerLayoutActivity).when(mockView).getContext(); - @Mock - protected SerenityDrawerLayoutActivity mockSerenityDrawerLayoutActivity; + onItemClickedListener.onClick(mockView); - protected SettingsMenuDrawerOnItemClickedListener onItemClickedListener; + verify(mockSerenityDrawerLayoutActivity).startActivity(any(Intent.class)); + verify(mockView).getContext(); + } - @Before - public void setUp() { - initMocks(this); - onItemClickedListener = new SettingsMenuDrawerOnItemClickedListener( - mockDrawerLayout); - } + @Test public void onClickClosesMenuDrawer() { + doNothing().when(mockSerenityDrawerLayoutActivity).startActivity(any(Intent.class)); + doReturn(mockSerenityDrawerLayoutActivity).when(mockView).getContext(); + doNothing().when(mockDrawerLayout).closeDrawers(); - @Test - public void onClickStartsSettingActivity() { - doNothing().when(mockSerenityDrawerLayoutActivity).startActivity( - any(Intent.class)); - doReturn(mockSerenityDrawerLayoutActivity).when(mockView).getContext(); + onItemClickedListener.onClick(mockView); - onItemClickedListener.onClick(mockView); + verify(mockDrawerLayout).closeDrawers(); + verify(mockView).getContext(); + } - verify(mockSerenityDrawerLayoutActivity).startActivity( - any(Intent.class)); - } + @Test public void onClickDiscoversActivityFromContextWrapper() { + MutableContextWrapper context = new MutableContextWrapper(mockSerenityDrawerLayoutActivity); - @Test - public void onClickClosesMenuDrawer() { - doNothing().when(mockSerenityDrawerLayoutActivity).startActivity( - any(Intent.class)); - doReturn(mockSerenityDrawerLayoutActivity).when(mockView).getContext(); - doNothing().when(mockDrawerLayout).closeDrawers(); + doNothing().when(mockSerenityDrawerLayoutActivity).startActivity(any(Intent.class)); + doReturn(context).when(mockView).getContext(); - onItemClickedListener.onClick(mockView); + onItemClickedListener.onClick(mockView); - verify(mockDrawerLayout).closeDrawers(); - } + verify(mockSerenityDrawerLayoutActivity).startActivity(any(Intent.class)); + verify(mockView).getContext(); + } }