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  
23  import javax.faces.component.UIParameter;
24  import javax.faces.component.behavior.AjaxBehavior;
25  import javax.faces.component.html.HtmlCommandButton;
26  import javax.faces.component.html.HtmlForm;
27  
28  import junit.framework.Test;
29  import junit.framework.TestSuite;
30  
31  import org.apache.myfaces.shared.config.MyfacesConfig;
32  import org.apache.myfaces.test.base.AbstractJsfTestCase;
33  import org.apache.myfaces.test.mock.MockExternalContext;
34  import org.apache.myfaces.test.mock.MockHttpServletRequest;
35  import org.apache.myfaces.test.mock.MockHttpServletResponse;
36  import org.apache.myfaces.test.mock.MockRenderKitFactory;
37  import org.apache.myfaces.test.mock.MockResponseWriter;
38  import org.apache.myfaces.test.mock.MockServletContext;
39  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
40  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
41  
42  public class HtmlButtonRendererTest extends AbstractJsfTestCase {
43  
44      private MockResponseWriter writer;
45      private HtmlCommandButton commandButton;
46      private HtmlForm form;
47      
48      public HtmlButtonRendererTest(String name) {
49          super(name);
50      }
51      
52      public static Test suite() {
53          return new TestSuite(HtmlButtonRendererTest.class);
54      }
55  
56      public void setUp() throws Exception {
57          super.setUp();
58          writer = new MockResponseWriter(new StringWriter(), null, null);
59          facesContext.setResponseWriter(writer);
60          commandButton = new HtmlCommandButton();
61          form = new HtmlForm();
62          commandButton.setParent(form);
63          
64          facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
65          facesContext.getRenderKit().addRenderer(
66                  commandButton.getFamily(),
67                  commandButton.getRendererType(),
68                  new HtmlButtonRenderer());
69          facesContext.getRenderKit().addRenderer(
70                  form.getFamily(),
71                  form.getRendererType(),
72                  new HtmlFormRenderer());
73          facesContext.getRenderKit().addRenderer(
74                  "javax.faces.Input",
75                  "javax.faces.Hidden",
76                  new HtmlHiddenRenderer());
77          
78          facesContext.getAttributes().put("org.apache.myfaces.RENDERED_JSF_JS", Boolean.TRUE);
79      }
80      
81      public void tearDown() throws Exception {
82          super.tearDown();
83          writer = null;
84      }
85  
86      public void testJSNotAllowedHtmlPropertyPassTru() throws Exception {
87          HtmlRenderedAttr[] attrs = {
88              //_AccesskeyProperty
89              new HtmlRenderedAttr("accesskey"),
90              //_UniversalProperties
91              new HtmlRenderedAttr("dir"), 
92              new HtmlRenderedAttr("lang"), 
93              new HtmlRenderedAttr("title"),
94  
95              /* If js is set to false, no need to bother over event attributes
96              //_FocusBlurProperties
97              new HtmlRenderedAttr("onfocus"), 
98              new HtmlRenderedAttr("onblur"),
99              //_ChangeSelectProperties
100             new HtmlRenderedAttr("onchange"), 
101             new HtmlRenderedAttr("onselect"),
102             //_EventProperties
103             new HtmlRenderedAttr("onclick", "onclick", "onclick=\""), 
104             new HtmlRenderedAttr("ondblclick"), 
105             new HtmlRenderedAttr("onkeydown"), 
106             new HtmlRenderedAttr("onkeypress"),
107             new HtmlRenderedAttr("onkeyup"), 
108             new HtmlRenderedAttr("onmousedown"), 
109             new HtmlRenderedAttr("onmousemove"), 
110             new HtmlRenderedAttr("onmouseout"),
111             new HtmlRenderedAttr("onmouseover"), 
112             new HtmlRenderedAttr("onmouseup"),
113             */
114             
115             //_StyleProperties
116             new HtmlRenderedAttr("style"), 
117             new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\""),
118             //_TabindexProperty
119             new HtmlRenderedAttr("tabindex")
120         };
121         
122         MockServletContext servletContext = new MockServletContext();
123         servletContext.addInitParameter("org.apache.myfaces.ALLOW_JAVASCRIPT", "false");
124         MockExternalContext mockExtCtx = new MockExternalContext(servletContext, 
125                 new MockHttpServletRequest(), new MockHttpServletResponse());
126         MyfacesConfig config = MyfacesConfig.getCurrentInstance(mockExtCtx);
127         facesContext.setExternalContext(mockExtCtx);
128     
129         HtmlCheckAttributesUtil.checkRenderedAttributes(
130                 commandButton, facesContext, writer, attrs);
131         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
132             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
133         }
134     }
135     
136     public void testAllowedHtmlPropertyPassTru() throws Exception {
137            HtmlRenderedAttr[] attrs = {
138                //_AccesskeyProperty
139                new HtmlRenderedAttr("accesskey"),
140                //_UniversalProperties
141                new HtmlRenderedAttr("dir"), 
142                new HtmlRenderedAttr("lang"), 
143                new HtmlRenderedAttr("title"),
144                //_FocusBlurProperties
145                new HtmlRenderedAttr("onfocus"), 
146                new HtmlRenderedAttr("onblur"),
147                //_ChangeSelectProperties
148                new HtmlRenderedAttr("onchange"), 
149                new HtmlRenderedAttr("onselect"),
150                //_EventProperties
151                //onclick is not allowed in this test case
152                new HtmlRenderedAttr("onclick", "onclick", "onclick=\""),
153                new HtmlRenderedAttr("ondblclick"), 
154                new HtmlRenderedAttr("onkeydown"), 
155                new HtmlRenderedAttr("onkeypress"),
156                new HtmlRenderedAttr("onkeyup"), 
157                new HtmlRenderedAttr("onmousedown"), 
158                new HtmlRenderedAttr("onmousemove"), 
159                new HtmlRenderedAttr("onmouseout"),
160                new HtmlRenderedAttr("onmouseover"), 
161                new HtmlRenderedAttr("onmouseup"),
162                //_StyleProperties
163                new HtmlRenderedAttr("style"), 
164                new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\""),
165                //_TabindexProperty
166                new HtmlRenderedAttr("tabindex")
167            };
168         
169         MockServletContext servletContext = new MockServletContext();
170         servletContext.addInitParameter("org.apache.myfaces.ALLOW_JAVASCRIPT", "true");
171         MockExternalContext mockExtCtx = new MockExternalContext(servletContext, 
172                 new MockHttpServletRequest(), new MockHttpServletResponse());
173         MyfacesConfig config = MyfacesConfig.getCurrentInstance(mockExtCtx);
174         facesContext.setExternalContext(mockExtCtx);
175         
176         HtmlCheckAttributesUtil.checkRenderedAttributes(
177                 commandButton, facesContext, writer, attrs);
178         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
179             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
180         }
181 
182     }
183     
184     /**
185      * Components that render client behaviors should always render "id" and "name" attribute
186      */
187     public void testClientBehaviorHolderRendersIdAndName() 
188     {
189         commandButton.addClientBehavior("focus", new AjaxBehavior());
190         try 
191         {
192             commandButton.encodeAll(facesContext);
193             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
194             assertTrue(output.matches(".+id=\".+\".+"));
195             assertTrue(output.matches(".+name=\".+\".+"));
196         }
197         catch (Exception e)
198         {
199             fail(e.getMessage());
200         }
201         
202     }
203     
204     /**
205      * If a h:commandButton has any UIParameter children, he should
206      * render them with a renderer of family javax.faces.Input and
207      * renderer type javax.faces.Hidden.
208      * If the disable attribute of a child UIParameter is true,
209      * he should be ignored.
210      * @throws Exception
211      */
212     public void testCommandButtonRendersNotDisabledUIParameters() throws Exception
213     {
214         UIParameter param1 = new UIParameter();
215         param1.setName("param1");
216         param1.setValue("value1");
217         param1.setDisable(true);
218         UIParameter param2 = new UIParameter();
219         param2.setName("param2");
220         param2.setValue("value2");
221         commandButton.getChildren().add(param1);
222         commandButton.getChildren().add(param2);
223         
224         commandButton.setValue("commandButton");
225         
226         commandButton.encodeAll(facesContext);
227         String output = writer.getWriter().toString();
228         assertFalse(output.contains("param1"));
229         assertFalse(output.contains("value1"));
230         assertTrue(output.contains("param2"));
231         assertTrue(output.contains("value2"));
232     }
233     
234 }