Title: [287030] trunk
Revision
287030
Author
[email protected]
Date
2021-12-14 10:20:24 -0800 (Tue, 14 Dec 2021)

Log Message

Allow override of system's preferred color scheme
https://bugs.webkit.org/show_bug.cgi?id=234199
<rdar://problem/86366044>

Reviewed by Tim Horton.

Source/WebCore:

Use a user-specified preference for color-scheme before reading
the system value.

* css/MediaQueryEvaluator.cpp:
(WebCore::prefersColorSchemeEvaluate): If the document loader was given
a preference, use that when evaluating the media query.
* loader/DocumentLoader.cpp: New property - colorSchemePreference.
(WebCore::DocumentLoader::colorSchemePreference const):
* loader/DocumentLoader.h:
(WebCore::DocumentLoader::setColorSchemePreference):
* dom/Document.cpp:
(WebCore::Document::useDarkAppearance const): Check the DocumentLoader here too.

Source/WebKit:

Expose new Private API that allows the user to give an explicit per-page preference
for light or dark color schemes rather than reading the system value.

* Scripts/webkit/messages.py:
(headers_for_type): Add header for DocumentLoader.

* Shared/WebsitePoliciesData.cpp: Encode the new value in the policies sent
to the Web Process.
(WebKit::WebsitePoliciesData::encode const):
(WebKit::WebsitePoliciesData::decode):
(WebKit::WebsitePoliciesData::applyToDocumentLoader):
* Shared/WebsitePoliciesData.h:

* UIProcess/API/APIWebsitePolicies.cpp: Expose a new policy "colorSchemePreference".
(API::WebsitePolicies::copy const):
(API::WebsitePolicies::data):
* UIProcess/API/APIWebsitePolicies.h:

* UIProcess/API/Cocoa/WKWebpagePreferences.mm: Expose a new private API "_colorSchemePreference".
(-[WKWebpagePreferences _colorSchemePreference]):
(-[WKWebpagePreferences _setColorSchemePreference:]):
* UIProcess/API/Cocoa/WKWebpagePreferencesPrivate.h:

Tools:

New API test that checks the color-scheme after
explicitly setting it.

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WebKit/color-scheme.html: Added. Note: Xcode was
being difficult and wouldn't let me put this file in the right order
for the Copy Build Phase :(
* TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm:

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (287029 => 287030)


--- trunk/Source/WebCore/ChangeLog	2021-12-14 18:03:05 UTC (rev 287029)
+++ trunk/Source/WebCore/ChangeLog	2021-12-14 18:20:24 UTC (rev 287030)
@@ -1,3 +1,24 @@
+2021-12-11  Dean Jackson  <[email protected]>
+
+        Allow override of system's preferred color scheme
+        https://bugs.webkit.org/show_bug.cgi?id=234199
+        <rdar://problem/86366044>
+
+        Reviewed by Tim Horton.
+
+        Use a user-specified preference for color-scheme before reading
+        the system value.
+
+        * css/MediaQueryEvaluator.cpp:
+        (WebCore::prefersColorSchemeEvaluate): If the document loader was given
+        a preference, use that when evaluating the media query.
+        * loader/DocumentLoader.cpp: New property - colorSchemePreference.
+        (WebCore::DocumentLoader::colorSchemePreference const):
+        * loader/DocumentLoader.h:
+        (WebCore::DocumentLoader::setColorSchemePreference):
+        * dom/Document.cpp:
+        (WebCore::Document::useDarkAppearance const): Check the DocumentLoader here too.
+
 2021-12-14  Alan Bujtas  <[email protected]>
 
         [LFC][IFC] Make the LineBox content (text runs and inline level boxes) relative to the root inline box.

Modified: trunk/Source/WebCore/css/MediaQueryEvaluator.cpp (287029 => 287030)


--- trunk/Source/WebCore/css/MediaQueryEvaluator.cpp	2021-12-14 18:03:05 UTC (rev 287029)
+++ trunk/Source/WebCore/css/MediaQueryEvaluator.cpp	2021-12-14 18:20:24 UTC (rev 287030)
@@ -35,6 +35,7 @@
 #include "CSSValueKeywords.h"
 #include "Chrome.h"
 #include "ChromeClient.h"
+#include "DocumentLoader.h"
 #include "Frame.h"
 #include "FrameView.h"
 #include "Logging.h"
@@ -807,8 +808,16 @@
         return false;
 
     auto keyword = downcast<CSSPrimitiveValue>(*value).valueID();
-    bool useDarkAppearance = frame.page()->useDarkAppearance();
+    bool useDarkAppearance = [&] () -> auto {
+        if (frame.document()->loader()) {
+            auto colorSchemePreference = frame.document()->loader()->colorSchemePreference();
+            if (colorSchemePreference != ColorSchemePreference::NoPreference)
+                return colorSchemePreference == ColorSchemePreference::Dark;
+        }
 
+        return frame.page()->useDarkAppearance();
+    }();
+
     switch (keyword) {
     case CSSValueDark:
         return useDarkAppearance;

Modified: trunk/Source/WebCore/dom/Document.cpp (287029 => 287030)


--- trunk/Source/WebCore/dom/Document.cpp	2021-12-14 18:03:05 UTC (rev 287029)
+++ trunk/Source/WebCore/dom/Document.cpp	2021-12-14 18:20:24 UTC (rev 287030)
@@ -7594,6 +7594,12 @@
     UNUSED_PARAM(style);
 #endif
 
+    if (DocumentLoader* documentLoader = loader()) {
+        auto colorSchemePreference = documentLoader->colorSchemePreference();
+        if (colorSchemePreference != ColorSchemePreference::NoPreference)
+            return colorSchemePreference == ColorSchemePreference::Dark;
+    }
+
     bool pageUsesDarkAppearance = false;
     if (Page* documentPage = page())
         pageUsesDarkAppearance = documentPage->useDarkAppearance();

Modified: trunk/Source/WebCore/loader/DocumentLoader.cpp (287029 => 287030)


--- trunk/Source/WebCore/loader/DocumentLoader.cpp	2021-12-14 18:03:05 UTC (rev 287029)
+++ trunk/Source/WebCore/loader/DocumentLoader.cpp	2021-12-14 18:20:24 UTC (rev 287030)
@@ -1411,6 +1411,11 @@
     return m_mouseEventPolicy;
 }
 
+ColorSchemePreference DocumentLoader::colorSchemePreference() const
+{
+    return m_colorSchemePreference;
+}
+
 void DocumentLoader::attachToFrame(Frame& frame)
 {
     if (m_frame == &frame)

Modified: trunk/Source/WebCore/loader/DocumentLoader.h (287029 => 287030)


--- trunk/Source/WebCore/loader/DocumentLoader.h	2021-12-14 18:03:05 UTC (rev 287029)
+++ trunk/Source/WebCore/loader/DocumentLoader.h	2021-12-14 18:20:24 UTC (rev 287030)
@@ -152,6 +152,12 @@
     Disallow,
 };
 
+enum class ColorSchemePreference : uint8_t {
+    NoPreference,
+    Light,
+    Dark
+};
+
 DECLARE_ALLOCATOR_WITH_HEAP_IDENTIFIER(DocumentLoader);
 class DocumentLoader
     : public RefCounted<DocumentLoader>
@@ -354,6 +360,9 @@
     ModalContainerObservationPolicy modalContainerObservationPolicy() const { return m_modalContainerObservationPolicy; }
     void setModalContainerObservationPolicy(ModalContainerObservationPolicy policy) { m_modalContainerObservationPolicy = policy; }
 
+    WEBCORE_EXPORT ColorSchemePreference colorSchemePreference() const;
+    void setColorSchemePreference(ColorSchemePreference preference) { m_colorSchemePreference = preference; }
+
     void addSubresourceLoader(ResourceLoader&);
     void removeSubresourceLoader(LoadCompletionType, ResourceLoader*);
     void addPlugInStreamLoader(ResourceLoader&);
@@ -679,6 +688,7 @@
     LegacyOverflowScrollingTouchPolicy m_legacyOverflowScrollingTouchPolicy { LegacyOverflowScrollingTouchPolicy::Default };
     MouseEventPolicy m_mouseEventPolicy { MouseEventPolicy::Default };
     ModalContainerObservationPolicy m_modalContainerObservationPolicy { ModalContainerObservationPolicy::Disabled };
+    ColorSchemePreference m_colorSchemePreference { ColorSchemePreference::NoPreference };
 
 #if ENABLE(SERVICE_WORKER)
     std::optional<ServiceWorkerRegistrationData> m_serviceWorkerRegistrationData;
@@ -804,4 +814,13 @@
     >;
 };
 
+template<> struct EnumTraits<WebCore::ColorSchemePreference> {
+    using values = EnumValues<
+        WebCore::ColorSchemePreference,
+        WebCore::ColorSchemePreference::NoPreference,
+        WebCore::ColorSchemePreference::Light,
+        WebCore::ColorSchemePreference::Dark
+    >;
+};
+
 } // namespace WTF

Modified: trunk/Source/WebKit/ChangeLog (287029 => 287030)


--- trunk/Source/WebKit/ChangeLog	2021-12-14 18:03:05 UTC (rev 287029)
+++ trunk/Source/WebKit/ChangeLog	2021-12-14 18:20:24 UTC (rev 287030)
@@ -1,3 +1,34 @@
+2021-12-11  Dean Jackson  <[email protected]>
+
+        Allow override of system's preferred color scheme
+        https://bugs.webkit.org/show_bug.cgi?id=234199
+        <rdar://problem/86366044>
+
+        Reviewed by Tim Horton.
+
+        Expose new Private API that allows the user to give an explicit per-page preference
+        for light or dark color schemes rather than reading the system value.
+
+        * Scripts/webkit/messages.py:
+        (headers_for_type): Add header for DocumentLoader.
+
+        * Shared/WebsitePoliciesData.cpp: Encode the new value in the policies sent
+        to the Web Process.
+        (WebKit::WebsitePoliciesData::encode const):
+        (WebKit::WebsitePoliciesData::decode):
+        (WebKit::WebsitePoliciesData::applyToDocumentLoader):
+        * Shared/WebsitePoliciesData.h:
+
+        * UIProcess/API/APIWebsitePolicies.cpp: Expose a new policy "colorSchemePreference".
+        (API::WebsitePolicies::copy const):
+        (API::WebsitePolicies::data):
+        * UIProcess/API/APIWebsitePolicies.h:
+
+        * UIProcess/API/Cocoa/WKWebpagePreferences.mm: Expose a new private API "_colorSchemePreference".
+        (-[WKWebpagePreferences _colorSchemePreference]):
+        (-[WKWebpagePreferences _setColorSchemePreference:]):
+        * UIProcess/API/Cocoa/WKWebpagePreferencesPrivate.h:
+
 2021-12-14  Jean-Yves Avenard  <[email protected]>
 
         Rename SharedBuffer classes.

Modified: trunk/Source/WebKit/Scripts/webkit/messages.py (287029 => 287030)


--- trunk/Source/WebKit/Scripts/webkit/messages.py	2021-12-14 18:03:05 UTC (rev 287029)
+++ trunk/Source/WebKit/Scripts/webkit/messages.py	2021-12-14 18:20:24 UTC (rev 287030)
@@ -756,6 +756,7 @@
         'WebCore::BrowsingContextGroupSwitchDecision': ['<WebCore/FrameLoaderTypes.h>'],
         'WebCore::COEPDisposition': ['<WebCore/CrossOriginEmbedderPolicy.h>'],
         'WebCore::COOPDisposition': ['<WebCore/CrossOriginOpenerPolicy.h>'],
+        'WebCore::ColorSchemePreference': ['<WebCore/DocumentLoader.h>'],
         'WebCore::CompositeOperator': ['<WebCore/GraphicsTypes.h>'],
         'WebCore::CreateNewGroupForHighlight': ['<WebCore/AppHighlight.h>'],
         'WebCore::DOMPasteAccessCategory': ['<WebCore/DOMPasteAccess.h>'],

Modified: trunk/Source/WebKit/Shared/WebsitePoliciesData.cpp (287029 => 287030)


--- trunk/Source/WebKit/Shared/WebsitePoliciesData.cpp	2021-12-14 18:03:05 UTC (rev 287029)
+++ trunk/Source/WebKit/Shared/WebsitePoliciesData.cpp	2021-12-14 18:20:24 UTC (rev 287030)
@@ -55,6 +55,7 @@
     encoder << allowsContentJavaScript;
     encoder << mouseEventPolicy;
     encoder << modalContainerObservationPolicy;
+    encoder << colorSchemePreference;
     encoder << idempotentModeAutosizingOnlyHonorsPercentages;
 }
 
@@ -152,6 +153,11 @@
     if (!modalContainerObservationPolicy)
         return std::nullopt;
 
+    std::optional<WebCore::ColorSchemePreference> colorSchemePreference;
+    decoder >> colorSchemePreference;
+    if (!colorSchemePreference)
+        return std::nullopt;
+
     std::optional<bool> idempotentModeAutosizingOnlyHonorsPercentages;
     decoder >> idempotentModeAutosizingOnlyHonorsPercentages;
     if (!idempotentModeAutosizingOnlyHonorsPercentages)
@@ -178,6 +184,7 @@
         WTFMove(*allowsContentJavaScript),
         WTFMove(*mouseEventPolicy),
         WTFMove(*modalContainerObservationPolicy),
+        WTFMove(*colorSchemePreference),
         WTFMove(*idempotentModeAutosizingOnlyHonorsPercentages),
     } };
 }
@@ -303,6 +310,7 @@
     }
 
     documentLoader.setModalContainerObservationPolicy(websitePolicies.modalContainerObservationPolicy);
+    documentLoader.setColorSchemePreference(websitePolicies.colorSchemePreference);
     documentLoader.setAllowContentChangeObserverQuirk(websitePolicies.allowContentChangeObserverQuirk);
     documentLoader.setIdempotentModeAutosizingOnlyHonorsPercentages(websitePolicies.idempotentModeAutosizingOnlyHonorsPercentages);
 

Modified: trunk/Source/WebKit/Shared/WebsitePoliciesData.h (287029 => 287030)


--- trunk/Source/WebKit/Shared/WebsitePoliciesData.h	2021-12-14 18:03:05 UTC (rev 287029)
+++ trunk/Source/WebKit/Shared/WebsitePoliciesData.h	2021-12-14 18:20:24 UTC (rev 287030)
@@ -72,6 +72,7 @@
     WebCore::AllowsContentJavaScript allowsContentJavaScript { WebCore::AllowsContentJavaScript::Yes };
     WebCore::MouseEventPolicy mouseEventPolicy { WebCore::MouseEventPolicy::Default };
     WebCore::ModalContainerObservationPolicy modalContainerObservationPolicy { WebCore::ModalContainerObservationPolicy::Disabled };
+    WebCore::ColorSchemePreference colorSchemePreference { WebCore::ColorSchemePreference::NoPreference };
     bool idempotentModeAutosizingOnlyHonorsPercentages { false };
 
     void encode(IPC::Encoder&) const;

Modified: trunk/Source/WebKit/UIProcess/API/APIWebsitePolicies.cpp (287029 => 287030)


--- trunk/Source/WebKit/UIProcess/API/APIWebsitePolicies.cpp	2021-12-14 18:03:05 UTC (rev 287029)
+++ trunk/Source/WebKit/UIProcess/API/APIWebsitePolicies.cpp	2021-12-14 18:20:24 UTC (rev 287030)
@@ -77,6 +77,7 @@
     policies->setCaptivePortalModeEnabled(m_captivePortalModeEnabled);
     policies->setMouseEventPolicy(m_mouseEventPolicy);
     policies->setModalContainerObservationPolicy(m_modalContainerObservationPolicy);
+    policies->setColorSchemePreference(m_colorSchemePreference);
     return policies;
 }
 
@@ -123,6 +124,7 @@
         m_allowsContentJavaScript,
         m_mouseEventPolicy,
         m_modalContainerObservationPolicy,
+        m_colorSchemePreference,
         m_idempotentModeAutosizingOnlyHonorsPercentages
     };
 }

Modified: trunk/Source/WebKit/UIProcess/API/APIWebsitePolicies.h (287029 => 287030)


--- trunk/Source/WebKit/UIProcess/API/APIWebsitePolicies.h	2021-12-14 18:03:05 UTC (rev 287029)
+++ trunk/Source/WebKit/UIProcess/API/APIWebsitePolicies.h	2021-12-14 18:20:24 UTC (rev 287030)
@@ -132,6 +132,9 @@
     void setCaptivePortalModeEnabled(std::optional<bool> captivePortalModeEnabled) { m_captivePortalModeEnabled = captivePortalModeEnabled; }
     bool isCaptivePortalModeExplicitlySet() const { return !!m_captivePortalModeEnabled; }
 
+    WebCore::ColorSchemePreference colorSchemePreference() const { return m_colorSchemePreference; }
+    void setColorSchemePreference(WebCore::ColorSchemePreference colorSchemePreference) { m_colorSchemePreference = colorSchemePreference; }
+
     WebCore::MouseEventPolicy mouseEventPolicy() const { return m_mouseEventPolicy; }
     void setMouseEventPolicy(WebCore::MouseEventPolicy policy) { m_mouseEventPolicy = policy; }
 
@@ -171,6 +174,7 @@
     WebCore::ModalContainerObservationPolicy m_modalContainerObservationPolicy { WebCore::ModalContainerObservationPolicy::Disabled };
     bool m_idempotentModeAutosizingOnlyHonorsPercentages { false };
     std::optional<bool> m_captivePortalModeEnabled;
+    WebCore::ColorSchemePreference m_colorSchemePreference { WebCore::ColorSchemePreference::NoPreference };
 };
 
 } // namespace API

Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebpagePreferences.mm (287029 => 287030)


--- trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebpagePreferences.mm	2021-12-14 18:03:05 UTC (rev 287029)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebpagePreferences.mm	2021-12-14 18:20:24 UTC (rev 287030)
@@ -501,6 +501,33 @@
     return _websitePolicies->captivePortalModeEnabled();
 }
 
+- (_WKWebsiteColorSchemePreference)_colorSchemePreference
+{
+    switch (_websitePolicies->colorSchemePreference()) {
+    case WebCore::ColorSchemePreference::NoPreference:
+        return _WKWebsiteColorSchemePreferenceNoPreference;
+    case WebCore::ColorSchemePreference::Light:
+        return _WKWebsiteColorSchemePreferenceLight;
+    case WebCore::ColorSchemePreference::Dark:
+        return _WKWebsiteColorSchemePreferenceDark;
+    }
+}
+
+- (void)_setColorSchemePreference:(_WKWebsiteColorSchemePreference)value
+{
+    switch (value) {
+    case _WKWebsiteColorSchemePreferenceNoPreference:
+        _websitePolicies->setColorSchemePreference(WebCore::ColorSchemePreference::NoPreference);
+        break;
+    case _WKWebsiteColorSchemePreferenceLight:
+        _websitePolicies->setColorSchemePreference(WebCore::ColorSchemePreference::Light);
+        break;
+    case _WKWebsiteColorSchemePreferenceDark:
+        _websitePolicies->setColorSchemePreference(WebCore::ColorSchemePreference::Dark);
+        break;
+    }
+}
+
 #if PLATFORM(IOS_FAMILY)
 
 - (void)setPreferredContentMode:(WKContentMode)contentMode

Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebpagePreferencesPrivate.h (287029 => 287030)


--- trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebpagePreferencesPrivate.h	2021-12-14 18:03:05 UTC (rev 287029)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebpagePreferencesPrivate.h	2021-12-14 18:20:24 UTC (rev 287030)
@@ -69,6 +69,13 @@
     _WKWebsiteModalContainerObservationPolicyDisallow,
 } WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
 
+// Allow overriding the system color-scheme with a per-website preference.
+typedef NS_OPTIONS(NSUInteger, _WKWebsiteColorSchemePreference) {
+    _WKWebsiteColorSchemePreferenceNoPreference,
+    _WKWebsiteColorSchemePreferenceLight,
+    _WKWebsiteColorSchemePreferenceDark,
+} WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
+
 @class _WKCustomHeaderFields;
 @class WKUserContentController;
 @class WKWebsiteDataStore;
@@ -96,4 +103,6 @@
 
 @property (nonatomic, setter=_setCaptivePortalModeEnabled:) BOOL _captivePortalModeEnabled WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
 
+@property (nonatomic, setter=_setColorSchemePreference:) _WKWebsiteColorSchemePreference _colorSchemePreference;
+
 @end

Modified: trunk/Tools/ChangeLog (287029 => 287030)


--- trunk/Tools/ChangeLog	2021-12-14 18:03:05 UTC (rev 287029)
+++ trunk/Tools/ChangeLog	2021-12-14 18:20:24 UTC (rev 287030)
@@ -1,3 +1,20 @@
+2021-12-11  Dean Jackson  <[email protected]>
+
+        Allow override of system's preferred color scheme
+        https://bugs.webkit.org/show_bug.cgi?id=234199
+        <rdar://problem/86366044>
+
+        Reviewed by Tim Horton.
+
+        New API test that checks the color-scheme after
+        explicitly setting it.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WebKit/color-scheme.html: Added. Note: Xcode was
+        being difficult and wouldn't let me put this file in the right order
+        for the Copy Build Phase :(
+        * TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm:
+
 2021-12-14  Angelos Oikonomopoulos  <[email protected]>
 
         [JSC] Fix LocalJumpError in run-jsc-stress-tests

Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (287029 => 287030)


--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2021-12-14 18:03:05 UTC (rev 287029)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2021-12-14 18:20:24 UTC (rev 287030)
@@ -176,6 +176,7 @@
 		3128A81323763FAC00D90D40 /* link-with-image.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 3128A81223763F0B00D90D40 /* link-with-image.html */; };
 		3128A8152376413300D90D40 /* image.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 3128A814237640FD00D90D40 /* image.html */; };
 		313C3A0221E567C300DBA86E /* SystemPreviewBlobNaming.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 313C3A0121E5677A00DBA86E /* SystemPreviewBlobNaming.html */; };
+		31903C912765077400363472 /* color-scheme.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 31903C902764FE1400363472 /* color-scheme.html */; };
 		31B76E4523299BDC007FED2C /* system-preview-trigger.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 31B76E4423299BA3007FED2C /* system-preview-trigger.html */; };
 		31E9BDA1247F4C62002E51A2 /* WebGLPrepareDisplayOnWebThread.mm in Sources */ = {isa = PBXBuildFile; fileRef = 31E9BDA0247F4C62002E51A2 /* WebGLPrepareDisplayOnWebThread.mm */; };
 		31E9BDA3247F5729002E51A2 /* webgl.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 31E9BDA2247F4DD0002E51A2 /* webgl.html */; };
@@ -1257,6 +1258,7 @@
 				9B9332CE2320C745002D50E8 /* cocoa-writer-markup-with-lists.html in Copy Resources */,
 				9BAE177B22E2BBFB00DF3098 /* cocoa-writer-markup-with-system-fonts.html in Copy Resources */,
 				E5036F78211BC25400BFDBE2 /* color-drop.html in Copy Resources */,
+				31903C912765077400363472 /* color-scheme.html in Copy Resources */,
 				0F16BED82304A1F300B4A167 /* composited.html in Copy Resources */,
 				F4B825D81EF4DBFB006E417F /* compressed-files.zip in Copy Resources */,
 				5C9E56871DF914AE00C9EE33 /* contentBlockerCheck.html in Copy Resources */,
@@ -1850,6 +1852,7 @@
 		3128A81223763F0B00D90D40 /* link-with-image.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = "link-with-image.html"; sourceTree = "<group>"; };
 		3128A814237640FD00D90D40 /* image.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = image.html; sourceTree = "<group>"; };
 		313C3A0121E5677A00DBA86E /* SystemPreviewBlobNaming.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = SystemPreviewBlobNaming.html; sourceTree = "<group>"; };
+		31903C902764FE1400363472 /* color-scheme.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "color-scheme.html"; sourceTree = "<group>"; };
 		31B76E4223298E2B007FED2C /* SystemPreview.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = SystemPreview.mm; sourceTree = "<group>"; };
 		31B76E4423299BA3007FED2C /* system-preview-trigger.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = "system-preview-trigger.html"; sourceTree = "<group>"; };
 		31E9BDA0247F4C62002E51A2 /* WebGLPrepareDisplayOnWebThread.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = WebGLPrepareDisplayOnWebThread.mm; sourceTree = "<group>"; };
@@ -4599,6 +4602,7 @@
 				9BD4239B1E04BFD000200395 /* chinese-character-with-image.html */,
 				1A50AA1F1A2A4EA500F4C345 /* close-from-within-create-page.html */,
 				9B270FED1DDC25FD002D53F3 /* closed-shadow-tree-test.html */,
+				31903C902764FE1400363472 /* color-scheme.html */,
 				5C9E56861DF9148E00C9EE33 /* contentBlockerCheck.html */,
 				F4034FA2275D5449003A81F8 /* cookie-consent-basic.html */,
 				2DDD4DA3270B8B3300659A61 /* cube.usdz */,

Added: trunk/Tools/TestWebKitAPI/Tests/WebKit/color-scheme.html (0 => 287030)


--- trunk/Tools/TestWebKitAPI/Tests/WebKit/color-scheme.html	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKit/color-scheme.html	2021-12-14 18:20:24 UTC (rev 287030)
@@ -0,0 +1,20 @@
+<html>
+<head>
+<script>
+function testColorScheme() {
+    if (window.matchMedia("(prefers-color-scheme: light)").matches) {
+        try {
+            window.webkit.messageHandlers.testHandler.postMessage("light-detected");
+        } catch(e) { }
+    }
+    if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
+        try {
+            window.webkit.messageHandlers.testHandler.postMessage("dark-detected");
+        } catch(e) { }
+    }
+}
+</script>
+</head>
+<body _onload_="testColorScheme()">
+</body>
+</html>
Property changes on: trunk/Tools/TestWebKitAPI/Tests/WebKit/color-scheme.html
___________________________________________________________________

Added: svn:eol-style

+native \ No newline at end of property

Added: svn:keywords

+Date Revision \ No newline at end of property

Added: svn:mime-type

+text/html \ No newline at end of property

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm (287029 => 287030)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm	2021-12-14 18:03:05 UTC (rev 287029)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WebsitePolicies.mm	2021-12-14 18:20:24 UTC (rev 287030)
@@ -1787,3 +1787,27 @@
     [replacementUserContentController _addUserScriptImmediately:makeScript(@"alert('testAlert3');").get()];
     EXPECT_WK_STREQ([uiDelegate waitForAlert], "testAlert3");
 }
+
+TEST(WebpagePreferences, UserExplicitlyPrefersColorSchemeLight)
+{
+    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+
+    configuration.get().defaultWebpagePreferences._colorSchemePreference = _WKWebsiteColorSchemePreferenceLight;
+
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+
+    [webView loadTestPageNamed:@"color-scheme"];
+    [webView waitForMessage:@"light-detected"];
+}
+
+TEST(WebpagePreferences, UserExplicitlyPrefersColorSchemeDark)
+{
+    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+
+    configuration.get().defaultWebpagePreferences._colorSchemePreference = _WKWebsiteColorSchemePreferenceDark;
+
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+
+    [webView loadTestPageNamed:@"color-scheme"];
+    [webView waitForMessage:@"dark-detected"];
+}
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to