Skip to content
Permalink
Browse files

properly update Memory and LayoutEditor's MemoryIcon (Label) when the…

… contents of a is a Reportable object.
  • Loading branch information...
pabender committed Jun 3, 2019
1 parent 64e5ee3 commit 021009c81f67be9b70cd82e444bcbab1216eece0
@@ -35,6 +35,7 @@
import jmri.NamedBeanHandleManager;
import jmri.Path;
import jmri.Reporter;
import jmri.Reportable;
import jmri.Sensor;
import jmri.Turnout;
import jmri.implementation.AbstractNamedBean;
@@ -882,7 +883,7 @@ void handleBlockChange(PropertyChangeEvent e) {
//copy block value to memory if there is a value
Object val = block.getValue();
if (val != null) {
if (!(val instanceof RosterEntry)) {
if (!(val instanceof RosterEntry) && !(val instanceof Reportable)) {
val = val.toString();
}
}
@@ -6,6 +6,7 @@
import javax.swing.JPopupMenu;
import jmri.jmrit.catalog.NamedIcon;
import jmri.jmrit.roster.RosterEntry;
import jmri.Reportable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@@ -99,6 +100,13 @@ public void displayState() {
_icon = false;
updateSize();
return;
} else if (val instanceof Reportable) {
setText(((Reportable)val).toReportString());
setIcon(null);
_text = true;
_icon = false;
updateSize();
return;
} else {
log.warn("can't display current value of " + getNamedMemory().getName()
+ ", val= " + val + " of Class " + val.getClass().getName());
@@ -1,6 +1,7 @@
package jmri.jmrit.display.layoutEditor;

import jmri.Block;
import jmri.Memory;
import jmri.util.JUnitUtil;
import org.junit.After;
import org.junit.Assert;
@@ -14,19 +15,18 @@
*/
public class LayoutBlockTest {

private LayoutBlock layoutBlock = null;

@Test
public void testCtor() {
LayoutBlock b = new LayoutBlock("test", "test");
Assert.assertNotNull("exists", b);
Assert.assertNotNull("exists", layoutBlock);
Assert.assertEquals("Test Block", layoutBlock.getUserName());
}

@Test
public void testBlockRename() {
// Create layout block and the related automatic block
LayoutBlock layoutBlock = new LayoutBlock("ILB999", "Test Block");
// initialize the layout block and the related automatic block
layoutBlock.initializeLayoutBlock();
Assert.assertNotNull(layoutBlock);
Assert.assertEquals("Test Block", layoutBlock.getUserName());

// Get the referenced block and change its user name
Block block = jmri.InstanceManager.getDefault(jmri.BlockManager.class).getByUserName("Test Block");
@@ -37,14 +37,94 @@ public void testBlockRename() {
Assert.assertEquals("New Test Block", layoutBlock.getUserName());
}

@Test
public void testSetMemoryFromStringBlockValue() {
// initialize the layout block and the related automatic block
layoutBlock.initializeLayoutBlock();

// get a memory and associate it with the layout block.
Memory mem = jmri.InstanceManager.getDefault(jmri.MemoryManager.class).provideMemory("IM1");

layoutBlock.setMemory(mem,"IM1");

// verify the memory is associated
Assert.assertEquals("memory saved",mem,layoutBlock.getMemory());

// Get the referenced block
Block block = jmri.InstanceManager.getDefault(jmri.BlockManager.class).getByUserName("Test Block");

// change the value of the block.
block.setValue("hello world");

// and verify the value is in the memory
Assert.assertEquals("memory content same as block value",block.getValue(),mem.getValue());

}

@Test
public void testSetMemoryFromRosterEntryBlockValue() throws Exception {
// initialize the layout block and the related automatic block
layoutBlock.initializeLayoutBlock();

// get a memory and associate it with the layout block.
Memory mem = jmri.InstanceManager.getDefault(jmri.MemoryManager.class).provideMemory("IM1");

layoutBlock.setMemory(mem,"IM1");

// verify the memory is associated
Assert.assertEquals("memory saved",mem,layoutBlock.getMemory());

// Get the referenced block
Block block = jmri.InstanceManager.getDefault(jmri.BlockManager.class).getByUserName("Test Block");

// add a roster entry as the block value
jmri.jmrit.roster.RosterEntry re = jmri.jmrit.roster.RosterEntry.fromFile(new java.io.File("java/test/jmri/jmrit/roster/ACL1012.xml"));

// change the value of the block.
block.setValue(re);

// and verify the value is in the memory
Assert.assertEquals("memory content same as block value",block.getValue(),mem.getValue());
}

@Test
public void testSetMemoryFromIdTagBlockValue() {
// initialize the layout block and the related automatic block
layoutBlock.initializeLayoutBlock();

// get a memory and associate it with the layout block.
Memory mem = jmri.InstanceManager.getDefault(jmri.MemoryManager.class).provideMemory("IM1");

layoutBlock.setMemory(mem,"IM1");

// verify the memory is associated
Assert.assertEquals("memory saved",mem,layoutBlock.getMemory());

// Get the referenced block
Block block = jmri.InstanceManager.getDefault(jmri.BlockManager.class).getByUserName("Test Block");

jmri.IdTag tag = new jmri.implementation.DefaultIdTag("1234");

// change the value of the block.
block.setValue(tag);

// and verify the value is in the memory
Assert.assertEquals("memory content same as block value",block.getValue(),mem.getValue());
}



// from here down is testing infrastructure
@Before
public void setUp() throws Exception {
JUnitUtil.setUp();
// Create layout block and the related automatic block
layoutBlock = new LayoutBlock("ILB999", "Test Block");
}

@After
public void tearDown() throws Exception {
layoutBlock = null;
JUnitUtil.tearDown();
}
// private final static Logger log = LoggerFactory.getLogger(LayoutBlockTest.class);
@@ -2,35 +2,48 @@

import java.awt.GraphicsEnvironment;
import jmri.util.JUnitUtil;
import org.junit.After;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import jmri.util.junit.annotations.*;
import org.junit.*;

/**
* Test simple functioning of MemoryIcon
*
* @author Paul Bender Copyright (C) 2016
*/
public class MemoryIconTest {
public class MemoryIconTest extends jmri.jmrit.display.MemoryIconTest {

@Test
public void testCtor() {
Assume.assumeFalse(GraphicsEnvironment.isHeadless());
MemoryIcon t = new MemoryIcon("test", new LayoutEditor());
Assert.assertNotNull("exists", t);
t.getEditor().dispose();
@Override
@Ignore("Superclass method assumes graphical icon (red X)")
@ToDo("rewrite superclass test so it works in this case.")
public void testShowEmpty() {
}

@Test
@Override
@Ignore("When test from superclass is run, Scale is not set")
@ToDo("rewrite superclass test so it works in this case.")
public void testGetAndSetScale(){
}



@Before
public void setUp() throws Exception {
@Override
public void setUp() {
JUnitUtil.setUp();
jmri.util.JUnitUtil.resetProfileManager();
if (!GraphicsEnvironment.isHeadless()) {
editor = new LayoutEditor();
p = to = new MemoryIcon("MemoryTest1", (LayoutEditor)editor );
to.setMemory("IM1");
}
}

@After
public void tearDown() throws Exception {
@Override
public void tearDown() {
to.getEditor().dispose();
JUnitUtil.tearDown();
}

0 comments on commit 021009c

Please sign in to comment.
You can’t perform that action at this time.