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  
20  package org.apache.myfaces.tobago.internal.renderkit.renderer;
21  
22  import org.apache.myfaces.tobago.component.LabelLayout;
23  import org.apache.myfaces.tobago.component.RendererTypes;
24  import org.apache.myfaces.tobago.component.Tags;
25  import org.apache.myfaces.tobago.component.UIButton;
26  import org.apache.myfaces.tobago.component.UIIn;
27  import org.apache.myfaces.tobago.component.UILink;
28  import org.apache.myfaces.tobago.component.UIOut;
29  import org.apache.myfaces.tobago.component.UISegmentLayout;
30  import org.apache.myfaces.tobago.component.UISelectItem;
31  import org.apache.myfaces.tobago.component.UISelectOneChoice;
32  import org.apache.myfaces.tobago.layout.SegmentMeasureList;
33  import org.apache.myfaces.tobago.util.ComponentUtils;
34  import org.junit.Assert;
35  import org.junit.jupiter.api.Test;
36  
37  import java.io.IOException;
38  
39  public class InRendererUnitTest extends RendererTestBase {
40  
41    @Test
42    public void inputGroupButtonAfter() throws IOException {
43      final UIIn c = (UIIn) ComponentUtils.createComponent(
44          facesContext, Tags.in.componentType(), RendererTypes.In, "id");
45  
46      final UIButton b = (UIButton) ComponentUtils.createComponent(
47          facesContext, Tags.button.componentType(), RendererTypes.Button, "button");
48      b.setLabel("button");
49      c.getFacets().put("after", b);
50  
51      c.encodeAll(facesContext);
52  
53      Assert.assertEquals(loadHtml("renderer/in/input-group-button-after.html"), formattedResult());
54    }
55  
56    @Test
57    public void inputGroupButtonBefore() throws IOException {
58      final UIIn c = (UIIn) ComponentUtils.createComponent(
59          facesContext, Tags.in.componentType(), RendererTypes.In, "id");
60  
61      final UIButton b = (UIButton) ComponentUtils.createComponent(
62          facesContext, Tags.button.componentType(), RendererTypes.Button, "button");
63      b.setLabel("button");
64      c.getFacets().put("before", b);
65  
66      c.encodeAll(facesContext);
67  
68      Assert.assertEquals(loadHtml("renderer/in/input-group-button-before.html"), formattedResult());
69    }
70  
71    @Test
72    public void inputGroupChoiceAfter() throws IOException {
73      final UIIn c = (UIIn) ComponentUtils.createComponent(
74          facesContext, Tags.in.componentType(), RendererTypes.In, "id");
75  
76      final UISelectOneChoice choice = (UISelectOneChoice) ComponentUtils.createComponent(
77          facesContext, Tags.selectOneChoice.componentType(), RendererTypes.SelectOneChoice, "choice");
78      c.getFacets().put("after", choice);
79  
80      final UISelectItem i1 = (UISelectItem) ComponentUtils.createComponent(
81          facesContext, Tags.selectItem.componentType(), null, "i1");
82      i1.setItemLabel("Stratocaster");
83      choice.getChildren().add(i1);
84      final UISelectItem i2 = (UISelectItem) ComponentUtils.createComponent(
85          facesContext, Tags.selectItem.componentType(), null, "i2");
86      i2.setItemLabel("Telecaster");
87      choice.getChildren().add(i2);
88  
89      c.encodeAll(facesContext);
90  
91      Assert.assertEquals(loadHtml("renderer/in/input-group-choice-after.html"), formattedResult());
92    }
93  
94    @Test
95    public void inputGroupDropdownAfter() throws IOException {
96      final UIIn c = (UIIn) ComponentUtils.createComponent(
97          facesContext, Tags.in.componentType(), RendererTypes.In, "id");
98  
99      final UIButton d = (UIButton) ComponentUtils.createComponent(
100         facesContext, Tags.button.componentType(), RendererTypes.Button, "button");
101     d.setLabel("dropdown");
102     d.setOmit(true);
103     c.getFacets().put("after", d);
104 
105     final UILink l1 = (UILink) ComponentUtils.createComponent(
106         facesContext, Tags.link.componentType(), RendererTypes.Link, "l1");
107     l1.setLabel("Link 1");
108     d.getChildren().add(l1);
109 
110     final UILink l2 = (UILink) ComponentUtils.createComponent(
111         facesContext, Tags.link.componentType(), RendererTypes.Link, "l2");
112     l2.setLabel("Link 2");
113     d.getChildren().add(l2);
114 
115     c.encodeAll(facesContext);
116 
117     Assert.assertEquals(loadHtml("renderer/in/input-group-dropdown-after.html"), formattedResult());
118   }
119 
120   @Test
121   public void inputGroupDropdownBefore() throws IOException {
122     final UIIn c = (UIIn) ComponentUtils.createComponent(
123         facesContext, Tags.in.componentType(), RendererTypes.In, "id");
124 
125     final UIButton d = (UIButton) ComponentUtils.createComponent(
126         facesContext, Tags.button.componentType(), RendererTypes.Button, "button");
127     d.setLabel("dropdown");
128     d.setOmit(true);
129     c.getFacets().put("before", d);
130 
131     final UILink l1 = (UILink) ComponentUtils.createComponent(
132         facesContext, Tags.link.componentType(), RendererTypes.Link, "l1");
133     l1.setLabel("Link 1");
134     d.getChildren().add(l1);
135 
136     final UILink l2 = (UILink) ComponentUtils.createComponent(
137         facesContext, Tags.link.componentType(), RendererTypes.Link, "l2");
138     l2.setLabel("Link 2");
139     d.getChildren().add(l2);
140 
141     c.encodeAll(facesContext);
142 
143     Assert.assertEquals(loadHtml("renderer/in/input-group-dropdown-before.html"), formattedResult());
144   }
145 
146   @Test
147   public void inputGroupOutAfter() throws IOException {
148     final UIIn c = (UIIn) ComponentUtils.createComponent(
149         facesContext, Tags.in.componentType(), RendererTypes.In, "id");
150 
151     final UIOut o = (UIOut) ComponentUtils.createComponent(
152         facesContext, Tags.out.componentType(), RendererTypes.Out, "out");
153     o.setValue("out");
154     c.getFacets().put("after", o);
155 
156     c.encodeAll(facesContext);
157 
158     Assert.assertEquals(loadHtml("renderer/in/input-group-out-after.html"), formattedResult());
159   }
160 
161   @Test
162   public void inputGroupOutBefore() throws IOException {
163     final UIIn c = (UIIn) ComponentUtils.createComponent(
164         facesContext, Tags.in.componentType(), RendererTypes.In, "id");
165 
166     final UIOut o = (UIOut) ComponentUtils.createComponent(
167         facesContext, Tags.out.componentType(), RendererTypes.Out, "out");
168     o.setValue("out");
169     c.getFacets().put("before", o);
170 
171     c.encodeAll(facesContext);
172 
173     Assert.assertEquals(loadHtml("renderer/in/input-group-out-before.html"), formattedResult());
174   }
175 
176   @Test
177   public void simple() throws IOException {
178     final UIIn c = (UIIn) ComponentUtils.createComponent(
179         facesContext, Tags.in.componentType(), RendererTypes.In, "id");
180     c.encodeAll(facesContext);
181 
182     Assert.assertEquals(loadHtml("renderer/in/simple.html"), formattedResult());
183   }
184 
185   @Test
186   public void labelLayoutFlexLeft() throws IOException {
187     final UIIn c = (UIIn) ComponentUtils.createComponent(
188         facesContext, Tags.in.componentType(), RendererTypes.In, "id");
189     c.setLabel("label");
190     c.setLabelLayout(LabelLayout.flexLeft); // same as default
191     c.encodeAll(facesContext);
192 
193     Assert.assertEquals(loadHtml("renderer/in/label-flexLeft.html"), formattedResult());
194   }
195 
196   @Test
197   public void labelLayoutFlexRight() throws IOException {
198     final UIIn c = (UIIn) ComponentUtils.createComponent(
199         facesContext, Tags.in.componentType(), RendererTypes.In, "id");
200     c.setLabel("label");
201     c.setLabelLayout(LabelLayout.flexRight);
202     c.encodeAll(facesContext);
203 
204     Assert.assertEquals(loadHtml("renderer/in/label-flexRight.html"), formattedResult());
205   }
206 
207   @Test
208   public void labelLayoutFlowLeft() throws IOException {
209     final UIIn c = (UIIn) ComponentUtils.createComponent(
210         facesContext, Tags.in.componentType(), RendererTypes.In, "id");
211     c.setLabel("label");
212     c.setLabelLayout(LabelLayout.flowLeft);
213     c.encodeAll(facesContext);
214 
215     Assert.assertEquals(loadHtml("renderer/in/label-flowLeft.html"), formattedResult());
216   }
217 
218   @Test
219   public void labelLayoutFlowRight() throws IOException {
220     final UIIn c = (UIIn) ComponentUtils.createComponent(
221         facesContext, Tags.in.componentType(), RendererTypes.In, "id");
222     c.setLabel("label");
223     c.setLabelLayout(LabelLayout.flowRight);
224     c.encodeAll(facesContext);
225 
226     Assert.assertEquals(loadHtml("renderer/in/label-flowRight.html"), formattedResult());
227   }
228 
229   @Test
230   public void labelLayoutTop() throws IOException {
231     final UIIn c = (UIIn) ComponentUtils.createComponent(
232         facesContext, Tags.in.componentType(), RendererTypes.In, "id");
233     c.setLabel("label");
234     c.setLabelLayout(LabelLayout.top);
235     c.encodeAll(facesContext);
236 
237     Assert.assertEquals(loadHtml("renderer/in/label-top.html"), formattedResult());
238   }
239 
240   @Test
241   public void labelLayoutGridLeft() throws IOException {
242     final UIIn c = (UIIn) ComponentUtils.createComponent(
243         facesContext, Tags.in.componentType(), RendererTypes.In, "id");
244     c.setLabel("label");
245     c.setLabelLayout(LabelLayout.gridLeft);
246     c.encodeAll(facesContext);
247 
248     Assert.assertEquals(loadHtml("renderer/in/label-gridLeft.html"), formattedResult());
249   }
250 
251   @Test
252   public void labelLayoutGridRight() throws IOException {
253     final UIIn c = (UIIn) ComponentUtils.createComponent(
254         facesContext, Tags.in.componentType(), RendererTypes.In, "id");
255     c.setLabel("label");
256     c.setLabelLayout(LabelLayout.gridRight);
257     c.encodeAll(facesContext);
258 
259     Assert.assertEquals(loadHtml("renderer/in/label-gridRight.html"), formattedResult());
260   }
261 
262   @Test
263   public void labelLayoutGridTop() throws IOException {
264     final UIIn c = (UIIn) ComponentUtils.createComponent(
265         facesContext, Tags.in.componentType(), RendererTypes.In, "id");
266     c.setLabel("label");
267     c.setLabelLayout(LabelLayout.gridTop);
268     c.encodeAll(facesContext);
269 
270     Assert.assertEquals(loadHtml("renderer/in/label-gridTop.html"), formattedResult());
271   }
272 
273   @Test
274   public void labelLayoutGridBottom() throws IOException {
275     final UIIn c = (UIIn) ComponentUtils.createComponent(
276         facesContext, Tags.in.componentType(), RendererTypes.In, "id");
277     c.setLabel("label");
278     c.setLabelLayout(LabelLayout.gridBottom);
279     c.encodeAll(facesContext);
280 
281     Assert.assertEquals(loadHtml("renderer/in/label-gridBottom.html"), formattedResult());
282   }
283 
284   @Test
285   public void labelLayoutSegmentLeft() throws IOException {
286 
287     final UISegmentLayout l = (UISegmentLayout) ComponentUtils.createComponent(
288         facesContext, Tags.segmentLayout.componentType(), RendererTypes.SegmentLayout, "segid");
289     l.setMedium(SegmentMeasureList.parse("3seg 9seg"));
290 
291     final UIIn c = (UIIn) ComponentUtils.createComponent(
292         facesContext, Tags.in.componentType(), RendererTypes.In, "id");
293     c.setLabel("label");
294     c.setLabelLayout(LabelLayout.segmentLeft);
295 
296     l.getChildren().add(c);
297     l.encodeAll(facesContext);
298 
299     Assert.assertEquals(loadHtml("renderer/in/label-segmentLeft.html"), formattedResult());
300   }
301 
302   @Test
303   public void labelLayoutSegmentRight() throws IOException {
304 
305     final UISegmentLayout l = (UISegmentLayout) ComponentUtils.createComponent(
306         facesContext, Tags.segmentLayout.componentType(), RendererTypes.SegmentLayout, "segid");
307     l.setMedium(SegmentMeasureList.parse("9seg 3seg"));
308 
309     final UIIn c = (UIIn) ComponentUtils.createComponent(
310         facesContext, Tags.in.componentType(), RendererTypes.In, "id");
311     c.setLabel("label");
312     c.setLabelLayout(LabelLayout.segmentRight);
313 
314     l.getChildren().add(c);
315     l.encodeAll(facesContext);
316 
317     Assert.assertEquals(loadHtml("renderer/in/label-segmentRight.html"), formattedResult());
318   }
319 
320   @Test
321   public void labelSkip() throws IOException {
322     final UIIn c = (UIIn) ComponentUtils.createComponent(
323         facesContext, Tags.in.componentType(), RendererTypes.In, "id");
324     c.setLabel("label");
325     c.setLabelLayout(LabelLayout.skip);
326     c.encodeAll(facesContext);
327 
328     Assert.assertEquals(loadHtml("renderer/in/label-skip.html"), formattedResult());
329   }
330 
331   @Test
332   public void labelNone() throws IOException {
333     final UIIn c = (UIIn) ComponentUtils.createComponent(
334         facesContext, Tags.in.componentType(), RendererTypes.In, "id");
335     c.setLabel("label");
336     c.setLabelLayout(LabelLayout.none);
337     c.encodeAll(facesContext);
338 
339     Assert.assertEquals(loadHtml("renderer/in/label-none.html"), formattedResult());
340   }
341 
342 }