Search in sources :

Example 21 with UIContext

use of com.github.bordertech.wcomponents.UIContext 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 22 with UIContext

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

the class TargetableInterceptor_Test method setUp.

@Before
public void setUp() {
    UIContext uic = new UIContextImpl();
    WPanel root = new WPanel();
    originalUI = new WLabel();
    targetUI = new TargetableWLabel();
    root.add(originalUI);
    root.add(targetUI);
    uic.setUI(root);
    setActiveContext(uic);
    interceptor = new TargetableInterceptor();
    interceptor.setBackingComponent(originalUI);
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext) UIContextImpl(com.github.bordertech.wcomponents.UIContextImpl) WPanel(com.github.bordertech.wcomponents.WPanel) WLabel(com.github.bordertech.wcomponents.WLabel) Before(org.junit.Before)

Example 23 with UIContext

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

the class TemplateRenderInterceptorTest method generateOutput.

/**
 * Render the component and execute the interceptor.
 *
 * @param testUI the test component
 * @param headers Request headers to set (key/value pairs).
 * @return the response
 */
private TestResult generateOutput(final MyComponent testUI, final Map<String, String> headers) {
    InterceptorComponent interceptor = new TemplateRenderInterceptor();
    interceptor.attachUI(testUI);
    MockHttpServletRequest backing = new MockHttpServletRequest();
    if (headers != null) {
        for (String headerName : headers.keySet()) {
            backing.setHeader(headerName, headers.get(headerName));
        }
    }
    ServletRequest request = new ServletRequest(backing);
    MockResponse response = new MockResponse();
    interceptor.attachResponse(response);
    StringWriter writer = new StringWriter();
    UIContext uic = createUIContext();
    uic.setLocale(new Locale("en"));
    setActiveContext(uic);
    try {
        interceptor.preparePaint(request);
        interceptor.paint(new WebXmlRenderContext(new PrintWriter(writer)));
    } finally {
        resetContext();
    }
    return new TestResult(writer.toString(), response.getContentType());
}
Also used : Locale(java.util.Locale) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) ServletRequest(com.github.bordertech.wcomponents.servlet.ServletRequest) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) StringWriter(java.io.StringWriter) UIContext(com.github.bordertech.wcomponents.UIContext) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) PrintWriter(java.io.PrintWriter)

Example 24 with UIContext

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

the class TransformXMLInterceptor_Test method generateOutput.

/**
 * Render the component and execute the interceptor.
 *
 * @param testUI the test component
 * @param headers Request headers to set (key/value pairs).
 * @return the response
 */
private TestResult generateOutput(final MyComponent testUI, final Map<String, String> headers) {
    InterceptorComponent interceptor = new TransformXMLInterceptor();
    interceptor.attachUI(testUI);
    MockHttpServletRequest backing = new MockHttpServletRequest();
    if (headers != null) {
        for (String headerName : headers.keySet()) {
            backing.setHeader(headerName, headers.get(headerName));
        }
    }
    ServletRequest request = new ServletRequest(backing);
    MockResponse response = new MockResponse();
    interceptor.attachResponse(response);
    StringWriter writer = new StringWriter();
    UIContext uic = createUIContext();
    uic.setLocale(new Locale("en"));
    setActiveContext(uic);
    try {
        interceptor.preparePaint(request);
        interceptor.paint(new WebXmlRenderContext(new PrintWriter(writer)));
    } finally {
        resetContext();
    }
    return new TestResult(writer.toString(), response.getContentType());
}
Also used : Locale(java.util.Locale) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) ServletRequest(com.github.bordertech.wcomponents.servlet.ServletRequest) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockResponse(com.github.bordertech.wcomponents.util.mock.MockResponse) StringWriter(java.io.StringWriter) UIContext(com.github.bordertech.wcomponents.UIContext) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) PrintWriter(java.io.PrintWriter)

Example 25 with UIContext

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

the class TransformXMLInterceptor_Test method testPaintWithCorruptCharacterAllowed.

/**
 * Ensure that the interceptor does nothing as long as the controlling property is disabled.
 *
 * @throws java.lang.NoSuchFieldException an exception
 * @throws java.lang.IllegalAccessException an exception
 */
@Test
public void testPaintWithCorruptCharacterAllowed() throws NoSuchFieldException, IllegalAccessException {
    /**
     * Have to use reflection to swap out the log implementation as there's no way to programmatically disable
     * logging without coupling to a log implementation.
     */
    // Override the logger temporarily.
    Field field = TransformXMLInterceptor.class.getDeclaredField("LOG");
    field.setAccessible(true);
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    Object oldValue = field.get(null);
    field.set(null, new NoLogLogger());
    MyComponent testUI = new MyComponent(TEST_CORRUPT_CHAR_XML);
    Config.getInstance().setProperty(ConfigurationProperties.THEME_CONTENT_PATH, "");
    Config.getInstance().setProperty(ConfigurationProperties.XSLT_ALLOW_CORRUPT_CHARACTER, "true");
    TransformXMLTestHelper.reloadTransformer();
    testUI.setLocked(true);
    UIContext uic = createUIContext();
    uic.setUI(testUI);
    setActiveContext(uic);
    TestResult actual = generateOutput(testUI, null);
    // Set the original value
    Field resetValueField = TransformXMLInterceptor.class.getDeclaredField("LOG");
    resetValueField.setAccessible(true);
    field.set(null, oldValue);
}
Also used : Field(java.lang.reflect.Field) UIContext(com.github.bordertech.wcomponents.UIContext) Test(org.junit.Test)

Aggregations

UIContext (com.github.bordertech.wcomponents.UIContext)114 Test (org.junit.Test)47 WComponent (com.github.bordertech.wcomponents.WComponent)18 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)15 SystemException (com.github.bordertech.wcomponents.util.SystemException)14 WApplication (com.github.bordertech.wcomponents.WApplication)13 UIContextImpl (com.github.bordertech.wcomponents.UIContextImpl)11 WText (com.github.bordertech.wcomponents.WText)11 PrintWriter (java.io.PrintWriter)11 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)10 SeleniumWComponentsWebDriver (com.github.bordertech.wcomponents.test.selenium.driver.SeleniumWComponentsWebDriver)9 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)7 DefaultWComponent (com.github.bordertech.wcomponents.DefaultWComponent)7 WDropdown (com.github.bordertech.wcomponents.WDropdown)7 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)7 StringWriter (java.io.StringWriter)7 AjaxOperation (com.github.bordertech.wcomponents.AjaxOperation)6 Environment (com.github.bordertech.wcomponents.Environment)6 MockWEnvironment (com.github.bordertech.wcomponents.MockWEnvironment)6 WButton (com.github.bordertech.wcomponents.WButton)6