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.ext;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UISelectItems;
27  import javax.faces.model.SelectItem;
28  
29  import junit.framework.Test;
30  import junit.framework.TestSuite;
31  
32  import org.apache.myfaces.component.html.ext.HtmlSelectOneRadio;
33  import org.apache.myfaces.custom.radio.HtmlRadio;
34  import org.apache.myfaces.test.AbstractTomahawkViewControllerTestCase;
35  import org.apache.myfaces.test.mock.MockResponseWriter;
36  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
37  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
38  
39  public class HtmlRadioRendererTest extends AbstractTomahawkViewControllerTestCase
40  {
41      private HtmlSelectOneRadio selectOneRadio;
42  
43      public HtmlRadioRendererTest(String name)
44      {
45          super(name);
46      }
47      
48      public static Test suite() {
49          return new TestSuite(HtmlRadioRendererTest.class);
50      }
51  
52      public void setUp() throws Exception
53      {
54          super.setUp();
55          selectOneRadio = new HtmlSelectOneRadio();
56      }
57  
58      public void tearDown() throws Exception
59      {
60          super.tearDown();
61          selectOneRadio = null;
62      }
63      
64      public void testHtmlPropertyPassTru() throws Exception
65      {
66          HtmlRenderedAttr[] attrs = {
67              //_AccesskeyProperty
68              new HtmlRenderedAttr("accesskey"),
69              //_UniversalProperties
70              new HtmlRenderedAttr("dir"), 
71              new HtmlRenderedAttr("lang"), 
72              new HtmlRenderedAttr("title"),
73              //_FocusBlurProperties
74              new HtmlRenderedAttr("onfocus"), 
75              new HtmlRenderedAttr("onblur"),
76              //_ChangeSelectProperties
77              new HtmlRenderedAttr("onchange"), 
78              new HtmlRenderedAttr("onselect"),
79              //_EventProperties
80              new HtmlRenderedAttr("onclick"), 
81              new HtmlRenderedAttr("ondblclick"), 
82              new HtmlRenderedAttr("onkeydown"), 
83              new HtmlRenderedAttr("onkeypress"),
84              new HtmlRenderedAttr("onkeyup"), 
85              new HtmlRenderedAttr("onmousedown"), 
86              new HtmlRenderedAttr("onmousemove"), 
87              new HtmlRenderedAttr("onmouseout"),
88              new HtmlRenderedAttr("onmouseover"), 
89              new HtmlRenderedAttr("onmouseup"),
90              //_StyleProperties
91              new HtmlRenderedAttr("style", 1), 
92              new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\"", 1),
93              //_TabindexProperty
94              new HtmlRenderedAttr("tabindex")
95          };
96          
97          List items = new ArrayList();
98          items.add(new SelectItem("mars"));
99  
100         UISelectItems selectItems = new UISelectItems();
101         selectItems.setValue(items);
102 
103         selectOneRadio.getChildren().add(selectItems);
104         
105         MockResponseWriter writer = (MockResponseWriter)facesContext.getResponseWriter();
106         HtmlCheckAttributesUtil.checkRenderedAttributes(
107                 selectOneRadio, facesContext, writer, attrs);
108         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
109             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
110         }
111     }
112     
113     
114     public void testHtmlPropertyRadioPassTru() throws Exception
115     {
116         HtmlRenderedAttr[] attrs = {
117             //_AccesskeyProperty
118             new HtmlRenderedAttr("accesskey"),
119             //_UniversalProperties
120             new HtmlRenderedAttr("dir"), 
121             new HtmlRenderedAttr("lang"), 
122             new HtmlRenderedAttr("title"),
123             //_FocusBlurProperties
124             new HtmlRenderedAttr("onfocus"), 
125             new HtmlRenderedAttr("onblur"),
126             //_ChangeSelectProperties
127             new HtmlRenderedAttr("onchange"), 
128             new HtmlRenderedAttr("onselect"),
129             //_EventProperties
130             new HtmlRenderedAttr("onclick"), 
131             new HtmlRenderedAttr("ondblclick"), 
132             new HtmlRenderedAttr("onkeydown"), 
133             new HtmlRenderedAttr("onkeypress"),
134             new HtmlRenderedAttr("onkeyup"), 
135             new HtmlRenderedAttr("onmousedown"), 
136             new HtmlRenderedAttr("onmousemove"), 
137             new HtmlRenderedAttr("onmouseout"),
138             new HtmlRenderedAttr("onmouseover"), 
139             new HtmlRenderedAttr("onmouseup"),
140             //_StyleProperties
141             new HtmlRenderedAttr("style", 1), 
142             new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\"", 1),
143             //_TabindexProperty
144             new HtmlRenderedAttr("tabindex"),
145             new HtmlRenderedAttr("id","option1", "id=\"option1\"", 1)
146         };
147         
148         List items = new ArrayList();
149         items.add(new SelectItem("mars"));
150 
151         UISelectItems selectItems = new UISelectItems();
152         selectItems.setValue(items);
153 
154         selectOneRadio = new HtmlSelectOneRadio();
155         
156         selectOneRadio.setId("selectOneRadio");
157         selectOneRadio.setLayout("spread");
158         
159         facesContext.getViewRoot().getChildren().add(selectOneRadio);
160         
161         selectOneRadio.getChildren().add(selectItems);
162         
163         HtmlRadio radio = new HtmlRadio();
164         radio.setId("option1");
165         radio.setIndex(0);
166         radio.setFor("selectOneRadio");
167         
168         facesContext.getViewRoot().getChildren().add(radio);
169         
170         MockResponseWriter writer = (MockResponseWriter)facesContext.getResponseWriter();
171         HtmlCheckAttributesUtil.checkRenderedAttributes(
172                 radio, facesContext, writer, attrs);
173         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
174             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
175         }
176     }
177     
178     public void testHtmlPropertyRadioSelectOnePassTru() throws Exception
179     {
180         HtmlRenderedAttr[] attrs = {
181             //_AccesskeyProperty
182             new HtmlRenderedAttr("accesskey"),
183             //_UniversalProperties
184             new HtmlRenderedAttr("dir"), 
185             new HtmlRenderedAttr("lang"), 
186             new HtmlRenderedAttr("title"),
187             //_FocusBlurProperties
188             new HtmlRenderedAttr("onfocus"), 
189             new HtmlRenderedAttr("onblur"),
190             //_ChangeSelectProperties
191             new HtmlRenderedAttr("onchange"), 
192             new HtmlRenderedAttr("onselect"),
193             //_EventProperties
194             new HtmlRenderedAttr("onclick"), 
195             new HtmlRenderedAttr("ondblclick"), 
196             new HtmlRenderedAttr("onkeydown"), 
197             new HtmlRenderedAttr("onkeypress"),
198             new HtmlRenderedAttr("onkeyup"), 
199             new HtmlRenderedAttr("onmousedown"), 
200             new HtmlRenderedAttr("onmousemove"), 
201             new HtmlRenderedAttr("onmouseout"),
202             new HtmlRenderedAttr("onmouseover"), 
203             new HtmlRenderedAttr("onmouseup"),
204             //_StyleProperties
205             new HtmlRenderedAttr("style", 1), 
206             new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\"", 1),
207             //_TabindexProperty
208             new HtmlRenderedAttr("tabindex")
209         };
210         
211         List items = new ArrayList();
212         items.add(new SelectItem("mars"));
213 
214         UISelectItems selectItems = new UISelectItems();
215         selectItems.setValue(items);
216 
217         selectOneRadio = new HtmlSelectOneRadio();
218         
219         selectOneRadio.setId("selectOneRadio");
220         selectOneRadio.setLayout("spread");
221         
222         facesContext.getViewRoot().getChildren().add(selectOneRadio);
223         
224         selectOneRadio.getChildren().add(selectItems);
225         
226         HtmlRadio radio = new HtmlRadio();
227         radio.setIndex(0);
228         radio.setFor("selectOneRadio");
229         
230         facesContext.getViewRoot().getChildren().add(radio);
231         
232         MockResponseWriter writer = (MockResponseWriter)facesContext.getResponseWriter();
233         HtmlCheckAttributesUtil.addBaseAttributes(selectOneRadio, attrs);
234         radio.encodeBegin(facesContext);
235         radio.encodeChildren(facesContext);
236         radio.encodeEnd(facesContext);
237         facesContext.renderResponse();
238         HtmlCheckAttributesUtil.checkRenderedAttributes(attrs, writer.getWriter().toString());        
239         
240         if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
241             fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
242         }
243     }    
244 
245 }