@Test(expected = NullPointerException.class) public void registerFeatures_should_throwExceptionWhenBindErrorDelegateIsNotSetAndAnExceptionOccurs() throws Exception { //GIVEN final LinearLayout parent = new LinearLayout(createMock(Context.class)); StubAdapterViewTypeDelegate stubAdapterViewTypeDelegate = createNiceMock(StubAdapterViewTypeDelegate.class); expect(stubAdapterViewTypeDelegate.getViewType()).andReturn(0).anyTimes(); stubAdapterViewTypeDelegate.bindViewHolder( anyObject(ViewHolder.class), eq("a0"), eq(singletonList("payload"))); expectLastCall().andThrow(new NullPointerException()); replay(stubAdapterViewTypeDelegate); List<ViewItem> items = new ArrayList<>(); List<FeatureController<String>> featureControllers = singletonList( new StubFeatureController<>( asList(stubAdapterViewTypeDelegate), items)); FeaturesAdapter<String> featuresAdapter = new FeaturesAdapter<>(featureControllers); fixAdapterForTesting(featuresAdapter); items.add(new ViewItem<>("a0", stubAdapterViewTypeDelegate)); featuresAdapter.updateFeatureItems("a"); //WHEN featuresAdapter.onBindViewHolder(new ViewHolder(parent) {}, 0, singletonList("payload")); //THEN verify(stubAdapterViewTypeDelegate); }
@Test public void onBindViewHolder_with_payload_should_useTheRightAdapterViewTypeDelegateForAGivenPosition() throws Exception { //GIVEN final LinearLayout parent = new LinearLayout(createMock(Context.class)); StubAdapterViewTypeDelegate stubAdapterViewTypeDelegate0 = createNiceMock(StubAdapterViewTypeDelegate.class); StubAdapterViewTypeDelegate stubAdapterViewTypeDelegate1 = createNiceMock(StubAdapterViewTypeDelegate.class); expect(stubAdapterViewTypeDelegate0.getViewType()).andReturn(0).anyTimes(); expect(stubAdapterViewTypeDelegate1.getViewType()).andReturn(1).anyTimes(); stubAdapterViewTypeDelegate1.bindViewHolder( anyObject(ViewHolder.class), eq("a1"), eq(singletonList("payload"))); replay(stubAdapterViewTypeDelegate0, stubAdapterViewTypeDelegate1); List<ViewItem> items = new ArrayList<>(); List<FeatureController<String>> featureControllers = singletonList( new StubFeatureController<>( asList(stubAdapterViewTypeDelegate0, stubAdapterViewTypeDelegate1), items)); FeaturesAdapter<String> featuresAdapter = new FeaturesAdapter<>(featureControllers); fixAdapterForTesting(featuresAdapter); items.add(new ViewItem<>("a0", stubAdapterViewTypeDelegate0)); items.add(new ViewItem<>("a1", stubAdapterViewTypeDelegate1)); featuresAdapter.updateFeatureItems("a"); //WHEN featuresAdapter.onBindViewHolder(new ViewHolder(parent) {}, 1, singletonList("payload")); //THEN verify(stubAdapterViewTypeDelegate0, stubAdapterViewTypeDelegate1); }
@Test public void onBindViewHolder_should_useTheRightAdapterViewTypeDelegateForAGivenPosition() throws Exception { //GIVEN final LinearLayout parent = new LinearLayout(createMock(Context.class)); StubAdapterViewTypeDelegate stubAdapterViewTypeDelegate0 = createNiceMock(StubAdapterViewTypeDelegate.class); StubAdapterViewTypeDelegate stubAdapterViewTypeDelegate1 = createNiceMock(StubAdapterViewTypeDelegate.class); expect(stubAdapterViewTypeDelegate0.getViewType()).andReturn(0).anyTimes(); expect(stubAdapterViewTypeDelegate1.getViewType()).andReturn(1).anyTimes(); stubAdapterViewTypeDelegate1.bindViewHolder(anyObject(ViewHolder.class), eq("a1")); replay(stubAdapterViewTypeDelegate0, stubAdapterViewTypeDelegate1); List<ViewItem> items = new ArrayList<>(); List<FeatureController<String>> featureControllers = singletonList( new StubFeatureController<>( asList(stubAdapterViewTypeDelegate0, stubAdapterViewTypeDelegate1), items)); FeaturesAdapter<String> featuresAdapter = new FeaturesAdapter<>(featureControllers); fixAdapterForTesting(featuresAdapter); items.add(new ViewItem<>("a0", stubAdapterViewTypeDelegate0)); items.add(new ViewItem<>("a1", stubAdapterViewTypeDelegate1)); featuresAdapter.updateFeatureItems("a"); //WHEN featuresAdapter.onBindViewHolder(new ViewHolder(parent) {}, 1); //THEN verify(stubAdapterViewTypeDelegate0, stubAdapterViewTypeDelegate1); }
@Test public void registerFeatures_should_callTheBindErrorDelegateWhenThisIsSetAndAnExceptionOccurs() throws Exception { //GIVEN final LinearLayout parent = new LinearLayout(createMock(Context.class)); StubAdapterViewTypeDelegate stubAdapterViewTypeDelegate = createNiceMock(StubAdapterViewTypeDelegate.class); expect(stubAdapterViewTypeDelegate.getViewType()).andReturn(0).anyTimes(); stubAdapterViewTypeDelegate.bindViewHolder( anyObject(ViewHolder.class), eq("a0"), eq(singletonList("payload"))); expectLastCall().andThrow(new NullPointerException()); replay(stubAdapterViewTypeDelegate); List<ViewItem> items = new ArrayList<>(); List<FeatureController<String>> featureControllers = singletonList( new StubFeatureController<>( asList(stubAdapterViewTypeDelegate), items)); FeaturesAdapter<String> featuresAdapter = new FeaturesAdapter<>(featureControllers); FeaturesAdapterErrorHandler featuresAdapterErrorHandler = createMock(FeaturesAdapterErrorHandler.class); featuresAdapterErrorHandler.onBindViewHolderError(anyObject(Throwable.class), eq(0)); featuresAdapter.setFeaturesAdapterErrorHandler(featuresAdapterErrorHandler); replay(featuresAdapterErrorHandler); fixAdapterForTesting(featuresAdapter); items.add(new ViewItem<>("a0", stubAdapterViewTypeDelegate)); featuresAdapter.updateFeatureItems("a"); //WHEN featuresAdapter.onBindViewHolder(new ViewHolder(parent) {}, 0, singletonList("payload")); //THEN verify(featuresAdapterErrorHandler, stubAdapterViewTypeDelegate); }