/** * Adds the given component as a child of this component. The tag is used to identify the child in a velocity * template. * * @param component the component to add. * @param tag the tag used to identify the component. * @deprecated Use {@link WTemplate} instead. */ @Deprecated void add(final WComponent component, final String tag) { add(component); component.setTag(tag); }
/** * test removeAll - child from shared list. */ @Test public void testRemoveAllCommon() { AbstractWComponent comp = new MockContainer(); WText text1 = new WText("XYZ"); WText text2 = new WText("ABC"); comp.add(text1); comp.add(text2); comp.removeAll(); // from shared list Assert.assertEquals("shared should not contain any children", 0, comp.getChildCount()); Assert.assertEquals("uic should not contain any children", 0, comp.getChildCount()); }
@Test public void testLock() { AbstractWComponent parent = new MockContainer(); AbstractWComponent child1 = new SimpleComponent(); AbstractWComponent child2 = new SimpleComponent(); parent.add(child1); parent.add(child2); child2.setVisible(false); Assert.assertFalse("Parent should not be locked", parent.isLocked()); Assert.assertFalse("Child 1 should not be locked", child1.isLocked()); Assert.assertFalse("Child 2 should not be locked", child2.isLocked()); parent.setLocked(true); Assert.assertTrue("Parent should be locked", parent.isLocked()); Assert.assertTrue("Child 1 should be locked", child1.isLocked()); Assert.assertTrue("Child 2 should be locked", child2.isLocked()); }
@Test public void testDynamicMove() { UIContext uic = createUIContext(); AbstractWComponent parent1 = new MockContainer(); AbstractWComponent parent2 = new MockContainer(); AbstractWComponent child = new SimpleComponent(); parent1.add(child); setActiveContext(uic); parent1.remove(child); parent2.add(child); Assert.assertEquals("Child component should have been moved to parent2", parent2, child. getParent()); }
/** * test remove - child from shared list. */ @Test public void testRemoveCommon() { AbstractWComponent comp = new MockContainer(); WText text1 = new WText("XYZ"); WText text2 = new WText("ABC"); comp.add(text1); comp.add(text2); comp.remove(text1); // from shared list Assert.assertNull("shared should not contain text1", text1.getParent()); Assert.assertSame("shared should contain text2", text2, comp.getChildAt(0)); }
@Test public void testDynamicChildOverride() { AbstractWComponent parent = new MockContainer(); AbstractWComponent staticChild = new SimpleComponent(); AbstractWComponent dynamicChild = new SimpleComponent(); parent.add(staticChild); setActiveContext(createUIContext()); parent.removeAll(); parent.add(dynamicChild); Assert.assertEquals("Incorrect number of children", 1, parent.getChildCount()); Assert.assertSame("Incorrect child component", dynamicChild, parent.getChildAt(0)); }
@Test public void testDynamicAddAndReset() { AbstractWComponent parent = new MockContainer(); AbstractWComponent child = new SimpleComponent(); setActiveContext(createUIContext()); parent.add(child); child.reset(); Assert.assertEquals("Child component should have reference to parent", parent, child. getParent()); Assert.assertEquals("Parent component should have reference to child", 0, parent. getIndexOfChild(child)); }
/** * 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 reset. */ @Test public void testReset() { UIContext uic1 = createUIContext(); UIContext uic2 = createUIContext(); AbstractWComponent comp = new MockContainer(); WText text1 = new WText("XYZ"); WText text2 = new WText("ABC"); comp.setLocked(true); setActiveContext(uic1); comp.add(text1); setActiveContext(uic2); comp.add(text2); setActiveContext(uic1); Assert.assertFalse("should have a component model", comp.hasNoComponentModel(uic1)); Assert.assertFalse("should have a component model", comp.hasNoComponentModel(uic1)); comp.reset(); // do it for uic1 only Assert.assertTrue("uic1 should not have a component model", comp.hasNoComponentModel(uic1)); Assert.assertFalse("uic2 should still have a component model", comp. hasNoComponentModel(uic2)); }
/** * 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()); }
/** * test tidyUpUIContextForTree. */ @Test public void testTidyUpUIContextForTree() { UIContext uic1 = createUIContext(); UIContext uic2 = createUIContext(); AbstractWComponent comp = new MockContainer(); comp.setLocked(true); WText text1 = new WText("XYZ"); WText text2 = new WText("ABC"); setActiveContext(uic1); comp.add(text1); setActiveContext(uic2); comp.add(text2); Assert.assertFalse("should have a component model", comp.hasNoComponentModel(uic1)); Assert.assertFalse("should have a component model", comp.hasNoComponentModel(uic2)); setActiveContext(uic1); comp.remove(text1); comp.tidyUpUIContextForTree(); // do it for uic1 only setActiveContext(uic2); comp.tidyUpUIContextForTree(); // do it for uic2 only - but no effect since not in default state Assert.assertTrue("uic1 should not have a component model", comp.hasNoComponentModel(uic1)); Assert.assertFalse("uic2 should still have a component model", comp. hasNoComponentModel(uic2)); }
parent.add(child1); Assert.assertEquals("getChildCount() incorrect for 1 child", 1, parent.getChildCount()); setActiveContext(uic); parent.add(child2); Assert.assertEquals("getChildCount() incorrect for 2 children", 2, parent.getChildCount()); setActiveContext(uic); parent.add(child1); parent.removeAll(); Assert.assertNull("Removed child's parent should be null", child1.getParent()); parent.add(child1); child2.add(child1); Assert.fail("Runtime exception expected when trying to re-parent"); } catch (RuntimeException expected) {
AbstractWComponent root = new MockContainer(); AbstractWComponent comp = new SimpleComponent(); root.add(comp); root.setLocked(true);
/** * test removeAll - child from specific uic list. */ @Test public void testRemoveAllSpecific() { AbstractWComponent comp = new MockContainer(); WText text1 = new WText("XYZ"); WText text2 = new WText("ABC"); comp.add(text1); comp.add(text2); comp.setLocked(true); UIContext uic1 = createUIContext(); UIContext uic2 = createUIContext(); setActiveContext(uic2); comp.removeAll(); // from uic2 list resetContext(); Assert.assertSame("shared should contain text1", text1, comp.getChildAt(0)); Assert.assertSame("shared should contain text2", text2, comp.getChildAt(1)); setActiveContext(uic1); Assert.assertSame("uic1 should contain text1", text1, comp.getChildAt(0)); Assert.assertSame("uic1 should contain text2", text2, comp.getChildAt(1)); setActiveContext(uic2); Assert.assertEquals("uic2 should not contain any children", 0, comp.getChildCount()); }
/** * test remove - child from specific uic list. */ @Test public void testRemoveSpecific() { UIContext uic1 = createUIContext(); UIContext uic2 = createUIContext(); AbstractWComponent comp = new MockContainer(); WText text1 = new WText("XYZ"); WText text2 = new WText("ABC"); comp.add(text1); comp.add(text2); comp.setLocked(true); // from uic2 list setActiveContext(uic2); comp.remove(text1); resetContext(); Assert.assertSame("shared should contain text1", text1, comp.getChildAt(0)); Assert.assertSame("shared should contain text2", text2, comp.getChildAt(1)); setActiveContext(uic1); Assert.assertSame("uic1 should contain text1", text1, comp.getChildAt(0)); Assert.assertSame("uic1 should contain text2", text2, comp.getChildAt(1)); setActiveContext(uic2); Assert.assertNull("uic2 should not contain text1", text1.getParent()); Assert.assertSame("uic2 should contain text2", text2, comp.getChildAt(0)); }