@Test
public void testAdjustDashPhase() {

  LineChartModel model = new LineChartModel();
  Range xRange = new Range(0, 15);
  Range yRange = new Range(0, 15);
  DefaultDataSeries<Long> testSeries = new DefaultDataSeries<>();
  testSeries.add(3, 0L);
  testSeries.add(3, 3L);  // hypotenuse relative to previous point = 3
  testSeries.add(6, 7L);  // hypotenuse relative to previous point = 5
  testSeries.add(9, 11L);  // hypotenuse relative to previous point = 5
  testSeries.add(15, 15L);
  RangedContinuousSeries rangedSeries = new RangedContinuousSeries("test", xRange, yRange, testSeries);
  model.add(rangedSeries);
  LineChart chart = new LineChart(model);
  // Set dimension to match the ranges, so each range unit is 1 pixel.
  chart.setSize(15, 15);
  // Create a dash pattern of total length 3
  BasicStroke stroke = new BasicStroke(1f, CAP_SQUARE, JOIN_MITER, 10.0f, new float[]{1.0f, 2.0f}, 0.0f);
  LineConfig config = new LineConfig(Color.BLACK).setStroke(stroke);
  chart.configure(rangedSeries, config);
  Assert.assertTrue(config.isAdjustDash());
  Assert.assertEquals(3, config.getDashLength(), 0);
  Assert.assertEquals(0, config.getAdjustedDashPhase(), 0);
  // First point == {3,0}
  Graphics2D fakeGraphics = mock(Graphics2D.class);
  when(fakeGraphics.create()).thenReturn(fakeGraphics);
  shiftRangeAndRepaintChart(chart, model, xRange, fakeGraphics, 0);
  Assert.assertEquals(0, config.getAdjustedDashPhase(), LineChart.EPSILON);
  // The new first point would be same as last. Dash phase should not have changed.
  shiftRangeAndRepaintChart(chart, model, xRange, fakeGraphics, 4);
  Assert.assertEquals(0, config.getAdjustedDashPhase(), LineChart.EPSILON);
  // The new first point would now be {6,7}, which is 8 pixels ahead relative to the previous first point {3,0}
  // New dash phase would be: (0 + 8) % 3
  shiftRangeAndRepaintChart(chart, model, xRange, fakeGraphics, 4);
  Assert.assertEquals(2, config.getAdjustedDashPhase(), LineChart.EPSILON);
  // The new first point would now be {9,11}, which is another 5 pixels ahead of the previous point
  // New dash phase would be: (2 + 5) % 3
  shiftRangeAndRepaintChart(chart, model, xRange, fakeGraphics, 4);
  Assert.assertEquals(1, config.getAdjustedDashPhase(), LineChart.EPSILON);
  // Shifts backward to ensure we get back the previous dash phases.
  shiftRangeAndRepaintChart(chart, model, xRange, fakeGraphics, -4);
  Assert.assertEquals(2, config.getAdjustedDashPhase(), LineChart.EPSILON);
  shiftRangeAndRepaintChart(chart, model, xRange, fakeGraphics, -4);
  Assert.assertEquals(0, config.getAdjustedDashPhase(), LineChart.EPSILON);
  shiftRangeAndRepaintChart(chart, model, xRange, fakeGraphics, -4);
  Assert.assertEquals(0, config.getAdjustedDashPhase(), LineChart.EPSILON);
  // Test that shifting by more than half would not update the dash phase.
  shiftRangeAndRepaintChart(chart, model, xRange, fakeGraphics, 8);
  Assert.assertEquals(0, config.getAdjustedDashPhase(), LineChart.EPSILON);
  shiftRangeAndRepaintChart(chart, model, xRange, fakeGraphics, -8);
  Assert.assertEquals(0, config.getAdjustedDashPhase(), LineChart.EPSILON);
}