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.shared.renderkit.html;
20  
21  import junit.framework.Assert;
22  import org.apache.myfaces.shared.renderkit.ClientBehaviorEvents;
23  import org.apache.myfaces.test.base.AbstractJsfTestCase;
24  
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.behavior.*;
27  import javax.faces.component.html.HtmlInputText;
28  
29  import java.util.*;
30  
31  public class HtmlRendererUtilsTest extends AbstractJsfTestCase
32  {
33  
34      public HtmlRendererUtilsTest(String name)
35      {
36          super(name);
37      }
38  
39      public void testBuildBehaviorChain()
40      {
41          Map<String, List<ClientBehavior>> behaviors = new HashMap<String, List<ClientBehavior>>();
42  
43          //Map<String, String> params = new HashMap<String, String>();
44          Collection<ClientBehaviorContext.Parameter> params = new ArrayList<ClientBehaviorContext.Parameter>();
45          
46          UIComponent component = new HtmlInputText();
47          Assert.assertEquals("", HtmlRendererUtils.buildBehaviorChain(facesContext, component, 
48                  component.getClientId(facesContext),
49                  ClientBehaviorEvents.CLICK, params, ClientBehaviorEvents.ACTION, params, behaviors, null,
50                  null));
51  
52          Assert.assertEquals("return jsf.util.chain(document.getElementById('j_id__v_0'), event,'huhn', 'suppe');",
53                  HtmlRendererUtils.buildBehaviorChain(facesContext,
54                          component, component.getClientId(facesContext), ClientBehaviorEvents.CLICK, 
55                          params, ClientBehaviorEvents.ACTION, params, behaviors, "huhn",
56                          "suppe"));
57  
58          ClientBehavior submittingBehavior = new ClientBehaviorBase()
59          {
60              @Override
61              public String getScript(ClientBehaviorContext behaviorContext)
62              {
63                  return "script()";
64              }
65  
66              @Override
67              public Set<ClientBehaviorHint> getHints()
68              {
69                  return EnumSet.allOf(ClientBehaviorHint.class);
70              }
71          };
72  
73          behaviors.put(ClientBehaviorEvents.CLICK, Arrays.asList(submittingBehavior));
74  
75          Assert.assertEquals("jsf.util.chain(document.getElementById('j_id__v_0'), event,'huhn', 'script()', 'suppe'); return false;",
76                  HtmlRendererUtils.buildBehaviorChain(facesContext,
77                          component, component.getClientId(facesContext),
78                          ClientBehaviorEvents.CLICK, params, ClientBehaviorEvents.ACTION, params, behaviors, "huhn",
79                          "suppe"));
80  
81      }
82      
83      public void testBuildBehaviorChain2()
84      {
85          Map<String, List<ClientBehavior>> behaviors = new HashMap<String, List<ClientBehavior>>();
86  
87          //Map<String, String> params = new HashMap<String, String>();
88          Collection<ClientBehaviorContext.Parameter> params = new ArrayList<ClientBehaviorContext.Parameter>();
89          
90          UIComponent component = new HtmlInputText();
91          Assert.assertEquals("", HtmlRendererUtils.buildBehaviorChain(facesContext, component, 
92                  ClientBehaviorEvents.CLICK, params, ClientBehaviorEvents.ACTION, params, behaviors, null,
93                  null));
94  
95          Assert.assertEquals("return jsf.util.chain(this, event,'huhn', 'suppe');",
96                  HtmlRendererUtils.buildBehaviorChain(facesContext,
97                          component, ClientBehaviorEvents.CLICK, params, ClientBehaviorEvents.ACTION, params, behaviors, "huhn",
98                          "suppe"));
99  
100         ClientBehavior submittingBehavior = new ClientBehaviorBase()
101         {
102             @Override
103             public String getScript(ClientBehaviorContext behaviorContext)
104             {
105                 return "script()";
106             }
107 
108             @Override
109             public Set<ClientBehaviorHint> getHints()
110             {
111                 return EnumSet.allOf(ClientBehaviorHint.class);
112             }
113         };
114 
115         behaviors.put(ClientBehaviorEvents.CLICK, Arrays.asList(submittingBehavior));
116 
117         Assert.assertEquals("jsf.util.chain(this, event,'huhn', 'script()', 'suppe'); return false;",
118                 HtmlRendererUtils.buildBehaviorChain(facesContext,
119                         component, 
120                         ClientBehaviorEvents.CLICK, params, ClientBehaviorEvents.ACTION, params, behaviors, "huhn",
121                         "suppe"));
122 
123     }    
124     
125     public void testEscapeJavaScriptForChain()
126     {
127         
128         Assert.assertEquals("var foo = &quot; \\\\&quot; test &quot;; alert(foo);", HtmlRendererUtils.escapeJavaScriptForChain("var foo = &quot; \\&quot; test &quot;; alert(foo);"));
129         
130         Assert.assertEquals("var foo = \\'bar \\'", HtmlRendererUtils.escapeJavaScriptForChain("var foo = 'bar '"));
131         
132         Assert.assertEquals("var foo = \\'bar \\\\\\' \\'", HtmlRendererUtils.escapeJavaScriptForChain("var foo = 'bar \\' '"));
133     }
134 }