OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/callback.h" | 5 #include "base/callback.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "net/url_request/url_request.h" | 7 #include "net/url_request/url_request.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "webkit/appcache/appcache.h" | 9 #include "webkit/appcache/appcache.h" |
10 #include "webkit/appcache/appcache_backend_impl.h" | 10 #include "webkit/appcache/appcache_backend_impl.h" |
11 #include "webkit/appcache/appcache_group.h" | 11 #include "webkit/appcache/appcache_group.h" |
12 #include "webkit/appcache/appcache_host.h" | 12 #include "webkit/appcache/appcache_host.h" |
| 13 #include "webkit/appcache/mock_appcache_policy.h" |
13 #include "webkit/appcache/mock_appcache_service.h" | 14 #include "webkit/appcache/mock_appcache_service.h" |
14 #include "webkit/quota/quota_manager.h" | 15 #include "webkit/quota/quota_manager.h" |
15 | 16 |
16 namespace appcache { | 17 namespace appcache { |
17 | 18 |
18 class AppCacheHostTest : public testing::Test { | 19 class AppCacheHostTest : public testing::Test { |
19 public: | 20 public: |
20 AppCacheHostTest() { | 21 AppCacheHostTest() { |
21 get_status_callback_.reset( | 22 get_status_callback_.reset( |
22 NewCallback(this, &AppCacheHostTest::GetStatusCallback)); | 23 NewCallback(this, &AppCacheHostTest::GetStatusCallback)); |
23 start_update_callback_.reset( | 24 start_update_callback_.reset( |
24 NewCallback(this, &AppCacheHostTest::StartUpdateCallback)); | 25 NewCallback(this, &AppCacheHostTest::StartUpdateCallback)); |
25 swap_cache_callback_.reset( | 26 swap_cache_callback_.reset( |
26 NewCallback(this, &AppCacheHostTest::SwapCacheCallback)); | 27 NewCallback(this, &AppCacheHostTest::SwapCacheCallback)); |
27 } | 28 } |
28 | 29 |
29 class MockFrontend : public AppCacheFrontend { | 30 class MockFrontend : public AppCacheFrontend { |
30 public: | 31 public: |
31 MockFrontend() | 32 MockFrontend() |
32 : last_host_id_(-222), last_cache_id_(-222), | 33 : last_host_id_(-222), last_cache_id_(-222), |
33 last_status_(appcache::OBSOLETE), | 34 last_status_(appcache::OBSOLETE), |
34 last_status_changed_(appcache::OBSOLETE), | 35 last_status_changed_(appcache::OBSOLETE), |
35 last_event_id_(appcache::OBSOLETE_EVENT) { | 36 last_event_id_(appcache::OBSOLETE_EVENT), |
| 37 content_blocked_(false) { |
36 } | 38 } |
37 | 39 |
38 virtual void OnCacheSelected( | 40 virtual void OnCacheSelected( |
39 int host_id, const appcache::AppCacheInfo& info) { | 41 int host_id, const appcache::AppCacheInfo& info) { |
40 last_host_id_ = host_id; | 42 last_host_id_ = host_id; |
41 last_cache_id_ = info.cache_id; | 43 last_cache_id_ = info.cache_id; |
42 last_status_ = info.status; | 44 last_status_ = info.status; |
43 } | 45 } |
44 | 46 |
45 virtual void OnStatusChanged(const std::vector<int>& host_ids, | 47 virtual void OnStatusChanged(const std::vector<int>& host_ids, |
(...skipping 15 matching lines...) Expand all Loading... |
61 const GURL& url, | 63 const GURL& url, |
62 int num_total, int num_complete) { | 64 int num_total, int num_complete) { |
63 last_event_id_ = PROGRESS_EVENT; | 65 last_event_id_ = PROGRESS_EVENT; |
64 } | 66 } |
65 | 67 |
66 virtual void OnLogMessage(int host_id, appcache::LogLevel log_level, | 68 virtual void OnLogMessage(int host_id, appcache::LogLevel log_level, |
67 const std::string& message) { | 69 const std::string& message) { |
68 } | 70 } |
69 | 71 |
70 virtual void OnContentBlocked(int host_id, const GURL& manifest_url) { | 72 virtual void OnContentBlocked(int host_id, const GURL& manifest_url) { |
| 73 content_blocked_ = true; |
71 } | 74 } |
72 | 75 |
73 int last_host_id_; | 76 int last_host_id_; |
74 int64 last_cache_id_; | 77 int64 last_cache_id_; |
75 appcache::Status last_status_; | 78 appcache::Status last_status_; |
76 appcache::Status last_status_changed_; | 79 appcache::Status last_status_changed_; |
77 appcache::EventID last_event_id_; | 80 appcache::EventID last_event_id_; |
| 81 bool content_blocked_; |
78 }; | 82 }; |
79 | 83 |
80 class MockQuotaManagerProxy : public quota::QuotaManagerProxy { | 84 class MockQuotaManagerProxy : public quota::QuotaManagerProxy { |
81 public: | 85 public: |
82 MockQuotaManagerProxy() : QuotaManagerProxy(NULL, NULL) {} | 86 MockQuotaManagerProxy() : QuotaManagerProxy(NULL, NULL) {} |
83 | 87 |
84 // Not needed for our tests. | 88 // Not needed for our tests. |
85 virtual void RegisterClient(quota::QuotaClient* client) {} | 89 virtual void RegisterClient(quota::QuotaClient* client) {} |
86 virtual void NotifyStorageAccessed(quota::QuotaClient::ID client_id, | 90 virtual void NotifyStorageAccessed(quota::QuotaClient::ID client_id, |
87 const GURL& origin, | 91 const GURL& origin, |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 EXPECT_EQ(kNoCacheId, mock_frontend_.last_cache_id_); | 429 EXPECT_EQ(kNoCacheId, mock_frontend_.last_cache_id_); |
426 EXPECT_EQ(UNCACHED, mock_frontend_.last_status_); | 430 EXPECT_EQ(UNCACHED, mock_frontend_.last_status_); |
427 | 431 |
428 // Simulate the parent being torn down. | 432 // Simulate the parent being torn down. |
429 backend_impl.UnregisterHost(kParentHostId); | 433 backend_impl.UnregisterHost(kParentHostId); |
430 parent_host = NULL; | 434 parent_host = NULL; |
431 EXPECT_EQ(NULL, backend_impl.GetHost(kParentHostId)); | 435 EXPECT_EQ(NULL, backend_impl.GetHost(kParentHostId)); |
432 EXPECT_EQ(NULL, worker_host->GetParentAppCacheHost()); | 436 EXPECT_EQ(NULL, worker_host->GetParentAppCacheHost()); |
433 } | 437 } |
434 | 438 |
| 439 TEST_F(AppCacheHostTest, SelectCacheAllowed) { |
| 440 scoped_refptr<MockQuotaManagerProxy> mock_quota_proxy( |
| 441 new MockQuotaManagerProxy); |
| 442 MockAppCachePolicy mock_appcache_policy; |
| 443 mock_appcache_policy.can_create_return_value_ = true; |
| 444 service_.set_quota_manager_proxy(mock_quota_proxy); |
| 445 service_.set_appcache_policy(&mock_appcache_policy); |
| 446 |
| 447 // Reset our mock frontend |
| 448 mock_frontend_.last_cache_id_ = -333; |
| 449 mock_frontend_.last_host_id_ = -333; |
| 450 mock_frontend_.last_status_ = OBSOLETE; |
| 451 mock_frontend_.last_event_id_ = OBSOLETE_EVENT; |
| 452 mock_frontend_.content_blocked_ = false; |
| 453 |
| 454 const GURL kDocAndOriginUrl(GURL("http://whatever/").GetOrigin()); |
| 455 const GURL kManifestUrl(GURL("http://whatever/cache.manifest")); |
| 456 { |
| 457 AppCacheHost host(1, &mock_frontend_, &service_); |
| 458 host.first_party_url_ = kDocAndOriginUrl; |
| 459 host.SelectCache(kDocAndOriginUrl, kNoCacheId, kManifestUrl); |
| 460 EXPECT_EQ(1, mock_quota_proxy->GetInUseCount(kDocAndOriginUrl)); |
| 461 |
| 462 // MockAppCacheService::LoadOrCreateGroup is asynchronous, so we shouldn't |
| 463 // have received an OnCacheSelected msg yet. |
| 464 EXPECT_EQ(-333, mock_frontend_.last_host_id_); |
| 465 EXPECT_EQ(-333, mock_frontend_.last_cache_id_); |
| 466 EXPECT_EQ(OBSOLETE, mock_frontend_.last_status_); |
| 467 // No error events either |
| 468 EXPECT_EQ(OBSOLETE_EVENT, mock_frontend_.last_event_id_); |
| 469 EXPECT_FALSE(mock_frontend_.content_blocked_); |
| 470 |
| 471 EXPECT_TRUE(host.is_selection_pending()); |
| 472 } |
| 473 EXPECT_EQ(0, mock_quota_proxy->GetInUseCount(kDocAndOriginUrl)); |
| 474 service_.set_quota_manager_proxy(NULL); |
| 475 } |
| 476 |
| 477 TEST_F(AppCacheHostTest, SelectCacheBlocked) { |
| 478 scoped_refptr<MockQuotaManagerProxy> mock_quota_proxy( |
| 479 new MockQuotaManagerProxy); |
| 480 MockAppCachePolicy mock_appcache_policy; |
| 481 mock_appcache_policy.can_create_return_value_ = false; |
| 482 service_.set_quota_manager_proxy(mock_quota_proxy); |
| 483 service_.set_appcache_policy(&mock_appcache_policy); |
| 484 |
| 485 // Reset our mock frontend |
| 486 mock_frontend_.last_cache_id_ = -333; |
| 487 mock_frontend_.last_host_id_ = -333; |
| 488 mock_frontend_.last_status_ = OBSOLETE; |
| 489 mock_frontend_.last_event_id_ = OBSOLETE_EVENT; |
| 490 mock_frontend_.content_blocked_ = false; |
| 491 |
| 492 const GURL kDocAndOriginUrl(GURL("http://whatever/").GetOrigin()); |
| 493 const GURL kManifestUrl(GURL("http://whatever/cache.manifest")); |
| 494 { |
| 495 AppCacheHost host(1, &mock_frontend_, &service_); |
| 496 host.first_party_url_ = kDocAndOriginUrl; |
| 497 host.SelectCache(kDocAndOriginUrl, kNoCacheId, kManifestUrl); |
| 498 EXPECT_EQ(1, mock_quota_proxy->GetInUseCount(kDocAndOriginUrl)); |
| 499 |
| 500 // We should have received an OnCacheSelected msg |
| 501 EXPECT_EQ(1, mock_frontend_.last_host_id_); |
| 502 EXPECT_EQ(kNoCacheId, mock_frontend_.last_cache_id_); |
| 503 EXPECT_EQ(UNCACHED, mock_frontend_.last_status_); |
| 504 |
| 505 // Also, an error event was raised |
| 506 EXPECT_EQ(ERROR_EVENT, mock_frontend_.last_event_id_); |
| 507 EXPECT_TRUE(mock_frontend_.content_blocked_); |
| 508 |
| 509 // Otherwise, see that it respond as if there is no cache selected. |
| 510 EXPECT_EQ(1, host.host_id()); |
| 511 EXPECT_EQ(&service_, host.service()); |
| 512 EXPECT_EQ(&mock_frontend_, host.frontend()); |
| 513 EXPECT_EQ(NULL, host.associated_cache()); |
| 514 EXPECT_FALSE(host.is_selection_pending()); |
| 515 EXPECT_TRUE(host.preferred_manifest_url().is_empty()); |
| 516 } |
| 517 EXPECT_EQ(0, mock_quota_proxy->GetInUseCount(kDocAndOriginUrl)); |
| 518 service_.set_quota_manager_proxy(NULL); |
| 519 } |
| 520 |
435 } // namespace appcache | 521 } // namespace appcache |
436 | |
OLD | NEW |