Search in sources :

Example 6 with AdapterDataObserver

use of androidx.recyclerview.widget.RecyclerView.AdapterDataObserver in project epoxy by airbnb.

the class EpoxyControllerTest method multipleMoves.

@Test
public void multipleMoves() {
    AdapterDataObserver observer = mock(AdapterDataObserver.class);
    final List<TestModel> testModels = new ArrayList<>();
    testModels.add(new TestModel(1));
    testModels.add(new TestModel(2));
    testModels.add(new TestModel(3));
    EpoxyController controller = new EpoxyController() {

        @Override
        protected void buildModels() {
            add(testModels);
        }
    };
    EpoxyControllerAdapter adapter = controller.getAdapter();
    adapter.registerAdapterDataObserver(observer);
    controller.requestModelBuild();
    testModels.add(0, testModels.remove(1));
    controller.moveModel(1, 0);
    verify(observer).onItemRangeMoved(1, 0, 1);
    testModels.add(2, testModels.remove(1));
    controller.moveModel(1, 2);
    verify(observer).onItemRangeMoved(1, 2, 1);
    assertEquals(testModels, adapter.getCurrentModels());
    controller.requestModelBuild();
    assertEquals(testModels, adapter.getCurrentModels());
}
Also used : ArrayList(java.util.ArrayList) AdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver) Test(org.junit.Test)

Example 7 with AdapterDataObserver

use of androidx.recyclerview.widget.RecyclerView.AdapterDataObserver in project epoxy by airbnb.

the class EpoxyControllerTest method moveModelOtherWay.

@Test
public void moveModelOtherWay() {
    AdapterDataObserver observer = mock(AdapterDataObserver.class);
    final List<TestModel> testModels = new ArrayList<>();
    testModels.add(new TestModel(1));
    testModels.add(new TestModel(2));
    testModels.add(new TestModel(3));
    EpoxyController controller = new EpoxyController() {

        @Override
        protected void buildModels() {
            add(testModels);
        }
    };
    EpoxyControllerAdapter adapter = controller.getAdapter();
    adapter.registerAdapterDataObserver(observer);
    controller.requestModelBuild();
    verify(observer).onItemRangeInserted(0, 3);
    testModels.add(2, testModels.remove(1));
    controller.moveModel(1, 2);
    verify(observer).onItemRangeMoved(1, 2, 1);
    assertEquals(testModels, adapter.getCurrentModels());
    controller.requestModelBuild();
    assertEquals(testModels, adapter.getCurrentModels());
    verifyNoMoreInteractions(observer);
}
Also used : ArrayList(java.util.ArrayList) AdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver) Test(org.junit.Test)

Example 8 with AdapterDataObserver

use of androidx.recyclerview.widget.RecyclerView.AdapterDataObserver in project epoxy by airbnb.

the class EpoxyControllerTest method moveModel.

@Test
public void moveModel() {
    AdapterDataObserver observer = mock(AdapterDataObserver.class);
    final List<TestModel> testModels = new ArrayList<>();
    testModels.add(new TestModel(1));
    testModels.add(new TestModel(2));
    testModels.add(new TestModel(3));
    EpoxyController controller = new EpoxyController() {

        @Override
        protected void buildModels() {
            add(testModels);
        }
    };
    EpoxyControllerAdapter adapter = controller.getAdapter();
    adapter.registerAdapterDataObserver(observer);
    controller.requestModelBuild();
    verify(observer).onItemRangeInserted(0, 3);
    testModels.add(0, testModels.remove(1));
    controller.moveModel(1, 0);
    verify(observer).onItemRangeMoved(1, 0, 1);
    assertEquals(testModels, adapter.getCurrentModels());
    controller.requestModelBuild();
    assertEquals(testModels, adapter.getCurrentModels());
    verifyNoMoreInteractions(observer);
}
Also used : ArrayList(java.util.ArrayList) AdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver) Test(org.junit.Test)

Example 9 with AdapterDataObserver

use of androidx.recyclerview.widget.RecyclerView.AdapterDataObserver in project epoxy by airbnb.

the class OnModelBindListenerTest method bindListenerChangesHashCode.

@Test
public void bindListenerChangesHashCode() {
    TestController controller = new TestController();
    AdapterDataObserver observerMock = mock(AdapterDataObserver.class);
    controller.getAdapter().registerAdapterDataObserver(observerMock);
    ModelWithClickListener_ model = new ModelWithClickListener_();
    controller.buildWithModel(model);
    verify(observerMock).onItemRangeInserted(eq(0), eq(1));
    // shouldn't change
    model = new ModelWithClickListener_();
    model.onBind(null);
    controller.buildWithModel(model);
    verify(observerMock, never()).onItemRangeChanged(eq(0), eq(1), any());
    model = new ModelWithClickListener_();
    BindListener listener1 = new BindListener();
    model.onBind(listener1);
    controller.buildWithModel(model);
    verify(observerMock, times(1)).onItemRangeChanged(eq(0), eq(1), any());
    model = new ModelWithClickListener_();
    model.onBind(listener1);
    controller.buildWithModel(model);
    verify(observerMock, times(1)).onItemRangeChanged(eq(0), eq(1), any());
}
Also used : ModelWithClickListener_(com.airbnb.epoxy.integrationtest.ModelWithClickListener_) AdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver) Test(org.junit.Test)

Example 10 with AdapterDataObserver

use of androidx.recyclerview.widget.RecyclerView.AdapterDataObserver in project epoxy by airbnb.

the class OnModelBindListenerTest method nullUnbindListenerChangesHashCode.

@Test
public void nullUnbindListenerChangesHashCode() {
    TestController controller = new TestController();
    AdapterDataObserver observerMock = mock(AdapterDataObserver.class);
    controller.getAdapter().registerAdapterDataObserver(observerMock);
    ModelWithClickListener_ model = new ModelWithClickListener_();
    controller.buildWithModel(model);
    verify(observerMock).onItemRangeInserted(eq(0), eq(1));
    model = new ModelWithClickListener_();
    model.onUnbind(new UnbindListener());
    controller.buildWithModel(model);
    model = new ModelWithClickListener_();
    model.onUnbind(null);
    controller.buildWithModel(model);
    verify(observerMock, times(2)).onItemRangeChanged(eq(0), eq(1), any());
}
Also used : ModelWithClickListener_(com.airbnb.epoxy.integrationtest.ModelWithClickListener_) AdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver) Test(org.junit.Test)

Aggregations

AdapterDataObserver (androidx.recyclerview.widget.RecyclerView.AdapterDataObserver)15 Test (org.junit.Test)14 ModelWithClickListener_ (com.airbnb.epoxy.integrationtest.ModelWithClickListener_)8 ArrayList (java.util.ArrayList)3 ModelWithDataBindingBindingModel_ (com.airbnb.epoxy.integrationtest.ModelWithDataBindingBindingModel_)2 MotionEvent (android.view.MotionEvent)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 GridLayoutManager (androidx.recyclerview.widget.GridLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 ItemAnimator (androidx.recyclerview.widget.RecyclerView.ItemAnimator)1 OnItemTouchListener (androidx.recyclerview.widget.RecyclerView.OnItemTouchListener)1 SimpleItemAnimator (androidx.recyclerview.widget.SimpleItemAnimator)1