1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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 = " \\\\" test "; alert(foo);", HtmlRendererUtils.escapeJavaScriptForChain("var foo = " \\" test "; 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 }