001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.util.tester.apps_5;
018:
019: import org.apache.wicket.Page;
020: import org.apache.wicket.WicketTestCase;
021: import org.apache.wicket.ajax.AjaxRequestTarget;
022: import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
023: import org.apache.wicket.ajax.markup.html.AjaxLink;
024: import org.apache.wicket.util.tester.ITestPageSource;
025:
026: /**
027: * Test that the clickLink method also works with AjaxLinks
028: *
029: * @author Frank Bille
030: */
031: public class AjaxLinkClickTest extends WicketTestCase {
032: private boolean linkClicked;
033: private AjaxRequestTarget ajaxRequestTarget;
034:
035: /**
036: * Construct.
037: */
038: public AjaxLinkClickTest() {
039: super ("AjaxLink click test");
040: }
041:
042: /**
043: * Make sure that our test flags are reset between every test.
044: * @see org.apache.wicket.WicketTestCase#setUp()
045: */
046: protected void setUp() throws Exception {
047: super .setUp();
048:
049: linkClicked = false;
050: ajaxRequestTarget = null;
051: }
052:
053: /**
054: * Test that an AjaxLink's onClick method is actually invoked.
055: */
056: public void testBasicAjaxLinkClick() {
057: // Create a link, which we test is actually invoked
058: final AjaxLink ajaxLink = new AjaxLink("ajaxLink") {
059: private static final long serialVersionUID = 1L;
060:
061: public void onClick(AjaxRequestTarget target) {
062: linkClicked = true;
063: ajaxRequestTarget = target;
064: }
065: };
066:
067: tester.startPage(new ITestPageSource() {
068: /**
069: *
070: */
071: private static final long serialVersionUID = 1L;
072:
073: public Page getTestPage() {
074: Page page = new MockPageWithLink();
075: page.add(ajaxLink);
076:
077: return page;
078: }
079: });
080:
081: tester.clickLink("ajaxLink");
082:
083: assertTrue(linkClicked);
084: assertNotNull(ajaxRequestTarget);
085: }
086:
087: /**
088: * Test that clickLink also works with AjaxFallbackLinks
089: *
090: * AjaxFallbackLinks should be clicked and interpreted as an AjaxLink, which
091: * means that AjaxRequestTarget is not null.
092: */
093: public void testAjaxFallbackLinkClick() {
094: final Page page = new MockPageWithLink();
095:
096: // Create a link, which we test is actually invoked
097: page.add(new AjaxFallbackLink("ajaxLink") {
098: private static final long serialVersionUID = 1L;
099:
100: public void onClick(AjaxRequestTarget target) {
101: linkClicked = true;
102: ajaxRequestTarget = target;
103: }
104: });
105:
106: tester.startPage(new ITestPageSource() {
107: /**
108: *
109: */
110: private static final long serialVersionUID = 1L;
111:
112: public Page getTestPage() {
113: return page;
114: }
115: });
116:
117: tester.clickLink("ajaxLink");
118:
119: assertTrue(linkClicked);
120: assertNotNull(ajaxRequestTarget);
121: }
122:
123: /**
124: * Test that when AJAX is disabled, the AjaxFallbackLink is invoked with
125: * null as AjaxRequestTarget.
126: */
127: public void testFallbackLinkWithAjaxDisabled() {
128: final Page page = new MockPageWithLink();
129:
130: // Create a link, which we test is actually invoked
131: page.add(new AjaxFallbackLink("ajaxLink") {
132: private static final long serialVersionUID = 1L;
133:
134: public void onClick(AjaxRequestTarget target) {
135: linkClicked = true;
136: ajaxRequestTarget = target;
137: }
138: });
139:
140: tester.startPage(new ITestPageSource() {
141: /**
142: *
143: */
144: private static final long serialVersionUID = 1L;
145:
146: public Page getTestPage() {
147: return page;
148: }
149: });
150:
151: // Click the link with ajax disabled
152: tester.clickLink("ajaxLink", false);
153:
154: assertTrue(linkClicked);
155: assertNull(ajaxRequestTarget);
156: }
157: }
|