Addition of tooltip behavior.
[utils] / wicket / components / src / test / java / org / wamblee / wicket / jquery / AbstractJQueryBehaviorTest.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.wicket.jquery;
17
18 import org.apache.wicket.Component;
19 import org.apache.wicket.Page;
20 import org.apache.wicket.markup.html.basic.Label;
21 import org.apache.wicket.markup.html.form.Form;
22 import org.apache.wicket.util.tester.TagTester;
23 import org.apache.wicket.util.tester.WicketTester;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.wamblee.wicket.MyPage;
28
29 import flexjson.JSONSerializer;
30
31 import static junit.framework.TestCase.*;
32
33 public class AbstractJQueryBehaviorTest {
34     private WicketTester wicket;
35
36     @Before
37     public void setUp() {
38         wicket = new WicketTester();
39     }
40
41     @After
42     public void tearDown() {
43         wicket.destroy();
44     }
45
46     @Test
47     public void testVerifyBasicFunc() {
48         MyPage page = new MyPage();
49         page.getLabel().add(new AbstractJQueryBehavior("initfunc"));
50         wicket.startPage(page);
51         wicket.assertRenderedPage(MyPage.class);
52         TagTester tag = wicket.getTagByWicketId("label");
53         assertNotNull(tag.getAttribute("id"));
54         String doc = wicket.getServletResponse().getDocument();
55         assertTrue(doc.contains("jquery-noconflict"));
56         assertTrue(doc.contains("initfunc"));
57     }
58
59     @Test
60     public void testCreateReadyJavascript() {
61         Component component = new Label("label");
62         AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc");
63         behavior.bind(component);
64         String readyHandler = behavior.createReadyFunction();
65         assertEquals("jQuery(function(){myfunc(\"#" + component.getMarkupId() +
66             "\",null);});", readyHandler);
67     }
68     
69     @Test(expected = IllegalStateException.class)
70     public void testNotAllowedOnPagePageBehavior() {
71         Page component = new Page() { 
72         };
73         AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") { 
74           @Override
75         protected boolean isPageAllowed() {
76             return false;
77         }  
78         };
79         behavior.bind(component);
80     }
81     
82     @Test
83     public void testCreateReadyJavascriptForPage() {
84         Page component = new Page() { 
85         };
86         AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") { 
87             @Override
88             protected boolean isPageAllowed() {
89                 return true;
90             }
91         };
92         behavior.bind(component);
93         String readyHandler = behavior.createReadyFunction();
94         assertEquals("jQuery(function(){myfunc(\"body\",null);});", readyHandler);
95     }
96     
97
98     @Test
99     public void testCreateReadyJavaScriptWithCustomConfigJavascript() {
100         Component component = new Label("label");
101         component.setOutputMarkupId(false);
102         AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") {
103             @Override
104             protected String getConfigurationJavascript() {
105                 return "bla";
106             }
107         };
108         behavior.bind(component);
109         String readyHandler = behavior.createReadyFunction();
110         assertEquals("jQuery(function(){myfunc(\"#" + component.getMarkupId() +
111             "\",bla);});", readyHandler);
112     }
113
114     public static final class X {
115         private int x = 10;
116         public int getX() {
117             return x;
118         }
119     }
120
121     @Test
122     public void testCreateReadyJavaScriptWithConfig() {
123         Component component = new Label("label");
124         component.setOutputMarkupId(false);
125         AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") {
126             @Override
127             protected Object getConfigurationObject() {
128                return new X();
129             }
130         };
131         behavior.bind(component);
132         String readyHandler = behavior.createReadyFunction();
133         String config = new JSONSerializer().serialize(new X()); 
134         assertEquals("jQuery(function(){myfunc(\"#" + component.getMarkupId() +
135             "\"," + config + ");});", readyHandler);
136     }
137     
138     @Test
139     public void testCreateReadyJavaScriptWithConfigWithCustomSerializer() {
140         Component component = new Label("label");
141         component.setOutputMarkupId(false);
142         AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") {
143             @Override
144             protected Object getConfigurationObject() {
145                return new X();
146             }
147             @Override
148             protected JSONSerializer getCustomSerializer() {
149                 return new JSONSerializer().exclude("x");
150             }
151         };
152         behavior.bind(component);
153         String readyHandler = behavior.createReadyFunction();
154         String config = new JSONSerializer().exclude("x").serialize(new X());
155         assertEquals("jQuery(function(){myfunc(\"#" + component.getMarkupId() +
156             "\"," + config + ");});", readyHandler);
157     }
158     
159
160 }