Commit bdd349cc authored by Rune Lillesveen's avatar Rune Lillesveen Committed by Commit Bot

Trigger events for prefers-color-scheme changes.

We triggered style changes as a result of prefers-color-scheme changing,
but we did not trigger 'change' events for matchMedia() as we skipped
the code in Document which did that.

Bug: 962156
Change-Id: Idba3ba9700aa24a2f9370360da22f6102fe82161
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1609821Reviewed-by: default avatarAnders Hartvoll Ruud <andruud@chromium.org>
Commit-Queue: Rune Lillesveen <futhark@chromium.org>
Cr-Commit-Position: refs/heads/master@{#659433}
parent ef653f62
...@@ -82,7 +82,7 @@ TEST_F(ApplyDarkModeCheckTest, MetaColorSchemeDark) { ...@@ -82,7 +82,7 @@ TEST_F(ApplyDarkModeCheckTest, MetaColorSchemeDark) {
PreferredColorScheme::kDark); PreferredColorScheme::kDark);
ColorSchemeSet schemes; ColorSchemeSet schemes;
schemes.Set(ColorScheme::kDark); schemes.Set(ColorScheme::kDark);
GetDocument().GetStyleEngine().SetMetaColorScheme(schemes); GetDocument().SetMetaColorScheme(schemes);
UpdateAllLifecyclePhasesForTest(); UpdateAllLifecyclePhasesForTest();
// Opting out of forced darkening when dark is among the supported color // Opting out of forced darkening when dark is among the supported color
......
...@@ -1798,7 +1798,6 @@ void StyleEngine::UpdateLayoutTreeRebuildRoot(ContainerNode* ancestor, ...@@ -1798,7 +1798,6 @@ void StyleEngine::UpdateLayoutTreeRebuildRoot(ContainerNode* ancestor,
void StyleEngine::UpdateColorScheme() { void StyleEngine::UpdateColorScheme() {
auto* settings = GetDocument().GetSettings(); auto* settings = GetDocument().GetSettings();
DCHECK(settings); DCHECK(settings);
PreferredColorScheme old_preferred_scheme = preferred_color_scheme_;
ColorScheme old_color_scheme = color_scheme_; ColorScheme old_color_scheme = color_scheme_;
preferred_color_scheme_ = settings->GetPreferredColorScheme(); preferred_color_scheme_ = settings->GetPreferredColorScheme();
...@@ -1824,8 +1823,6 @@ void StyleEngine::UpdateColorScheme() { ...@@ -1824,8 +1823,6 @@ void StyleEngine::UpdateColorScheme() {
} }
} }
} }
if (preferred_color_scheme_ != old_preferred_scheme)
MediaQueryAffectingValueChanged();
} }
void StyleEngine::ColorSchemeChanged() { void StyleEngine::ColorSchemeChanged() {
......
...@@ -8010,6 +8010,16 @@ TrustedTypePolicyFactory* Document::GetTrustedTypes() const { ...@@ -8010,6 +8010,16 @@ TrustedTypePolicyFactory* Document::GetTrustedTypes() const {
return ExecutingWindow() ? ExecutingWindow()->trustedTypes() : nullptr; return ExecutingWindow() ? ExecutingWindow()->trustedTypes() : nullptr;
} }
void Document::SetMetaColorScheme(const ColorSchemeSet& meta_color_scheme) {
GetStyleEngine().SetMetaColorScheme(meta_color_scheme);
MediaQueryAffectingValueChanged();
}
void Document::ColorSchemeChanged() {
GetStyleEngine().ColorSchemeChanged();
MediaQueryAffectingValueChanged();
}
template class CORE_TEMPLATE_EXPORT Supplement<Document>; template class CORE_TEMPLATE_EXPORT Supplement<Document>;
} // namespace blink } // namespace blink
......
...@@ -1545,6 +1545,9 @@ class CORE_EXPORT Document : public ContainerNode, ...@@ -1545,6 +1545,9 @@ class CORE_EXPORT Document : public ContainerNode,
TrustedTypePolicyFactory* GetTrustedTypes() const override; TrustedTypePolicyFactory* GetTrustedTypes() const override;
void SetMetaColorScheme(const ColorSchemeSet&);
void ColorSchemeChanged();
protected: protected:
void DidUpdateSecurityOrigin() final; void DidUpdateSecurityOrigin() final;
......
...@@ -41,6 +41,8 @@ ...@@ -41,6 +41,8 @@
#include "third_party/blink/renderer/bindings/core/v8/isolated_world_csp.h" #include "third_party/blink/renderer/bindings/core/v8/isolated_world_csp.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h" #include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h" #include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"
#include "third_party/blink/renderer/core/css/media_query_list_listener.h"
#include "third_party/blink/renderer/core/css/media_query_matcher.h"
#include "third_party/blink/renderer/core/dom/document_fragment.h" #include "third_party/blink/renderer/core/dom/document_fragment.h"
#include "third_party/blink/renderer/core/dom/node_with_index.h" #include "third_party/blink/renderer/core/dom/node_with_index.h"
#include "third_party/blink/renderer/core/dom/range.h" #include "third_party/blink/renderer/core/dom/range.h"
...@@ -343,6 +345,15 @@ class MockWebApplicationCacheHost : public blink::WebApplicationCacheHost { ...@@ -343,6 +345,15 @@ class MockWebApplicationCacheHost : public blink::WebApplicationCacheHost {
bool without_manifest_was_called_ = false; bool without_manifest_was_called_ = false;
}; };
class PrefersColorSchemeTestListener final : public MediaQueryListListener {
public:
void NotifyMediaQueryChanged() override { notified_ = true; }
bool IsNotified() const { return notified_; }
private:
bool notified_ = false;
};
} // anonymous namespace } // anonymous namespace
TEST_F(DocumentTest, CreateRangeAdjustedToTreeScopeWithPositionInShadowTree) { TEST_F(DocumentTest, CreateRangeAdjustedToTreeScopeWithPositionInShadowTree) {
...@@ -1191,6 +1202,24 @@ TEST_F(DocumentTest, ElementFromPointWithPageZoom) { ...@@ -1191,6 +1202,24 @@ TEST_F(DocumentTest, ElementFromPointWithPageZoom) {
EXPECT_EQ(GetDocument().ElementFromPoint(1, 12), GetDocument().body()); EXPECT_EQ(GetDocument().ElementFromPoint(1, 12), GetDocument().body());
} }
TEST_F(DocumentTest, PrefersColorSchemeChanged) {
UpdateAllLifecyclePhasesForTest();
auto* list = GetDocument().GetMediaQueryMatcher().MatchMedia(
"(prefers-color-scheme: dark)");
auto* listener = MakeGarbageCollected<PrefersColorSchemeTestListener>();
list->AddListener(listener);
EXPECT_FALSE(listener->IsNotified());
GetDocument().GetSettings()->SetPreferredColorScheme(
PreferredColorScheme::kDark);
GetDocument().ServiceScriptedAnimations(base::TimeTicks());
EXPECT_TRUE(listener->IsNotified());
}
/** /**
* Tests for viewport-fit propagation. * Tests for viewport-fit propagation.
*/ */
......
...@@ -490,7 +490,7 @@ void HTMLMetaElement::ProcessColorScheme(const AtomicString& content) { ...@@ -490,7 +490,7 @@ void HTMLMetaElement::ProcessColorScheme(const AtomicString& content) {
color_scheme_set.Set(ColorScheme::kDark); color_scheme_set.Set(ColorScheme::kDark);
} }
} }
GetDocument().GetStyleEngine().SetMetaColorScheme(color_scheme_set); GetDocument().SetMetaColorScheme(color_scheme_set);
} }
void HTMLMetaElement::NameRemoved(const AtomicString& name_value) { void HTMLMetaElement::NameRemoved(const AtomicString& name_value) {
......
...@@ -110,7 +110,7 @@ TEST_F(LayoutThemeTest, RootElementColorChange) { ...@@ -110,7 +110,7 @@ TEST_F(LayoutThemeTest, RootElementColorChange) {
PreferredColorScheme::kDark); PreferredColorScheme::kDark);
ColorSchemeSet color_schemes; ColorSchemeSet color_schemes;
color_schemes.Set(ColorScheme::kDark); color_schemes.Set(ColorScheme::kDark);
GetDocument().GetStyleEngine().SetMetaColorScheme(color_schemes); GetDocument().SetMetaColorScheme(color_schemes);
UpdateAllLifecyclePhasesForTest(); UpdateAllLifecyclePhasesForTest();
document_element_style = GetDocument().documentElement()->GetComputedStyle(); document_element_style = GetDocument().documentElement()->GetComputedStyle();
......
...@@ -716,7 +716,7 @@ void Page::SettingsChanged(SettingsDelegate::ChangeType change_type) { ...@@ -716,7 +716,7 @@ void Page::SettingsChanged(SettingsDelegate::ChangeType change_type) {
for (Frame* frame = MainFrame(); frame; for (Frame* frame = MainFrame(); frame;
frame = frame->Tree().TraverseNext()) { frame = frame->Tree().TraverseNext()) {
if (auto* local_frame = DynamicTo<LocalFrame>(frame)) if (auto* local_frame = DynamicTo<LocalFrame>(frame))
local_frame->GetDocument()->GetStyleEngine().ColorSchemeChanged(); local_frame->GetDocument()->ColorSchemeChanged();
} }
break; break;
case SettingsDelegate::kSpatialNavigationChange: case SettingsDelegate::kSpatialNavigationChange:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment