[HtmlUnit] SVN: [15530] trunk/htmlunit/src

rbri--- via HtmlUnit-develop <[email protected]> Mon, 20 Aug 2018 06:41:50 +0000
Newsgroups gmane.comp.java.htmlunit.devel
Message-ID <[email protected]>
Revision: 15530
          http://sourceforge.net/p/htmlunit/code/15530
Author:   rbri
Date:     2018-08-20 06:39:52 +0000 (Mon, 20 Aug 2018)
Log Message:
-----------
next step in event refactoring (wip)

Modified Paths:
--------------
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/BeforeUnloadEvent.java
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventTarget.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/Node2Test.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeTest.java

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/BeforeUnloadEvent.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/BeforeUnloadEvent.java	2018-08-17 19:48:56 UTC (rev 15529)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/BeforeUnloadEvent.java	2018-08-20 06:39:52 UTC (rev 15530)
@@ -117,7 +117,7 @@
 
         if (!Undefined.isUndefined(returnValue) && (returnValue != null || browserVersion.isIE())) {
             if (!browserVersion.hasFeature(EVENT_BEFORE_UNLOAD_USES_HANDLER_RETURN_ONLY_IF_FIRST)
-                    || !getReturnValueDefault(browserVersion).equals(getReturnValue())) {
+                    || getReturnValueDefault(browserVersion).equals(getReturnValue())) {
                 setReturnValue(returnValue);
             }
         }

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventTarget.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventTarget.java	2018-08-17 19:48:56 UTC (rev 15529)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventTarget.java	2018-08-20 06:39:52 UTC (rev 15530)
@@ -112,6 +112,16 @@
      * @return the result
      */
     public ScriptResult fireEvent(final Event event) {
+        fireEventImpl(event);
+        // This is deprecated but there're still a few places using ScriptResult.getNewPage()
+        return new ScriptResult(null, getWindow().getWebWindow().getWebClient().getCurrentWindow().getEnclosedPage());
+    }
+
+    /**
+     * Fires the event on the node with capturing and bubbling phase.
+     * @param event the event
+     */
+    private void fireEventImpl(final Event event) {
         final Window window = getWindow();
         final Object[] args = new Object[] {event};
 
@@ -161,7 +171,7 @@
                     final ScriptResult r = elc.executeCapturingListeners(event, args);
                     result = ScriptResult.combine(r, result);
                     if (event.isPropagationStopped()) {
-                        return result;
+                        return;
                     }
                 }
             }
@@ -178,7 +188,7 @@
                     final ScriptResult r = elc.executeAtTargetListeners(event, args);
                     result = ScriptResult.combine(r, result);
                     if (event.isPropagationStopped()) {
-                        return result;
+                        return;
                     }
                 }
             }
@@ -207,7 +217,7 @@
                         final ScriptResult r = elc.executeBubblingListeners(event, args);
                         result = ScriptResult.combine(r, result);
                         if (event.isPropagationStopped()) {
-                            return result;
+                            return;
                         }
                     }
                 }
@@ -230,8 +240,6 @@
             event.endFire();
             window.setCurrentEvent(previousEvent); // reset event
         }
-
-        return result;
     }
 
     /**

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/Node2Test.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/Node2Test.java	2018-08-17 19:48:56 UTC (rev 15529)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/Node2Test.java	2018-08-20 06:39:52 UTC (rev 15530)
@@ -14,17 +14,10 @@
  */
 package com.gargoylesoftware.htmlunit.javascript.host.dom;
 
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-import org.apache.commons.lang3.ArrayUtils;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
 import com.gargoylesoftware.htmlunit.BrowserRunner;
-import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts;
 import com.gargoylesoftware.htmlunit.SimpleWebTestCase;
 import com.gargoylesoftware.htmlunit.html.HtmlPage;
 
@@ -53,122 +46,9 @@
             + "  var b = document.getElementById('b');\n"
             + "  a.replaceChild(b, b);\n"
             + "}\n"
-            + "</script></head><body onload='doTest()'><div id='a'><div id='b'/></div></html>";
+            + "</script></head>\n"
+            + "<body onload='doTest()'><div id='a'><div id='b'/></div></html>";
         final HtmlPage page = loadPageWithAlerts(html);
         assertNotNull(page.getHtmlElementById("b").getParentNode());
     }
-
-    /**
-     * @throws Exception if the test fails
-     */
-    @Test
-    @Alerts({"1", "2"})
-    public void eventListener() throws Exception {
-        final String html
-            = "<html><head>\n"
-            + "<script>\n"
-            + "  function clicking1() {\n"
-            + "    alert(1);\n"
-            + "  }\n"
-            + "  function clicking2() {\n"
-            + "    alert(2);\n"
-            + "  }\n"
-            + "  function test() {\n"
-            + "    var e = document.getElementById('myAnchor');\n"
-            + "     e.addEventListener('click', clicking1, false);\n"
-            + "     e.addEventListener('click', clicking2, false);\n"
-            + "  }\n"
-            + "</script></head><body onload='test()'>\n"
-            + "  <a href='" + URL_SECOND + "' id='myAnchor'>Click me</a>\n"
-            + "</body></html>";
-
-        final List<String> collectedAlerts = new ArrayList<>();
-        final HtmlPage page = loadPage(html, collectedAlerts);
-        final HtmlPage page2 = page.getHtmlElementById("myAnchor").click();
-        //IE doesn't have specific order
-        Collections.sort(collectedAlerts);
-        assertEquals(getExpectedAlerts(), collectedAlerts);
-        assertEquals(URL_SECOND.toExternalForm(), page2.getUrl().toExternalForm());
-    }
-
-    /**
-     * @throws Exception if the test fails
-     */
-    @Test
-    @Alerts({"1", "2"})
-    public void eventListener_return_false() throws Exception {
-        final String html
-            = "<html><head>\n"
-            + "<script>\n"
-            + "  function clicking1() {\n"
-            + "    alert(1);\n"
-            + "  }\n"
-            + "  function clicking2() {\n"
-            + "    alert(2);\n"
-            + "    return false;\n"
-            + "  }\n"
-            + "  function test() {\n"
-            + "    var e = document.getElementById('myAnchor');\n"
-            + "    e.addEventListener('click', clicking1, false);\n"
-            + "    e.addEventListener('click', clicking2, false);\n"
-            + "  }\n"
-            + "</script></head><body onload='test()'>\n"
-            + "  <a href='" + URL_SECOND + "' id='myAnchor'>Click me</a>\n"
-            + "</body></html>";
-
-        final List<String> collectedAlerts = new ArrayList<>();
-        final HtmlPage page = loadPage(html, collectedAlerts);
-        final HtmlPage page2 = page.getHtmlElementById("myAnchor").click();
-        //IE doesn't have specific order
-        Collections.sort(collectedAlerts);
-        assertEquals(getExpectedAlerts(), collectedAlerts);
-
-        final URL expectedURL;
-        if (getBrowserVersion().isIE()) {
-            expectedURL = URL_FIRST;
-        }
-        else {
-            expectedURL = URL_SECOND;
-        }
-        assertEquals(expectedURL.toExternalForm(), page2.getUrl().toExternalForm());
-    }
-
-    /**
-     * @throws Exception if the test fails
-     */
-    @Test
-    @Alerts({"1", "2", "§§URL§§second/"})
-    public void eventListener_returnValue_false() throws Exception {
-        final String html
-            = "<html><head>\n"
-            + "<script>\n"
-            + "  function clicking1() {\n"
-            + "    alert(1);\n"
-            + "  }\n"
-            + "  function clicking2() {\n"
-            + "    alert(2);\n"
-            + "    if (window.event)\n"
-            + "      window.event.returnValue = false;\n"
-            + "  }\n"
-            + "  function test() {\n"
-            + "    var e = document.getElementById('myAnchor');\n"
-            + "    e.addEventListener('click', clicking1, false);\n"
-            + "    e.addEventListener('click', clicking2, false);\n"
-            + "  }\n"
-            + "</script></head><body onload='test()'>\n"
-            + "  <a href='" + URL_SECOND + "' id='myAnchor'>Click me</a>\n"
-            + "</body></html>";
-
-        expandExpectedAlertsVariables(URL_FIRST);
-
-        final List<String> collectedAlerts = new ArrayList<>();
-        final HtmlPage page = loadPage(html, collectedAlerts);
-        final HtmlPage page2 = page.getHtmlElementById("myAnchor").click();
-        //IE doesn't have specific order
-        Collections.sort(collectedAlerts);
-        assertEquals(ArrayUtils.subarray(getExpectedAlerts(), 0, 2), collectedAlerts);
-
-        assertEquals(getExpectedAlerts()[2], page2.getUrl().toExternalForm());
-    }
-
 }

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeTest.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeTest.java	2018-08-17 19:48:56 UTC (rev 15529)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/dom/NodeTest.java	2018-08-20 06:39:52 UTC (rev 15530)
@@ -19,6 +19,7 @@
 import static com.gargoylesoftware.htmlunit.javascript.host.xml.XMLDocumentTest.callLoadXMLDocumentFromString;
 import static com.gargoylesoftware.htmlunit.javascript.host.xml.XMLDocumentTest.callSerializeXMLDocumentToString;
 
+import org.apache.commons.lang3.ArrayUtils;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.openqa.selenium.By;
@@ -1279,4 +1280,110 @@
 
         loadPageWithAlerts2(html);
     }
+
+    /**
+     * @throws Exception if the test fails
+     */
+    @Test
+    @Alerts({"1", "2", "§§URL§§second"})
+    public void eventListener() throws Exception {
+        final String html
+            = "<html><head>\n"
+            + "<script>\n"
+            + "  function clicking1() {\n"
+            + "    alert(1);\n"
+            + "  }\n"
+            + "  function clicking2() {\n"
+            + "    alert(2);\n"
+            + "  }\n"
+            + "  function test() {\n"
+            + "    var e = document.getElementById('myAnchor');\n"
+            + "     e.addEventListener('click', clicking1, false);\n"
+            + "     e.addEventListener('click', clicking2, false);\n"
+            + "  }\n"
+            + "</script></head><body onload='test()'>\n"
+            + "  <a href='second' id='myAnchor'>Click me</a>\n"
+            + "</body></html>";
+
+        getMockWebConnection().setDefaultResponse("<html><body>Test</body></html>");
+        expandExpectedAlertsVariables(URL_FIRST);
+
+        final WebDriver driver = loadPage2(html);
+        driver.findElement(By.id("myAnchor")).click();
+        verifyAlerts(driver, ArrayUtils.subarray(getExpectedAlerts(), 0, 2));
+        Thread.sleep(200); // FF60 WebDriver
+        assertEquals(getExpectedAlerts()[2], driver.getCurrentUrl());
+    }
+
+    /**
+     * @throws Exception if the test fails
+     */
+    @Test
+    @Alerts({"1", "2", "§§URL§§second"})
+    public void eventListener_return_false() throws Exception {
+        final String html
+            = "<html><head>\n"
+            + "<script>\n"
+            + "  function clicking1() {\n"
+            + "    alert(1);\n"
+            + "  }\n"
+            + "  function clicking2() {\n"
+            + "    alert(2);\n"
+            + "    return false;\n"
+            + "  }\n"
+            + "  function test() {\n"
+            + "    var e = document.getElementById('myAnchor');\n"
+            + "    e.addEventListener('click', clicking1, false);\n"
+            + "    e.addEventListener('click', clicking2, false);\n"
+            + "  }\n"
+            + "</script></head><body onload='test()'>\n"
+            + "  <a href='second' id='myAnchor'>Click me</a>\n"
+            + "</body></html>";
+
+        getMockWebConnection().setDefaultResponse("<html><body>Test</body></html>");
+        expandExpectedAlertsVariables(URL_FIRST);
+
+        final WebDriver driver = loadPage2(html);
+        driver.findElement(By.id("myAnchor")).click();
+        verifyAlerts(driver, ArrayUtils.subarray(getExpectedAlerts(), 0, 2));
+        Thread.sleep(200); // FF60 WebDriver
+        assertEquals(getExpectedAlerts()[2], driver.getCurrentUrl());
+    }
+
+    /**
+     * @throws Exception if the test fails
+     */
+    @Test
+    @Alerts(DEFAULT = {"1", "2", "§§URL§§second"},
+            CHROME = {"1", "2", "§§URL§§"})
+    public void eventListener_returnValue_false() throws Exception {
+        final String html
+            = "<html><head>\n"
+            + "<script>\n"
+            + "  function clicking1() {\n"
+            + "    alert(1);\n"
+            + "  }\n"
+            + "  function clicking2() {\n"
+            + "    alert(2);\n"
+            + "    if (window.event)\n"
+            + "      window.event.returnValue = false;\n"
+            + "  }\n"
+            + "  function test() {\n"
+            + "    var e = document.getElementById('myAnchor');\n"
+            + "    e.addEventListener('click', clicking1, false);\n"
+            + "    e.addEventListener('click', clicking2, false);\n"
+            + "  }\n"
+            + "</script></head><body onload='test()'>\n"
+            + "  <a href='second' id='myAnchor'>Click me</a>\n"
+            + "</body></html>";
+
+        getMockWebConnection().setDefaultResponse("<html><body>Test</body></html>");
+        expandExpectedAlertsVariables(URL_FIRST);
+
+        final WebDriver driver = loadPage2(html);
+        driver.findElement(By.id("myAnchor")).click();
+        verifyAlerts(driver, ArrayUtils.subarray(getExpectedAlerts(), 0, 2));
+        Thread.sleep(200); // FF60 WebDriver
+        assertEquals(getExpectedAlerts()[2], driver.getCurrentUrl());
+    }
 }


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
HtmlUnit-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/htmlunit-develop