Search in sources :

Example 1 with AdapterDataObserver

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

the class ModelClickListenerTest method modelClickListenerIsDiffed.

@Test
public void modelClickListenerIsDiffed() {
    // Internally we wrap the model click listener with an anonymous click listener. We can't hash
    // the anonymous click listener since that changes the model state, instead our anonymous
    // click listener should use the hashCode of the user's click listener
    ModelClickListener modelClickListener = new ModelClickListener();
    ViewClickListener viewClickListener = new ViewClickListener();
    TestController controller = new TestController();
    AdapterDataObserver observerMock = mock(AdapterDataObserver.class);
    controller.getAdapter().registerAdapterDataObserver(observerMock);
    ModelWithClickListener_ model = new ModelWithClickListener_();
    controller.setModel(model);
    controller.requestModelBuild();
    verify(observerMock).onItemRangeInserted(eq(0), eq(1));
    model = new ModelWithClickListener_();
    model.clickListener(modelClickListener);
    controller.setModel(model);
    lifecycleHelper.buildModelsAndBind(controller);
    // The second update shouldn't cause a item change
    model = new ModelWithClickListener_();
    model.clickListener(modelClickListener);
    controller.setModel(model);
    lifecycleHelper.buildModelsAndBind(controller);
    model = new ModelWithClickListener_();
    model.clickListener(viewClickListener);
    controller.setModel(model);
    lifecycleHelper.buildModelsAndBind(controller);
    verify(observerMock, times(2)).onItemRangeChanged(eq(0), eq(1), any());
    verifyNoMoreInteractions(observerMock);
}
Also used : ModelWithClickListener_(com.airbnb.epoxy.integrationtest.ModelWithClickListener_) AdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver) Test(org.junit.Test)

Example 2 with AdapterDataObserver

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

the class DataBindingModelIntegrationTest method typesWithOutHashCodeAreNotDiffed.

@Test
public void typesWithOutHashCodeAreNotDiffed() {
    SimpleEpoxyController controller = new SimpleEpoxyController();
    AdapterDataObserver observerMock = mock(AdapterDataObserver.class);
    controller.getAdapter().registerAdapterDataObserver(observerMock);
    ModelWithDataBindingBindingModel_ firstModel = new ModelWithDataBindingBindingModel_().clickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        }
    }).id(1);
    controller.setModels(Collections.singletonList(firstModel));
    verify(observerMock).onItemRangeInserted(0, 1);
    ModelWithDataBindingBindingModel_ secondModel = new ModelWithDataBindingBindingModel_().clickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        }
    }).id(1);
    controller.setModels(Collections.singletonList(secondModel));
    verifyNoMoreInteractions(observerMock);
}
Also used : OnClickListener(android.view.View.OnClickListener) ModelWithDataBindingBindingModel_(com.airbnb.epoxy.integrationtest.ModelWithDataBindingBindingModel_) View(android.view.View) AdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver) Test(org.junit.Test)

Example 3 with AdapterDataObserver

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

the class EpoxyControllerTest method basicBuildModels.

@Test
public void basicBuildModels() {
    AdapterDataObserver observer = mock(AdapterDataObserver.class);
    EpoxyController controller = new EpoxyController() {

        @Override
        protected void buildModels() {
            new TestModel().addTo(this);
        }

        @Override
        protected void onExceptionSwallowed(RuntimeException exception) {
            noExceptionsDuringBasicBuildModels = false;
        }
    };
    controller.getAdapter().registerAdapterDataObserver(observer);
    controller.requestModelBuild();
    assertTrue(noExceptionsDuringBasicBuildModels);
    assertEquals(1, controller.getAdapter().getItemCount());
    verify(observer).onItemRangeInserted(0, 1);
    verifyNoMoreInteractions(observer);
}
Also used : AdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver) Test(org.junit.Test)

Example 4 with AdapterDataObserver

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

the class DataBindingModelIntegrationTest method typesWithHashCodeAreDiffed.

@Test
public void typesWithHashCodeAreDiffed() {
    SimpleEpoxyController controller = new SimpleEpoxyController();
    AdapterDataObserver observerMock = mock(AdapterDataObserver.class);
    controller.getAdapter().registerAdapterDataObserver(observerMock);
    ModelWithDataBindingBindingModel_ firstModel = new ModelWithDataBindingBindingModel_().stringValue("value1").id(1);
    controller.setModels(Collections.singletonList(firstModel));
    verify(observerMock).onItemRangeInserted(0, 1);
    ModelWithDataBindingBindingModel_ secondModel = new ModelWithDataBindingBindingModel_().stringValue("value2").id(1);
    controller.setModels(Collections.singletonList(secondModel));
    verify(observerMock).onItemRangeChanged(eq(0), eq(1), any());
    verifyNoMoreInteractions(observerMock);
}
Also used : ModelWithDataBindingBindingModel_(com.airbnb.epoxy.integrationtest.ModelWithDataBindingBindingModel_) AdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver) Test(org.junit.Test)

Example 5 with AdapterDataObserver

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

the class ModelClickListenerTest method viewClickListenerIsDiffed.

@Test
public void viewClickListenerIsDiffed() {
    TestController controller = new TestController();
    AdapterDataObserver observerMock = mock(AdapterDataObserver.class);
    controller.getAdapter().registerAdapterDataObserver(observerMock);
    ModelWithClickListener_ model = new ModelWithClickListener_();
    controller.setModel(model);
    controller.requestModelBuild();
    verify(observerMock).onItemRangeInserted(eq(0), eq(1));
    ViewClickListener viewClickListener = new ViewClickListener();
    model = new ModelWithClickListener_();
    model.clickListener(viewClickListener);
    controller.setModel(model);
    controller.requestModelBuild();
    // The second update shouldn't cause a item change
    model = new ModelWithClickListener_();
    model.clickListener(viewClickListener);
    controller.setModel(model);
    controller.requestModelBuild();
    ModelClickListener modelClickListener = new ModelClickListener();
    model = new ModelWithClickListener_();
    model.clickListener(modelClickListener);
    controller.setModel(model);
    controller.requestModelBuild();
    verify(observerMock, times(2)).onItemRangeChanged(eq(0), eq(1), any());
    verifyNoMoreInteractions(observerMock);
}
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