summaryrefslogtreecommitdiffstats
path: root/webkit/appcache/appcache_update_job_unittest.cc
diff options
context:
space:
mode:
authormichaeln@chromium.org <michaeln@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-17 22:20:36 +0000
committermichaeln@chromium.org <michaeln@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-17 22:20:36 +0000
commitea776d0208899b770ea386b994216ef476197a46 (patch)
treecb614ced5bd6e764e01f244813d07b0a48870a0a /webkit/appcache/appcache_update_job_unittest.cc
parenta3d3a7cf3b81cc8d00490bb7bfd92b57f0146d9c (diff)
downloadchromium_src-ea776d0208899b770ea386b994216ef476197a46.zip
chromium_src-ea776d0208899b770ea386b994216ef476197a46.tar.gz
chromium_src-ea776d0208899b770ea386b994216ef476197a46.tar.bz2
Introduce an AppCachePolicy interface that allows the containing browser to determine appcache permissions. The policy can allow or deny loading existing manifests or the creation of new manifests. The policy check for creating new manifests can be async to allow for a user prompt.
BUG=none TEST=new unit tests added Review URL: http://codereview.chromium.org/565042 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@39280 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/appcache/appcache_update_job_unittest.cc')
-rw-r--r--webkit/appcache/appcache_update_job_unittest.cc105
1 files changed, 103 insertions, 2 deletions
diff --git a/webkit/appcache/appcache_update_job_unittest.cc b/webkit/appcache/appcache_update_job_unittest.cc
index d41e486..42efbff 100644
--- a/webkit/appcache/appcache_update_job_unittest.cc
+++ b/webkit/appcache/appcache_update_job_unittest.cc
@@ -7,10 +7,12 @@
#include "base/stl_util-inl.h"
#include "base/thread.h"
#include "base/waitable_event.h"
+#include "net/base/net_errors.h"
#include "net/url_request/url_request_test_job.h"
#include "net/url_request/url_request_unittest.h"
#include "webkit/appcache/appcache_group.h"
#include "webkit/appcache/appcache_host.h"
+#include "webkit/appcache/appcache_policy.h"
#include "webkit/appcache/appcache_response.h"
#include "webkit/appcache/appcache_update_job.h"
#include "webkit/appcache/mock_appcache_service.h"
@@ -276,6 +278,43 @@ bool HttpHeadersRequestTestJob::already_checked_ = false;
class AppCacheUpdateJobTest : public testing::Test,
public AppCacheGroup::UpdateObserver {
public:
+ class MockAppCachePolicy : public AppCachePolicy {
+ public:
+ MockAppCachePolicy()
+ : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)),
+ can_create_return_value_(net::OK), return_immediately_(true),
+ callback_(NULL) {
+ }
+
+ virtual bool CanLoadAppCache(const GURL& manifest_url) {
+ return true;
+ }
+
+ virtual int CanCreateAppCache(const GURL& manifest_url,
+ net::CompletionCallback* callback) {
+ requested_manifest_url_ = manifest_url;
+ callback_ = callback;
+ if (return_immediately_)
+ return can_create_return_value_;
+
+ MessageLoop::current()->PostTask(FROM_HERE,
+ method_factory_.NewRunnableMethod(
+ &MockAppCachePolicy::CompleteCanCreateAppCache));
+ return net::ERR_IO_PENDING;
+ }
+
+ void CompleteCanCreateAppCache() {
+ callback_->Run(can_create_return_value_);
+ }
+
+ ScopedRunnableMethodFactory<MockAppCachePolicy> method_factory_;
+ int can_create_return_value_;
+ bool return_immediately_;
+ GURL requested_manifest_url_;
+ net::CompletionCallback* callback_;
+ };
+
+
AppCacheUpdateJobTest()
: ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)),
do_checks_after_update_finished_(false),
@@ -355,6 +394,52 @@ class AppCacheUpdateJobTest : public testing::Test,
UpdateFinished();
}
+ void ImmediatelyBlockCacheAttemptTest() {
+ BlockCacheAttemptTest(true);
+ }
+
+ void DelayedBlockCacheAttemptTest() {
+ BlockCacheAttemptTest(false);
+ }
+
+ void BlockCacheAttemptTest(bool immediately) {
+ ASSERT_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type());
+
+ GURL manifest_url = GURL("http://failme");
+
+ // Setup to block the cache attempt immediately.
+ policy_.return_immediately_ = immediately;
+ policy_.can_create_return_value_ = net::ERR_ACCESS_DENIED;
+
+ MakeService();
+ group_ = new AppCacheGroup(service_.get(), manifest_url,
+ service_->storage()->NewGroupId());
+
+ AppCacheUpdateJob* update = new AppCacheUpdateJob(service_.get(), group_);
+ group_->update_job_ = update;
+
+ MockFrontend mock_frontend;
+ AppCacheHost host(1, &mock_frontend, service_.get());
+
+ update->StartUpdate(&host, GURL());
+ EXPECT_EQ(manifest_url, policy_.requested_manifest_url_);
+
+ // Verify state.
+ EXPECT_EQ(AppCacheUpdateJob::CACHE_ATTEMPT, update->update_type_);
+ EXPECT_EQ(AppCacheUpdateJob::FETCH_MANIFEST, update->internal_state_);
+ EXPECT_EQ(AppCacheGroup::CHECKING, group_->update_status());
+
+ // Verify notifications.
+ MockFrontend::RaisedEvents& events = mock_frontend.raised_events_;
+ size_t expected = 1;
+ EXPECT_EQ(expected, events.size());
+ EXPECT_EQ(1U, events[0].first.size());
+ EXPECT_EQ(host.host_id(), events[0].first[0]);
+ EXPECT_EQ(CHECKING_EVENT, events[0].second);
+
+ WaitForUpdateToFinish();
+ }
+
void StartUpgradeAttemptTest() {
ASSERT_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type());
@@ -727,9 +812,15 @@ class AppCacheUpdateJobTest : public testing::Test,
void BasicCacheAttemptSuccessTest() {
ASSERT_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type());
+ GURL manifest_url = http_server_->TestServerPage("files/manifest1");
+
+ // We also test the async AppCachePolicy return path in this test case.
+ policy_.return_immediately_ = false;
+ policy_.can_create_return_value_ = net::OK;
+
MakeService();
group_ = new AppCacheGroup(
- service_.get(), http_server_->TestServerPage("files/manifest1"),
+ service_.get(), manifest_url,
service_->storage()->NewGroupId());
AppCacheUpdateJob* update = new AppCacheUpdateJob(service_.get(), group_);
group_->update_job_ = update;
@@ -737,7 +828,7 @@ class AppCacheUpdateJobTest : public testing::Test,
MockFrontend* frontend = MakeMockFrontend();
AppCacheHost* host = MakeHost(1, frontend);
update->StartUpdate(host, GURL());
- EXPECT_TRUE(update->manifest_url_request_ != NULL);
+ EXPECT_EQ(manifest_url, policy_.requested_manifest_url_);;
// Set up checks for when update job finishes.
do_checks_after_update_finished_ = true;
@@ -2485,6 +2576,7 @@ class AppCacheUpdateJobTest : public testing::Test,
void MakeService() {
service_.reset(new MockAppCacheService());
service_->set_request_context(request_context_);
+ service_->set_appcache_policy(&policy_);
}
AppCache* MakeCacheForGroup(int64 cache_id, int64 manifest_response_id) {
@@ -2777,6 +2869,7 @@ class AppCacheUpdateJobTest : public testing::Test,
scoped_refptr<AppCacheGroup> group_;
scoped_refptr<AppCache> protect_newest_cache_;
scoped_ptr<base::WaitableEvent> event_;
+ MockAppCachePolicy policy_;
scoped_ptr<AppCacheResponseWriter> response_writer_;
scoped_ptr<net::CompletionCallbackImpl<AppCacheUpdateJobTest> >
@@ -2873,6 +2966,14 @@ TEST_F(AppCacheUpdateJobTest, StartCacheAttempt) {
RunTestOnIOThread(&AppCacheUpdateJobTest::StartCacheAttemptTest);
}
+TEST_F(AppCacheUpdateJobTest, ImmediatelyBlockCacheAttemptTest) {
+ RunTestOnIOThread(&AppCacheUpdateJobTest::ImmediatelyBlockCacheAttemptTest);
+}
+
+TEST_F(AppCacheUpdateJobTest, DelayedBlockCacheAttemptTest) {
+ RunTestOnIOThread(&AppCacheUpdateJobTest::DelayedBlockCacheAttemptTest);
+}
+
TEST_F(AppCacheUpdateJobTest, StartUpgradeAttempt) {
RunTestOnIOThread(&AppCacheUpdateJobTest::StartUpgradeAttemptTest);
}