Merge "[SuwLib] Set up mockito"
This commit is contained in:
commit
bd1d7582d5
|
@ -16,7 +16,8 @@
|
|||
|
||||
package com.android.setupwizardlib.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import android.support.test.filters.SmallTest;
|
||||
|
@ -30,6 +31,8 @@ import com.android.setupwizardlib.view.HeaderRecyclerView.HeaderAdapter;
|
|||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
/**
|
||||
* Test for {@link com.android.setupwizardlib.view.HeaderRecyclerView}
|
||||
|
@ -41,12 +44,14 @@ public class HeaderRecyclerViewTest {
|
|||
private TestAdapter mWrappedAdapter;
|
||||
private HeaderAdapter mHeaderAdapter;
|
||||
|
||||
private TestDataObserver mObserver;
|
||||
@Mock
|
||||
private RecyclerView.AdapterDataObserver mObserver;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mWrappedAdapter = new TestAdapter();
|
||||
mObserver = new TestDataObserver();
|
||||
|
||||
mHeaderAdapter = new HeaderAdapter(mWrappedAdapter);
|
||||
mHeaderAdapter.registerAdapterDataObserver(mObserver);
|
||||
|
@ -59,7 +64,7 @@ public class HeaderRecyclerViewTest {
|
|||
public void testNotifyChanged() {
|
||||
mWrappedAdapter.notifyDataSetChanged();
|
||||
|
||||
assertEquals("onChanged", mObserver.lastNotification);
|
||||
verify(mObserver).onChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -69,9 +74,7 @@ public class HeaderRecyclerViewTest {
|
|||
public void testNotifyItemChangedNoHeader() {
|
||||
mWrappedAdapter.notifyItemChanged(12);
|
||||
|
||||
assertEquals("onItemRangeChanged", mObserver.lastNotification);
|
||||
assertEquals(12, mObserver.lastArg1);
|
||||
assertEquals(1, mObserver.lastArg2);
|
||||
verify(mObserver).onItemRangeChanged(eq(12), eq(1), eq(null));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -83,9 +86,7 @@ public class HeaderRecyclerViewTest {
|
|||
mHeaderAdapter.setHeader(new View(InstrumentationRegistry.getTargetContext()));
|
||||
mWrappedAdapter.notifyItemChanged(12);
|
||||
|
||||
assertEquals("onItemRangeChanged", mObserver.lastNotification);
|
||||
assertEquals(13, mObserver.lastArg1);
|
||||
assertEquals(1, mObserver.lastArg2);
|
||||
verify(mObserver).onItemRangeChanged(eq(13), eq(1), eq(null));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -95,9 +96,7 @@ public class HeaderRecyclerViewTest {
|
|||
public void testNotifyItemInsertedNoHeader() {
|
||||
mWrappedAdapter.notifyItemInserted(12);
|
||||
|
||||
assertEquals("onItemRangeInserted", mObserver.lastNotification);
|
||||
assertEquals(12, mObserver.lastArg1);
|
||||
assertEquals(1, mObserver.lastArg2);
|
||||
verify(mObserver).onItemRangeInserted(eq(12), eq(1));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -109,9 +108,7 @@ public class HeaderRecyclerViewTest {
|
|||
mHeaderAdapter.setHeader(new View(InstrumentationRegistry.getTargetContext()));
|
||||
mWrappedAdapter.notifyItemInserted(12);
|
||||
|
||||
assertEquals("onItemRangeInserted", mObserver.lastNotification);
|
||||
assertEquals(13, mObserver.lastArg1);
|
||||
assertEquals(1, mObserver.lastArg2);
|
||||
verify(mObserver).onItemRangeInserted(eq(13), eq(1));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -121,9 +118,7 @@ public class HeaderRecyclerViewTest {
|
|||
public void testNotifyItemRemovedNoHeader() {
|
||||
mWrappedAdapter.notifyItemRemoved(12);
|
||||
|
||||
assertEquals("onItemRangeRemoved", mObserver.lastNotification);
|
||||
assertEquals(12, mObserver.lastArg1);
|
||||
assertEquals(1, mObserver.lastArg2);
|
||||
verify(mObserver).onItemRangeRemoved(eq(12), eq(1));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -135,9 +130,7 @@ public class HeaderRecyclerViewTest {
|
|||
mHeaderAdapter.setHeader(new View(InstrumentationRegistry.getTargetContext()));
|
||||
mWrappedAdapter.notifyItemRemoved(12);
|
||||
|
||||
assertEquals("onItemRangeRemoved", mObserver.lastNotification);
|
||||
assertEquals(13, mObserver.lastArg1);
|
||||
assertEquals(1, mObserver.lastArg2);
|
||||
verify(mObserver).onItemRangeRemoved(eq(13), eq(1));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -147,10 +140,7 @@ public class HeaderRecyclerViewTest {
|
|||
public void testNotifyItemMovedNoHeader() {
|
||||
mWrappedAdapter.notifyItemMoved(12, 18);
|
||||
|
||||
assertEquals("onItemRangeMoved", mObserver.lastNotification);
|
||||
assertEquals(12, mObserver.lastArg1);
|
||||
assertEquals(18, mObserver.lastArg2);
|
||||
assertEquals(1, mObserver.lastArg3);
|
||||
verify(mObserver).onItemRangeMoved(eq(12), eq(18), eq(1));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -162,10 +152,7 @@ public class HeaderRecyclerViewTest {
|
|||
mHeaderAdapter.setHeader(new View(InstrumentationRegistry.getTargetContext()));
|
||||
mWrappedAdapter.notifyItemMoved(12, 18);
|
||||
|
||||
assertEquals("onItemRangeMoved", mObserver.lastNotification);
|
||||
assertEquals(13, mObserver.lastArg1);
|
||||
assertEquals(19, mObserver.lastArg2);
|
||||
assertEquals(1, mObserver.lastArg3);
|
||||
verify(mObserver).onItemRangeMoved(eq(13), eq(19), eq(1));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -187,55 +174,4 @@ public class HeaderRecyclerViewTest {
|
|||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test observer which stores the last observed notification.
|
||||
*/
|
||||
// TODO: set up mockito for this instead
|
||||
public static class TestDataObserver extends RecyclerView.AdapterDataObserver {
|
||||
|
||||
public String lastNotification;
|
||||
public int lastArg1 = -1;
|
||||
public int lastArg2 = -1;
|
||||
public int lastArg3 = -1;
|
||||
|
||||
@Override
|
||||
public void onChanged() {
|
||||
super.onChanged();
|
||||
lastNotification = "onChanged";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemRangeChanged(int positionStart, int itemCount) {
|
||||
super.onItemRangeChanged(positionStart, itemCount);
|
||||
lastNotification = "onItemRangeChanged";
|
||||
lastArg1 = positionStart;
|
||||
lastArg2 = itemCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemRangeInserted(int positionStart, int itemCount) {
|
||||
super.onItemRangeInserted(positionStart, itemCount);
|
||||
lastNotification = "onItemRangeInserted";
|
||||
lastArg1 = positionStart;
|
||||
lastArg2 = itemCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
|
||||
super.onItemRangeMoved(fromPosition, toPosition, itemCount);
|
||||
lastNotification = "onItemRangeMoved";
|
||||
lastArg1 = fromPosition;
|
||||
lastArg2 = toPosition;
|
||||
lastArg3 = itemCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemRangeRemoved(int positionStart, int itemCount) {
|
||||
super.onItemRangeRemoved(positionStart, itemCount);
|
||||
lastNotification = "onItemRangeRemoved";
|
||||
lastArg1 = positionStart;
|
||||
lastArg2 = itemCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,10 @@ android.sourceSets {
|
|||
dependencies {
|
||||
androidTestCompile 'com.android.support.test:rules:0.5'
|
||||
androidTestCompile 'com.android.support.test:runner:0.5'
|
||||
androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
|
||||
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
|
||||
androidTestCompile 'junit:junit:4.+'
|
||||
androidTestCompile 'org.mockito:mockito-core:1.9.5'
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue