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.markup.html.basic.Label;
20 import org.apache.wicket.markup.html.form.Form;
21 import org.apache.wicket.util.tester.TagTester;
22 import org.apache.wicket.util.tester.WicketTester;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.wamblee.wicket.MyPage;
28 import flexjson.JSONSerializer;
30 import static junit.framework.TestCase.*;
32 public class AbstractJQueryBehaviorTest {
33 private WicketTester wicket;
37 wicket = new WicketTester();
41 public void tearDown() {
46 public void testVerifyBasicFunc() {
47 MyPage page = new MyPage();
48 page.getLabel().add(new AbstractJQueryBehavior("initfunc"));
49 wicket.startPage(page);
50 wicket.assertRenderedPage(MyPage.class);
51 TagTester tag = wicket.getTagByWicketId("label");
52 assertNotNull(tag.getAttribute("id"));
53 String doc = wicket.getServletResponse().getDocument();
54 assertTrue(doc.contains("jquery-noconflict"));
55 assertTrue(doc.contains("initfunc"));
59 public void testCreateReadyJavascript() {
60 Component component = new Label("label");
61 component.setOutputMarkupId(true);
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);
70 public void testCreateReadyJavaScriptWithCustomConfigJavascript() {
71 Component component = new Label("label");
72 component.setOutputMarkupId(false);
73 AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") {
75 protected String getConfigurationJavascript() {
79 behavior.bind(component);
80 String readyHandler = behavior.createReadyFunction();
81 assertEquals("jQuery(function(){myfunc(\"#" + component.getMarkupId() +
82 "\",bla);});", readyHandler);
85 public static final class X {
93 public void testCreateReadyJavaScriptWithConfig() {
94 Component component = new Label("label");
95 component.setOutputMarkupId(false);
96 AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") {
98 protected Object getConfigurationObject() {
102 behavior.bind(component);
103 String readyHandler = behavior.createReadyFunction();
104 String config = new JSONSerializer().serialize(new X());
105 assertEquals("jQuery(function(){myfunc(\"#" + component.getMarkupId() +
106 "\"," + config + ");});", readyHandler);
110 public void testCreateReadyJavaScriptWithConfigWithCustomSerializer() {
111 Component component = new Label("label");
112 component.setOutputMarkupId(false);
113 AbstractJQueryBehavior behavior = new AbstractJQueryBehavior("myfunc") {
115 protected Object getConfigurationObject() {
119 protected JSONSerializer getCustomSerializer() {
120 return new JSONSerializer().exclude("x");
123 behavior.bind(component);
124 String readyHandler = behavior.createReadyFunction();
125 String config = new JSONSerializer().exclude("x").serialize(new X());
126 assertEquals("jQuery(function(){myfunc(\"#" + component.getMarkupId() +
127 "\"," + config + ");});", readyHandler);