/** * Add a field using the label and components passed in. * * @param label the label to use for the field * @param field the component to use for the field * @return the field which was added to the layout. */ public WField addField(final WLabel label, final WComponent field) { WField wField = new WField(label, field); add(wField); return wField; }
/** * Add a field consisting of a WButton with a null label. * * @param button the WButton to add to the layout. * @return the field added to the layout. */ public WField addField(final WButton button) { WField field = new WField((WLabel) null, button); add(field); return field; }
@Test public void testAddValidator() { MyValidator validator = new MyValidator(); WTextField textField = new WTextField(); WField field = new WField("dummy", textField); field.addValidator(validator); List<FieldValidator> validators = new ArrayList<>(); for (Iterator<FieldValidator> i = textField.getValidators(); i.hasNext();) { validators.add(i.next()); } Assert.assertTrue("addValidator Assert.failed", validators.contains(validator)); }
@Test public void testGetField() { WTextField textField = new WTextField(); WField field = new WField("dummy", textField); Assert.assertEquals("getField returned incorrect field", textField, field.getField()); }
@Test public void testSetMandatory() { WTextField textField = new WTextField(); WField field = new WField("dummy", textField); Assert.assertFalse("Field should not be mandatory by default", textField.isMandatory()); field.setMandatory(true); Assert.assertTrue("Field should be mandatory after setMandatory(true)", textField. isMandatory()); field.setMandatory(false); Assert.assertFalse("Field should be mandatory after setMandatory(true)", textField. isMandatory()); }
@Test public void testInputWidthAccessors() { WField field = new WField("dummy1", new WTextField()); assertAccessorsCorrect(field, "inputWidth", 0, 1, 2); }
@Test(expected = IllegalArgumentException.class) public void testSetInputWidthInvalidGreater100() { WField field = new WField("dummy3", new WTextField()); field.setInputWidth(101); }
@Test public void testSetInputWidthRange() { WField field = new WField("dummy2", new WTextField()); field.setInputWidth(-1); Assert.assertEquals("Incorrect width percentage from setter -1", 0, field.getInputWidth()); field.setInputWidth(0); Assert.assertEquals("Incorrect width percentage from setter 0", 0, field.getInputWidth()); field.setInputWidth(100); Assert. assertEquals("Incorrect width percentage from setter 100", 100, field. getInputWidth()); }
@Test public void testSetLabelText() throws IOException, SAXException, XpathException { String label1 = "WField_Test.testRenderedFormat"; String label2 = "WField_Test.testRenderedFormat"; WField field = new WField(label1, new WTextField()); setActiveContext(createUIContext()); field.setLabelText(label2); // UIC should have the new label, default should be unchanged Assert.assertEquals("Incorrect label text", label2, field.getLabelText()); resetContext(); Assert.assertEquals("Incorrect label text", label1, field.getLabelText()); }
/** * Test validate - where isVisible, isValidate and it not Disableable and there are validation errors. */ @Test public void testValidate() { AbstractWComponent comp = new MockContainer(); comp.setLocked(true); setActiveContext(createUIContext()); WTextField date = new WTextField(); WField partialDateField = new WField("Text", date); partialDateField.addValidator(new MyValidator()); comp.add(partialDateField); List<Diagnostic> diags = new ArrayList<>(); comp.validate(diags); Assert.assertEquals("There be an error diagnostic", 1, diags.size()); }
/** * test validate - where isInvisible. */ @Test public void testValidateIfIsInvisible() { AbstractWComponent comp = new MockContainer(); comp.setLocked(true); setActiveContext(createUIContext()); comp.setVisible(false); // skips validation WTextField date = new WTextField(); WField partialDateField = new WField("Text", date); partialDateField.addValidator(new MyValidator()); comp.add(partialDateField); List<Diagnostic> diags = new ArrayList<>(); comp.validate(diags); Assert.assertEquals("There be no error diagnostic", 0, diags.size()); }