OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/bind.h" | 5 #include "base/bind.h" |
6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "content/browser/appcache/appcache.h" |
| 10 #include "content/browser/appcache/appcache_backend_impl.h" |
| 11 #include "content/browser/appcache/appcache_group.h" |
| 12 #include "content/browser/appcache/appcache_host.h" |
9 #include "content/browser/appcache/mock_appcache_policy.h" | 13 #include "content/browser/appcache/mock_appcache_policy.h" |
10 #include "content/browser/appcache/mock_appcache_service.h" | 14 #include "content/browser/appcache/mock_appcache_service.h" |
11 #include "net/url_request/url_request.h" | 15 #include "net/url_request/url_request.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "webkit/browser/appcache/appcache.h" | |
14 #include "webkit/browser/appcache/appcache_backend_impl.h" | |
15 #include "webkit/browser/appcache/appcache_group.h" | |
16 #include "webkit/browser/appcache/appcache_host.h" | |
17 #include "webkit/browser/quota/quota_manager.h" | 17 #include "webkit/browser/quota/quota_manager.h" |
18 | 18 |
19 using appcache::AppCache; | |
20 using appcache::AppCacheBackendImpl; | |
21 using appcache::AppCacheEntry; | |
22 using appcache::AppCacheFrontend; | |
23 using appcache::AppCacheGroup; | |
24 using appcache::AppCacheHost; | |
25 using appcache::kAppCacheNoCacheId; | |
26 using appcache::APPCACHE_ERROR_EVENT; | |
27 using appcache::APPCACHE_STATUS_OBSOLETE; | |
28 using appcache::APPCACHE_OBSOLETE_EVENT; | |
29 using appcache::APPCACHE_PROGRESS_EVENT; | |
30 using appcache::AppCacheStatus; | |
31 using appcache::APPCACHE_STATUS_UNCACHED; | |
32 | |
33 namespace content { | 19 namespace content { |
34 | 20 |
35 class AppCacheHostTest : public testing::Test { | 21 class AppCacheHostTest : public testing::Test { |
36 public: | 22 public: |
37 AppCacheHostTest() { | 23 AppCacheHostTest() { |
38 get_status_callback_ = | 24 get_status_callback_ = |
39 base::Bind(&AppCacheHostTest::GetStatusCallback, | 25 base::Bind(&AppCacheHostTest::GetStatusCallback, |
40 base::Unretained(this)); | 26 base::Unretained(this)); |
41 start_update_callback_ = | 27 start_update_callback_ = |
42 base::Bind(&AppCacheHostTest::StartUpdateCallback, | 28 base::Bind(&AppCacheHostTest::StartUpdateCallback, |
43 base::Unretained(this)); | 29 base::Unretained(this)); |
44 swap_cache_callback_ = | 30 swap_cache_callback_ = |
45 base::Bind(&AppCacheHostTest::SwapCacheCallback, | 31 base::Bind(&AppCacheHostTest::SwapCacheCallback, |
46 base::Unretained(this)); | 32 base::Unretained(this)); |
47 } | 33 } |
48 | 34 |
49 class MockFrontend : public AppCacheFrontend { | 35 class MockFrontend : public AppCacheFrontend { |
50 public: | 36 public: |
51 MockFrontend() | 37 MockFrontend() |
52 : last_host_id_(-222), last_cache_id_(-222), | 38 : last_host_id_(-222), last_cache_id_(-222), |
53 last_status_(appcache::APPCACHE_STATUS_OBSOLETE), | 39 last_status_(APPCACHE_STATUS_OBSOLETE), |
54 last_status_changed_(appcache::APPCACHE_STATUS_OBSOLETE), | 40 last_status_changed_(APPCACHE_STATUS_OBSOLETE), |
55 last_event_id_(appcache::APPCACHE_OBSOLETE_EVENT), | 41 last_event_id_(APPCACHE_OBSOLETE_EVENT), |
56 content_blocked_(false) { | 42 content_blocked_(false) { |
57 } | 43 } |
58 | 44 |
59 virtual void OnCacheSelected( | 45 virtual void OnCacheSelected( |
60 int host_id, const appcache::AppCacheInfo& info) OVERRIDE { | 46 int host_id, const AppCacheInfo& info) OVERRIDE { |
61 last_host_id_ = host_id; | 47 last_host_id_ = host_id; |
62 last_cache_id_ = info.cache_id; | 48 last_cache_id_ = info.cache_id; |
63 last_status_ = info.status; | 49 last_status_ = info.status; |
64 } | 50 } |
65 | 51 |
66 virtual void OnStatusChanged(const std::vector<int>& host_ids, | 52 virtual void OnStatusChanged(const std::vector<int>& host_ids, |
67 appcache::AppCacheStatus status) OVERRIDE { | 53 AppCacheStatus status) OVERRIDE { |
68 last_status_changed_ = status; | 54 last_status_changed_ = status; |
69 } | 55 } |
70 | 56 |
71 virtual void OnEventRaised(const std::vector<int>& host_ids, | 57 virtual void OnEventRaised(const std::vector<int>& host_ids, |
72 appcache::AppCacheEventID event_id) OVERRIDE { | 58 AppCacheEventID event_id) OVERRIDE { |
73 last_event_id_ = event_id; | 59 last_event_id_ = event_id; |
74 } | 60 } |
75 | 61 |
76 virtual void OnErrorEventRaised( | 62 virtual void OnErrorEventRaised( |
77 const std::vector<int>& host_ids, | 63 const std::vector<int>& host_ids, |
78 const appcache::AppCacheErrorDetails& details) OVERRIDE { | 64 const AppCacheErrorDetails& details) OVERRIDE { |
79 last_event_id_ = APPCACHE_ERROR_EVENT; | 65 last_event_id_ = APPCACHE_ERROR_EVENT; |
80 } | 66 } |
81 | 67 |
82 virtual void OnProgressEventRaised(const std::vector<int>& host_ids, | 68 virtual void OnProgressEventRaised(const std::vector<int>& host_ids, |
83 const GURL& url, | 69 const GURL& url, |
84 int num_total, | 70 int num_total, |
85 int num_complete) OVERRIDE { | 71 int num_complete) OVERRIDE { |
86 last_event_id_ = APPCACHE_PROGRESS_EVENT; | 72 last_event_id_ = APPCACHE_PROGRESS_EVENT; |
87 } | 73 } |
88 | 74 |
89 virtual void OnLogMessage(int host_id, | 75 virtual void OnLogMessage(int host_id, |
90 appcache::AppCacheLogLevel log_level, | 76 AppCacheLogLevel log_level, |
91 const std::string& message) OVERRIDE { | 77 const std::string& message) OVERRIDE { |
92 } | 78 } |
93 | 79 |
94 virtual void OnContentBlocked(int host_id, | 80 virtual void OnContentBlocked(int host_id, |
95 const GURL& manifest_url) OVERRIDE { | 81 const GURL& manifest_url) OVERRIDE { |
96 content_blocked_ = true; | 82 content_blocked_ = true; |
97 } | 83 } |
98 | 84 |
99 int last_host_id_; | 85 int last_host_id_; |
100 int64 last_cache_id_; | 86 int64 last_cache_id_; |
101 appcache::AppCacheStatus last_status_; | 87 AppCacheStatus last_status_; |
102 appcache::AppCacheStatus last_status_changed_; | 88 AppCacheStatus last_status_changed_; |
103 appcache::AppCacheEventID last_event_id_; | 89 AppCacheEventID last_event_id_; |
104 bool content_blocked_; | 90 bool content_blocked_; |
105 }; | 91 }; |
106 | 92 |
107 class MockQuotaManagerProxy : public quota::QuotaManagerProxy { | 93 class MockQuotaManagerProxy : public quota::QuotaManagerProxy { |
108 public: | 94 public: |
109 MockQuotaManagerProxy() : QuotaManagerProxy(NULL, NULL) {} | 95 MockQuotaManagerProxy() : QuotaManagerProxy(NULL, NULL) {} |
110 | 96 |
111 // Not needed for our tests. | 97 // Not needed for our tests. |
112 virtual void RegisterClient(quota::QuotaClient* client) OVERRIDE {} | 98 virtual void RegisterClient(quota::QuotaClient* client) OVERRIDE {} |
113 virtual void NotifyStorageAccessed(quota::QuotaClient::ID client_id, | 99 virtual void NotifyStorageAccessed(quota::QuotaClient::ID client_id, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 last_callback_param_ = param; | 151 last_callback_param_ = param; |
166 } | 152 } |
167 | 153 |
168 base::MessageLoop message_loop_; | 154 base::MessageLoop message_loop_; |
169 | 155 |
170 // Mock classes for the 'host' to work with | 156 // Mock classes for the 'host' to work with |
171 MockAppCacheService service_; | 157 MockAppCacheService service_; |
172 MockFrontend mock_frontend_; | 158 MockFrontend mock_frontend_; |
173 | 159 |
174 // Mock callbacks we expect to receive from the 'host' | 160 // Mock callbacks we expect to receive from the 'host' |
175 appcache::GetStatusCallback get_status_callback_; | 161 content::GetStatusCallback get_status_callback_; |
176 appcache::StartUpdateCallback start_update_callback_; | 162 content::StartUpdateCallback start_update_callback_; |
177 appcache::SwapCacheCallback swap_cache_callback_; | 163 content::SwapCacheCallback swap_cache_callback_; |
178 | 164 |
179 AppCacheStatus last_status_result_; | 165 AppCacheStatus last_status_result_; |
180 bool last_swap_result_; | 166 bool last_swap_result_; |
181 bool last_start_result_; | 167 bool last_start_result_; |
182 void* last_callback_param_; | 168 void* last_callback_param_; |
183 }; | 169 }; |
184 | 170 |
185 TEST_F(AppCacheHostTest, Basic) { | 171 TEST_F(AppCacheHostTest, Basic) { |
186 // Construct a host and test what state it appears to be in. | 172 // Construct a host and test what state it appears to be in. |
187 AppCacheHost host(1, &mock_frontend_, &service_); | 173 AppCacheHost host(1, &mock_frontend_, &service_); |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 AppCache* cache1 = new AppCache(service_.storage(), 111); | 366 AppCache* cache1 = new AppCache(service_.storage(), 111); |
381 cache1->set_complete(true); | 367 cache1->set_complete(true); |
382 group1->AddCache(cache1); | 368 group1->AddCache(cache1); |
383 host.SetSwappableCache(group1.get()); | 369 host.SetSwappableCache(group1.get()); |
384 EXPECT_EQ(cache1, host.swappable_cache_.get()); | 370 EXPECT_EQ(cache1, host.swappable_cache_.get()); |
385 | 371 |
386 mock_frontend_.last_host_id_ = -222; // to verify we received OnCacheSelected | 372 mock_frontend_.last_host_id_ = -222; // to verify we received OnCacheSelected |
387 | 373 |
388 host.AssociateCompleteCache(cache1); | 374 host.AssociateCompleteCache(cache1); |
389 EXPECT_FALSE(host.swappable_cache_.get()); // was same as associated cache | 375 EXPECT_FALSE(host.swappable_cache_.get()); // was same as associated cache |
390 EXPECT_EQ(appcache::APPCACHE_STATUS_IDLE, host.GetStatus()); | 376 EXPECT_EQ(APPCACHE_STATUS_IDLE, host.GetStatus()); |
391 // verify OnCacheSelected was called | 377 // verify OnCacheSelected was called |
392 EXPECT_EQ(host.host_id(), mock_frontend_.last_host_id_); | 378 EXPECT_EQ(host.host_id(), mock_frontend_.last_host_id_); |
393 EXPECT_EQ(cache1->cache_id(), mock_frontend_.last_cache_id_); | 379 EXPECT_EQ(cache1->cache_id(), mock_frontend_.last_cache_id_); |
394 EXPECT_EQ(appcache::APPCACHE_STATUS_IDLE, mock_frontend_.last_status_); | 380 EXPECT_EQ(APPCACHE_STATUS_IDLE, mock_frontend_.last_status_); |
395 | 381 |
396 AppCache* cache2 = new AppCache(service_.storage(), 222); | 382 AppCache* cache2 = new AppCache(service_.storage(), 222); |
397 cache2->set_complete(true); | 383 cache2->set_complete(true); |
398 group1->AddCache(cache2); | 384 group1->AddCache(cache2); |
399 EXPECT_EQ(cache2, host.swappable_cache_.get()); // updated to newest | 385 EXPECT_EQ(cache2, host.swappable_cache_.get()); // updated to newest |
400 | 386 |
401 scoped_refptr<AppCacheGroup> group2( | 387 scoped_refptr<AppCacheGroup> group2( |
402 new AppCacheGroup(service_.storage(), GURL("http://foo.com"), | 388 new AppCacheGroup(service_.storage(), GURL("http://foo.com"), |
403 service_.storage()->NewGroupId())); | 389 service_.storage()->NewGroupId())); |
404 AppCache* cache3 = new AppCache(service_.storage(), 333); | 390 AppCache* cache3 = new AppCache(service_.storage(), 333); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 EXPECT_EQ(&mock_frontend_, host.frontend()); | 531 EXPECT_EQ(&mock_frontend_, host.frontend()); |
546 EXPECT_EQ(NULL, host.associated_cache()); | 532 EXPECT_EQ(NULL, host.associated_cache()); |
547 EXPECT_FALSE(host.is_selection_pending()); | 533 EXPECT_FALSE(host.is_selection_pending()); |
548 EXPECT_TRUE(host.preferred_manifest_url().is_empty()); | 534 EXPECT_TRUE(host.preferred_manifest_url().is_empty()); |
549 } | 535 } |
550 EXPECT_EQ(0, mock_quota_proxy->GetInUseCount(kDocAndOriginUrl)); | 536 EXPECT_EQ(0, mock_quota_proxy->GetInUseCount(kDocAndOriginUrl)); |
551 service_.set_quota_manager_proxy(NULL); | 537 service_.set_quota_manager_proxy(NULL); |
552 } | 538 } |
553 | 539 |
554 } // namespace content | 540 } // namespace content |
OLD | NEW |