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.ext;
20  
21  import javax.faces.component.html.HtmlForm;
22  
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  
26  import org.apache.myfaces.component.html.ext.HtmlCommandButton;
27  import org.apache.myfaces.shared_impl.config.MyfacesConfig;
28  import org.apache.myfaces.test.AbstractTomahawkViewControllerTestCase;
29  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
30  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
31  import org.apache.shale.test.mock.MockExternalContext;
32  import org.apache.shale.test.mock.MockHttpServletRequest;
33  import org.apache.shale.test.mock.MockHttpServletResponse;
34  import org.apache.shale.test.mock.MockResponseWriter;
35  import org.apache.shale.test.mock.MockServletContext;
36  
37  public class HtmlButtonExRendererTest extends AbstractTomahawkViewControllerTestCase {
38  
39      private MockResponseWriter writer;
40      private HtmlCommandButton commandButton;
41      private HtmlForm form;
42      
43      
44      public HtmlButtonExRendererTest(String name) {
45          super(name);
46      }
47      
48      public static Test suite() {
49          return new TestSuite(HtmlButtonExRendererTest.class);
50      }
51      
52      public void setUp() throws Exception {
53          super.setUp();
54          writer = (MockResponseWriter)facesContext.getResponseWriter();
55          commandButton = new HtmlCommandButton();
56          form = new HtmlForm();
57          commandButton.setParent(form);
58      }
59      
60      public void tearDown() throws Exception {
61          super.tearDown();
62          writer = null;
63          form = null;
64          commandButton = null;
65      }
66  
67      public void testJSAllowedHtmlPropertyPassTru() throws Exception 
68      {
69          HtmlRenderedAttr[] attrs = {
70                  //_AccesskeyProperty
71                  new HtmlRenderedAttr("accesskey"),
72                  //_UniversalProperties
73                  new HtmlRenderedAttr("dir"), 
74                  new HtmlRenderedAttr("lang"), 
75                  new HtmlRenderedAttr("title"),
76                  //_FocusBlurProperties
77                  new HtmlRenderedAttr("onfocus"), 
78                  new HtmlRenderedAttr("onblur"),
79                  //_ChangeSelectProperties
80                  new HtmlRenderedAttr("onchange"), 
81                  new HtmlRenderedAttr("onselect"),
82                  //_EventProperties
83                  new HtmlRenderedAttr("onclick", "onclick", "onclick=\""), 
84                  new HtmlRenderedAttr("ondblclick"), 
85                  new HtmlRenderedAttr("onkeydown"), 
86                  new HtmlRenderedAttr("onkeypress"),
87                  new HtmlRenderedAttr("onkeyup"), 
88                  new HtmlRenderedAttr("onmousedown"), 
89                  new HtmlRenderedAttr("onmousemove"), 
90                  new HtmlRenderedAttr("onmouseout"),
91                  new HtmlRenderedAttr("onmouseover"), 
92                  new HtmlRenderedAttr("onmouseup"),
93                  //_StyleProperties
94                  new HtmlRenderedAttr("style"), 
95                  new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\""),
96                  //_TabindexProperty
97                  new HtmlRenderedAttr("tabindex")
98              };
99          
100         MockServletContext servletContext = new MockServletContext();
101         servletContext.addInitParameter("org.apache.myfaces.ALLOW_JAVASCRIPT", "true");
102         MockExternalContext mockExtCtx = new MockExternalContext(servletContext, 
103                 new MockHttpServletRequest(), new MockHttpServletResponse());
104         MyfacesConfig config = MyfacesConfig.getCurrentInstance(mockExtCtx);
105         facesContext.setExternalContext(mockExtCtx);
106     
107         HtmlCheckAttributesUtil.checkRenderedAttributes(
108                 commandButton, facesContext, writer, attrs);
109         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) 
110         {
111             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
112         }
113     }
114     
115     public void testJSNotAllowedHtmlPropertyPassTru() throws Exception 
116     {
117         HtmlRenderedAttr[] attrs = HtmlCheckAttributesUtil.generateBasicAttrs();
118         
119         HtmlCheckAttributesUtil.checkRenderedAttributes(
120                 commandButton, facesContext, writer, attrs);
121         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) 
122         {
123             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
124         }
125 
126     }
127 }