@Test public void testFormAdd() throws Exception { Form form = new Form(request); assertEquals(0, form.size()); Button button = Button.button("OK"); form.add(button); assertEquals(1, form.size()); TextField field = new TextField("name"); form.add(field, "Name"); form.add(null); // ignored form.add(null, "Name"); // ignored assertEquals(2, form.size()); assertSame(field, form.get(1)); assertSame(field, form.get("name")); assertNull(form.get("x")); assertNull(form.get(null)); try { // cannot add twice form.add(field); fail(); } catch (IllegalStateException e) { } form.setRequired(true); assertTrue(field.isRequired()); form.setReadOnly(true); assertTrue(field.isReadOnly()); assertTrue(form.validate(true)); form.remove(field); assertEquals(1, form.size()); }
@Test public void testFormRead() throws Exception { Form form = new Form(request); TextField field = new TextField("name"); form.add(field, "Name"); field.setRequired(true); form.clearErrorControl(); when(request.getRequest()).thenReturn(request); when(request.getParameter("name")).thenReturn(null); assertFalse(form.read()); assertFalse(form.isOk()); assertSame(field, form.getErrorControl()); when(request.getParameter("name")).thenReturn("John"); assertTrue(form.read()); assertTrue(form.isOk()); assertNull(form.getErrorControl()); }
@Test public void testTextField() { TextField field = new TextField("email"); assertEquals("email", field.getName()); assertEquals("text", field.getInputType()); field.setInputType(TextField.INPUT_TYPE_EMAIL); assertEquals("email", field.getInputType()); assertSame(field, field.toFocusControl()); assertSame(field, field.toInputField()); assertEquals(0, field.getMaxLength()); assertEquals(0, field.getSize()); field.setMaxLength(100); assertEquals(50, field.getSize()); assertEquals(100, field.getMaxLength()); field.setSize(25); assertEquals(25, field.getSize()); assertEquals(100, field.getMaxLength()); assertNull(field.getPattern()); field.setPattern("*@*"); assertEquals("*@*", field.getPattern()); assertNull(field.getPlaceholder()); field.setPlaceholder("enter email"); assertEquals("enter email", field.getPlaceholder()); assertNull(field.getId()); field.setId("xyz"); assertEquals("xyz", field.getId()); assertEquals("", field.getLabel()); assertFalse(field.hasLabel()); field.setLabel("label"); assertEquals("label", field.getLabel()); field.setLabel(null); assertEquals("", field.getLabel()); assertNull(field.getData()); field.setData(this); assertSame(this, field.getData()); assertEquals(1, field.getRows()); assertEquals(Control.Category.INPUT, field.getCategory()); assertEquals("", field.format()); assertEquals("TextField[email=null]", field.toString()); assertOut( field, "<input type='email' name='email' value='' size='25' maxlength='100' id='xyz' placeholder='enter email' pattern='*@*'>"); }