Mercurial > hg > release > thermostat-0.13
changeset 881:4902d8be1833
Fix Backend list selection in AgentInformationDisplayFrame
Now that I am working on each plugin registering its own backend, I have
noticed an issue with the Backends list and associated description in
the AgentInformationDisplayFrame. When I click on a backend, the wrong
description is shown. This commit fixes the ListSelectionListener to
properly determine the selected row.
Reviewed-by: vanaltj, jerboaa
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-December/004923.html
author | Elliott Baron <ebaron@redhat.com> |
---|---|
date | Fri, 21 Dec 2012 10:01:37 -0500 |
parents | acbfd9752b11 |
children | cb81daef9f58 |
files | client/swing/src/main/java/com/redhat/thermostat/client/swing/views/AgentInformationDisplayFrame.java client/swing/src/test/java/com/redhat/thermostat/client/swing/views/AgentInformationDisplayFrameTest.java |
diffstat | 2 files changed, 53 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/views/AgentInformationDisplayFrame.java Thu Dec 20 19:12:54 2012 +0100 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/views/AgentInformationDisplayFrame.java Fri Dec 21 10:01:37 2012 -0500 @@ -452,7 +452,8 @@ return; } - int rowIndex = e.getFirstIndex(); + ListSelectionModel model = (ListSelectionModel) e.getSource(); + int rowIndex = model.getMinSelectionIndex(); String backendName = (String) backendsTableModel.getValueAt(rowIndex, 0); ActionEvent<ConfigurationAction> event = new ActionEvent<>(AgentInformationDisplayFrame.this, ConfigurationAction.SHOW_BACKEND_DESCRIPTION);
--- a/client/swing/src/test/java/com/redhat/thermostat/client/swing/views/AgentInformationDisplayFrameTest.java Thu Dec 20 19:12:54 2012 +0100 +++ b/client/swing/src/test/java/com/redhat/thermostat/client/swing/views/AgentInformationDisplayFrameTest.java Fri Dec 21 10:01:37 2012 -0500 @@ -40,10 +40,12 @@ import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import net.java.openjdk.cacio.ctc.junit.CacioFESTRunner; @@ -61,10 +63,11 @@ import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import com.redhat.thermostat.client.core.views.AgentInformationDisplayView; import com.redhat.thermostat.client.core.views.AgentInformationDisplayView.ConfigurationAction; -import com.redhat.thermostat.client.swing.views.AgentInformationDisplayFrame; import com.redhat.thermostat.common.ActionEvent; import com.redhat.thermostat.common.ActionListener; @@ -236,29 +239,62 @@ @GUITest @Test public void testBackendDescriptionIsQueriedAndDisplayed() { - final String BACKEND_NAME = "foo"; - final String BACKEND_STATUS = "bar"; - final String BACKEND_DESCRIPTION = "baz"; + final ActionEvent<ConfigurationAction> action = + new ActionEvent<>(agentConfigFrame, ConfigurationAction.SHOW_BACKEND_DESCRIPTION); + final String[] BACKEND_NAMES = { "foo1", "foo2", "foo3" }; + final String[] BACKEND_STATUSES = { "bar1", "bar2", "bar3" }; + final String[] BACKEND_DESCRIPTIONS = { "baz1", "baz2", "baz3" }; - Map<String, String> statusMap = new HashMap<>(); - statusMap.put(BACKEND_NAME, BACKEND_STATUS); + // Ordered by insertion + Map<String, String> statusMap = new LinkedHashMap<>(); + statusMap.put(BACKEND_NAMES[0], BACKEND_STATUSES[0]); + statusMap.put(BACKEND_NAMES[1], BACKEND_STATUSES[1]); + statusMap.put(BACKEND_NAMES[2], BACKEND_STATUSES[2]); + + final Map<String, String> descMap = new HashMap<>(); + descMap.put(BACKEND_NAMES[0], BACKEND_DESCRIPTIONS[0]); + descMap.put(BACKEND_NAMES[1], BACKEND_DESCRIPTIONS[1]); + descMap.put(BACKEND_NAMES[2], BACKEND_DESCRIPTIONS[2]); + + // Add hook for ActionListener + doAnswer(new Answer<Object>() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Object arg = invocation.getArguments()[0]; + ActionEvent<?> event = (ActionEvent<?>) arg; + String backendName = (String) event.getPayload(); + String description = descMap.get(backendName); + agentConfigFrame.setSelectedAgentBackendDescription(description); + return null; + } + }).when(l).actionPerformed(eq(action)); fixture.show(); agentConfigFrame.setSelectedAgentBackendStatus(statusMap); - assertEquals(1, fixture.table("backends").rowCount()); + assertEquals(3, fixture.table("backends").rowCount()); String[] rowContents = fixture.table("backends").contents()[0]; - assertArrayEquals(new String[] { BACKEND_NAME, BACKEND_STATUS }, rowContents); - - fixture.table("backends").selectRows(0); + assertArrayEquals(new String[] { BACKEND_NAMES[0], BACKEND_STATUSES[0] }, rowContents); + rowContents = fixture.table("backends").contents()[1]; + assertArrayEquals(new String[] { BACKEND_NAMES[1], BACKEND_STATUSES[1] }, rowContents); + rowContents = fixture.table("backends").contents()[2]; + assertArrayEquals(new String[] { BACKEND_NAMES[2], BACKEND_STATUSES[2] }, rowContents); - verify(l).actionPerformed(new ActionEvent<ConfigurationAction>(agentConfigFrame, ConfigurationAction.SHOW_BACKEND_DESCRIPTION)); - - agentConfigFrame.setSelectedAgentBackendDescription(BACKEND_DESCRIPTION); - - assertEquals(BACKEND_DESCRIPTION, fixture.textBox("backendDescription").text()); + // selectRows(0) doesn't trigger listener, since it is selected by default + // so start with row 1 + fixture.table("backends").selectRows(1); + fixture.robot.waitForIdle(); + assertEquals(BACKEND_DESCRIPTIONS[1], fixture.textBox("backendDescription").text()); + + fixture.table("backends").selectRows(2); + fixture.robot.waitForIdle(); + assertEquals(BACKEND_DESCRIPTIONS[2], fixture.textBox("backendDescription").text()); + + fixture.table("backends").selectRows(0); + fixture.robot.waitForIdle(); + assertEquals(BACKEND_DESCRIPTIONS[0], fixture.textBox("backendDescription").text()); } }