Skip to content

Commit

Permalink
Issue #365 Correctly obtain the context
Browse files Browse the repository at this point in the history
Devices before Android 5.0 use the AppCompat library
underneath the covers.  This returns a ContextWrapper
instead of Activity.  So need to loop through this an get
the Activity.
  • Loading branch information
kingargyle committed Sep 9, 2017
1 parent 8e56891 commit 15f50e7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 41 deletions.
2 changes: 1 addition & 1 deletion serenity-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ android {
unitTests.returnDefaultValues = true
unitTests.all {
maxParallelForks = 2
forkEvery = 50
forkEvery = 75
minHeapSize = "512m"
maxHeapSize = "1024m"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}

0 comments on commit 15f50e7

Please sign in to comment.