private PropertyViewAttributeSpy createAttribute( boolean withAlwaysPreInitializingView, boolean twoWayBinding) { PropertyViewAttributeSpy viewAttribute = new PropertyViewAttributeSpy(withAlwaysPreInitializingView); viewAttribute.initialize( aPropertyViewAttributeConfig( mock(View.class), aValueModelAttribute(PROPERTY_NAME, twoWayBinding))); return viewAttribute; }
private void bindAttribute(boolean twoWayBinding, boolean preInitializeView) { if (twoWayBinding) when(bindingContext.<Integer>getPropertyValueModel(PROPERTY_NAME)).thenReturn(valueModel); else when(bindingContext.<Integer>getReadOnlyPropertyValueModel(PROPERTY_NAME)) .thenReturn(valueModel); when(bindingContext.shouldPreInitializeViews()).thenReturn(preInitializeView); if (preInitializeView) { attribute.preInitializeView(bindingContext); } attribute.bindTo(bindingContext); }
@Test public void givenAPropertyViewAttributeWithTwoWayBinding_whenTheViewIsUpdated_thenViewShouldNotReceiveAFurtherUpdate() { setupAndBindAttribute(TWO_WAY_BINDING, DONT_PRE_INITIALIZE_VIEW); attribute.simulateViewUpdate(A_NEW_VALUE); assertThat(attribute.viewUpdateNotificationCount, is(0)); }
@Test public void givenAPropertyViewAttributeWithTwoWayBinding_whenTheViewIsUpdated_thenValueModelShouldBeUpdated() { setupAndBindAttribute(TWO_WAY_BINDING, DONT_PRE_INITIALIZE_VIEW); attribute.simulateViewUpdate(A_NEW_VALUE); assertThat(valueModel.getValue(), is(A_NEW_VALUE)); }
@Test public void givenATwoWayBindingViewAttributeOfAlwaysRestoreValueProperty_whenTheViewIsUpdated_thenViewShouldReceiveAnUpdateWithRestoredValue() { attribute = aPropertyViewAttributeSpy() .withPreInitializeView(false) .withTwoWaybinding(true) .withPropertyName(PROPERTY_NAME) .build(); valueModel = new AlwaysRestoreValueModel(); bindAttribute(TWO_WAY_BINDING, DONT_PRE_INITIALIZE_VIEW); attribute.simulateViewUpdate(A_NEW_VALUE); assertThat(attribute.viewUpdateNotificationCount, is(1)); assertThat(attribute.updatedValue, is(AlwaysRestoreValueModel.ALWAYS_RESTORE_VALUE)); }