Search in sources :

Example 61 with MockRequest

use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.

the class UIContextImpl_Test method testFocusableInWRepeaterNested.

@Test
public void testFocusableInWRepeaterNested() {
    // Beans list
    final String optionX = "X";
    final String optionY = "Y";
    final List<String> beans = Arrays.asList(new String[] { optionX, optionY });
    // Another beans list for nested repeater
    final String optionA = "A";
    final String optionB = "B";
    final String optionC = "C";
    final List<String> beans2 = Arrays.asList(new String[] { optionA, optionB, optionC });
    // Setup the nested repeater
    WRepeater repeater2 = new WRepeater();
    WComponent content2 = new WBeanComponent() {

        @Override
        public void handleRequest(final Request request) {
            super.handleRequest(request);
            UIContext uic = UIContextHolder.getCurrent();
            if (uic instanceof SubUIContext) {
                SubUIContext suic = (SubUIContext) uic;
                // Set the component on the third row to have focus
                if (suic.getRowIndex() == 2) {
                    uic.setFocussed(this, uic);
                }
            }
        }
    };
    repeater2.setRepeatedComponent(content2);
    // Setup the top level repeater
    WRepeater repeater = new WRepeater();
    WContainer content = new WContainer() {

        @Override
        public void handleRequest(final Request request) {
            super.handleRequest(request);
            UIContext uic = UIContextHolder.getCurrent();
            if (uic instanceof SubUIContext) {
                SubUIContext suic = (SubUIContext) uic;
                if (suic.getRowIndex() == 0) {
                    // Set the bean list on the nested repeater, only for the first row
                    WRepeater nested = (WRepeater) getChildAt(0);
                    nested.setBeanList(beans2);
                }
            }
        }
    };
    content.add(repeater2);
    repeater.setRepeatedComponent(content);
    // Set the repeater as the "UI" for context
    UIContext uic = new UIContextImpl();
    uic.setUI(repeater);
    setActiveContext(uic);
    // Setup the bean list on the top level repeater
    repeater.setBeanList(beans);
    // ServiceRequest on the repeater (to create the SubUiContexts)
    MockRequest request = new MockRequest();
    repeater.serviceRequest(request);
    // Simulate clearing of scratch map
    uic.clearScratchMap();
    // Get the focused component
    String focusId = uic.getFocussedId();
    // Get the ID of the component on the second row
    String rowComponentId = null;
    try {
        UIContext suic = repeater.getRowContext(optionX);
        UIContextHolder.pushContext(suic);
        UIContext suic2 = repeater2.getRowContext(optionC);
        UIContextHolder.pushContext(suic2);
        rowComponentId = content2.getId();
    } finally {
        UIContextHolder.reset();
    }
    Assert.assertNotNull("Focus ID should not be null", focusId);
    Assert.assertEquals("Focus ID should be the ID for the component on the 2nd row", rowComponentId, focusId);
}
Also used : SubUIContext(com.github.bordertech.wcomponents.WRepeater.SubUIContext) SubUIContext(com.github.bordertech.wcomponents.WRepeater.SubUIContext) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 62 with MockRequest

use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.

the class WButton_Test method getButtonText.

/**
 * Returns the text for a button created with the given data.
 *
 * @param useSharedText true if the button should have shared text
 * @param useUserText true if the button should have user text
 * @param useSharedValue true if the button should have a shared value
 * @param useUserValue true if the button should have a user value
 * @param useBeanValue true if the button should have a bean provider
 * @param nullBean true if the provided bean should be null
 *
 * @return the button text that will be displayed to the user.
 */
private String getButtonText(final boolean useSharedText, final boolean useUserText, final boolean useSharedValue, final boolean useUserValue, final boolean useBeanValue, final boolean nullBean) {
    WButton button = createButton(useSharedText, useUserText, useSharedValue, useUserValue, useBeanValue, nullBean);
    setActiveContext(createUIContext());
    try {
        button.preparePaintComponent(new MockRequest());
        return button.getText();
    } finally {
        UIContextHolder.reset();
    }
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest)

Example 63 with MockRequest

use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.

the class WButton_Test method getButtonValue.

/**
 * Returns the value for a button created with the given data.
 *
 * @param useSharedText true if the button should have shared text
 * @param useUserText true if the button should have user text
 * @param useSharedValue true if the button should have a shared value
 * @param useUserValue true if the button should have a user value
 * @param useBeanValue true if the button should have a bean provider
 * @param nullBean true if the provided bean should be null
 *
 * @return the button value will be used for a user.
 */
private String getButtonValue(final boolean useSharedText, final boolean useUserText, final boolean useSharedValue, final boolean useUserValue, final boolean useBeanValue, final boolean nullBean) {
    WButton button = createButton(useSharedText, useUserText, useSharedValue, useUserValue, useBeanValue, nullBean);
    setActiveContext(createUIContext());
    try {
        button.preparePaintComponent(new MockRequest());
        return button.getValue();
    } finally {
        UIContextHolder.reset();
    }
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest)

Example 64 with MockRequest

use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.

the class WButton_Test method testSetPressed.

@Test
public void testSetPressed() {
    WButton button = new WButton("Test");
    button.setLocked(true);
    setActiveContext(createUIContext());
    button.setPressed(true, new MockRequest());
    Assert.assertTrue("Button should be pressed", button.isPressed());
    Assert.assertFalse("Button should not be in default state", button.isDefaultState());
    button.setPressed(false, new MockRequest());
    Assert.assertFalse("Button should be pressed", button.isPressed());
    button.setDisabled(true);
    button.setPressed(true, new MockRequest());
    Assert.assertTrue("Button should be disabled", button.isDisabled());
    Assert.assertFalse("Button should be pressed when disabled", button.isPressed());
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Example 65 with MockRequest

use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.

the class WButton_Test method testActionSetPressed.

@Test
public void testActionSetPressed() {
    WButton button = new WButton("Test");
    button.setLocked(true);
    UIContext uic = createUIContext();
    setActiveContext(uic);
    uic.setUI(button);
    TestAction testAction = new TestAction();
    testAction.reset();
    button.setAction(testAction);
    button.setPressed(true, new MockRequest());
    Assert.assertEquals("Button was not pressed", true, button.isPressed());
}
Also used : MockRequest(com.github.bordertech.wcomponents.util.mock.MockRequest) Test(org.junit.Test)

Aggregations

MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)250 Test (org.junit.Test)216 UIContext (com.github.bordertech.wcomponents.UIContext)22 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)17 MockResponse (com.github.bordertech.wcomponents.util.mock.MockResponse)16 ErrorCodeEscape (com.github.bordertech.wcomponents.ErrorCodeEscape)13 PrintWriter (java.io.PrintWriter)13 Request (com.github.bordertech.wcomponents.Request)12 ArrayList (java.util.ArrayList)10 Date (java.util.Date)8 Diagnostic (com.github.bordertech.wcomponents.validation.Diagnostic)7 StringWriter (java.io.StringWriter)7 MockWEnvironment (com.github.bordertech.wcomponents.MockWEnvironment)6 SubUIContext (com.github.bordertech.wcomponents.WRepeater.SubUIContext)6 WServlet (com.github.bordertech.wcomponents.servlet.WServlet)5 BigDecimal (java.math.BigDecimal)5 Escape (com.github.bordertech.wcomponents.Escape)4 WTextField (com.github.bordertech.wcomponents.WTextField)4 NullWriter (com.github.bordertech.wcomponents.util.NullWriter)4 ActionEscape (com.github.bordertech.wcomponents.ActionEscape)3