View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.renderkit.html;
20  
21  import java.io.StringWriter;
22  import javax.faces.component.behavior.AjaxBehavior;
23  
24  import javax.faces.component.html.HtmlOutputText;
25  import javax.faces.component.html.HtmlPanelGroup;
26  
27  import junit.framework.Test;
28  import static junit.framework.TestCase.assertTrue;
29  import static junit.framework.TestCase.fail;
30  import junit.framework.TestSuite;
31  
32  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
33  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
34  import org.apache.myfaces.test.base.AbstractJsfTestCase;
35  import org.apache.myfaces.test.mock.MockRenderKitFactory;
36  import org.apache.myfaces.test.mock.MockResponseWriter;
37  
38  /**
39   * @author Bruno Aranda (latest modification by $Author$)
40   * @version $Revision$ $Date$
41   */
42  public class HtmlGroupRendererTest extends AbstractJsfTestCase
43  {
44      private static String PANEL_CHILD_TEXT = "PANEL";
45  
46      private MockResponseWriter writer ;
47      private HtmlPanelGroup panelGroup;
48  
49      public HtmlGroupRendererTest(String name)
50      {
51          super(name);
52      }
53      
54      public static Test suite() {
55          return new TestSuite(HtmlGroupRendererTest.class);
56      }
57  
58      public void setUp() throws Exception
59      {
60          super.setUp();
61  
62          panelGroup = new HtmlPanelGroup();
63  
64          HtmlOutputText panelChildOutputText = new HtmlOutputText();
65          panelChildOutputText.setValue(PANEL_CHILD_TEXT);
66          panelGroup.getChildren().add(panelChildOutputText);
67  
68          writer = new MockResponseWriter(new StringWriter(), null, null);
69          facesContext.setResponseWriter(writer);
70  
71          facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
72          facesContext.getRenderKit().addRenderer(
73                  panelGroup.getFamily(),
74                  panelGroup.getRendererType(),
75                  new HtmlGroupRenderer());
76          facesContext.getRenderKit().addRenderer(
77                  panelChildOutputText.getFamily(),
78                  panelChildOutputText.getRendererType(),
79                  new HtmlTextRenderer());
80          facesContext.getRenderKit().addClientBehaviorRenderer(
81                  AjaxBehavior.BEHAVIOR_ID, new HtmlAjaxBehaviorRenderer());
82          
83          facesContext.getAttributes().put("org.apache.myfaces.RENDERED_JSF_JS", Boolean.TRUE);
84      }
85  
86      public void tearDown()throws Exception
87      {
88          super.tearDown();
89          writer = null;
90      }
91      
92      public void testHtmlPropertyPassTru() throws Exception
93      { 
94          HtmlRenderedAttr[] attrs = {
95                  //_EventProperties
96                  new HtmlRenderedAttr("onclick"),
97                  new HtmlRenderedAttr("ondblclick"), 
98                  new HtmlRenderedAttr("onkeydown"), 
99                  new HtmlRenderedAttr("onkeypress"),
100                 new HtmlRenderedAttr("onkeyup"), 
101                 new HtmlRenderedAttr("onmousedown"), 
102                 new HtmlRenderedAttr("onmousemove"), 
103                 new HtmlRenderedAttr("onmouseout"),
104                 new HtmlRenderedAttr("onmouseover"), 
105                 new HtmlRenderedAttr("onmouseup"),
106                 //_StyleProperties
107                 new HtmlRenderedAttr("style"), 
108                 new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\"")
109                 }; 
110             //HtmlCheckAttributesUtil.generateBasicReadOnlyAttrs();
111 
112         HtmlCheckAttributesUtil.checkRenderedAttributes(
113                 panelGroup, facesContext, writer, attrs);
114         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
115             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
116         }
117     }
118     
119     public void testHtmlPropertyPassTruNotRendered() throws Exception
120     { 
121         HtmlRenderedAttr[] attrs = HtmlCheckAttributesUtil.generateAttrsNotRenderedForReadOnly();        
122 
123         HtmlCheckAttributesUtil.checkRenderedAttributes(
124                 panelGroup, facesContext, writer, attrs);
125         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
126             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
127         }
128     }
129     
130     public void testClientBehaviorHolderRendersIdAndNameOutputLink() 
131     {
132         panelGroup.addClientBehavior("keypress", new AjaxBehavior());
133         try 
134         {
135             panelGroup.encodeAll(facesContext);
136             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
137             assertTrue(output.matches(".+id=\".+\".+"));
138             assertTrue(output.matches(".+jsf.ajax.request.+"));
139         }
140         catch (Exception e)
141         {
142             fail(e.getMessage());
143         }
144     }
145 }