From eb7104cb850bad7ba571d3d50cd95e9e65585ddf Mon Sep 17 00:00:00 2001 From: Matthew Horan Date: Sun, 13 Jan 2013 17:19:46 -0500 Subject: [PATCH] Restore viewport dimensions after rendering screenshots Fixes #388. --- spec/driver_rendering_spec.rb | 28 ++++++++++++++++++++++++---- src/Render.cpp | 3 +-- src/WebPage.cpp | 3 ++- src/WebPage.h | 2 +- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/spec/driver_rendering_spec.rb b/spec/driver_rendering_spec.rb index de8431e..351fb65 100644 --- a/spec/driver_rendering_spec.rb +++ b/spec/driver_rendering_spec.rb @@ -43,7 +43,7 @@ describe Capybara::Webkit::Driver, "rendering an image" do end it "height should be at least 10px" do - @image[:height].should >= 10 + @image[:height].should be >= 10 end end @@ -55,7 +55,12 @@ describe Capybara::Webkit::Driver, "rendering an image" do end it "height should match the height given" do - @image[:height].should > 10 + @image[:height].should == 400 + end + + it "should reset window dimensions to their default value" do + driver.evaluate_script('window.innerWidth').should == 1680 + driver.evaluate_script('window.innerHeight').should == 1050 end end @@ -63,11 +68,26 @@ describe Capybara::Webkit::Driver, "rendering an image" do before { render(:width => 50, :height => 10) } it "width should be greater than the width given" do - @image[:width].should > 50 + @image[:width].should be > 50 end it "height should be greater than the height given" do - @image[:height].should > 10 + @image[:height].should be > 10 + end + + it "should restore viewport dimensions after rendering" do + driver.evaluate_script('window.innerWidth').should == 1680 + driver.evaluate_script('window.innerHeight').should == 1050 + end + end + + context "with a custom viewport size" do + before { driver.resize_window(800, 600) } + + it "should restore viewport dimensions after rendering" do + render({}) + driver.evaluate_script('window.innerWidth').should == 800 + driver.evaluate_script('window.innerHeight').should == 600 end end end diff --git a/src/Render.cpp b/src/Render.cpp index c682bf3..181873b 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -11,9 +11,8 @@ void Render::start() { int height = arguments()[2].toInt(); QSize size(width, height); - page()->setViewportSize(size); - bool result = page()->render( imagePath ); + bool result = page()->render( imagePath, size ); emitFinished(result); } diff --git a/src/WebPage.cpp b/src/WebPage.cpp index cf6d47f..54babdd 100644 --- a/src/WebPage.cpp +++ b/src/WebPage.cpp @@ -193,12 +193,13 @@ QString WebPage::failureString() { return message + m_errorPageMessage; } -bool WebPage::render(const QString &fileName) { +bool WebPage::render(const QString &fileName, const QSize &minimumSize) { QFileInfo fileInfo(fileName); QDir dir; dir.mkpath(fileInfo.absolutePath()); QSize viewportSize = this->viewportSize(); + this->setViewportSize(minimumSize); QSize pageSize = this->mainFrame()->contentsSize(); if (pageSize.isEmpty()) { return false; diff --git a/src/WebPage.h b/src/WebPage.h index d470391..1460aac 100644 --- a/src/WebPage.h +++ b/src/WebPage.h @@ -20,7 +20,7 @@ class WebPage : public QWebPage { void setPromptText(QString action); int getLastStatus(); void setCustomNetworkAccessManager(); - bool render(const QString &fileName); + bool render(const QString &fileName, const QSize &minimumSize); virtual bool extension (Extension extension, const ExtensionOption *option=0, ExtensionReturn *output=0); void setSkipImageLoading(bool skip); QVariantList consoleMessages();