media/W3C/video/networkState/networkState_during_progress.html is flaky
https://bugs.webkit.org/show_bug.cgi?id=76280

Reviewed by Eric Carlson.

The onprogress event must be received when networkState is
NETWORK_LOADING, make sure in the transition from loading to idle
that the progress event is fired synchronously, so that it is
received before the networkState changes to NETWORK_IDLE.

Source/WebCore:

Tested by media/W3C/video/networkState/networkState_during_progress.html

* html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::changeNetworkStateFromLoadingToIdle):

LayoutTests:

* TestExpectations:
* platform/gtk/TestExpectations:
* platform/mac/TestExpectations:
* platform/win/TestExpectations:


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@251460 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/ChangeLog b/LayoutTests/ChangeLog
index 5e3cb62..afda365 100644
--- a/LayoutTests/ChangeLog
+++ b/LayoutTests/ChangeLog
@@ -1,3 +1,20 @@
+2019-10-22  Charlie Turner  <cturner@igalia.com>
+
+        media/W3C/video/networkState/networkState_during_progress.html is flaky
+        https://bugs.webkit.org/show_bug.cgi?id=76280
+
+        Reviewed by Eric Carlson.
+
+        The onprogress event must be received when networkState is
+        NETWORK_LOADING, make sure in the transition from loading to idle
+        that the progress event is fired synchronously, so that it is
+        received before the networkState changes to NETWORK_IDLE.
+
+        * TestExpectations:
+        * platform/gtk/TestExpectations:
+        * platform/mac/TestExpectations:
+        * platform/win/TestExpectations:
+
 2019-10-22  Russell Epstein  <repstein@apple.com>
 
         [ iOS ] Three editing/pasteboard/smart-paste-paragraph tests have been flaky since they landed in r243124 (203264)
diff --git a/LayoutTests/TestExpectations b/LayoutTests/TestExpectations
index 47173b7..6b1ef73 100644
--- a/LayoutTests/TestExpectations
+++ b/LayoutTests/TestExpectations
@@ -831,9 +831,6 @@
 webkit.org/b/139862 editing/spelling/editing-multiple-words-with-markers.html [ Timeout Pass ]
 webkit.org/b/139903 editing/spelling/grammar-paste.html [ Timeout Pass ]
 
-# media/W3C/video/networkState/networkState_during_progress.html is flaky
-webkit.org/b/76280 media/W3C/video/networkState/networkState_during_progress.html [ Pass Failure ]
-
 # This test will run slowly in debug mode, but is plenty fast in release.
 [ Debug ] js/slow-stress/emscripten-memops.html [ Skip ]
 
diff --git a/LayoutTests/platform/gtk/TestExpectations b/LayoutTests/platform/gtk/TestExpectations
index 56bb004..97c9600 100644
--- a/LayoutTests/platform/gtk/TestExpectations
+++ b/LayoutTests/platform/gtk/TestExpectations
@@ -1566,8 +1566,6 @@
 webkit.org/b/133000 js/slow-stress/call-spread.html [ Pass Timeout ]
 webkit.org/b/133001 fast/workers/worker-context-gc.html [ Pass Timeout ]
 
-webkit.org/b/133865 media/W3C/video/networkState/networkState_during_progress.html [ Failure Pass ]
-
 webkit.org/b/133869 media/video-seek-after-end.html [ Failure Pass ]
 
 webkit.org/b/36642 fast/replaced/border-radius-clip.html [ Failure Pass ]
diff --git a/LayoutTests/platform/mac/TestExpectations b/LayoutTests/platform/mac/TestExpectations
index 85c869f..07384d1 100644
--- a/LayoutTests/platform/mac/TestExpectations
+++ b/LayoutTests/platform/mac/TestExpectations
@@ -781,7 +781,6 @@
 ## --- Start flaky media tests ---
 webkit.org/b/34331 http/tests/media/video-referer.html [ Pass Timeout ]
 webkit.org/b/35297 media/video-display-aspect-ratio.html [ Pass Failure ]
-webkit.org/b/82976 media/W3C/video/networkState/networkState_during_progress.html [ Pass Failure ]
 webkit.org/b/85525 media/video-played-reset.html [ Pass Failure ]
 webkit.org/b/112659 media/video-playing-and-pause.html [ Failure Timeout ]
 webkit.org/b/114744 media/video-layer-crash.html [ Pass Timeout Failure ]
diff --git a/LayoutTests/platform/win/TestExpectations b/LayoutTests/platform/win/TestExpectations
index 1928e39..50a1357 100644
--- a/LayoutTests/platform/win/TestExpectations
+++ b/LayoutTests/platform/win/TestExpectations
@@ -897,8 +897,6 @@
 media/encrypted-media/encrypted-media-not-loaded.html [ Skip ] # [ WontFix ]
 media/encrypted-media/encrypted-media-syntax.html [ Skip ] # [ WontFix ]
 
-webkit.org/b/103442 media/W3C/video/networkState/networkState_during_progress.html [ Skip ]
-
 # No CORS support for media elements is implemented yet.
 webkit.org/b/176626 http/tests/security/canvas-remote-read-remote-video-allowed-anonymous.html [ Crash Failure ]
 webkit.org/b/176653 http/tests/security/canvas-remote-read-remote-video-allowed-with-credentials.html [ Crash Failure ]
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index 00b3b16..ad0ef9d 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,20 @@
+2019-10-22  Charlie Turner  <cturner@igalia.com>
+
+        media/W3C/video/networkState/networkState_during_progress.html is flaky
+        https://bugs.webkit.org/show_bug.cgi?id=76280
+
+        Reviewed by Eric Carlson.
+
+        The onprogress event must be received when networkState is
+        NETWORK_LOADING, make sure in the transition from loading to idle
+        that the progress event is fired synchronously, so that it is
+        received before the networkState changes to NETWORK_IDLE.
+
+        Tested by media/W3C/video/networkState/networkState_during_progress.html
+
+        * html/HTMLMediaElement.cpp:
+        (WebCore::HTMLMediaElement::changeNetworkStateFromLoadingToIdle):
+
 2019-10-22  Peng Liu  <peng.liu6@apple.com>
 
         [Picture-in-Picture Web API] The implementation needs runtime logging
diff --git a/Source/WebCore/html/HTMLMediaElement.cpp b/Source/WebCore/html/HTMLMediaElement.cpp
index 4d189a2..4e1fc83 100644
--- a/Source/WebCore/html/HTMLMediaElement.cpp
+++ b/Source/WebCore/html/HTMLMediaElement.cpp
@@ -2370,11 +2370,16 @@
     if (hasMediaControls() && m_player->didLoadingProgress())
         mediaControls()->bufferingProgressed();
 
-    // Schedule one last progress event so we guarantee that at least one is fired
-    // for files that load very quickly.
-    scheduleEvent(eventNames().progressEvent);
-    scheduleEvent(eventNames().suspendEvent);
+    // Schedule one last *synchronous* progress event so we guarantee
+    // that at least one is fired for files that load very
+    // quickly. This must be synchronous since it's very easy for the
+    // async queue to dispatch this to JS after we set m_networkState
+    // to IDLE below, which breaks the invariant of onprogress
+    // happening during NETWORK_LOADING.
+    dispatchEvent(Event::create(eventNames().progressEvent, Event::CanBubble::No, Event::IsCancelable::Yes));
+
     m_networkState = NETWORK_IDLE;
+    scheduleEvent(eventNames().suspendEvent);
 }
 
 void HTMLMediaElement::mediaPlayerReadyStateChanged(MediaPlayer*)