| 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/bind.h" |
| 6 #include "base/bind_helpers.h" |
| 6 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 7 #include "net/url_request/url_request.h" | 8 #include "net/url_request/url_request.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "webkit/appcache/appcache.h" | 10 #include "webkit/appcache/appcache.h" |
| 10 #include "webkit/appcache/appcache_backend_impl.h" | 11 #include "webkit/appcache/appcache_backend_impl.h" |
| 11 #include "webkit/appcache/appcache_group.h" | 12 #include "webkit/appcache/appcache_group.h" |
| 12 #include "webkit/appcache/appcache_host.h" | 13 #include "webkit/appcache/appcache_host.h" |
| 13 #include "webkit/appcache/mock_appcache_policy.h" | 14 #include "webkit/appcache/mock_appcache_policy.h" |
| 14 #include "webkit/appcache/mock_appcache_service.h" | 15 #include "webkit/appcache/mock_appcache_service.h" |
| 15 #include "webkit/quota/quota_manager.h" | 16 #include "webkit/quota/quota_manager.h" |
| 16 | 17 |
| 17 namespace appcache { | 18 namespace appcache { |
| 18 | 19 |
| 19 class AppCacheHostTest : public testing::Test { | 20 class AppCacheHostTest : public testing::Test { |
| 20 public: | 21 public: |
| 21 AppCacheHostTest() { | 22 AppCacheHostTest() { |
| 22 get_status_callback_.reset( | 23 get_status_callback_ = |
| 23 NewCallback(this, &AppCacheHostTest::GetStatusCallback)); | 24 base::Bind(&AppCacheHostTest::GetStatusCallback, |
| 24 start_update_callback_.reset( | 25 base::Unretained(this)); |
| 25 NewCallback(this, &AppCacheHostTest::StartUpdateCallback)); | 26 start_update_callback_ = |
| 26 swap_cache_callback_.reset( | 27 base::Bind(&AppCacheHostTest::StartUpdateCallback, |
| 27 NewCallback(this, &AppCacheHostTest::SwapCacheCallback)); | 28 base::Unretained(this)); |
| 29 swap_cache_callback_ = |
| 30 base::Bind(&AppCacheHostTest::SwapCacheCallback, |
| 31 base::Unretained(this)); |
| 28 } | 32 } |
| 29 | 33 |
| 30 class MockFrontend : public AppCacheFrontend { | 34 class MockFrontend : public AppCacheFrontend { |
| 31 public: | 35 public: |
| 32 MockFrontend() | 36 MockFrontend() |
| 33 : last_host_id_(-222), last_cache_id_(-222), | 37 : last_host_id_(-222), last_cache_id_(-222), |
| 34 last_status_(appcache::OBSOLETE), | 38 last_status_(appcache::OBSOLETE), |
| 35 last_status_changed_(appcache::OBSOLETE), | 39 last_status_changed_(appcache::OBSOLETE), |
| 36 last_event_id_(appcache::OBSOLETE_EVENT), | 40 last_event_id_(appcache::OBSOLETE_EVENT), |
| 37 content_blocked_(false) { | 41 content_blocked_(false) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 void SwapCacheCallback(bool result, void* param) { | 132 void SwapCacheCallback(bool result, void* param) { |
| 129 last_swap_result_ = result; | 133 last_swap_result_ = result; |
| 130 last_callback_param_ = param; | 134 last_callback_param_ = param; |
| 131 } | 135 } |
| 132 | 136 |
| 133 // Mock classes for the 'host' to work with | 137 // Mock classes for the 'host' to work with |
| 134 MockAppCacheService service_; | 138 MockAppCacheService service_; |
| 135 MockFrontend mock_frontend_; | 139 MockFrontend mock_frontend_; |
| 136 | 140 |
| 137 // Mock callbacks we expect to receive from the 'host' | 141 // Mock callbacks we expect to receive from the 'host' |
| 138 scoped_ptr<appcache::GetStatusCallback> get_status_callback_; | 142 appcache::GetStatusCallback get_status_callback_; |
| 139 scoped_ptr<appcache::StartUpdateCallback> start_update_callback_; | 143 appcache::StartUpdateCallback start_update_callback_; |
| 140 scoped_ptr<appcache::SwapCacheCallback> swap_cache_callback_; | 144 appcache::SwapCacheCallback swap_cache_callback_; |
| 141 | 145 |
| 142 Status last_status_result_; | 146 Status last_status_result_; |
| 143 bool last_swap_result_; | 147 bool last_swap_result_; |
| 144 bool last_start_result_; | 148 bool last_start_result_; |
| 145 void* last_callback_param_; | 149 void* last_callback_param_; |
| 146 }; | 150 }; |
| 147 | 151 |
| 148 TEST_F(AppCacheHostTest, Basic) { | 152 TEST_F(AppCacheHostTest, Basic) { |
| 149 // Construct a host and test what state it appears to be in. | 153 // Construct a host and test what state it appears to be in. |
| 150 AppCacheHost host(1, &mock_frontend_, &service_); | 154 AppCacheHost host(1, &mock_frontend_, &service_); |
| 151 EXPECT_EQ(1, host.host_id()); | 155 EXPECT_EQ(1, host.host_id()); |
| 152 EXPECT_EQ(&service_, host.service()); | 156 EXPECT_EQ(&service_, host.service()); |
| 153 EXPECT_EQ(&mock_frontend_, host.frontend()); | 157 EXPECT_EQ(&mock_frontend_, host.frontend()); |
| 154 EXPECT_EQ(NULL, host.associated_cache()); | 158 EXPECT_EQ(NULL, host.associated_cache()); |
| 155 EXPECT_FALSE(host.is_selection_pending()); | 159 EXPECT_FALSE(host.is_selection_pending()); |
| 156 | 160 |
| 157 // See that the callbacks are delivered immediately | 161 // See that the callbacks are delivered immediately |
| 158 // and respond as if there is no cache selected. | 162 // and respond as if there is no cache selected. |
| 159 last_status_result_ = OBSOLETE; | 163 last_status_result_ = OBSOLETE; |
| 160 host.GetStatusWithCallback(get_status_callback_.get(), | 164 host.GetStatusWithCallback(get_status_callback_, reinterpret_cast<void*>(1)); |
| 161 reinterpret_cast<void*>(1)); | |
| 162 EXPECT_EQ(UNCACHED, last_status_result_); | 165 EXPECT_EQ(UNCACHED, last_status_result_); |
| 163 EXPECT_EQ(reinterpret_cast<void*>(1), last_callback_param_); | 166 EXPECT_EQ(reinterpret_cast<void*>(1), last_callback_param_); |
| 164 | 167 |
| 165 last_start_result_ = true; | 168 last_start_result_ = true; |
| 166 host.StartUpdateWithCallback(start_update_callback_.get(), | 169 host.StartUpdateWithCallback(start_update_callback_, |
| 167 reinterpret_cast<void*>(2)); | 170 reinterpret_cast<void*>(2)); |
| 168 EXPECT_FALSE(last_start_result_); | 171 EXPECT_FALSE(last_start_result_); |
| 169 EXPECT_EQ(reinterpret_cast<void*>(2), last_callback_param_); | 172 EXPECT_EQ(reinterpret_cast<void*>(2), last_callback_param_); |
| 170 | 173 |
| 171 last_swap_result_ = true; | 174 last_swap_result_ = true; |
| 172 host.SwapCacheWithCallback(swap_cache_callback_.get(), | 175 host.SwapCacheWithCallback(swap_cache_callback_, reinterpret_cast<void*>(3)); |
| 173 reinterpret_cast<void*>(3)); | |
| 174 EXPECT_FALSE(last_swap_result_); | 176 EXPECT_FALSE(last_swap_result_); |
| 175 EXPECT_EQ(reinterpret_cast<void*>(3), last_callback_param_); | 177 EXPECT_EQ(reinterpret_cast<void*>(3), last_callback_param_); |
| 176 } | 178 } |
| 177 | 179 |
| 178 TEST_F(AppCacheHostTest, SelectNoCache) { | 180 TEST_F(AppCacheHostTest, SelectNoCache) { |
| 179 scoped_refptr<MockQuotaManagerProxy> mock_quota_proxy( | 181 scoped_refptr<MockQuotaManagerProxy> mock_quota_proxy( |
| 180 new MockQuotaManagerProxy); | 182 new MockQuotaManagerProxy); |
| 181 service_.set_quota_manager_proxy(mock_quota_proxy); | 183 service_.set_quota_manager_proxy(mock_quota_proxy); |
| 182 | 184 |
| 183 // Reset our mock frontend | 185 // Reset our mock frontend |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 const int kMockCacheId = 333; | 278 const int kMockCacheId = 333; |
| 277 | 279 |
| 278 // Put it in a state where we're waiting on a cache | 280 // Put it in a state where we're waiting on a cache |
| 279 // load prior to finishing cache selection. | 281 // load prior to finishing cache selection. |
| 280 host.pending_selected_cache_id_ = kMockCacheId; | 282 host.pending_selected_cache_id_ = kMockCacheId; |
| 281 EXPECT_TRUE(host.is_selection_pending()); | 283 EXPECT_TRUE(host.is_selection_pending()); |
| 282 | 284 |
| 283 // The callback should not occur until we finish cache selection. | 285 // The callback should not occur until we finish cache selection. |
| 284 last_status_result_ = OBSOLETE; | 286 last_status_result_ = OBSOLETE; |
| 285 last_callback_param_ = reinterpret_cast<void*>(-1); | 287 last_callback_param_ = reinterpret_cast<void*>(-1); |
| 286 host.GetStatusWithCallback(get_status_callback_.get(), | 288 host.GetStatusWithCallback(get_status_callback_, reinterpret_cast<void*>(1)); |
| 287 reinterpret_cast<void*>(1)); | |
| 288 EXPECT_EQ(OBSOLETE, last_status_result_); | 289 EXPECT_EQ(OBSOLETE, last_status_result_); |
| 289 EXPECT_EQ(reinterpret_cast<void*>(-1), last_callback_param_); | 290 EXPECT_EQ(reinterpret_cast<void*>(-1), last_callback_param_); |
| 290 | 291 |
| 291 // Satisfy the load with NULL, a failure. | 292 // Satisfy the load with NULL, a failure. |
| 292 host.OnCacheLoaded(NULL, kMockCacheId); | 293 host.OnCacheLoaded(NULL, kMockCacheId); |
| 293 | 294 |
| 294 // Cache selection should have finished | 295 // Cache selection should have finished |
| 295 EXPECT_FALSE(host.is_selection_pending()); | 296 EXPECT_FALSE(host.is_selection_pending()); |
| 296 EXPECT_EQ(1, mock_frontend_.last_host_id_); | 297 EXPECT_EQ(1, mock_frontend_.last_host_id_); |
| 297 EXPECT_EQ(kNoCacheId, mock_frontend_.last_cache_id_); | 298 EXPECT_EQ(kNoCacheId, mock_frontend_.last_cache_id_); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 308 const GURL kMockManifestUrl("http://foo.bar/baz"); | 309 const GURL kMockManifestUrl("http://foo.bar/baz"); |
| 309 | 310 |
| 310 // Put it in a state where we're waiting on a cache | 311 // Put it in a state where we're waiting on a cache |
| 311 // load prior to finishing cache selection. | 312 // load prior to finishing cache selection. |
| 312 host.pending_selected_manifest_url_ = kMockManifestUrl; | 313 host.pending_selected_manifest_url_ = kMockManifestUrl; |
| 313 EXPECT_TRUE(host.is_selection_pending()); | 314 EXPECT_TRUE(host.is_selection_pending()); |
| 314 | 315 |
| 315 // The callback should not occur until we finish cache selection. | 316 // The callback should not occur until we finish cache selection. |
| 316 last_status_result_ = OBSOLETE; | 317 last_status_result_ = OBSOLETE; |
| 317 last_callback_param_ = reinterpret_cast<void*>(-1); | 318 last_callback_param_ = reinterpret_cast<void*>(-1); |
| 318 host.GetStatusWithCallback(get_status_callback_.get(), | 319 host.GetStatusWithCallback(get_status_callback_, reinterpret_cast<void*>(1)); |
| 319 reinterpret_cast<void*>(1)); | |
| 320 EXPECT_EQ(OBSOLETE, last_status_result_); | 320 EXPECT_EQ(OBSOLETE, last_status_result_); |
| 321 EXPECT_EQ(reinterpret_cast<void*>(-1), last_callback_param_); | 321 EXPECT_EQ(reinterpret_cast<void*>(-1), last_callback_param_); |
| 322 | 322 |
| 323 // Satisfy the load will NULL, a failure. | 323 // Satisfy the load will NULL, a failure. |
| 324 host.OnGroupLoaded(NULL, kMockManifestUrl); | 324 host.OnGroupLoaded(NULL, kMockManifestUrl); |
| 325 | 325 |
| 326 // Cache selection should have finished | 326 // Cache selection should have finished |
| 327 EXPECT_FALSE(host.is_selection_pending()); | 327 EXPECT_FALSE(host.is_selection_pending()); |
| 328 EXPECT_EQ(1, mock_frontend_.last_host_id_); | 328 EXPECT_EQ(1, mock_frontend_.last_host_id_); |
| 329 EXPECT_EQ(kNoCacheId, mock_frontend_.last_cache_id_); | 329 EXPECT_EQ(kNoCacheId, mock_frontend_.last_cache_id_); |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 EXPECT_EQ(&mock_frontend_, host.frontend()); | 512 EXPECT_EQ(&mock_frontend_, host.frontend()); |
| 513 EXPECT_EQ(NULL, host.associated_cache()); | 513 EXPECT_EQ(NULL, host.associated_cache()); |
| 514 EXPECT_FALSE(host.is_selection_pending()); | 514 EXPECT_FALSE(host.is_selection_pending()); |
| 515 EXPECT_TRUE(host.preferred_manifest_url().is_empty()); | 515 EXPECT_TRUE(host.preferred_manifest_url().is_empty()); |
| 516 } | 516 } |
| 517 EXPECT_EQ(0, mock_quota_proxy->GetInUseCount(kDocAndOriginUrl)); | 517 EXPECT_EQ(0, mock_quota_proxy->GetInUseCount(kDocAndOriginUrl)); |
| 518 service_.set_quota_manager_proxy(NULL); | 518 service_.set_quota_manager_proxy(NULL); |
| 519 } | 519 } |
| 520 | 520 |
| 521 } // namespace appcache | 521 } // namespace appcache |
| OLD | NEW |