@Test
public void cancelPreviewTest() {

  VirtualTimeScheduler timeScheduler = new VirtualTimeScheduler();
  Ref<Rectangle> screenViewSize = new Ref<>(scaleRectangle(FULL_SIZE, SCALE));
  // Create a high quality image bigger than the screenView that will be scaled.
  ImagePool.Image imageHQ = getTestImage(IMAGE_WIDTH, IMAGE_HEIGHT);
  ImagePool.Image imageNoScale = getTestImage(SCREEN_VIEW_WIDTH, SCREEN_VIEW_HEIGHT);
  ScreenView screenView = createScreenViewMock(screenViewSize, createRenderResultMock(imageHQ), createRenderResultMock(imageNoScale));
  ScreenViewLayer layer = new ScreenViewLayer(screenView, timeScheduler);
  //noinspection UndesirableClassUsage
  BufferedImage output = new BufferedImage(SCREEN_VIEW_WIDTH, SCREEN_VIEW_HEIGHT, BufferedImage.TYPE_INT_ARGB);
  Graphics2D g = createGraphicsAndClean(output, screenViewSize.get());
  layer.paint(g);
  // This has scheduled a task to resize the image
  assertEquals(1, timeScheduler.getActionsQueued());
  // Advance time without triggering the debounce timeout
  timeScheduler.advanceBy(30, TimeUnit.MILLISECONDS);
  assertEquals(1, timeScheduler.getActionsQueued());
  // Get a new image that does not need resizing so it will cancel the existing timer
  layer.paint(g);
  assertEquals(0, timeScheduler.getActionsQueued());
}