/** * Tests view on users and devices. */ @Test public void testGetUsersAndDevices() { final List<CommUser> users = model.getUsersAndDevices().keySet().asList(); assertSame(agent1, users.get(0)); assertSame(agent2, users.get(1)); assertSame(agent3, users.get(2)); assertSame(agent4, users.get(3)); assertSame(agent5, users.get(4)); assertSame(users, model.getUsersAndDevices().keySet().asList()); final CommUser agent6 = new RangedAgent(new Point(6, 6), 4); model.register(agent6); assertSame(agent6, model.getUsersAndDevices().keySet().asList().get(5)); }
final Agent rangedNoPos = new RangedAgent(null, 5); final Agent ranged = new RangedAgent(new Point(1, 1), 5); final Agent noPos = new Agent(null); assertFalse(rangedNoPos.getPosition().isPresent());
/** * Tests what happens if an agent no longer has a position. */ @Test public void testSenderIsRemovedDuringSend() { final Agent a = new Agent(new Point(0, 0)); final Agent b = new RangedAgent(new Point(0, 0), 5); model.register(a); model.register(b); // a is removed during the same tick as that it sends a message, this // doesn't matter since it has no range, therefore it sends anyway. a.device().broadcast(Contents.YO); a.setPosition(null); model.afterTick(TimeLapseFactory.create(0, 100)); assertEquals(1, agent1.device().getReceivedCount()); // b is removed during the same tick as that is sends a message, since it is // a ranged device the message is not sent and will stay in the outbox. b.device().broadcast(Contents.YO); b.setPosition(null); model.afterTick(TimeLapseFactory.create(0, 100)); assertEquals(1, agent1.device().getReceivedCount()); assertEquals(1, b.device().getOutbox().size()); }
/** * Test for send message with limited range. */ @Test public void testSendDirectMsgWithRange() { final Agent ranged = new RangedAgent(new Point(0, 5), 5); model.register(ranged); // is possible, is within range ranged.device().send(Contents.YO, agent1); // not possible, too far ranged.device().send(Contents.YO, agent3); // receiving messages from outside range is possible agent3.device().send(Contents.YO, ranged); model.afterTick(TimeLapseFactory.create(0, 100)); final Message m = agent1.device().getUnreadMessages().get(0); assertSame(ranged, m.getSender()); assertTrue(agent3.device().getUnreadMessages().isEmpty()); assertSame(agent3, ranged.device().getUnreadMessages().get(0) .getSender()); }
/** * Test event dispatching. */ @Test public void testEvent() { final ListenerEventHistory history = new ListenerEventHistory(); model.getEventAPI().addListener(history, EventTypes.ADD_COMM_USER); final CommUser agent6 = new RangedAgent(new Point(6, 6), 4); model.register(agent6); assertEquals(1, history.getHistory().size()); assertTrue(history.getHistory().get(0) instanceof CommModelEvent); final CommModelEvent event = (CommModelEvent) history.getHistory().get(0); assertSame(agent6, event.getUser()); assertSame(model, event.getIssuer()); assertSame(agent6, model.getUsersAndDevices().inverse().get(event.getDevice())); }
/** * Test that a broadcast only ends up at agents within the range. */ @Test public void testBroadcastWithRange() { final Agent ranged = new RangedAgent(new Point(0, 5), 5); model.register(ranged); ranged.device().broadcast(Contents.YO); model.afterTick(TimeLapseFactory.create(0, 100)); assertFalse(agent1.device().getUnreadMessages().isEmpty()); assertFalse(agent2.device().getUnreadMessages().isEmpty()); assertTrue(agent3.device().getUnreadMessages().isEmpty()); assertFalse(agent4.device().getUnreadMessages().isEmpty()); assertTrue(agent5.device().getUnreadMessages().isEmpty()); }