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

rbri--- via HtmlUnit-develop <[email protected]> Thu, 30 Aug 2018 08:13:36 +0000
Newsgroups gmane.comp.java.htmlunit.devel
Message-ID <[email protected]>
Revision: 15556
          http://sourceforge.net/p/htmlunit/code/15556
Author:   rbri
Date:     2018-08-30 08:13:33 +0000 (Thu, 30 Aug 2018)
Log Message:
-----------
event refactoring - fix the remaining test case

Modified Paths:
--------------
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/DomNode.java
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlElement.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/selenium/TypingTest.java

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java	2018-08-28 17:44:31 UTC (rev 15555)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java	2018-08-30 08:13:33 UTC (rev 15556)
@@ -415,6 +415,10 @@
     @BrowserFeature({CHROME, FF})
     HTMLELEMENT_ALIGN_INVALID,
 
+    /** Detaching the active element from the dom tree triggers no keyup event. */
+    @BrowserFeature(IE)
+    HTMLELEMENT_DETACH_ACTIVE_TRIGGERS_NO_KEYUP_EVENT,
+
     /** Removing the active element from the dom tree triggers the onblur event. */
     @BrowserFeature(CHROME)
     HTMLELEMENT_REMOVE_ACTIVE_TRIGGERS_BLUR_EVENT,

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/DomNode.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/DomNode.java	2018-08-28 17:44:31 UTC (rev 15555)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/DomNode.java	2018-08-30 08:13:33 UTC (rev 15556)
@@ -1185,6 +1185,10 @@
         nextSibling_ = null;
         previousSibling_ = null;
         parent_ = null;
+        attachedToPage_ = false;
+        for (DomNode descendant : getDescendants()) {
+            descendant.attachedToPage_ = false;
+        }
     }
 
     private void fireRemoval(final DomNode exParent) {

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlElement.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlElement.java	2018-08-28 17:44:31 UTC (rev 15555)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlElement.java	2018-08-30 08:13:33 UTC (rev 15556)
@@ -14,6 +14,7 @@
  */
 package com.gargoylesoftware.htmlunit.html;
 
+import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.HTMLELEMENT_DETACH_ACTIVE_TRIGGERS_NO_KEYUP_EVENT;
 import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.HTMLELEMENT_REMOVE_ACTIVE_TRIGGERS_BLUR_EVENT;
 import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.KEYBOARD_EVENT_SPECIAL_KEYPRESS;
 
@@ -537,7 +538,7 @@
         }
 
         final Event keyDown = new KeyboardEvent(this, Event.TYPE_KEY_DOWN, c,
-                shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_);
+                                                shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_);
         final ScriptResult keyDownResult = fireEvent(keyDown);
 
         if (!keyDown.isAborted(keyDownResult)) {
@@ -555,15 +556,32 @@
         if (this instanceof HtmlTextInput
                 || this instanceof HtmlTextArea
                 || this instanceof HtmlPasswordInput) {
-            fireKeyboardEvent(Event.TYPE_INPUT, c, shiftPressed_ || isShiftNeeded);
+            fireEvent(new KeyboardEvent(this, Event.TYPE_INPUT, c,
+                                        shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_));
         }
 
-        fireKeyboardEvent(Event.TYPE_KEY_UP, c, shiftPressed_ || isShiftNeeded);
+        HtmlElement eventSource = this;
+        if (!isAttachedToPage()) {
+            final BrowserVersion browserVersion = page.getWebClient().getBrowserVersion();
+            if (browserVersion.hasFeature(HTMLELEMENT_DETACH_ACTIVE_TRIGGERS_NO_KEYUP_EVENT)) {
+                eventSource = null;
+            }
+            else {
+                eventSource = page.getBody();
+            }
+        }
 
-        if (isShiftNeeded) {
-            final Event shiftUp = new KeyboardEvent(this, Event.TYPE_KEY_UP, KeyboardEvent.DOM_VK_SHIFT,
-                    false, ctrlPressed_, altPressed_);
-            fireEvent(shiftUp);
+        if (eventSource != null) {
+            final Event keyUp = new KeyboardEvent(this, Event.TYPE_KEY_UP, c,
+                                                    shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_);
+            eventSource.fireEvent(keyUp);
+
+            if (isShiftNeeded) {
+                final Event shiftUp = new KeyboardEvent(this, Event.TYPE_KEY_UP,
+                                        KeyboardEvent.DOM_VK_SHIFT,
+                                        false, ctrlPressed_, altPressed_);
+                eventSource.fireEvent(shiftUp);
+            }
         }
 
         final HtmlForm form = getEnclosingForm();
@@ -578,10 +596,6 @@
         return webClient.getCurrentWindow().getEnclosedPage();
     }
 
-    private void fireKeyboardEvent(final String eventType, final char c, final boolean shift) {
-        fireEvent(new KeyboardEvent(this, eventType, c, shift, ctrlPressed_, altPressed_));
-    }
-
     /**
      * Simulates typing the specified key code while this element has focus, returning the page contained
      * by this element's window after typing. Note that it may or may not be the same as the original page,

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/selenium/TypingTest.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/selenium/TypingTest.java	2018-08-28 17:44:31 UTC (rev 15555)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/selenium/TypingTest.java	2018-08-30 08:13:33 UTC (rev 15556)
@@ -28,6 +28,7 @@
 
 import com.gargoylesoftware.htmlunit.BrowserRunner;
 import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts;
+import com.gargoylesoftware.htmlunit.BrowserRunner.BuggyWebDriver;
 
 /**
  * Modified from
@@ -343,11 +344,10 @@
      */
     @Test
     @Alerts(DEFAULT = {"keydown (target) keyup (target) keyup (body)",
-                "keydown (target) keyup (target) keyup (body) keydown (target) a pressed; removing"},
-            CHROME = {"keydown (target) keyup (target) keyup (body)",
-                "keydown (target) keyup (target) keyup (body) keydown (target) a pressed; removing keyup (body)"},
-            FF60 = {"keydown (target) keyup (target) keyup (body)",
-                "keydown (target) keyup (target) keyup (body) keydown (target) a pressed; removing keyup (body)"})
+                "keydown (target) a pressed; removing keyup (body)"},
+            IE = {"keydown (target) keyup (target) keyup (body)",
+                "keydown (target) a pressed; removing"})
+    @BuggyWebDriver
     public void canSafelyTypeOnElementThatIsRemovedFromTheDomOnKeyPress() {
         final WebDriver driver = getWebDriver("/key_tests/remove_on_keypress.html");
 
@@ -358,12 +358,10 @@
 
         input.sendKeys("b");
         assertEquals(getExpectedAlerts()[0], getValueText(log).replace('\n', ' '));
+        log.clear();
 
         input.sendKeys("a");
 
-        // Some drivers (IE, Firefox) do not always generate the final keyup event since the element
-        // is removed from the DOM in response to the keypress (note, this is a product of how events
-        // are generated and does not match actual user behavior).
         assertEquals(getExpectedAlerts()[1], getValueText(log).replace('\n', ' '));
     }
 


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot