visibilitychange:hidden doesn't fire during page navigations
https://bugs.webkit.org/show_bug.cgi?id=151234
<rdar://problem/23688763>

Reviewed by Ryosuke Niwa.

LayoutTests/imported/w3c:

Import page-visibility WPT tests from upstream.

* resources/import-expectations.json:
* web-platform-tests/html/browsers/the-window-object/apis-for-creating-and-navigating-browsing-contexts-by-name/no_window_open_when_term_nesting_level_nonzero.window-expected.txt:
* web-platform-tests/page-visibility/*: Added.

Source/WebCore:

Fire a visibilitychange during document unload, as per the specification:
- https://www.w3.org/TR/page-visibility/#reacting-to-visibilitychange-changes
- https://html.spec.whatwg.org/multipage/browsing-the-web.html#unloading-document-visibility-change-steps

Note that the specification currently says to fire the visibilitychange event before the pagehide event.
However, Both Chrome and Firefox fire the pagehide event then the visibilitychange event. This change
aligns our behavior with both Chrome and Firefox. The following bug has been filed against the
specification:
- https://github.com/w3c/page-visibility/issues/67

We also fire a visibilitychange event when coming out of the back/forward cache. This makes sense given
that we fire one when the document enters the back/forward cache. This is also Firefox's behavior.
I have verified that the new fast/history/back-forward-cache-visibility-state.html layout test is passing
in Firefox.

Tests: fast/history/back-forward-cache-visibility-state.html
       imported/w3c/web-platform-tests/page-visibility/idlharness.window.html
       imported/w3c/web-platform-tests/page-visibility/iframe-unload.html
       imported/w3c/web-platform-tests/page-visibility/onvisibilitychange.html
       imported/w3c/web-platform-tests/page-visibility/test_attributes_exist.html
       imported/w3c/web-platform-tests/page-visibility/test_child_document.html
       imported/w3c/web-platform-tests/page-visibility/test_default_view.html
       imported/w3c/web-platform-tests/page-visibility/test_read_only.html
       imported/w3c/web-platform-tests/page-visibility/unload-bubbles.html
       imported/w3c/web-platform-tests/page-visibility/unload.html

* dom/Document.cpp:
(WebCore::Document::visibilityState const):
(WebCore::Document::setHiddenDueToDismissal):
* dom/Document.h:
* history/CachedPage.cpp:
(WebCore::firePageShowAndPopStateEvents):
* loader/FrameLoader.cpp:
(WebCore::FrameLoader::dispatchUnloadEvents):

LayoutTests:

Add test coverage for the visibilitychange event and document.visibilitystate when entering
and coming out of the back/forward cache.

* fast/history/back-forward-cache-visibility-state-expected.txt: Added.
* fast/history/back-forward-cache-visibility-state.html: Added.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@267614 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/loader/FrameLoader.cpp b/Source/WebCore/loader/FrameLoader.cpp
index 5d847e9..58e83fb 100644
--- a/Source/WebCore/loader/FrameLoader.cpp
+++ b/Source/WebCore/loader/FrameLoader.cpp
@@ -3280,8 +3280,8 @@
                 m_frame.document()->domWindow()->dispatchEvent(PageTransitionEvent::create(eventNames().pagehideEvent, m_frame.document()->backForwardCacheState() == Document::AboutToEnterBackForwardCache), m_frame.document());
             }
 
-            // FIXME: update Page Visibility state here.
-            // https://bugs.webkit.org/show_bug.cgi?id=116770
+            // This takes care of firing the visibilitychange event and making sure the document is reported as hidden.
+            m_frame.document()->setVisibilityHiddenDueToDismissal(true);
 
             if (m_frame.document()->backForwardCacheState() == Document::NotInBackForwardCache) {
                 Ref<Event> unloadEvent(Event::create(eventNames().unloadEvent, Event::CanBubble::No, Event::IsCancelable::No));