Use Mockito for verifying that a listener has been called. am: 8cf1dce2d2

am: 7aec4ef1bf

* commit '7aec4ef1bfc34dad7863b33301fecd58df908693':
  Use Mockito for verifying that a listener has been called.

GitOrigin-RevId=f9956872843d264ee5055fa07168effb83ccf154

PiperOrigin-RevId: 140559699
This commit is contained in:
Kirill Grouchnikov 2016-11-30 03:48:25 +00:00 committed by Travis Collins
parent eac74bbf58
commit 3b761d62d2
2 changed files with 12 additions and 14 deletions

View File

@ -13,6 +13,9 @@ dependencies {
androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.1') {
exclude module: 'support-annotations'
}
androidTestCompile 'org.mockito:mockito-core:1.9.5'
androidTestCompile 'com.google.dexmaker:dexmaker:1.0'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.0'
testCompile 'junit:junit:4.12'
}

View File

@ -47,6 +47,7 @@ import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.*;
import static org.hamcrest.core.AllOf.allOf;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class NavigationViewTest
extends BaseInstrumentationTestCase<NavigationViewActivity> {
@ -58,8 +59,6 @@ public class NavigationViewTest
private NavigationView mNavigationView;
private int mLastSelectedNavigationItemId;
public NavigationViewTest() {
super(NavigationViewActivity.class);
}
@ -398,14 +397,9 @@ public class NavigationViewTest
mDrawerLayout.isDrawerOpen(GravityCompat.START));
// Register a listener
mNavigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
mLastSelectedNavigationItemId = item.getItemId();
return false;
}
});
NavigationView.OnNavigationItemSelectedListener mockedListener =
mock(NavigationView.OnNavigationItemSelectedListener.class);
mNavigationView.setNavigationItemSelectedListener(mockedListener);
// Click one of our items
onView(allOf(withText(mMenuStringContent.get(R.id.destination_profile)),
@ -414,10 +408,11 @@ public class NavigationViewTest
assertTrue("Drawer is still open after click",
mDrawerLayout.isDrawerOpen(GravityCompat.START));
// And that our listener has been notified of the click
assertEquals("Selected item ID", R.id.destination_profile, mLastSelectedNavigationItemId);
verify(mockedListener, times(1)).onNavigationItemSelected(
mNavigationView.getMenu().findItem(R.id.destination_profile));
// Reset the tracker field and set null listener
mLastSelectedNavigationItemId = -1;
// Set null listener to test that the next click is not going to notify the
// previously set listener
mNavigationView.setNavigationItemSelectedListener(null);
// Click one of our items
@ -427,7 +422,7 @@ public class NavigationViewTest
assertTrue("Drawer is still open after click",
mDrawerLayout.isDrawerOpen(GravityCompat.START));
// And that our previous listener has not been notified of the click
assertEquals("Selected item ID", -1, mLastSelectedNavigationItemId);
verifyNoMoreInteractions(mockedListener);
}
private void verifyCheckedAppearance(@IdRes int checkedItemId,