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.view.facelets.tag.composite;
20  
21  import javax.faces.component.UICommand;
22  import javax.faces.component.UIComponent;
23  import javax.faces.component.UINamingContainer;
24  import javax.faces.component.UIViewRoot;
25  
26  import org.apache.myfaces.view.facelets.FaceletTestCase;
27  import org.apache.myfaces.view.facelets.bean.HelloWorld;
28  import org.junit.Assert;
29  import org.junit.Test;
30  
31  public class CompositeComponentClientBehaviorTestCase extends FaceletTestCase
32  {
33      @Test
34      public void testSimpleClientBehavior() throws Exception
35      {
36          HelloWorld helloWorld = new HelloWorld(); 
37          
38          facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
39                  helloWorld);
40          
41          UIViewRoot root = facesContext.getViewRoot();
42          vdl.buildView(facesContext, root, "testSimpleClientBehavior.xhtml");
43          
44          UIComponent form = root.findComponent("testForm1");
45          Assert.assertNotNull(form);
46          UINamingContainer compositeComponent = (UINamingContainer) form.getChildren().get(0);
47          Assert.assertNotNull(compositeComponent);
48          UICommand button = (UICommand) compositeComponent.findComponent("button");
49          Assert.assertNotNull(button);
50          Assert.assertNotNull(button.getClientBehaviors().get("action"));
51          Assert.assertEquals(1, button.getClientBehaviors().get("action").size());
52          
53          //StringWriter sw = new StringWriter();
54          //MockResponseWriter mrw = new MockResponseWriter(sw);
55          //facesContext.setResponseWriter(mrw);
56          
57          //root.encodeAll(facesContext);
58          //sw.flush();
59          //System.out.print(sw.toString());
60      }
61      
62      @Test
63      public void testSimpleClientBehaviorDefault() throws Exception
64      {
65          HelloWorld helloWorld = new HelloWorld(); 
66          
67          facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
68                  helloWorld);
69          
70          UIViewRoot root = facesContext.getViewRoot();
71          vdl.buildView(facesContext, root, "testSimpleClientBehaviorDefault.xhtml");
72          
73          UIComponent form = root.findComponent("testForm1");
74          Assert.assertNotNull(form);
75          UINamingContainer compositeComponent = (UINamingContainer) form.getChildren().get(0);
76          Assert.assertNotNull(compositeComponent);
77          UICommand button = (UICommand) compositeComponent.findComponent("button");
78          Assert.assertNotNull(button);
79          Assert.assertNotNull(button.getClientBehaviors().get("dblclick"));
80          Assert.assertEquals(1, button.getClientBehaviors().get("dblclick").size());
81          
82          //StringWriter sw = new StringWriter();
83          //MockResponseWriter mrw = new MockResponseWriter(sw);
84          //facesContext.setResponseWriter(mrw);
85          
86          //root.encodeAll(facesContext);
87          //sw.flush();
88          //System.out.print(sw.toString());
89      }
90  
91      @Test
92      public void testSimpleClientBehaviorDefaultNoEvent() throws Exception
93      {
94          HelloWorld helloWorld = new HelloWorld(); 
95          
96          facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
97                  helloWorld);
98          
99          UIViewRoot root = facesContext.getViewRoot();
100         vdl.buildView(facesContext, root, "testSimpleClientBehaviorDefaultNoEvent.xhtml");
101         
102         UIComponent form = root.findComponent("testForm1");
103         Assert.assertNotNull(form);
104         UINamingContainer compositeComponent = (UINamingContainer) form.getChildren().get(0);
105         Assert.assertNotNull(compositeComponent);
106         UICommand button = (UICommand) compositeComponent.findComponent("button");
107         Assert.assertNotNull(button);
108         Assert.assertNotNull(button.getClientBehaviors().get("action"));
109         Assert.assertEquals(1, button.getClientBehaviors().get("action").size());
110         
111         //StringWriter sw = new StringWriter();
112         //MockResponseWriter mrw = new MockResponseWriter(sw);
113         //facesContext.setResponseWriter(mrw);
114         
115         //root.encodeAll(facesContext);
116         //sw.flush();
117         //System.out.print(sw.toString());
118     }
119 
120     @Test
121     public void testSimpleClientBehaviorAjaxWrap() throws Exception
122     {
123         HelloWorld helloWorld = new HelloWorld(); 
124         
125         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
126                 helloWorld);
127         
128         UIViewRoot root = facesContext.getViewRoot();
129         vdl.buildView(facesContext, root, "testSimpleClientBehaviorAjaxWrap.xhtml");
130         
131         UIComponent form = root.findComponent("testForm1");
132         Assert.assertNotNull(form);
133         UINamingContainer compositeComponent = (UINamingContainer) form.getChildren().get(0);
134         Assert.assertNotNull(compositeComponent);
135         UICommand button = (UICommand) compositeComponent.findComponent("button");
136         Assert.assertNotNull(button);
137         Assert.assertNotNull(button.getClientBehaviors().get("action"));
138         Assert.assertEquals(1, button.getClientBehaviors().get("action").size());
139         
140         //StringWriter sw = new StringWriter();
141         //MockResponseWriter mrw = new MockResponseWriter(sw);
142         //facesContext.setResponseWriter(mrw);
143         
144         //root.encodeAll(facesContext);
145         //sw.flush();
146         //System.out.print(sw.toString());
147     }
148 
149     @Test
150     public void testSimpleClientBehaviorDefaultAjaxWrap() throws Exception
151     {
152         HelloWorld helloWorld = new HelloWorld(); 
153         
154         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
155                 helloWorld);
156         
157         UIViewRoot root = facesContext.getViewRoot();
158         vdl.buildView(facesContext, root, "testSimpleClientBehaviorDefaultAjaxWrap.xhtml");
159         
160         UIComponent form = root.findComponent("testForm1");
161         Assert.assertNotNull(form);
162         UINamingContainer compositeComponent = (UINamingContainer) form.getChildren().get(0);
163         Assert.assertNotNull(compositeComponent);
164         UICommand button = (UICommand) compositeComponent.findComponent("button");
165         Assert.assertNotNull(button);
166         Assert.assertNotNull(button.getClientBehaviors().get("dblclick"));
167         Assert.assertEquals(1, button.getClientBehaviors().get("dblclick").size());
168         
169         //StringWriter sw = new StringWriter();
170         //MockResponseWriter mrw = new MockResponseWriter(sw);
171         //facesContext.setResponseWriter(mrw);
172         
173         //root.encodeAll(facesContext);
174         //sw.flush();
175         //System.out.print(sw.toString());
176     }
177 
178     @Test
179     public void testSimpleClientBehaviorDefaultNoEventAjaxWrap() throws Exception
180     {
181         HelloWorld helloWorld = new HelloWorld(); 
182         
183         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
184                 helloWorld);
185         
186         UIViewRoot root = facesContext.getViewRoot();
187         vdl.buildView(facesContext, root, "testSimpleClientBehaviorDefaultNoEventAjaxWrap.xhtml");
188         
189         UIComponent form = root.findComponent("testForm1");
190         Assert.assertNotNull(form);
191         UINamingContainer compositeComponent = (UINamingContainer) form.getChildren().get(0);
192         Assert.assertNotNull(compositeComponent);
193         UICommand button = (UICommand) compositeComponent.findComponent("button");
194         Assert.assertNotNull(button);
195         Assert.assertNotNull(button.getClientBehaviors().get("action"));
196         Assert.assertEquals(1, button.getClientBehaviors().get("action").size());
197         
198         //StringWriter sw = new StringWriter();
199         //MockResponseWriter mrw = new MockResponseWriter(sw);
200         //facesContext.setResponseWriter(mrw);
201         
202         //root.encodeAll(facesContext);
203         //sw.flush();
204         //System.out.print(sw.toString());
205     }
206     
207     @Test
208     public void testCompositeClientBehavior() throws Exception
209     {
210         HelloWorld helloWorld = new HelloWorld(); 
211         
212         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
213                 helloWorld);
214         
215         UIViewRoot root = facesContext.getViewRoot();
216         vdl.buildView(facesContext, root, "testCompositeClientBehavior.xhtml");
217         
218         UIComponent form = root.findComponent("testForm1");
219         Assert.assertNotNull(form);
220         UINamingContainer compositeComponent = (UINamingContainer) form.getChildren().get(0);
221         Assert.assertNotNull(compositeComponent);
222         UINamingContainer compositeComponent2 = (UINamingContainer) compositeComponent.findComponent("button3");
223         Assert.assertNotNull(compositeComponent2);
224         UICommand button = (UICommand) compositeComponent2.findComponent("button");
225         Assert.assertNotNull(button);
226         //One added in testCompositeActionSource, the other one
227         //inside compositeActionSource.xhtml
228         Assert.assertNotNull(button.getClientBehaviors().get("action"));
229         Assert.assertEquals(1, button.getClientBehaviors().get("action").size());
230         
231         //StringWriter sw = new StringWriter();
232         //MockResponseWriter mrw = new MockResponseWriter(sw);
233         //facesContext.setResponseWriter(mrw);
234         
235         //root.encodeAll(facesContext);
236         //sw.flush();
237         //System.out.print(sw.toString());
238     }
239 
240     @Test
241     public void testCompositeDoubleClientBehavior() throws Exception
242     {
243         HelloWorld helloWorld = new HelloWorld(); 
244         
245         facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
246                 helloWorld);
247         
248         UIViewRoot root = facesContext.getViewRoot();
249         vdl.buildView(facesContext, root, "testCompositeDoubleClientBehavior.xhtml");
250         
251         UIComponent form = root.findComponent("testForm1");
252         Assert.assertNotNull(form);
253         UINamingContainer compositeComponent = (UINamingContainer) form.getChildren().get(0);
254         Assert.assertNotNull(compositeComponent);
255         UINamingContainer compositeComponent2 = (UINamingContainer) compositeComponent.findComponent("compositeClientBehavior");
256         Assert.assertNotNull(compositeComponent2);
257         UINamingContainer compositeComponent3 = (UINamingContainer) compositeComponent2.findComponent("button3");
258         Assert.assertNotNull(compositeComponent3);
259         UICommand button = (UICommand) compositeComponent3.findComponent("button");
260         Assert.assertNotNull(button);
261         //One added in testCompositeActionSource, the other one
262         //inside compositeActionSource.xhtml
263         Assert.assertNotNull(button.getClientBehaviors().get("action"));
264         Assert.assertEquals(1, button.getClientBehaviors().get("action").size());
265         
266         //StringWriter sw = new StringWriter();
267         //MockResponseWriter mrw = new MockResponseWriter(sw);
268         //facesContext.setResponseWriter(mrw);
269         
270         //root.encodeAll(facesContext);
271         //sw.flush();
272         //System.out.print(sw.toString());
273     }
274 }