Search in sources :

Example 26 with WButton

use of com.github.bordertech.wcomponents.WButton in project wcomponents by BorderTech.

the class SubordinateControlInterceptor_Test method testServiceRequestApplyControls.

@Test
public void testServiceRequestApplyControls() {
    // Create Target
    WButton target = new WButton();
    target.setAction(new Action() {

        @Override
        public void execute(final ActionEvent event) {
            buttonClicked = true;
        }
    });
    // Create Control - Enable/Disable Button
    WCheckBox box = new WCheckBox();
    Rule rule = new Rule();
    rule.setCondition(new Equal(box, Boolean.TRUE));
    rule.addActionOnTrue(new Enable(target));
    rule.addActionOnFalse(new Disable(target));
    WSubordinateControl control = new WSubordinateControl();
    control.addRule(rule);
    // Create component tree
    WContainer root = new WContainer();
    root.add(control);
    root.add(box);
    root.add(target);
    // Setup Intercepter
    SubordinateControlInterceptor interceptor = new SubordinateControlInterceptor();
    interceptor.setBackingComponent(root);
    UIContext uic = createUIContext();
    uic.setUI(root);
    setActiveContext(uic);
    buttonClicked = false;
    // Test Service Request - Empty Request and no control registered, so the control should not be applied
    MockRequest request = new MockRequest();
    interceptor.serviceRequest(request);
    // Target should still be enabled (control not applied)
    Assert.assertFalse("After service request target should be enabled", target.isDisabled());
    // Button not clicked
    Assert.assertFalse("Button should not have been clicked", buttonClicked);
    // Test Service Request - Try to click button while it is disabled and should not be clicked
    target.setDisabled(true);
    request.setParameter(target.getId(), "x");
    interceptor.serviceRequest(request);
    // Target should still be disabled (control not applied, as still not registered)
    Assert.assertTrue("After service request target should be disabled", target.isDisabled());
    // Button not clicked
    Assert.assertFalse("Button should not have been clicked while disabled", buttonClicked);
    // Test Prepare Paint - Should register and apply the subordinate control
    target.setDisabled(false);
    request = new MockRequest();
    interceptor.preparePaint(request);
    // Target should be disabled (Disabled by control as box is not selected)
    Assert.assertTrue("After service request target should be disabled", target.isDisabled());
    // Test Service Request - Simulate button click as it was enabled on the client by the check box being selected.
    // As the controls have been registered from the Prepare Paint, they will be applied in the Service Request and
    // this will enable the button and allow it to be clicked.
    buttonClicked = false;
    request.setParameter(target.getId(), "x");
    setupCheckBoxRequest(box, request, true);
    interceptor.serviceRequest(request);
    // Target should be enabled (enabled by control as box is selected)
    Assert.assertFalse("After service request target should be enabled", target.isDisabled());
    // Button should have been clicked
    Assert.assertTrue("Button should have been clicked", buttonClicked);
// // Check Subordinate Controls have not been cleared from session
// Assert.assertNotNull("Registered Controls should not have been cleared on the session",
// request.getSessionAttribute(SubordinateControlHelper.SUBORDINATE_CONTROL_SESSION_KEY));
// 
// interceptor.preparePaint(request);
// 
// // Check Subordinate Controls have been cleared from session
// Assert.assertNull("Registered Controls should have been cleared on the session",
// request.getSessionAttribute(SubordinateControlHelper.SUBORDINATE_CONTROL_SESSION_KEY));
}
Also used : Action(com.github.bordertech.wcomponents.Action) WContainer(com.github.bordertech.wcomponents.WContainer) UIContext(com.github.bordertech.wcomponents.UIContext) ActionEvent(com.github.bordertech.wcomponents.ActionEvent) WSubordinateControl(com.github.bordertech.wcomponents.subordinate.WSubordinateControl) WButton(com.github.bordertech.wcomponents.WButton) WCheckBox(com.github.bordertech.wcomponents.WCheckBox) Equal(com.github.bordertech.wcomponents.subordinate.Equal) Enable(com.github.bordertech.wcomponents.subordinate.Enable) Rule(com.github.bordertech.wcomponents.subordinate.Rule) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Disable(com.github.bordertech.wcomponents.subordinate.Disable) Test(org.junit.Test)

Example 27 with WButton

use of com.github.bordertech.wcomponents.WButton in project wcomponents by BorderTech.

the class ProfileContainer_Test method testAfterPaint.

/**
 * Test afterPaint.
 */
@Test
public void testAfterPaint() {
    ProfileContainer app = new ProfileContainer();
    app.setLocked(true);
    UIContext uic = new UIContextImpl();
    uic.setUI(app);
    setActiveContext(uic);
    WButton button = new WButton("PUSH");
    app.add(button);
    WLabel label = new WLabel("HERE");
    app.add(label);
    StringWriter outStr = new StringWriter();
    PrintWriter writer = new PrintWriter(outStr);
    RenderContext renderContext = new WebXmlRenderContext(writer);
    app.afterPaint(renderContext);
    // expecting 1 root class, 3 components, class names as shown, profiler
    // class
    String profileLine0 = PROFILER_UIC_HEADER;
    String profileLine1 = PROFILER_LINE1.replaceAll("<<NUM_ROOTS>>", "1");
    String profileLine2 = PROFILER_LINE2.replaceAll("<<NUM_COMPONENTS>>", "4");
    String profileLine31 = PROFILER_LINE3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.WButton");
    String profileLine32 = PROFILER_LINE3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.monitor.ProfileContainer");
    String profileLine33 = PROFILER_LINE3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.WLabel");
    String profileLine4 = PROFILER_PROFILE_HEADER.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.monitor.ProfileContainer");
    String[] expectedResults = { profileLine0, profileLine1, profileLine2, profileLine31, profileLine32, profileLine33, profileLine4 };
    String result = outStr.toString();
    for (int i = 0; i < expectedResults.length; i++) {
        Assert.assertTrue("result should contain substring " + i + "  ", result.indexOf(expectedResults[i]) != -1);
    }
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) RenderContext(com.github.bordertech.wcomponents.RenderContext) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) StringWriter(java.io.StringWriter) UIContext(com.github.bordertech.wcomponents.UIContext) UIContextImpl(com.github.bordertech.wcomponents.UIContextImpl) WButton(com.github.bordertech.wcomponents.WButton) WLabel(com.github.bordertech.wcomponents.WLabel) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 28 with WButton

use of com.github.bordertech.wcomponents.WButton in project wcomponents by BorderTech.

the class UicStatsAsHtml_Test method testWriter.

/**
 * Test writer.
 */
@Test
public void testWriter() {
    // Set up a mock UI to test
    WApplication app = new WApplication();
    app.setLocked(true);
    UIContext uic = new UIContextImpl();
    uic.setUI(app);
    setActiveContext(uic);
    WButton button = new WButton("PUSH");
    app.add(button);
    WLabel label = new WLabel("HERE");
    app.add(label);
    // Run the UIStats extract
    UicStats stats = new UicStats(uic);
    stats.analyseWC(app);
    StringWriter outStr = new StringWriter();
    PrintWriter writer = new PrintWriter(outStr);
    UicStatsAsHtml.write(writer, stats);
    // expecting 1 root class, 3 components, class names as shown
    String uicStatsHtml1 = UICSTATS_HTML1.replaceAll("<<NUM_ROOTS>>", "1");
    String uicStatsHtml2 = UICSTATS_HTML2.replaceAll("<<NUM_COMPONENTS>>", "4");
    String uicStatsHtml31 = UICSTATS_HTML3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.WApplication");
    String uicStatsHtml32 = UICSTATS_HTML3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.WButton");
    String uicStatsHtml33 = UICSTATS_HTML3.replaceAll("<<CLASS_NAME>>", "com.github.bordertech.wcomponents.WLabel");
    String[] expectedResults = { uicStatsHtml1, uicStatsHtml2, uicStatsHtml31, uicStatsHtml32, uicStatsHtml33 };
    String result = outStr.toString();
    for (int i = 0; i < expectedResults.length; i++) {
        Assert.assertTrue("result should contain substring " + i + "  ", result.indexOf(expectedResults[i]) != -1);
    }
}
Also used : StringWriter(java.io.StringWriter) WApplication(com.github.bordertech.wcomponents.WApplication) UIContext(com.github.bordertech.wcomponents.UIContext) UIContextImpl(com.github.bordertech.wcomponents.UIContextImpl) WButton(com.github.bordertech.wcomponents.WButton) WLabel(com.github.bordertech.wcomponents.WLabel) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 29 with WButton

use of com.github.bordertech.wcomponents.WButton in project wcomponents by BorderTech.

the class UicStats_Test method testGetWCTreeStats.

/**
 * Test getWCTreeStats.
 */
@Test
public void testGetWCTreeStats() {
    stats.analyseWC(app);
    Map<WComponent, Stat> resultStats = stats.getWCTreeStats(app);
    // label
    for (Map.Entry<WComponent, Stat> entry : resultStats.entrySet()) {
        WComponent comp = entry.getKey();
        Stat stat = entry.getValue();
        if (comp instanceof WLabel) {
            Assert.assertEquals("this should be the label created", label, comp);
            Assert.assertEquals("stat should have correct label name", label.getId(), stat.getName());
        } else if (comp instanceof WButton) {
            Assert.assertEquals("this should be the button in the app", button, comp);
            Assert.assertEquals("stat should have correct button name", button.getId(), stat.getName());
        } else if (comp instanceof WApplication) {
            Assert.assertEquals("this should be the app", app, comp);
            Assert.assertEquals("stat should have correct app name", app.getId(), stat.getName());
        }
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) Stat(com.github.bordertech.wcomponents.monitor.UicStats.Stat) WApplication(com.github.bordertech.wcomponents.WApplication) WButton(com.github.bordertech.wcomponents.WButton) Map(java.util.Map) WLabel(com.github.bordertech.wcomponents.WLabel) Test(org.junit.Test)

Example 30 with WButton

use of com.github.bordertech.wcomponents.WButton in project wcomponents by BorderTech.

the class WAjaxControlRenderer_Test method testDoPaint.

@Test
public void testDoPaint() throws IOException, SAXException, XpathException {
    WContainer root = new WContainer();
    WButton trigger = new WButton("x");
    WPanel target1 = new WPanel();
    WPanel target2 = new WPanel();
    WPanel target3 = new WPanel();
    WAjaxControl control = new WAjaxControl(trigger);
    root.add(trigger);
    root.add(target1);
    root.add(target2);
    root.add(target3);
    root.add(control);
    // No Targets
    assertSchemaMatch(root);
    assertXpathEvaluatesTo("0", "count(//ui:ajaxtrigger)", root);
    // With Targets
    control.addTargets(new AjaxTarget[] { target1, target2, target3 });
    setActiveContext(createUIContext());
    assertSchemaMatch(root);
    assertXpathEvaluatesTo(trigger.getId(), "//ui:ajaxtrigger/@triggerId", root);
    assertXpathNotExists("//ui:ajaxtrigger/@loadOnce", root);
    assertXpathNotExists("//ui:ajaxtrigger/@delay", root);
    assertXpathEvaluatesTo("3", "count(//ui:ajaxtrigger/ui:ajaxtargetid)", root);
    assertXpathEvaluatesTo(target1.getId(), "//ui:ajaxtrigger/ui:ajaxtargetid[1]/@targetId", root);
    assertXpathEvaluatesTo(target2.getId(), "//ui:ajaxtrigger/ui:ajaxtargetid[2]/@targetId", root);
    assertXpathEvaluatesTo(target3.getId(), "//ui:ajaxtrigger/ui:ajaxtargetid[3]/@targetId", root);
    control.setLoadOnce(true);
    assertSchemaMatch(root);
    assertXpathEvaluatesTo("true", "//ui:ajaxtrigger/@loadOnce", root);
    // remove loadOnce then reset it using loadCount
    control.setLoadOnce(false);
    assertSchemaMatch(root);
    assertXpathNotExists("//ui:ajaxtrigger/@loadOnce", root);
    // With Targets and optional attributes
    // any number greateer than 0...
    control.setLoadCount(6);
    control.setDelay(1000);
    assertSchemaMatch(root);
    assertXpathEvaluatesTo(trigger.getId(), "//ui:ajaxtrigger/@triggerId", root);
    assertXpathEvaluatesTo("true", "//ui:ajaxtrigger/@loadOnce", root);
    assertXpathEvaluatesTo("1000", "//ui:ajaxtrigger/@delay", root);
    assertXpathEvaluatesTo("3", "count(//ui:ajaxtrigger/ui:ajaxtargetid)", root);
}
Also used : WContainer(com.github.bordertech.wcomponents.WContainer) WAjaxControl(com.github.bordertech.wcomponents.WAjaxControl) WPanel(com.github.bordertech.wcomponents.WPanel) WButton(com.github.bordertech.wcomponents.WButton) Test(org.junit.Test)

Aggregations

WButton (com.github.bordertech.wcomponents.WButton)76 Test (org.junit.Test)39 ActionEvent (com.github.bordertech.wcomponents.ActionEvent)25 Action (com.github.bordertech.wcomponents.Action)20 WAjaxControl (com.github.bordertech.wcomponents.WAjaxControl)18 WTextField (com.github.bordertech.wcomponents.WTextField)17 WHeading (com.github.bordertech.wcomponents.WHeading)16 WContainer (com.github.bordertech.wcomponents.WContainer)14 WPanel (com.github.bordertech.wcomponents.WPanel)13 WFieldLayout (com.github.bordertech.wcomponents.WFieldLayout)11 ValidatingAction (com.github.bordertech.wcomponents.validation.ValidatingAction)10 WLabel (com.github.bordertech.wcomponents.WLabel)9 WTableColumn (com.github.bordertech.wcomponents.WTableColumn)8 UIContext (com.github.bordertech.wcomponents.UIContext)7 ExplanatoryText (com.github.bordertech.wcomponents.examples.common.ExplanatoryText)7 WText (com.github.bordertech.wcomponents.WText)6 WDataTable (com.github.bordertech.wcomponents.WDataTable)5 WFieldSet (com.github.bordertech.wcomponents.WFieldSet)5 WTable (com.github.bordertech.wcomponents.WTable)5 AdapterBasicTableModel (com.github.bordertech.wcomponents.AdapterBasicTableModel)4