Search in sources :

Example 11 with UIContextImpl

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

the class AbstractContainerHelper method createUIContext.

/**
 * Creates and initialises a new UIContext.
 *
 * @return a new UIContext.
 */
protected UIContext createUIContext() {
    // Create UIC
    UIContext uic = new UIContextImpl();
    uic.setUI(getUI());
    return uic;
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext) UIContextImpl(com.github.bordertech.wcomponents.UIContextImpl)

Example 12 with UIContextImpl

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

the class WComponentTestCase method resetUIContext.

/**
 * Replaces the UIContext with a new copy, to emulate a fresh session.
 */
protected void resetUIContext() {
    uic = new UIContextImpl();
    uic.setUI(getWrappedUi());
}
Also used : UIContextImpl(com.github.bordertech.wcomponents.UIContextImpl)

Example 13 with UIContextImpl

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

the class DiagnosticImpl_Test method testGetDescription.

@Test
public void testGetDescription() {
    final UIContext uic = new UIContextImpl();
    final WTextField input = new WTextField();
    final String noArgsMessage = "The field is required";
    final String fieldArgMessage = "The field ''{0}'' is required";
    // Test with no formatting
    DiagnosticImpl diag = new DiagnosticImpl(Diagnostic.INFO, uic, input, noArgsMessage);
    Assert.assertEquals("Incorrect description text", noArgsMessage, diag.getDescription());
    // Test with formatting, but missing label text should default to empty String
    diag = new DiagnosticImpl(Diagnostic.INFO, uic, input, fieldArgMessage, input);
    Assert.assertEquals("Incorrect description text", "The field '' is required", diag.getDescription());
    // Test with formatting with accessible text set
    input.setAccessibleText("a");
    Assert.assertEquals("Incorrect description text", "The field 'a' is required", diag.getDescription());
    // Test with formatting with toolTip
    input.setAccessibleText(null);
    input.setToolTip("a tooltip");
    Assert.assertEquals("Incorrect description text", "The field 'a tooltip' is required", diag.getDescription());
    // Test with label set
    WLabel label = new WLabel("bc", input);
    Assert.assertEquals("Incorrect description text", "The field 'bc' is required", diag.getDescription());
    // Test with label set, with a colon at the end
    label.setText("def:");
    Assert.assertEquals("Incorrect description text", "The field 'def' is required", diag.getDescription());
}
Also used : UIContext(com.github.bordertech.wcomponents.UIContext) UIContextImpl(com.github.bordertech.wcomponents.UIContextImpl) WTextField(com.github.bordertech.wcomponents.WTextField) WLabel(com.github.bordertech.wcomponents.WLabel) Test(org.junit.Test)

Example 14 with UIContextImpl

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

the class WComponentRenderPerfTest method runRenderTest.

/**
 * Runs the render test.
 */
private static void runRenderTest() {
    UIContextImpl uic = new UIContextImpl();
    PrintWriter printWriter = new PrintWriter(new NullWriter());
    RenderContext renderContext = new WebXmlRenderContext(printWriter);
    WComponent component = null;
    long baseLineMemory = getHeapUsed();
    try {
        component = (WComponent) Class.forName(CLASS_TO_TEST).newInstance();
    } catch (Exception e) {
        String msg = "Unable to instantiate test component: " + CLASS_TO_TEST;
        LOG.error(LINE_PREFIX + msg);
        throw new SystemException(msg, e);
    }
    long memBeforePaint = getHeapUsed() - baseLineMemory;
    // Set up velocity etc. to obtain a memory reading, and
    // so that the performance results aren't skewed too much
    UIContextHolder.pushContext(uic);
    try {
        component.paint(renderContext);
        long memAfterOnePaint = getHeapUsed() - baseLineMemory;
        long startTime = System.currentTimeMillis();
        // Figure out the loop overhead
        for (int i = 0; i < NUM_RENDERS; i++) {
        }
        long loopOverhead = System.currentTimeMillis() - startTime;
        startTime = System.currentTimeMillis();
        // Now run the render test
        for (int i = 0; i < NUM_RENDERS; i++) {
            component.paint(renderContext);
        }
        long elapsedTime = System.currentTimeMillis() - startTime - loopOverhead;
        long memAfterAllPaints = getHeapUsed() - baseLineMemory;
        LOG.info(LINE_PREFIX + "Memory use before paint: " + memBeforePaint);
        LOG.info(LINE_PREFIX + "Memory use after 1 paint: " + memAfterOnePaint);
        LOG.info(LINE_PREFIX + "Memory use after " + NUM_RENDERS + " paints: " + memAfterAllPaints);
        LOG.info(LINE_PREFIX + "Render time: " + (elapsedTime / (double) NUM_RENDERS) + "ms");
        Object[] treeAndSession = new Object[] { component, uic };
        ObjectGraphNode root = ObjectGraphDump.dump(treeAndSession);
        LOG.info(LINE_PREFIX + "Component mem use: " + ((ObjectGraphNode) root.getChildAt(0)).getSize());
        LOG.info(LINE_PREFIX + "UIC mem use: " + ((ObjectGraphNode) root.getChildAt(1)).getSize());
    } finally {
        UIContextHolder.popContext();
    }
}
Also used : WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) RenderContext(com.github.bordertech.wcomponents.RenderContext) WebXmlRenderContext(com.github.bordertech.wcomponents.servlet.WebXmlRenderContext) NullWriter(com.github.bordertech.wcomponents.util.NullWriter) IOException(java.io.IOException) SystemException(com.github.bordertech.wcomponents.util.SystemException) WComponent(com.github.bordertech.wcomponents.WComponent) SystemException(com.github.bordertech.wcomponents.util.SystemException) UIContextImpl(com.github.bordertech.wcomponents.UIContextImpl) ObjectGraphNode(com.github.bordertech.wcomponents.util.ObjectGraphNode) PrintWriter(java.io.PrintWriter)

Example 15 with UIContextImpl

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

the class WServlet_Test method testServiceAjaxRequest.

@Test
public void testServiceAjaxRequest() throws ServletException, IOException {
    AjaxTestUI ui = new AjaxTestUI();
    ui.setLocked(true);
    MockHttpSession session1 = new MockHttpSession();
    UIContextImpl uic1 = new UIContextImpl();
    uic1.setEnvironment(new WServlet.WServletEnvironment("app", "http://localhost", ""));
    uic1.setUI(ui);
    uic1.getEnvironment().setSessionToken("1");
    uic1.getEnvironment().setStep(1);
    MockHttpSession session2 = new MockHttpSession();
    UIContextImpl uic2 = new UIContextImpl();
    uic2.setEnvironment(new WServlet.WServletEnvironment("app", "http://localhost", ""));
    uic2.setUI(ui);
    uic2.getEnvironment().setSessionToken("2");
    // Request cycle for session 1
    setActiveContext(uic1);
    ui.serviceRequest(new ServletRequest(new MockHttpServletRequest(session1)));
    ui.preparePaint(new ServletRequest(new MockHttpServletRequest(session1)));
    ui.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
    // Request cycle for session 2
    setActiveContext(uic2);
    ui.serviceRequest(new ServletRequest(new MockHttpServletRequest(session2)));
    ui.preparePaint(new ServletRequest(new MockHttpServletRequest(session2)));
    ui.paint(new WebXmlRenderContext(new PrintWriter(new NullWriter())));
    // check handle request / paint counts for each session - label should not have been painted for either.
    setActiveContext(uic1);
    Assert.assertEquals("HandleRequest should have been called for main panel in session1", 1, ui.mainPanel.getHandleRequestCount());
    Assert.assertEquals("HandleRequest should have been called for ajax panel in session1", 1, ui.ajaxPanel.getHandleRequestCount());
    Assert.assertEquals("HandleRequest should have been called for label panel in session1", 1, ui.label.getHandleRequestCount());
    Assert.assertEquals("Main panel should have painted in session1", 1, ui.mainPanel.getPaintCount());
    Assert.assertEquals("Ajax panel should have painted in session1", 1, ui.ajaxPanel.getPaintCount());
    Assert.assertEquals("Label should not have painted in session1", 0, ui.label.getPaintCount());
    setActiveContext(uic2);
    Assert.assertEquals("HandleRequest should have been called for main panel in session2", 1, ui.mainPanel.getHandleRequestCount());
    Assert.assertEquals("HandleRequest should have been called for ajax panel in session2", 1, ui.ajaxPanel.getHandleRequestCount());
    Assert.assertEquals("HandleRequest should have been called for label panel in session2", 1, ui.label.getHandleRequestCount());
    Assert.assertEquals("Main panel should have painted in session2", 1, ui.mainPanel.getPaintCount());
    Assert.assertEquals("Ajax panel should have painted in session2", 1, ui.ajaxPanel.getPaintCount());
    Assert.assertEquals("Label should not have painted in session2", 0, ui.label.getPaintCount());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) MockHttpServletRequest(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpServletRequest) UIContextImpl(com.github.bordertech.wcomponents.UIContextImpl) MockHttpSession(com.github.bordertech.wcomponents.util.mock.servlet.MockHttpSession) NullWriter(com.github.bordertech.wcomponents.util.NullWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

UIContextImpl (com.github.bordertech.wcomponents.UIContextImpl)17 UIContext (com.github.bordertech.wcomponents.UIContext)11 Test (org.junit.Test)7 Before (org.junit.Before)6 WLabel (com.github.bordertech.wcomponents.WLabel)5 WTextField (com.github.bordertech.wcomponents.WTextField)4 PrintWriter (java.io.PrintWriter)4 WApplication (com.github.bordertech.wcomponents.WApplication)3 WButton (com.github.bordertech.wcomponents.WButton)3 MockWEnvironment (com.github.bordertech.wcomponents.MockWEnvironment)2 RenderContext (com.github.bordertech.wcomponents.RenderContext)2 WText (com.github.bordertech.wcomponents.WText)2 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)2 NullWriter (com.github.bordertech.wcomponents.util.NullWriter)2 SystemException (com.github.bordertech.wcomponents.util.SystemException)2 MockRequest (com.github.bordertech.wcomponents.util.mock.MockRequest)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 FatalErrorPage (com.github.bordertech.wcomponents.FatalErrorPage)1 TestLookupTable (com.github.bordertech.wcomponents.TestLookupTable)1