Title: [136493] trunk
Revision
136493
Author
apav...@chromium.org
Date
2012-12-04 02:25:44 -0800 (Tue, 04 Dec 2012)

Log Message

Web Inspector: [Overrides] Add ability to override the page CSS media type
https://bugs.webkit.org/show_bug.cgi?id=103752

Reviewed by Pavel Feldman.

Source/WebCore:

This change emulates the CSS media types through the instrumentation of FrameView::mediaType().
Even though it already checks the associated FrameLoaderClient::overrideMediaType(), instrumenting
every single platform FrameLoaderClient implementation out there seems wrong.

Test: inspector/styles/media-emulation.html

* English.lproj/localizedStrings.js:
* inspector/Inspector.json: Add Page.setEmulatedMedia().
* inspector/InspectorInstrumentation.cpp:
(WebCore::InspectorInstrumentation::applyEmulatedMediaImpl): Added.
* inspector/InspectorInstrumentation.h:
(WebCore::InspectorInstrumentation::applyEmulatedMedia): Added.
* inspector/InspectorPageAgent.cpp:
(WebCore::InspectorPageAgent::restore): Restores the media emulation state.
(WebCore::InspectorPageAgent::disable): Resets the media emulation state.
(WebCore::InspectorPageAgent::setEmulatedMedia): Added.
(WebCore::InspectorPageAgent::applyEmulatedMedia): Applies the emulated CSS media if needed.
* inspector/InspectorPageAgent.h:
* inspector/front-end/CSSStyleModel.js: Add the list of media types.
* inspector/front-end/OverridesView.js: Add the CSS media type override UI.
(WebInspector.OverridesView):
(WebInspector.OverridesView.prototype._createMediaEmulationElement): Builds the UI.
(WebInspector.OverridesView.prototype._emulateMediaChanged): Handles the control value changes.
* inspector/front-end/Settings.js: Add the "emulatedCSSMedia" setting.
* page/FrameView.cpp:
(WebCore::FrameView::mediaType):
  Uses InspectorInstrumentation::applyEmulatedMedia() to provide the emulated value.

LayoutTests:

The test invokes PageAgent.setEmulatedMedia() and emits the WebInspector.cssModel.mediaQueryResultChanged() signal.

* inspector/styles/media-emulation-expected.txt: Added.
* inspector/styles/media-emulation.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (136492 => 136493)


--- trunk/LayoutTests/ChangeLog	2012-12-04 10:00:54 UTC (rev 136492)
+++ trunk/LayoutTests/ChangeLog	2012-12-04 10:25:44 UTC (rev 136493)
@@ -1,3 +1,15 @@
+2012-12-03  Alexander Pavlov  <apav...@chromium.org>
+
+        Web Inspector: [Overrides] Add ability to override the page CSS media type
+        https://bugs.webkit.org/show_bug.cgi?id=103752
+
+        Reviewed by Pavel Feldman.
+
+        The test invokes PageAgent.setEmulatedMedia() and emits the WebInspector.cssModel.mediaQueryResultChanged() signal.
+
+        * inspector/styles/media-emulation-expected.txt: Added.
+        * inspector/styles/media-emulation.html: Added.
+
 2012-12-04  Ryosuke Niwa  <rn...@webkit.org>
 
         Fix a test and re-enable fast/harness/perftests on Chromium.

Added: trunk/LayoutTests/inspector/styles/media-emulation-expected.txt (0 => 136493)


--- trunk/LayoutTests/inspector/styles/media-emulation-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/inspector/styles/media-emulation-expected.txt	2012-12-04 10:25:44 UTC (rev 136493)
@@ -0,0 +1,77 @@
+Tests that emulated CSS media is reflected in the Styles pane.
+
+Original style:
+[expanded] 
+
+[expanded] 
+element.style  { ()
+
+======== Matched CSS Rules ========
+[expanded] 
+#main  { (media-emulation.html:5)
+color: red;
+
+[expanded] 
+div  { (user agent stylesheet)
+display: block;
+
+
+print media emulated:
+[expanded] 
+
+[expanded] 
+element.style  { ()
+
+======== Matched CSS Rules ========
+[expanded] 
+@media print (media-emulation.html:7)
+#main  { (media-emulation.html:8)
+color: black;
+
+[expanded] 
+#main  { (media-emulation.html:5)
+/-- overloaded --/ color: red;
+
+[expanded] 
+div  { (user agent stylesheet)
+display: block;
+
+
+tty media emulated:
+[expanded] 
+
+[expanded] 
+element.style  { ()
+
+======== Matched CSS Rules ========
+[expanded] 
+@media tty (media-emulation.html:11)
+#main  { (media-emulation.html:12)
+color: green;
+
+[expanded] 
+#main  { (media-emulation.html:5)
+/-- overloaded --/ color: red;
+
+[expanded] 
+div  { (user agent stylesheet)
+display: block;
+
+
+No media emulated:
+[expanded] 
+
+[expanded] 
+element.style  { ()
+
+======== Matched CSS Rules ========
+[expanded] 
+#main  { (media-emulation.html:5)
+color: red;
+
+[expanded] 
+div  { (user agent stylesheet)
+display: block;
+
+
+
Property changes on: trunk/LayoutTests/inspector/styles/media-emulation-expected.txt
___________________________________________________________________

Added: svn:eol-style

Added: trunk/LayoutTests/inspector/styles/media-emulation.html (0 => 136493)


--- trunk/LayoutTests/inspector/styles/media-emulation.html	                        (rev 0)
+++ trunk/LayoutTests/inspector/styles/media-emulation.html	2012-12-04 10:25:44 UTC (rev 136493)
@@ -0,0 +1,70 @@
+<html>
+<head>
+
+<style type="text/css">
+#main { color: red; }
+
+@media print {
+#main { color: black; }
+}
+
+@media tty {
+#main { color: green; }
+}
+</style>
+
+<script src=""
+<script src=""
+<script>
+
+function test()
+{
+    InspectorTest.selectNodeAndWaitForStyles("main", step0);
+
+    function step0()
+    {
+        InspectorTest.addResult("Original style:");
+        InspectorTest.dumpSelectedElementStyles();
+        applyEmulatedMedia("print");
+        InspectorTest.waitForStyles("main", step1);
+    }
+
+    function step1() {
+        InspectorTest.addResult("print media emulated:");
+        InspectorTest.dumpSelectedElementStyles();
+        applyEmulatedMedia("tty");
+        InspectorTest.waitForStyles("main", step2);
+    }
+
+    function step2()
+    {
+        InspectorTest.addResult("tty media emulated:");
+        InspectorTest.dumpSelectedElementStyles();
+        applyEmulatedMedia("");
+        InspectorTest.waitForStyles("main", step3);
+    }
+
+    function step3()
+    {
+        InspectorTest.addResult("No media emulated:");
+        InspectorTest.dumpSelectedElementStyles();
+        InspectorTest.completeTest();
+    }
+
+    function applyEmulatedMedia(media)
+    {
+        PageAgent.setEmulatedMedia(media);
+        WebInspector.cssModel.mediaQueryResultChanged();
+    }
+}
+</script>
+</head>
+
+<body _onload_="runTest()">
+<p>
+Tests that emulated CSS media is reflected in the Styles pane.
+</p>
+
+<div id="main"></div>
+</body>
+</html>
Property changes on: trunk/LayoutTests/inspector/styles/media-emulation.html
___________________________________________________________________

Added: svn:eol-style

Modified: trunk/Source/WebCore/ChangeLog (136492 => 136493)


--- trunk/Source/WebCore/ChangeLog	2012-12-04 10:00:54 UTC (rev 136492)
+++ trunk/Source/WebCore/ChangeLog	2012-12-04 10:25:44 UTC (rev 136493)
@@ -1,3 +1,38 @@
+2012-12-04  Alexander Pavlov  <apav...@chromium.org>
+
+        Web Inspector: [Overrides] Add ability to override the page CSS media type
+        https://bugs.webkit.org/show_bug.cgi?id=103752
+
+        Reviewed by Pavel Feldman.
+
+        This change emulates the CSS media types through the instrumentation of FrameView::mediaType().
+        Even though it already checks the associated FrameLoaderClient::overrideMediaType(), instrumenting
+        every single platform FrameLoaderClient implementation out there seems wrong.
+
+        Test: inspector/styles/media-emulation.html
+
+        * English.lproj/localizedStrings.js:
+        * inspector/Inspector.json: Add Page.setEmulatedMedia().
+        * inspector/InspectorInstrumentation.cpp:
+        (WebCore::InspectorInstrumentation::applyEmulatedMediaImpl): Added.
+        * inspector/InspectorInstrumentation.h:
+        (WebCore::InspectorInstrumentation::applyEmulatedMedia): Added.
+        * inspector/InspectorPageAgent.cpp:
+        (WebCore::InspectorPageAgent::restore): Restores the media emulation state.
+        (WebCore::InspectorPageAgent::disable): Resets the media emulation state.
+        (WebCore::InspectorPageAgent::setEmulatedMedia): Added.
+        (WebCore::InspectorPageAgent::applyEmulatedMedia): Applies the emulated CSS media if needed.
+        * inspector/InspectorPageAgent.h:
+        * inspector/front-end/CSSStyleModel.js: Add the list of media types.
+        * inspector/front-end/OverridesView.js: Add the CSS media type override UI.
+        (WebInspector.OverridesView):
+        (WebInspector.OverridesView.prototype._createMediaEmulationElement): Builds the UI.
+        (WebInspector.OverridesView.prototype._emulateMediaChanged): Handles the control value changes.
+        * inspector/front-end/Settings.js: Add the "emulatedCSSMedia" setting.
+        * page/FrameView.cpp:
+        (WebCore::FrameView::mediaType):
+          Uses InspectorInstrumentation::applyEmulatedMedia() to provide the emulated value.
+
 2012-12-03  Carlos Garcia Campos  <cgar...@igalia.com>
 
         [GTK] Simplify GObject DOM bindings code for handling properties

Modified: trunk/Source/WebCore/English.lproj/localizedStrings.js (136492 => 136493)


--- trunk/Source/WebCore/English.lproj/localizedStrings.js	2012-12-04 10:00:54 UTC (rev 136492)
+++ trunk/Source/WebCore/English.lproj/localizedStrings.js	2012-12-04 10:25:44 UTC (rev 136493)
@@ -173,6 +173,7 @@
 localizedStrings["Element state: %s"] = "Element state: %s";
 localizedStrings["Elements Panel"] = "Elements Panel";
 localizedStrings["Elements"] = "Elements";
+localizedStrings["Emulate CSS media"] = "Emulate CSS media";
 localizedStrings["Emulate touch events"] = "Emulate touch events";
 localizedStrings["Enable Breakpoint"] = "Enable Breakpoint";
 localizedStrings["Enable breakpoint"] = "Enable breakpoint";

Modified: trunk/Source/WebCore/inspector/Inspector.json (136492 => 136493)


--- trunk/Source/WebCore/inspector/Inspector.json	2012-12-04 10:00:54 UTC (rev 136492)
+++ trunk/Source/WebCore/inspector/Inspector.json	2012-12-04 10:25:44 UTC (rev 136493)
@@ -429,6 +429,14 @@
                 "hidden": true
             },
             {
+                "name": "setEmulatedMedia",
+                "parameters": [
+                    { "name": "media", "type": "string", "description": "Media type to emulate. Empty string disables the override." }
+                ],
+                "description": "Emulates the given media for CSS media queries.",
+                "hidden": true
+            },
+            {
                 "name": "getCompositingBordersVisible",
                 "description": "Indicates the visibility of compositing borders.",
                 "returns": [

Modified: trunk/Source/WebCore/inspector/InspectorInstrumentation.cpp (136492 => 136493)


--- trunk/Source/WebCore/inspector/InspectorInstrumentation.cpp	2012-12-04 10:00:54 UTC (rev 136492)
+++ trunk/Source/WebCore/inspector/InspectorInstrumentation.cpp	2012-12-04 10:25:44 UTC (rev 136493)
@@ -645,6 +645,12 @@
     return false;
 }
 
+void InspectorInstrumentation::applyEmulatedMediaImpl(InstrumentingAgents* instrumentingAgents, String* media)
+{
+    if (InspectorPageAgent* pageAgent = instrumentingAgents->inspectorPageAgent())
+        pageAgent->applyEmulatedMedia(media);
+}
+
 void InspectorInstrumentation::willSendRequestImpl(InstrumentingAgents* instrumentingAgents, unsigned long identifier, DocumentLoader* loader, ResourceRequest& request, const ResourceResponse& redirectResponse)
 {
     if (InspectorTimelineAgent* timelineAgent = instrumentingAgents->inspectorTimelineAgent())

Modified: trunk/Source/WebCore/inspector/InspectorInstrumentation.h (136492 => 136493)


--- trunk/Source/WebCore/inspector/InspectorInstrumentation.h	2012-12-04 10:00:54 UTC (rev 136492)
+++ trunk/Source/WebCore/inspector/InspectorInstrumentation.h	2012-12-04 10:25:44 UTC (rev 136493)
@@ -158,6 +158,7 @@
     static void applyUserAgentOverride(Frame*, String*);
     static void applyScreenWidthOverride(Frame*, long*);
     static void applyScreenHeightOverride(Frame*, long*);
+    static void applyEmulatedMedia(Frame*, String*);
     static bool shouldApplyScreenWidthOverride(Frame*);
     static bool shouldApplyScreenHeightOverride(Frame*);
     static void willSendRequest(Frame*, unsigned long identifier, DocumentLoader*, ResourceRequest&, const ResourceResponse& redirectResponse);
@@ -350,6 +351,7 @@
     static void applyUserAgentOverrideImpl(InstrumentingAgents*, String*);
     static void applyScreenWidthOverrideImpl(InstrumentingAgents*, long*);
     static void applyScreenHeightOverrideImpl(InstrumentingAgents*, long*);
+    static void applyEmulatedMediaImpl(InstrumentingAgents*, String*);
     static bool shouldApplyScreenWidthOverrideImpl(InstrumentingAgents*);
     static bool shouldApplyScreenHeightOverrideImpl(InstrumentingAgents*);
     static void willSendRequestImpl(InstrumentingAgents*, unsigned long identifier, DocumentLoader*, ResourceRequest&, const ResourceResponse& redirectResponse);
@@ -1065,6 +1067,15 @@
     return false;
 }
 
+inline void InspectorInstrumentation::applyEmulatedMedia(Frame* frame, String* media)
+{
+#if ENABLE(INSPECTOR)
+    FAST_RETURN_IF_NO_FRONTENDS(void());
+    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForFrame(frame))
+        applyEmulatedMediaImpl(instrumentingAgents, media);
+#endif
+}
+
 inline bool InspectorInstrumentation::shouldApplyScreenHeightOverride(Frame* frame)
 {
 #if ENABLE(INSPECTOR)

Modified: trunk/Source/WebCore/inspector/InspectorPageAgent.cpp (136492 => 136493)


--- trunk/Source/WebCore/inspector/InspectorPageAgent.cpp	2012-12-04 10:00:54 UTC (rev 136492)
+++ trunk/Source/WebCore/inspector/InspectorPageAgent.cpp	2012-12-04 10:25:44 UTC (rev 136493)
@@ -97,6 +97,7 @@
 #if ENABLE(TOUCH_EVENTS)
 static const char touchEventEmulationEnabled[] = "touchEventEmulationEnabled";
 #endif
+static const char pageAgentEmulatedMedia[] = "pageAgentEmulatedMedia";
 }
 
 static bool decodeBuffer(const char* buffer, unsigned size, const String& textEncodingName, String* result)
@@ -363,6 +364,8 @@
         setShowPaintRects(0, showPaintRects);
         bool showFPSCounter = m_state->getBoolean(PageAgentState::pageAgentShowFPSCounter);
         setShowFPSCounter(0, showFPSCounter);
+        String emulatedMedia = m_state->getString(PageAgentState::pageAgentEmulatedMedia);
+        setEmulatedMedia(0, emulatedMedia);
 
         int currentWidth = static_cast<int>(m_state->getLong(PageAgentState::pageAgentScreenWidthOverride));
         int currentHeight = static_cast<int>(m_state->getLong(PageAgentState::pageAgentScreenHeightOverride));
@@ -397,6 +400,7 @@
     setScriptExecutionDisabled(0, false);
     setShowPaintRects(0, false);
     setShowFPSCounter(0, false);
+    setEmulatedMedia(0, "");
 
     // When disabling the agent, reset the override values.
     m_state->setLong(PageAgentState::pageAgentScreenWidthOverride, 0);
@@ -1147,6 +1151,29 @@
 #endif
 }
 
+void InspectorPageAgent::setEmulatedMedia(ErrorString*, const String& media)
+{
+    String currentMedia = m_state->getString(PageAgentState::pageAgentEmulatedMedia);
+    if (media == currentMedia)
+        return;
+
+    m_state->setString(PageAgentState::pageAgentEmulatedMedia, media);
+    Document* document = 0;
+    if (m_page->mainFrame())
+        document = m_page->mainFrame()->document();
+    if (document) {
+        document->styleResolverChanged(RecalcStyleImmediately);
+        document->updateLayout();
+    }
+}
+
+void InspectorPageAgent::applyEmulatedMedia(String* media)
+{
+    String emulatedMedia = m_state->getString(PageAgentState::pageAgentEmulatedMedia);
+    if (!emulatedMedia.isEmpty())
+        *media = emulatedMedia;
+}
+
 void InspectorPageAgent::getCompositingBordersVisible(ErrorString* error, bool* outParam)
 {
     Settings* settings = m_page->settings();

Modified: trunk/Source/WebCore/inspector/InspectorPageAgent.h (136492 => 136493)


--- trunk/Source/WebCore/inspector/InspectorPageAgent.h	2012-12-04 10:00:54 UTC (rev 136492)
+++ trunk/Source/WebCore/inspector/InspectorPageAgent.h	2012-12-04 10:25:44 UTC (rev 136493)
@@ -120,6 +120,7 @@
     virtual void clearDeviceOrientationOverride(ErrorString*);
     virtual void canOverrideDeviceOrientation(ErrorString*, bool*);
     virtual void setTouchEmulationEnabled(ErrorString*, bool);
+    virtual void setEmulatedMedia(ErrorString*, const String&);
     virtual void getCompositingBordersVisible(ErrorString*, bool* out_param);
     virtual void setCompositingBordersVisible(ErrorString*, bool);
 
@@ -138,6 +139,7 @@
     void loaderDetachedFromFrame(DocumentLoader*);
     void applyScreenWidthOverride(long*);
     void applyScreenHeightOverride(long*);
+    void applyEmulatedMedia(String*);
     void didPaint(GraphicsContext*, const LayoutRect&);
     void didLayout();
     void didScroll();

Modified: trunk/Source/WebCore/inspector/front-end/CSSStyleModel.js (136492 => 136493)


--- trunk/Source/WebCore/inspector/front-end/CSSStyleModel.js	2012-12-04 10:00:54 UTC (rev 136492)
+++ trunk/Source/WebCore/inspector/front-end/CSSStyleModel.js	2012-12-04 10:25:44 UTC (rev 136493)
@@ -77,6 +77,8 @@
     RegionLayoutUpdated: "RegionLayoutUpdated"
 }
 
+WebInspector.CSSStyleModel.MediaTypes = ["all", "braille", "embossed", "handheld", "print", "projection", "screen", "speech", "tty", "tv"];
+
 WebInspector.CSSStyleModel.prototype = {
     /**
      * @param {DOMAgent.NodeId} nodeId

Modified: trunk/Source/WebCore/inspector/front-end/OverridesView.js (136492 => 136493)


--- trunk/Source/WebCore/inspector/front-end/OverridesView.js	2012-12-04 10:00:54 UTC (rev 136492)
+++ trunk/Source/WebCore/inspector/front-end/OverridesView.js	2012-12-04 10:25:44 UTC (rev 136493)
@@ -60,6 +60,7 @@
     if (Capabilities.canOverrideDeviceOrientation)
         appendBlockTo(container, this._createDeviceOrientationOverrideControl());
     appendBlockTo(container, this._createCheckboxSetting(WebInspector.UIString("Emulate touch events"), WebInspector.settings.emulateTouchEvents));
+    appendBlockTo(container, this._createMediaEmulationElement());
 
     this._statusElement = document.createElement("span");
     this._statusElement.textContent = WebInspector.UIString("Overrides");
@@ -594,5 +595,50 @@
         return fieldsetElement;
     },
 
+    _createMediaEmulationElement: function()
+    {
+        const p = document.createElement("p");
+        const labelElement = p.createChild("label");
+        const checkboxElement = labelElement.createChild("input");
+        checkboxElement.type = "checkbox";
+        checkboxElement.checked = false;
+        this._metricsCheckboxElement = checkboxElement;
+        labelElement.appendChild(document.createTextNode(WebInspector.UIString("Emulate CSS media")));
+
+        var mediaSelectElement = p.createChild("select");
+        var mediaTypes = WebInspector.CSSStyleModel.MediaTypes;
+        var defaultMedia = WebInspector.settings.emulatedCSSMedia.get();
+        for (var i = 0; i < mediaTypes.length; ++i) {
+            var mediaType = mediaTypes[i];
+            if (mediaType === "all") {
+                // "all" is not a device-specific media type.
+                continue;
+            }
+            var option = document.createElement("option");
+            option.text = mediaType;
+            option.value = mediaType;
+            mediaSelectElement.add(option);
+            if (mediaType === defaultMedia)
+                mediaSelectElement.selectedIndex = mediaSelectElement.options.length - 1;
+        }
+        mediaSelectElement.disabled = true;
+        var boundListener = this._emulateMediaChanged.bind(this, checkboxElement, mediaSelectElement);
+        checkboxElement.addEventListener("click", boundListener, false);
+        mediaSelectElement.addEventListener("change", boundListener, false);
+        return p;
+    },
+
+    _emulateMediaChanged: function(checkbox, select)
+    {
+        select.disabled = !checkbox.checked;
+        if (checkbox.checked) {
+            var media = select.options[select.selectedIndex].value;
+            WebInspector.settings.emulatedCSSMedia.set(media);
+            PageAgent.setEmulatedMedia(media);
+        } else
+            PageAgent.setEmulatedMedia("");
+        WebInspector.cssModel.mediaQueryResultChanged();
+    },
+
     __proto__: WebInspector.View.prototype
 }

Modified: trunk/Source/WebCore/inspector/front-end/Settings.js (136492 => 136493)


--- trunk/Source/WebCore/inspector/front-end/Settings.js	2012-12-04 10:00:54 UTC (rev 136492)
+++ trunk/Source/WebCore/inspector/front-end/Settings.js	2012-12-04 10:25:44 UTC (rev 136493)
@@ -113,8 +113,8 @@
     this.cssReloadTimeout = this.createSetting("cssReloadTimeout", 1000);
     this.showCpuOnTimelineRuler = this.createSetting("showCpuOnTimelineRuler", false);
     this.showMetricsRulers = this.createSetting("showMetricsRulers", false);
+    this.emulatedCSSMedia = this.createSetting("emulatedCSSMedia", "print");
 
-
     // If there are too many breakpoints in a storage, it is likely due to a recent bug that caused
     // periodical breakpoints duplication leading to inspector slowness.
     if (this.breakpoints.get().length > 500000)

Modified: trunk/Source/WebCore/page/FrameView.cpp (136492 => 136493)


--- trunk/Source/WebCore/page/FrameView.cpp	2012-12-04 10:00:54 UTC (rev 136492)
+++ trunk/Source/WebCore/page/FrameView.cpp	2012-12-04 10:25:44 UTC (rev 136493)
@@ -1335,6 +1335,7 @@
 {
     // See if we have an override type.
     String overrideType = m_frame->loader()->client()->overrideMediaType();
+    InspectorInstrumentation::applyEmulatedMedia(m_frame.get(), &overrideType);
     if (!overrideType.isNull())
         return overrideType;
     return m_mediaType;
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to