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              new HtmlRenderedAttr("role"),
95  
96              /* If js is set to false, no need to bother over event attributes
97              //_FocusBlurProperties
98              new HtmlRenderedAttr("onfocus"), 
99              new HtmlRenderedAttr("onblur"),
100             //_ChangeSelectProperties
101             new HtmlRenderedAttr("onchange"), 
102             new HtmlRenderedAttr("onselect"),
103             //_EventProperties
104             new HtmlRenderedAttr("onclick", "onclick", "onclick=\""), 
105             new HtmlRenderedAttr("ondblclick"), 
106             new HtmlRenderedAttr("onkeydown"), 
107             new HtmlRenderedAttr("onkeypress"),
108             new HtmlRenderedAttr("onkeyup"), 
109             new HtmlRenderedAttr("onmousedown"), 
110             new HtmlRenderedAttr("onmousemove"), 
111             new HtmlRenderedAttr("onmouseout"),
112             new HtmlRenderedAttr("onmouseover"), 
113             new HtmlRenderedAttr("onmouseup"),
114             */
115             
116             //_StyleProperties
117             new HtmlRenderedAttr("style"), 
118             new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\""),
119             //_TabindexProperty
120             new HtmlRenderedAttr("tabindex")
121         };
122         
123         MockServletContext servletContext = new MockServletContext();
124         servletContext.addInitParameter("org.apache.myfaces.ALLOW_JAVASCRIPT", "false");
125         MockExternalContext mockExtCtx = new MockExternalContext(servletContext, 
126                 new MockHttpServletRequest(), new MockHttpServletResponse());
127         MyfacesConfig config = MyfacesConfig.getCurrentInstance(mockExtCtx);
128         facesContext.setExternalContext(mockExtCtx);
129     
130         HtmlCheckAttributesUtil.checkRenderedAttributes(
131                 commandButton, facesContext, writer, attrs);
132         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
133             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
134         }
135     }
136     
137     public void testAllowedHtmlPropertyPassTru() throws Exception {
138            HtmlRenderedAttr[] attrs = {
139                //_AccesskeyProperty
140                new HtmlRenderedAttr("accesskey"),
141                //_UniversalProperties
142                new HtmlRenderedAttr("dir"), 
143                new HtmlRenderedAttr("lang"), 
144                new HtmlRenderedAttr("title"),
145                new HtmlRenderedAttr("role"),
146                //_FocusBlurProperties
147                new HtmlRenderedAttr("onfocus"), 
148                new HtmlRenderedAttr("onblur"),
149                //_ChangeSelectProperties
150                new HtmlRenderedAttr("onchange"), 
151                new HtmlRenderedAttr("onselect"),
152                //_EventProperties
153                //onclick is not allowed in this test case
154                new HtmlRenderedAttr("onclick", "onclick", "onclick=\""),
155                new HtmlRenderedAttr("ondblclick"), 
156                new HtmlRenderedAttr("onkeydown"), 
157                new HtmlRenderedAttr("onkeypress"),
158                new HtmlRenderedAttr("onkeyup"), 
159                new HtmlRenderedAttr("onmousedown"), 
160                new HtmlRenderedAttr("onmousemove"), 
161                new HtmlRenderedAttr("onmouseout"),
162                new HtmlRenderedAttr("onmouseover"), 
163                new HtmlRenderedAttr("onmouseup"),
164                //_StyleProperties
165                new HtmlRenderedAttr("style"), 
166                new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\""),
167                //_TabindexProperty
168                new HtmlRenderedAttr("tabindex")
169            };
170         
171         MockServletContext servletContext = new MockServletContext();
172         servletContext.addInitParameter("org.apache.myfaces.ALLOW_JAVASCRIPT", "true");
173         MockExternalContext mockExtCtx = new MockExternalContext(servletContext, 
174                 new MockHttpServletRequest(), new MockHttpServletResponse());
175         MyfacesConfig config = MyfacesConfig.getCurrentInstance(mockExtCtx);
176         facesContext.setExternalContext(mockExtCtx);
177         
178         HtmlCheckAttributesUtil.checkRenderedAttributes(
179                 commandButton, facesContext, writer, attrs);
180         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
181             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
182         }
183 
184     }
185     
186     /**
187      * Components that render client behaviors should always render "id" and "name" attribute
188      */
189     public void testClientBehaviorHolderRendersIdAndName() 
190     {
191         commandButton.addClientBehavior("focus", new AjaxBehavior());
192         try 
193         {
194             commandButton.encodeAll(facesContext);
195             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
196             assertTrue(output.matches(".+id=\".+\".+"));
197             assertTrue(output.matches(".+name=\".+\".+"));
198         }
199         catch (Exception e)
200         {
201             fail(e.getMessage());
202         }
203         
204     }
205     
206     /**
207      * If a h:commandButton has any UIParameter children, he should
208      * render them with a renderer of family javax.faces.Input and
209      * renderer type javax.faces.Hidden.
210      * If the disable attribute of a child UIParameter is true,
211      * he should be ignored.
212      * @throws Exception
213      */
214     public void testCommandButtonRendersNotDisabledUIParameters() throws Exception
215     {
216         UIParameter param1 = new UIParameter();
217         param1.setName("param1");
218         param1.setValue("value1");
219         param1.setDisable(true);
220         UIParameter param2 = new UIParameter();
221         param2.setName("param2");
222         param2.setValue("value2");
223         commandButton.getChildren().add(param1);
224         commandButton.getChildren().add(param2);
225         
226         commandButton.setValue("commandButton");
227         
228         commandButton.encodeAll(facesContext);
229         String output = writer.getWriter().toString();
230         assertFalse(output.contains("param1"));
231         assertFalse(output.contains("value1"));
232         assertTrue(output.contains("param2"));
233         assertTrue(output.contains("value2"));
234     }
235     
236 }