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$)
42   * @version $Revision$ $Date$
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              new HtmlRenderedAttr("role"),
93              //_FocusBlurProperties
94              new HtmlRenderedAttr("onfocus"), 
95              new HtmlRenderedAttr("onblur"),
96              //_ChangeSelectProperties
97              new HtmlRenderedAttr("onchange"), 
98              new HtmlRenderedAttr("onselect"),
99              //_EventProperties
100             new HtmlRenderedAttr("onclick"), 
101             new HtmlRenderedAttr("ondblclick"), 
102             new HtmlRenderedAttr("onkeydown"), 
103             new HtmlRenderedAttr("onkeypress"),
104             new HtmlRenderedAttr("onkeyup"), 
105             new HtmlRenderedAttr("onmousedown"), 
106             new HtmlRenderedAttr("onmousemove"), 
107             new HtmlRenderedAttr("onmouseout"),
108             new HtmlRenderedAttr("onmouseover"), 
109             new HtmlRenderedAttr("onmouseup"),
110             //_StyleProperties
111             new HtmlRenderedAttr("style", 1), 
112             new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\"", 1),
113 
114             //_TabindexProperty
115             new HtmlRenderedAttr("tabindex")
116         };
117         
118         List<SelectItem> items = new ArrayList<SelectItem>();
119         items.add(new SelectItem("mars"));
120 
121         UISelectItems selectItems = new UISelectItems();
122         selectItems.setValue(items);
123 
124         selectOneRadio.getChildren().add(selectItems);
125 
126         HtmlCheckAttributesUtil.checkRenderedAttributes(
127                 selectOneRadio, facesContext, writer, attrs);
128         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
129             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
130         }
131     }
132     
133     /**
134      * Components that render client behaviors should always render "id" and "name" attribute
135      */
136     public void testClientBehaviorHolderRendersIdAndName() 
137     {
138         UISelectItem item1 = new UISelectItem();
139         item1.setItemLabel("#1");
140         item1.setItemValue("#1");
141         
142         UISelectItem item2 = new UISelectItem();
143         item2.setItemLabel("#2");
144         item2.setItemValue("#2");
145         
146         selectOneRadio.addClientBehavior("keypress", new AjaxBehavior());
147         try 
148         {
149             selectOneRadio.getChildren().add(item1);
150             selectOneRadio.getChildren().add(item2);
151             selectOneRadio.encodeAll(facesContext);
152             String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
153             assertTrue(output.matches(".+id=\".+\".+"));
154             assertTrue(output.matches(".+name=\".+\".+"));
155         }
156         catch (Exception e)
157         {
158             fail(e.getMessage());
159         }
160         
161     }
162 }