### Question:
ConstraintTracker { public void addListener(ConstraintListener<T> listener) { if (mListeners.add(listener)) { if (mListeners.size() == 1) { mCurrentState = getInitialState(); Log.d(TAG, String.format("%s: initial state = %s", getClass().getSimpleName(), mCurrentState)); startTracking(); } listener.onConstraintChanged(mCurrentState); } } ConstraintTracker(Context context); void addListener(ConstraintListener<T> listener); void removeListener(ConstraintListener<T> listener); void setState(T newState); abstract T getInitialState(); abstract void startTracking(); abstract void stopTracking();  }### Answer:
@Test public void testAddListener_manyListeners_doesNotCallGetInitialStateAndStartTrackingTwice() { ConstraintListener<Boolean> constraintListener1 = mock(ConstraintListener.class); ConstraintListener<Boolean> constraintListener2 = mock(ConstraintListener.class); assertThat(mTracker.mIsTracking, is(false)); assertThat(mTracker.mStartTrackingCount, is(0)); assertThat(mTracker.mGetInitialStateCount, is(0)); mTracker.addListener(constraintListener1); assertThat(mTracker.mIsTracking, is(true)); assertThat(mTracker.mStartTrackingCount, is(1)); assertThat(mTracker.mGetInitialStateCount, is(1)); mTracker.addListener(constraintListener2); assertThat(mTracker.mIsTracking, is(true)); assertThat(mTracker.mStartTrackingCount, is(1)); assertThat(mTracker.mGetInitialStateCount, is(1)); }
@Test public void testAddListener_sameListener_doesNotCallGetInitialStateAndStartTrackingTwice() { ConstraintListener<Boolean> constraintListener = mock(ConstraintListener.class); assertThat(mTracker.mIsTracking, is(false)); assertThat(mTracker.mStartTrackingCount, is(0)); assertThat(mTracker.mGetInitialStateCount, is(0)); mTracker.addListener(constraintListener); assertThat(mTracker.mIsTracking, is(true)); assertThat(mTracker.mStartTrackingCount, is(1)); assertThat(mTracker.mGetInitialStateCount, is(1)); mTracker.addListener(constraintListener); assertThat(mTracker.mIsTracking, is(true)); assertThat(mTracker.mStartTrackingCount, is(1)); assertThat(mTracker.mGetInitialStateCount, is(1)); }
@Test public void testAddListener_notifiesAddedListener() { ConstraintListener<Boolean> constraintListener1 = mock(ConstraintListener.class); ConstraintListener<Boolean> constraintListener2 = mock(ConstraintListener.class); mTracker.mInitialState = true; mTracker.addListener(constraintListener1); verify(constraintListener1).onConstraintChanged(true); mTracker.addListener(constraintListener2); verify(constraintListener2).onConstraintChanged(true); verifyNoMoreInteractions(constraintListener1); }
@Test public void testAddListener_constraintNotSet() { ConstraintListener<Boolean> constraintListener1 = mock(ConstraintListener.class); ConstraintListener<Boolean> constraintListener2 = mock(ConstraintListener.class); mTracker.addListener(constraintListener1); mTracker.addListener(constraintListener2); verify(constraintListener1).onConstraintChanged(null); verify(constraintListener2).onConstraintChanged(null); }