2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.wicket.jquery;
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;
29 import flexjson.JSONSerializer;
31 import static junit.framework.TestCase.*;
33 public class AbstractJQueryBehaviorTest {
34 private WicketTester wicket;
38 wicket = new WicketTester();
42 public void tearDown() {
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"));
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);
69 @Test(expected = IllegalStateException.class)
70 public void testNotAllowedOnPagePageBehavior() {
71 Page component = new Page() {
73 AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") {
75 protected boolean isPageAllowed() {
79 behavior.bind(component);
83 public void testCreateReadyJavascriptForPage() {
84 Page component = new Page() {
86 AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") {
88 protected boolean isPageAllowed() {
92 behavior.bind(component);
93 String readyHandler = behavior.createReadyFunction();
94 assertEquals("jQuery(function(){myfunc(\"body\",null);});", readyHandler);
99 public void testCreateReadyJavaScriptWithCustomConfigJavascript() {
100 Component component = new Label("label");
101 component.setOutputMarkupId(false);
102 AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") {
104 protected String getConfigurationJavascript() {
108 behavior.bind(component);
109 String readyHandler = behavior.createReadyFunction();
110 assertEquals("jQuery(function(){myfunc(\"#" + component.getMarkupId() +
111 "\",bla);});", readyHandler);
114 public static final class X {
122 public void testCreateReadyJavaScriptWithConfig() {
123 Component component = new Label("label");
124 component.setOutputMarkupId(false);
125 AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") {
127 protected Object getConfigurationObject() {
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);
139 public void testCreateReadyJavaScriptWithConfigWithCustomSerializer() {
140 Component component = new Label("label");
141 component.setOutputMarkupId(false);
142 AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") {
144 protected Object getConfigurationObject() {
148 protected JSONSerializer getCustomSerializer() {
149 return new JSONSerializer().exclude("x");
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);