Chat extends DefaultComponent { @Override public void init() { super.init(); messageList = new List(page, "messages", DocumentCollections.MESSAGES.getMessages(this), 20, true, true ) { @Override public HamsterComponent renderElement(DataModel data) { Document<Messages> message = (Document<Messages>) data; Document<Users> author = message.get(Messages.USER); if (author == null) { author = Users.DEFAULT_USER; } DefaultComponent messageBox = new DefaultComponent(page, false, MESSGAE); messageBox.addElement(author.getStringSource(Users.NAME)); messageBox.addElement(message.getStringSource(Messages.TEXT)); messageBox.acquireDataModel(message); messageBox.acquireDataModel(author, Users.NAME); return messageBox; } @Override public void dataChanged(DataEvent e) { scrollDown(getId()); super.dataChanged(e); } }; messageList.setTemplate(MESSGAES); this.addElement(messageList); Form userNameForm = new Form(page) { @Override public void evaluate() { UIContext.getUser().set(Users.NAME, getString("name")); UIContext.getUser().writeToDatabase(); } }; InputField nameInput = new InputField(page, "name", UIContext.getUser().get(Users.NAME)) .setSubmitOnKeyUp(true); userNameForm.addElement(nameInput); this.addElement(userNameForm); final TextField messageInput = new TextField(page, "text", ""); Form messageForm = new Form(page) { @Override public void evaluate() { DocumentCollections.MESSAGES.addMessage(getString("text")); messageInput.forceFocus(); } }; messageInput.requestContinousFocus(0); messageInput.setCSSClass("textbox"); messageForm.addElement(messageInput); messageForm.addSubmitButton("Send"); messageForm.setResetAfterSending(true); this.addElement(messageForm); scrollDown(messageList.getId()); } Chat();  Chat(HamsterPage page); @Override void onShow(); @Override /** * The init method will be called by the framework for initializing the UI Component tree of this Component. Here we * can initialize all our child UI Components */ void init();  }
@Test public void testUpdateForNewMessage() throws InterruptedException { final ExampleChatPage page = createTestPage(testUIEngine); page.init(); ModificationManager m = page.getModificationManager(); DocumentCollections.MESSAGES.addMessage("TestMessage1"); String intialHTML = page.getInitialHTMLCode(); assertTrue(intialHTML + " did not contain TestMessage1", intialHTML.contains("TestMessage1")); System.out.println("initialHTML:\n" + intialHTML); DocumentCollections.MESSAGES.addMessage("TestMessage2"); String ajaxDelta = m.getModificiationXML(); assertTrue(ajaxDelta + " did not contain TestMessage2", ajaxDelta.contains("TestMessage2")); }