From 567ba92147f755fef14c53d1b88928aa00f5798f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petteri=20R=C3=A4ty?= Date: Wed, 1 May 2013 17:54:32 +0300 Subject: [PATCH 1/3] Initial Application Cache Support Tests passing for a simple application cache support. To be added is better handling of the tmp path and resetting testing at least. --- spec/driver_spec.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++ src/WebPage.cpp | 2 ++ 2 files changed, 49 insertions(+) diff --git a/spec/driver_spec.rb b/spec/driver_spec.rb index ecc62be..0d30e51 100644 --- a/spec/driver_spec.rb +++ b/spec/driver_spec.rb @@ -1763,6 +1763,53 @@ describe Capybara::Webkit::Driver do end end + context "offline application cache" do + let(:driver) do + @visited = [] + visited = @visited + + driver_for_app do + get '/8d853f09-4275-409d-954d-ebbf6e2ce732' do + content_type 'text/cache-manifest' + visited << true + <<-TEXT +CACHE MANIFEST + TEXT + end + + # UUID urls so that this gets isolated from other tests + get '/f8742c39-8bef-4196-b1c3-80f8a3d65f3e' do + <<-HTML + + + + + + + + HTML + end + end + end + + before { visit("/f8742c39-8bef-4196-b1c3-80f8a3d65f3e") } + + it "has proper state available" do + driver.find_xpath("//*[@id='state']").first.visible_text.should == '0' + sleep 1 + @visited.should eq([true]), 'manifest was not downloaded' + driver.find_xpath("//*[@id='finished']").first.visible_text.should == 'cached' + end + end + context "form app with server-side handler" do let(:driver) do driver_for_app do diff --git a/src/WebPage.cpp b/src/WebPage.cpp index 585dad6..7dd4e11 100644 --- a/src/WebPage.cpp +++ b/src/WebPage.cpp @@ -40,6 +40,8 @@ WebPage::WebPage(WebPageManager *manager, QObject *parent) : QWebPage(parent) { settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, true); settings()->setAttribute(QWebSettings::JavascriptCanCloseWindows, true); settings()->setAttribute(QWebSettings::LocalStorageDatabaseEnabled, true); + settings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled, true); + settings()->setOfflineWebApplicationCachePath("tmp"); createWindow(); } From 51b68f34d32a95c94fc1896a0836720db3e2512f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petteri=20R=C3=A4ty?= Date: Thu, 2 May 2013 10:41:31 +0300 Subject: [PATCH 2/3] App Offline Cache driver.reset! support --- spec/driver_spec.rb | 26 ++++++++++++++++++++++++-- src/WebPageManager.cpp | 6 ++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/spec/driver_spec.rb b/spec/driver_spec.rb index 0d30e51..65978ce 100644 --- a/spec/driver_spec.rb +++ b/spec/driver_spec.rb @@ -1771,14 +1771,16 @@ describe Capybara::Webkit::Driver do driver_for_app do get '/8d853f09-4275-409d-954d-ebbf6e2ce732' do content_type 'text/cache-manifest' - visited << true + visited << 'manifest' <<-TEXT CACHE MANIFEST +/4aaffa31-f42d-403e-a19e-6b248d608087 TEXT end # UUID urls so that this gets isolated from other tests get '/f8742c39-8bef-4196-b1c3-80f8a3d65f3e' do + visited << 'complex' <<-HTML @@ -1797,6 +1799,16 @@ CACHE MANIFEST HTML end + + get '/4aaffa31-f42d-403e-a19e-6b248d608087' do + visited << 'simple' + <<-HTML + + + + + HTML + end end end @@ -1805,9 +1817,19 @@ CACHE MANIFEST it "has proper state available" do driver.find_xpath("//*[@id='state']").first.visible_text.should == '0' sleep 1 - @visited.should eq([true]), 'manifest was not downloaded' + @visited.should eq(['complex', 'manifest', 'simple']), 'files were not downloaded in expected order' driver.find_xpath("//*[@id='finished']").first.visible_text.should == 'cached' end + + it "is cleared on driver reset!" do + sleep 1 + @visited.should eq(['complex', 'manifest', 'simple']), 'files were not downloaded in expected order' + driver.reset! + @visited.clear + visit '/4aaffa31-f42d-403e-a19e-6b248d608087' + sleep 1 + @visited.should eq(['simple', 'manifest', 'simple']), 'simple action was used from cache instead of server' + end end context "form app with server-side handler" do diff --git a/src/WebPageManager.cpp b/src/WebPageManager.cpp index 54779af..4851521 100644 --- a/src/WebPageManager.cpp +++ b/src/WebPageManager.cpp @@ -124,6 +124,12 @@ void WebPageManager::reset() { WebPage *page = m_pages.takeFirst(); page->deleteLater(); } + + qint64 size = QWebSettings::offlineWebApplicationCacheQuota(); + // No public function was found to wrap the empty() call to + // WebCore::cacheStorage().empty() + QWebSettings::setOfflineWebApplicationCacheQuota(size); + createPage()->setFocus(); } From a9073703dd54e6902a2b1776722d0d177fcf50a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petteri=20R=C3=A4ty?= Date: Thu, 2 May 2013 10:54:09 +0300 Subject: [PATCH 3/3] Use offline app cache support when ./tmp/ exists In Rails projects it's a sure bet that tmp exists so this automatically covers most people. With the README addition other people know to create the directory. --- README.md | 7 +++++++ src/WebPage.cpp | 7 +++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b206dba..1d4984e 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,13 @@ If you're using capybara-webkit with Sinatra, don't forget to set Capybara.app = MySinatraApp.new ``` +Offline Application Cache +------------------------- + +The offline application cache needs a directory to write to for the cached files. Capybara-webkit +will look at if the working directory has a tmp directory and when it exists offline application +cache will be enabled. + Non-Standard Driver Methods --------------------------- diff --git a/src/WebPage.cpp b/src/WebPage.cpp index 7dd4e11..b506423 100644 --- a/src/WebPage.cpp +++ b/src/WebPage.cpp @@ -40,8 +40,11 @@ WebPage::WebPage(WebPageManager *manager, QObject *parent) : QWebPage(parent) { settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, true); settings()->setAttribute(QWebSettings::JavascriptCanCloseWindows, true); settings()->setAttribute(QWebSettings::LocalStorageDatabaseEnabled, true); - settings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled, true); - settings()->setOfflineWebApplicationCachePath("tmp"); + + if(QFileInfo("tmp").isDir()) { + settings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled, true); + settings()->setOfflineWebApplicationCachePath("tmp"); + } createWindow(); }