Need to clear viewport dependent units when switching print mode.

Switching between print and screen changes the size of the initial
containing block. Viewport dependent lengths need to be recalculated
and the cache for matched properties cleared.

Normally, notifyResizeForViewportUnits() is called from
performPreLayoutTasks when the initial containing block size changes.
That does not happen when laying out for printing and going back to
screen layout. We skip setting m_lastLayoutSize in
sendResizeEventIfNeeded. We probably do that to avoid triggering resize
events going back and forth between the print (preview) size.

Make sure we clear the matched properties cache from StyleResolver when
updating the media type.

R=mstensho@opera.com
BUG=677799

Review-Url: https://codereview.chromium.org/2613733003
Cr-Commit-Position: refs/heads/master@{#441642}
diff --git a/third_party/WebKit/Source/core/css/resolver/StyleResolver.cpp b/third_party/WebKit/Source/core/css/resolver/StyleResolver.cpp
index ccb0e1d..0fd4216e 100644
--- a/third_party/WebKit/Source/core/css/resolver/StyleResolver.cpp
+++ b/third_party/WebKit/Source/core/css/resolver/StyleResolver.cpp
@@ -1925,8 +1925,11 @@
 
 void StyleResolver::updateMediaType() {
   if (FrameView* view = document().view()) {
+    bool wasPrint = m_printMediaType;
     m_printMediaType =
         equalIgnoringCase(view->mediaType(), MediaTypeNames::print);
+    if (wasPrint != m_printMediaType)
+      m_matchedPropertiesCache.clearViewportDependent();
   }
 }
 
diff --git a/third_party/WebKit/Source/web/tests/WebViewTest.cpp b/third_party/WebKit/Source/web/tests/WebViewTest.cpp
index f190ac91..45e41f1 100644
--- a/third_party/WebKit/Source/web/tests/WebViewTest.cpp
+++ b/third_party/WebKit/Source/web/tests/WebViewTest.cpp
@@ -89,6 +89,7 @@
 #include "public/web/WebFrameContentDumper.h"
 #include "public/web/WebHitTestResult.h"
 #include "public/web/WebInputMethodController.h"
+#include "public/web/WebPrintParams.h"
 #include "public/web/WebScriptSource.h"
 #include "public/web/WebSettings.h"
 #include "public/web/WebTreeScopeType.h"
@@ -4299,4 +4300,33 @@
             *devToolsEmulator->visibleContentRectForPainting());
 }
 
+TEST_P(WebViewTest, ResizeForPrintingViewportUnits) {
+  WebViewImpl* webView = m_webViewHelper.initialize();
+  webView->resize(WebSize(800, 600));
+
+  WebURL baseURL = URLTestHelpers::toKURL("http://example.com/");
+  FrameTestHelpers::loadHTMLString(webView->mainFrame(),
+                                   "<style>#vw { width: 100vw }</style>"
+                                   "<div id=vw></div>",
+                                   baseURL);
+
+  WebLocalFrameImpl* frame = webView->mainFrameImpl();
+  Document* document = frame->frame()->document();
+  Element* vwElement = document->getElementById("vw");
+
+  EXPECT_EQ(800, vwElement->offsetWidth());
+
+  WebPrintParams printParams;
+  printParams.printContentArea.width = 500;
+  printParams.printContentArea.height = 500;
+
+  frame->printBegin(printParams, WebNode());
+  webView->resize(WebSize(500, 500));
+  EXPECT_EQ(500, vwElement->offsetWidth());
+
+  webView->resize(WebSize(800, 600));
+  frame->printEnd();
+  EXPECT_EQ(800, vwElement->offsetWidth());
+}
+
 }  // namespace blink