@Test
public void transitsToIdleWhenApiInitiatedTracingEnds() {

  myServices.enableCpuApiTracing(true);
  // API-initiated tracing starts.
  Cpu.CpuTraceConfiguration apiTracingConfig = Cpu.CpuTraceConfiguration.newBuilder()
    .setInitiationType(Cpu.TraceInitiationType.INITIATED_BY_API)
    .setUserOptions(Cpu.CpuTraceConfiguration.UserOptions.newBuilder().setTraceType(Cpu.CpuTraceType.ART))
    .build();
  addTraceInfoHelper(1, FAKE_DEVICE_ID, FAKE_PROCESS.getPid(), 100, -1, apiTracingConfig);
  myTimer.tick(FakeTimer.ONE_SECOND_IN_NS);
  assertThat(myStage.getCaptureState()).isEqualTo(CpuProfilerStage.CaptureState.CAPTURING);
  myCpuService.clearTraceInfo();
  addTraceInfoHelper(1, FAKE_DEVICE_ID, FAKE_PROCESS.getPid(), 100, 101, apiTracingConfig);
  myTimer.tick(FakeTimer.ONE_SECOND_IN_NS);
  assertThat(myStage.getCaptureState()).isEqualTo(CpuProfilerStage.CaptureState.IDLE);
}