### Question:
StateDraweeView extends SimpleDraweeView { public void setState(@NonNull State state) { if (this.state != null) { this.state.detach(this); } this.state = state; if (isViewAttachedToWindow()) { this.state.attach(this); } } StateDraweeView(@NonNull final Context context, @NonNull final State state);  StateDraweeView(final Context context, final GenericDraweeHierarchy hierarchy);  StateDraweeView(final Context context);  StateDraweeView(final Context context, final AttributeSet attrs);  StateDraweeView(final Context context, final AttributeSet attrs, final int defStyle);  StateDraweeView(final Context context, final AttributeSet attrs, final int defStyleAttr,
        final int defStyleRes); void setState(@NonNull State state); @Override void setImageLevel(final int level); @Override void setEnabled(final boolean enabled); void setOnLevelListener(@Nullable OnLevelListener listener); void setOnEnabledListener(@Nullable OnEnabledListener listener); @Override void setImageDrawable(final Drawable drawable); @Override void setImageBitmap(final Bitmap bm); @Override void setImageResource(final int resId); @Override void setImageURI(final Uri uri); @Override void setActualImageResource(@DrawableRes final int resourceId); @Override void setActualImageResource(@DrawableRes final int resourceId,
        @javax.annotation.Nullable final Object callerContext); @Override void setImageURI(final Uri uri, @javax.annotation.Nullable final Object callerContext); @Override void setImageURI(@javax.annotation.Nullable final String uriString); @Override void setImageURI(@javax.annotation.Nullable final String uriString,
        @javax.annotation.Nullable final Object callerContext); @Override String toString();  }### Answer:
@Test public void test_StateIsRetained() { State testState; final StateDraweeView view = new StateDraweeView(RuntimeEnvironment.application); view.setState(testState = new EnableState()); Assert.assertEquals(testState, view.state); view.setState(testState = new LevelState(0, 0)); Assert.assertEquals(testState, view.state); }
@Test public void test_StateDetachesOldOnes() { final StateDraweeView view = Mockito.spy(new StateDraweeView(RuntimeEnvironment.application)); State testState1 = Mockito.spy(EnableState.class); testState1.add(BinaryState.STATE_OFF, R.drawable.ui_ic_clear); testState1.add(BinaryState.STATE_ON, R.drawable.ui_ic_clear); State testState2 = Mockito.spy(EnableState.class); testState2.add(BinaryState.STATE_OFF, R.drawable.ui_ic_clear); testState1.add(BinaryState.STATE_ON, R.drawable.ui_ic_clear); view.setState(testState1); view.setState(testState2); Mockito.verify(testState1).detach(view); }