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));
}
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);
}
}
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);
}
}
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());
}
}
}
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);
}
Aggregations