### Question:
ManagedChannelImpl extends ManagedChannel implements
    InternalInstrumented<ChannelStats> { @Override public void notifyWhenStateChanged(final ConnectivityState source, final Runnable callback) { final class NotifyStateChanged implements Runnable { @Override public void run() { channelStateManager.notifyWhenStateChanged(callback, executor, source); } } syncContext.execute(new NotifyStateChanged()); } ManagedChannelImpl(
      AbstractManagedChannelImplBuilder<?> builder,
      ClientTransportFactory clientTransportFactory,
      BackoffPolicy.Provider backoffPolicyProvider,
      ObjectPool<? extends Executor> balancerRpcExecutorPool,
      Supplier<Stopwatch> stopwatchSupplier,
      List<ClientInterceptor> interceptors,
      final TimeProvider timeProvider); @Override ListenableFuture<ChannelStats> getStats(); @Override InternalLogId getLogId(); @Override ManagedChannelImpl shutdown(); @Override ManagedChannelImpl shutdownNow(); @Override boolean isShutdown(); @Override boolean awaitTermination(long timeout, TimeUnit unit); @Override boolean isTerminated(); @Override ClientCall<ReqT, RespT> newCall(MethodDescriptor<ReqT, RespT> method,
      CallOptions callOptions); @Override String authority(); @Override ConnectivityState getState(boolean requestConnection); @Override void notifyWhenStateChanged(final ConnectivityState source, final Runnable callback); @Override void resetConnectBackoff(); @Override void enterIdle(); @Override String toString(); @Override Map<String, String> getLoadBalanceModeMap(); @Override void setLoadBalanceModeMap(Map<String, String> map); @Override NameResolver getNameResolver(); @Override LoadBalancer getLoadBalancer();  }### Answer:
@Test public void notifyWhenStateChanged() { final AtomicBoolean stateChanged = new AtomicBoolean(); Runnable onStateChanged = new Runnable() { @Override public void run() { stateChanged.set(true); } }; channelBuilder.nameResolverFactory( new FakeNameResolverFactory.Builder(expectedUri).setResolvedAtStart(false).build()); createChannel(); assertEquals(IDLE, channel.getState(false)); channel.notifyWhenStateChanged(IDLE, onStateChanged); executor.runDueTasks(); assertFalse(stateChanged.get()); helper.updateBalancingState(CONNECTING, mockPicker); executor.runDueTasks(); assertTrue(stateChanged.get()); stateChanged.set(false); channel.notifyWhenStateChanged(IDLE, onStateChanged); executor.runDueTasks(); assertTrue(stateChanged.get()); }