Search in sources :

Example 1 with Listener

use of org.csstudio.display.builder.rcp.run.DisplayNavigation.Listener in project org.csstudio.display.builder by kasemir.

the class DisplayNavigationTest method testNavigation.

@Test
public void testNavigation() {
    final DisplayNavigation navigation = new DisplayNavigation();
    final Listener listener = nav -> changes.incrementAndGet();
    navigation.addListener(listener);
    // No history
    assertThat(navigation.getBackwardDisplays().size(), equalTo(0));
    assertThat(navigation.getForwardDisplays().size(), equalTo(0));
    assertThat(changes.get(), equalTo(0));
    // Current display, still no history
    DisplayInfo display = new DisplayInfo("/path/a.opi", "A", new Macros());
    navigation.setCurrentDisplay(display);
    System.out.println(navigation);
    assertThat(navigation.getBackwardDisplays().size(), equalTo(0));
    assertThat(navigation.getForwardDisplays().size(), equalTo(0));
    assertThat(changes.get(), equalTo(0));
    // Open new displays: Now 5 items available to go back
    for (int i = 1; i <= 5; ++i) navigation.setCurrentDisplay(new DisplayInfo("/path/N" + i + ".opi", "N" + i, new Macros()));
    System.out.println(navigation);
    assertThat(navigation.getBackwardDisplays().size(), equalTo(5));
    assertThat(navigation.getForwardDisplays().size(), equalTo(0));
    assertThat(changes.get(), equalTo(5));
    display = navigation.goBackward(2);
    System.out.println(navigation);
    assertThat(display.getName(), equalTo("N3"));
    assertThat(navigation.getBackwardDisplays().size(), equalTo(3));
    assertThat(navigation.getForwardDisplays().size(), equalTo(2));
    assertThat(changes.get(), equalTo(6));
    // Informing navigation about the display that's already current
    // (equal, not necessarily identical) has no impact on history
    navigation.setCurrentDisplay(new DisplayInfo(display.getPath(), display.getName(), new Macros()));
    assertThat(navigation.getBackwardDisplays().size(), equalTo(3));
    assertThat(navigation.getForwardDisplays().size(), equalTo(2));
    assertThat(changes.get(), equalTo(6));
    // Navigate back to the first display
    display = navigation.goBackward(3);
    System.out.println(navigation);
    assertThat(display.getName(), equalTo("A"));
    assertThat(navigation.getBackwardDisplays().size(), equalTo(0));
    assertThat(navigation.getForwardDisplays().size(), equalTo(5));
    assertThat(changes.get(), equalTo(7));
    // Navigate forward to "N1"
    display = navigation.goForward(1);
    System.out.println(navigation);
    assertThat(display.getName(), equalTo("N1"));
    assertThat(navigation.getBackwardDisplays().size(), equalTo(1));
    assertThat(navigation.getForwardDisplays().size(), equalTo(4));
    assertThat(changes.get(), equalTo(8));
    // Clear forward chain by opening different display
    display = new DisplayInfo("/path/x.opi", "X", new Macros());
    navigation.setCurrentDisplay(display);
    System.out.println(navigation);
    assertThat(navigation.getBackwardDisplays().size(), equalTo(2));
    assertThat(navigation.getForwardDisplays().size(), equalTo(0));
    assertThat(changes.get(), equalTo(9));
    navigation.removeListener(listener);
}
Also used : Assert.assertThat(org.junit.Assert.assertThat) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Macros(org.csstudio.display.builder.model.macros.Macros) Listener(org.csstudio.display.builder.rcp.run.DisplayNavigation.Listener) DisplayNavigation(org.csstudio.display.builder.rcp.run.DisplayNavigation) Test(org.junit.Test) Listener(org.csstudio.display.builder.rcp.run.DisplayNavigation.Listener) DisplayNavigation(org.csstudio.display.builder.rcp.run.DisplayNavigation) Macros(org.csstudio.display.builder.model.macros.Macros) Test(org.junit.Test)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Macros (org.csstudio.display.builder.model.macros.Macros)1 DisplayNavigation (org.csstudio.display.builder.rcp.run.DisplayNavigation)1 Listener (org.csstudio.display.builder.rcp.run.DisplayNavigation.Listener)1 CoreMatchers.equalTo (org.hamcrest.CoreMatchers.equalTo)1 Assert.assertThat (org.junit.Assert.assertThat)1 Test (org.junit.Test)1