@Test
public void captureBeforeFirstActivity() throws Exception {

  // CPU recording is not supported in new pipeline yet.
  Assume.assumeFalse(myIsUnifiedPipeline);
  // Generate and select a capture in the stage
  CpuProfilerTestUtils.captureSuccessfully(myProfilerStage, myService, myTransportService, CpuProfilerTestUtils.readValidTrace());
  CpuCapture capture = myProfilerStage.getCapture();
  assertNotNull(capture);
  int tid = capture.getMainThreadId();
  // Create a series with trace file's main thread tid and the capture range
  DataSeries<CpuProfilerStage.ThreadState> series = createThreadSeries(tid);
  // Create the thread activities to be 1 second after the capture
  myService.addThreads(tid, "main", Arrays.asList(
    CpuProfiler.GetThreadsResponse.ThreadActivity.newBuilder()
      .setTimestamp(TimeUnit.MICROSECONDS.toNanos((long)capture.getRange().getMin()) + TimeUnit.SECONDS.toNanos(1))
      .setNewState(Cpu.CpuThreadData.State.RUNNING).build(),
    CpuProfiler.GetThreadsResponse.ThreadActivity.newBuilder()
      .setTimestamp(TimeUnit.MICROSECONDS.toNanos((long)capture.getRange().getMin()) + TimeUnit.SECONDS.toNanos(2))
      .setNewState(Cpu.CpuThreadData.State.SLEEPING).build()));
  List<SeriesData<CpuProfilerStage.ThreadState>> dataSeries = series.getDataForRange(new Range(Long.MIN_VALUE, Long.MAX_VALUE));
  assertNotNull(dataSeries);
  // We expect the portions of the thread activities that are within the capture range to be duplicated with a "_CAPTURED" suffix.
  // The first activity happens inside the capture, therefore it has only the "_CAPTURED" state.
  assertEquals(3, dataSeries.size());
  assertEquals(CpuProfilerStage.ThreadState.RUNNING_CAPTURED, dataSeries.get(0).value);
  assertEquals(CpuProfilerStage.ThreadState.SLEEPING_CAPTURED, dataSeries.get(1).value);
  assertEquals(CpuProfilerStage.ThreadState.SLEEPING, dataSeries.get(2).value);
}