### Question:
BatteryChargingTracker extends BroadcastReceiverConstraintTracker<Boolean> { @Override public void onBroadcastReceive(Context context, @NonNull Intent intent) { String action = intent.getAction(); if (action == null) { return; } Log.d(TAG, String.format("Received %s", action)); switch (action) { case BatteryManager.ACTION_CHARGING: setState(true); break; case BatteryManager.ACTION_DISCHARGING: setState(false); break; case Intent.ACTION_POWER_CONNECTED: setState(true); break; case Intent.ACTION_POWER_DISCONNECTED: setState(false); break; } } BatteryChargingTracker(Context context); @Override Boolean getInitialState(); @Override IntentFilter getIntentFilter(); @Override void onBroadcastReceive(Context context, @NonNull Intent intent);  }### Answer:
@Test @SmallTest public void testOnBroadcastReceive_invalidIntentAction_doesNotNotifyListeners() { mockContextReturns(createBatteryChangedIntent(true)); mTracker.addListener(mListener); verify(mListener).onConstraintChanged(true); mTracker.onBroadcastReceive( InstrumentationRegistry.getTargetContext(), new Intent("INVALID")); verifyNoMoreInteractions(mListener); }
@Test @SmallTest @SdkSuppress(maxSdkVersion = 22) public void testOnBroadcastReceive_notifiesListeners_beforeApi23() { mockContextReturns(createBatteryChangedIntent(false)); mTracker.addListener(mListener); verify(mListener).onConstraintChanged(false); mTracker.onBroadcastReceive(mMockContext, createChargingIntent(true)); verify(mListener).onConstraintChanged(true); mTracker.onBroadcastReceive(mMockContext, createChargingIntent(false)); verify(mListener, times(2)).onConstraintChanged(false); }
@Test @SmallTest @SdkSuppress(minSdkVersion = 23) public void testOnBroadcastReceive_notifiesListeners_afterApi23() { mockContextReturns(null); mTracker.addListener(mListener); verify(mListener, never()).onConstraintChanged(anyBoolean()); mTracker.onBroadcastReceive(mMockContext, createChargingIntent_afterApi23(true)); verify(mListener).onConstraintChanged(true); mTracker.onBroadcastReceive(mMockContext, createChargingIntent_afterApi23(false)); verify(mListener).onConstraintChanged(false); }