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 java.util.ArrayList;
23  import java.util.List;
24  
25  import javax.faces.component.UISelectItem;
26  import javax.faces.component.UISelectItems;
27  import javax.faces.component.behavior.AjaxBehavior;
28  import javax.faces.component.html.HtmlSelectOneRadio;
29  import javax.faces.model.SelectItem;
30  
31  import junit.framework.Test;
32  import junit.framework.TestSuite;
33  
34  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
35  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
36  import org.apache.myfaces.test.base.AbstractJsfTestCase;
37  import org.apache.myfaces.test.mock.MockRenderKitFactory;
38  import org.apache.myfaces.test.mock.MockResponseWriter;
39  
40  /**
41   * @author Bruno Aranda (latest modification by $Author: lu4242 $)
42   * @version $Revision: 942849 $ $Date: 2010-05-10 14:10:01 -0500 (Mon, 10 May 2010) $
43   */
44  public class HtmlRadioRendererTest extends AbstractJsfTestCase
45  {
46      private MockResponseWriter writer ;
47      private HtmlSelectOneRadio selectOneRadio;
48  
49      public HtmlRadioRendererTest(String name)
50      {
51          super(name);
52      }
53      
54      public static Test suite() {
55          return new TestSuite(HtmlRadioRendererTest.class);
56      }
57  
58      public void setUp() throws Exception
59      {
60          super.setUp();
61  
62          selectOneRadio = new HtmlSelectOneRadio();
63  
64          writer = new MockResponseWriter(new StringWriter(), null, null);
65          facesContext.setResponseWriter(writer);
66  
67          facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
68          facesContext.getRenderKit().addRenderer(
69                  selectOneRadio.getFamily(),
70                  selectOneRadio.getRendererType(),
71                  new HtmlRadioRenderer());
72          
73          facesContext.getAttributes().put("org.apache.myfaces.RENDERED_JSF_JS", Boolean.TRUE);
74      }
75  
76      public void tearDown() throws Exception
77      {
78          super.tearDown();
79          selectOneRadio = null;
80          writer = null;
81      }
82      
83      public void testHtmlPropertyPassTru() throws Exception
84      {
85          HtmlRenderedAttr[] attrs = {
86              //_AccesskeyProperty
87              new HtmlRenderedAttr("accesskey"),
88              //_UniversalProperties
89              new HtmlRenderedAttr("dir"), 
90              new HtmlRenderedAttr("lang"), 
91              new HtmlRenderedAttr("title"),
92              //_FocusBlurProperties
93              new HtmlRenderedAttr("onfocus"), 
94              new HtmlRenderedAttr("onblur"),
95              //_ChangeSelectProperties
96              new HtmlRenderedAttr("onchange"), 
97              new HtmlRenderedAttr("onselect"),
98              //_EventProperties
99              new HtmlRenderedAttr("onclick"), 
100             new HtmlRenderedAttr("ondblclick"), 
101             new HtmlRenderedAttr("onkeydown"), 
102             new HtmlRenderedAttr("onkeypress"),
103             new HtmlRenderedAttr("onkeyup"), 
104             new HtmlRenderedAttr("onmousedown"), 
105             new HtmlRenderedAttr("onmousemove"), 
106             new HtmlRenderedAttr("onmouseout"),
107             new HtmlRenderedAttr("onmouseover"), 
108             new HtmlRenderedAttr("onmouseup"),
109             //_StyleProperties
110             new HtmlRenderedAttr("style", 1), 
111             new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\"", 1),
112 
113             //_TabindexProperty
114             new HtmlRenderedAttr("tabindex")
115         };
116         
117         List<SelectItem> items = new ArrayList<SelectItem>();
118         items.add(new SelectItem("mars"));
119 
120         UISelectItems selectItems = new UISelectItems();
121         selectItems.setValue(items);
122 
123         selectOneRadio.getChildren().add(selectItems);
124 
125         HtmlCheckAttributesUtil.checkRenderedAttributes(
126                 selectOneRadio, facesContext, writer, attrs);
127         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
128             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
129         }
130     }
131     
132     /**
133      * Components that render client behaviors should always render "id" and "name" attribute
134      */
135     public void testClientBehaviorHolderRendersIdAndName() 
136     {
137         UISelectItem item1 = new UISelectItem();
138         item1.setItemLabel("#1");
139         item1.setItemValue("#1");
140         
141         UISelectItem item2 = new UISelectItem();
142         item2.setItemLabel("#2");
143         item2.setItemValue("#2");
144         
145         selectOneRadio.addClientBehavior("keypress", new AjaxBehavior());
146         try 
147         {
148             selectOneRadio.getChildren().add(item1);
149             selectOneRadio.getChildren().add(item2);
150             selectOneRadio.encodeAll(facesContext);
151             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
152             assertTrue(output.matches(".+id=\".+\".+"));
153             assertTrue(output.matches(".+name=\".+\".+"));
154         }
155         catch (Exception e)
156         {
157             fail(e.getMessage());
158         }
159         
160     }
161 }