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