From 405af0385fcad59f09aaf28d25d73b805326592c Mon Sep 17 00:00:00 2001 From: tfennelly Date: Fri, 21 Aug 2015 13:41:21 +0100 Subject: [PATCH] Reverting the WebClientResponseLoadListenable stuff No need for it as it turns out. We can get the "current" page (i.e the form submit result) from the webclient. --- .../htmlunit/html/HtmlFormUtil.java | 53 ++++------------ .../org/jvnet/hudson/test/HudsonTestCase.java | 29 +-------- .../org/jvnet/hudson/test/JenkinsRule.java | 28 +-------- .../test/WebClientResponseLoadListenable.java | 61 ------------------- 4 files changed, 13 insertions(+), 158 deletions(-) delete mode 100644 test/src/main/java/org/jvnet/hudson/test/WebClientResponseLoadListenable.java diff --git a/test/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlFormUtil.java b/test/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlFormUtil.java index 8e971f3b9d..b92d3ac1c6 100644 --- a/test/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlFormUtil.java +++ b/test/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlFormUtil.java @@ -26,12 +26,7 @@ package com.gargoylesoftware.htmlunit.html; import com.gargoylesoftware.htmlunit.ElementNotFoundException; import com.gargoylesoftware.htmlunit.Page; import com.gargoylesoftware.htmlunit.WebClient; -import com.gargoylesoftware.htmlunit.WebResponse; -import com.gargoylesoftware.htmlunit.WebWindow; -import org.junit.Assert; -import org.jvnet.hudson.test.WebClientResponseLoadListenable; -import javax.annotation.Nonnull; import java.io.IOException; import java.util.List; @@ -53,34 +48,22 @@ public class HtmlFormUtil { final HtmlPage htmlPage = (HtmlPage) htmlForm.getPage(); final WebClient webClient = htmlPage.getWebClient(); - Page resultPage = null; try { - resultPage = htmlForm.submit((SubmittableElement) submitElement); + htmlForm.submit((SubmittableElement) submitElement); } finally { // The HtmlForm submit doesn't really do anything. It just adds a "LoadJob" // to an internal queue. What we are doing here is manually forcing the load of - // the response for that submit LoadJob and listening in on the WebClient - // instance for the response, allowing us to create the correct HtmlPage - // object for return to the tests. - if (webClient instanceof WebClientResponseLoadListenable) { - FormSubmitResponseLoadListener loadListener = new FormSubmitResponseLoadListener(); - - ((WebClientResponseLoadListenable) webClient).addResponseLoadListener(loadListener); - try { - webClient.loadDownloadedResponses(); - resultPage = loadListener.getPage(); - } finally { - ((WebClientResponseLoadListenable) webClient).removeResponseLoadListener(loadListener); - } - - if (resultPage == htmlPage) { - // We're still on the same page (form submit didn't bring us anywhere). - // Hackery. Seems like YUI is messing us about. - return submitElement.click(); - } - } else { - Assert.fail("WebClient doesn't implement WebClientResponseLoadListenable."); + // the response for that submit LoadJob and then getting the enclosing page + // from the current window on the WebClient, allowing us to return the correct + // HtmlPage object to the test. + webClient.loadDownloadedResponses(); + Page resultPage = webClient.getCurrentWindow().getEnclosedPage(); + + if (resultPage == htmlPage) { + // We're still on the same page (form submit didn't bring us anywhere). + // Hackery. Seems like YUI is messing us about. + return submitElement.click(); } return resultPage; @@ -125,18 +108,4 @@ public class HtmlFormUtil { } throw new ElementNotFoundException("button", "caption", caption); } - - private static class FormSubmitResponseLoadListener implements WebClientResponseLoadListenable.WebClientResponseLoadListener { - private WebWindow webWindow; - @Override - public void onLoad(@Nonnull WebResponse webResponse, @Nonnull WebWindow webWindow) { - this.webWindow = webWindow; - } - private Page getPage() { - if (webWindow == null) { - Assert.fail("Expected FormSubmitResponseLoadListener to be called on form submit."); - } - return webWindow.getEnclosedPage(); - } - } } diff --git a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java index ec2a7daa20..1aafd57ad6 100644 --- a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java +++ b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java @@ -1625,11 +1625,9 @@ public abstract class HudsonTestCase extends TestCase implements RootAction { * Extends {@link com.gargoylesoftware.htmlunit.WebClient} and provide convenience methods * for accessing Hudson. */ - public class WebClient extends com.gargoylesoftware.htmlunit.WebClient implements WebClientResponseLoadListenable { + public class WebClient extends com.gargoylesoftware.htmlunit.WebClient { private static final long serialVersionUID = 5808915989048338267L; - private List loadListeners = new CopyOnWriteArrayList<>(); - public WebClient() { // default is IE6, but this causes 'n.doScroll('left')' to fail in event-debug.js:1907 as HtmlUnit doesn't implement such a method, // so trying something else, until we discover another problem. @@ -1691,31 +1689,6 @@ public abstract class HudsonTestCase extends TestCase implements RootAction { //setTimeout(60*1000); } - @Override - public void addResponseLoadListener(@Nonnull WebClientResponseLoadListener listener) { - removeResponseLoadListener(listener); - loadListeners.add(listener); - } - - @Override - public void removeResponseLoadListener(@Nonnull WebClientResponseLoadListener listener) { - loadListeners.remove(listener); - } - - @Override - public Page loadWebResponseInto(WebResponse webResponse, WebWindow webWindow) throws IOException, FailingHttpStatusCodeException { - try { - return super.loadWebResponseInto(webResponse, webWindow); - } finally { - if (!loadListeners.isEmpty()) { - for (WebClientResponseLoadListener listener : loadListeners) { - listener.onLoad(webResponse, webWindow); - } - } - } - } - - /** * Logs in to Jenkins. */ diff --git a/test/src/main/java/org/jvnet/hudson/test/JenkinsRule.java b/test/src/main/java/org/jvnet/hudson/test/JenkinsRule.java index a83c32e4c5..1f3623b72a 100644 --- a/test/src/main/java/org/jvnet/hudson/test/JenkinsRule.java +++ b/test/src/main/java/org/jvnet/hudson/test/JenkinsRule.java @@ -1830,11 +1830,9 @@ public class JenkinsRule implements TestRule, MethodRule, RootAction { * Extends {@link com.gargoylesoftware.htmlunit.WebClient} and provide convenience methods * for accessing Hudson. */ - public class WebClient extends com.gargoylesoftware.htmlunit.WebClient implements WebClientResponseLoadListenable { + public class WebClient extends com.gargoylesoftware.htmlunit.WebClient { private static final long serialVersionUID = 5808915989048338267L; - private List loadListeners = new CopyOnWriteArrayList<>(); - public WebClient() { // default is IE6, but this causes 'n.doScroll('left')' to fail in event-debug.js:1907 as HtmlUnit doesn't implement such a method, // so trying something else, until we discover another problem. @@ -1894,30 +1892,6 @@ public class JenkinsRule implements TestRule, MethodRule, RootAction { //setTimeout(60*1000); } - @Override - public void addResponseLoadListener(@Nonnull WebClientResponseLoadListener listener) { - removeResponseLoadListener(listener); - loadListeners.add(listener); - } - - @Override - public void removeResponseLoadListener(@Nonnull WebClientResponseLoadListener listener) { - loadListeners.remove(listener); - } - - @Override - public Page loadWebResponseInto(WebResponse webResponse, WebWindow webWindow) throws IOException, FailingHttpStatusCodeException { - try { - return super.loadWebResponseInto(webResponse, webWindow); - } finally { - if (!loadListeners.isEmpty()) { - for (WebClientResponseLoadListener listener : loadListeners) { - listener.onLoad(webResponse, webWindow); - } - } - } - } - /** * Logs in to Jenkins. */ diff --git a/test/src/main/java/org/jvnet/hudson/test/WebClientResponseLoadListenable.java b/test/src/main/java/org/jvnet/hudson/test/WebClientResponseLoadListenable.java deleted file mode 100644 index f903123487..0000000000 --- a/test/src/main/java/org/jvnet/hudson/test/WebClientResponseLoadListenable.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * The MIT License - * - * Copyright (c) 2015, CloudBees, Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.jvnet.hudson.test; - -import com.gargoylesoftware.htmlunit.WebResponse; -import com.gargoylesoftware.htmlunit.WebWindow; - -import javax.annotation.Nonnull; - -/** - * Hook into the HtmlUnit {@link org.jvnet.hudson.test.JenkinsRule.WebClient} - * response loading into a "window". - * @author tom.fennelly@gmail.com - */ -public interface WebClientResponseLoadListenable { - - /** - * Add a listener. - * @param listener The listener. - */ - void addResponseLoadListener(@Nonnull WebClientResponseLoadListener listener); - - /** - * Removing a listener. - * @param listener The listener. - */ - void removeResponseLoadListener(@Nonnull WebClientResponseLoadListener listener); - - /** - * Load listener interface. - */ - public interface WebClientResponseLoadListener { - /** - * Response load event. - * @param webResponse The response. - * @param webWindow The window into which the response is being loaded. - */ - void onLoad(@Nonnull WebResponse webResponse, @Nonnull WebWindow webWindow); - } -} -- GitLab