OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authos. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/scoped_ptr.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 #include "webkit/appcache/appcache.h" |
| 8 #include "webkit/appcache/appcache_group.h" |
| 9 #include "webkit/appcache/appcache_host.h" |
| 10 #include "webkit/appcache/appcache_service.h" |
| 11 |
| 12 namespace appcache { |
| 13 |
| 14 class AppCacheHostTest : public testing::Test { |
| 15 public: |
| 16 AppCacheHostTest() { |
| 17 get_status_callback_.reset( |
| 18 NewCallback(this, &AppCacheHostTest::GetStatusCallback)); |
| 19 start_update_callback_.reset( |
| 20 NewCallback(this, &AppCacheHostTest::StartUpdateCallback)); |
| 21 swap_cache_callback_.reset( |
| 22 NewCallback(this, &AppCacheHostTest::SwapCacheCallback)); |
| 23 } |
| 24 |
| 25 class MockFrontend : public AppCacheFrontend { |
| 26 public: |
| 27 MockFrontend() |
| 28 : last_host_id_(-222), last_cache_id_(-222), |
| 29 last_status_(appcache::OBSOLETE) { |
| 30 } |
| 31 |
| 32 virtual void OnCacheSelected(int host_id, int64 cache_id , |
| 33 appcache::Status status) { |
| 34 last_host_id_ = host_id; |
| 35 last_cache_id_ = cache_id; |
| 36 last_status_ = status; |
| 37 } |
| 38 |
| 39 virtual void OnStatusChanged(const std::vector<int>& host_ids, |
| 40 appcache::Status status) { |
| 41 } |
| 42 |
| 43 virtual void OnEventRaised(const std::vector<int>& host_ids, |
| 44 appcache::EventID event_id) { |
| 45 } |
| 46 |
| 47 int last_host_id_; |
| 48 int64 last_cache_id_; |
| 49 appcache::Status last_status_; |
| 50 }; |
| 51 |
| 52 void GetStatusCallback(Status status, void* param) { |
| 53 last_status_result_ = status; |
| 54 last_callback_param_ = param; |
| 55 } |
| 56 |
| 57 void StartUpdateCallback(bool result, void* param) { |
| 58 last_start_result_ = result; |
| 59 last_callback_param_ = param; |
| 60 } |
| 61 |
| 62 void SwapCacheCallback(bool result, void* param) { |
| 63 last_swap_result_ = result; |
| 64 last_callback_param_ = param; |
| 65 } |
| 66 |
| 67 // Mock classes for the 'host' to work with |
| 68 AppCacheService service_; // TODO(michaeln): make service mockable? |
| 69 MockFrontend mock_frontend_; |
| 70 |
| 71 // Mock callbacks we expect to receive from the 'host' |
| 72 scoped_ptr<appcache::GetStatusCallback> get_status_callback_; |
| 73 scoped_ptr<appcache::StartUpdateCallback> start_update_callback_; |
| 74 scoped_ptr<appcache::SwapCacheCallback> swap_cache_callback_; |
| 75 |
| 76 Status last_status_result_; |
| 77 bool last_swap_result_; |
| 78 bool last_start_result_; |
| 79 void* last_callback_param_; |
| 80 }; |
| 81 |
| 82 TEST_F(AppCacheHostTest, Basic) { |
| 83 // Construct a host and test what state it appears to be in. |
| 84 AppCacheHost host(1, &mock_frontend_, &service_); |
| 85 EXPECT_EQ(1, host.host_id()); |
| 86 EXPECT_EQ(&service_, host.service()); |
| 87 EXPECT_EQ(&mock_frontend_, host.frontend()); |
| 88 EXPECT_EQ(NULL, host.associated_cache()); |
| 89 EXPECT_FALSE(host.is_selection_pending()); |
| 90 |
| 91 // See that the callbacks are delivered immediately |
| 92 // and respond as if there is no cache selected. |
| 93 last_status_result_ = OBSOLETE; |
| 94 host.GetStatusWithCallback(get_status_callback_.get(), |
| 95 reinterpret_cast<void*>(1)); |
| 96 EXPECT_EQ(UNCACHED, last_status_result_); |
| 97 EXPECT_EQ(reinterpret_cast<void*>(1), last_callback_param_); |
| 98 |
| 99 last_start_result_ = true; |
| 100 host.StartUpdateWithCallback(start_update_callback_.get(), |
| 101 reinterpret_cast<void*>(2)); |
| 102 EXPECT_FALSE(last_start_result_); |
| 103 EXPECT_EQ(reinterpret_cast<void*>(2), last_callback_param_); |
| 104 |
| 105 last_swap_result_ = true; |
| 106 host.SwapCacheWithCallback(swap_cache_callback_.get(), |
| 107 reinterpret_cast<void*>(3)); |
| 108 EXPECT_FALSE(last_swap_result_); |
| 109 EXPECT_EQ(reinterpret_cast<void*>(3), last_callback_param_); |
| 110 } |
| 111 |
| 112 TEST_F(AppCacheHostTest, SelectNoCache) { |
| 113 // Reset our mock frontend |
| 114 mock_frontend_.last_cache_id_ = -333; |
| 115 mock_frontend_.last_host_id_ = -333; |
| 116 mock_frontend_.last_status_ = OBSOLETE; |
| 117 |
| 118 AppCacheHost host(1, &mock_frontend_, &service_); |
| 119 host.SelectCache(GURL("http://whatever/"), kNoCacheId, GURL::EmptyGURL()); |
| 120 |
| 121 // We should have received an OnCacheSelected msg |
| 122 EXPECT_EQ(1, mock_frontend_.last_host_id_); |
| 123 EXPECT_EQ(kNoCacheId, mock_frontend_.last_cache_id_); |
| 124 EXPECT_EQ(UNCACHED, mock_frontend_.last_status_); |
| 125 |
| 126 // Otherwise, see that it respond as if there is no cache selected. |
| 127 EXPECT_EQ(1, host.host_id()); |
| 128 EXPECT_EQ(&service_, host.service()); |
| 129 EXPECT_EQ(&mock_frontend_, host.frontend()); |
| 130 EXPECT_EQ(NULL, host.associated_cache()); |
| 131 EXPECT_FALSE(host.is_selection_pending()); |
| 132 } |
| 133 |
| 134 TEST_F(AppCacheHostTest, ForeignEntry) { |
| 135 // Reset our mock frontend |
| 136 mock_frontend_.last_cache_id_ = -333; |
| 137 mock_frontend_.last_host_id_ = -333; |
| 138 mock_frontend_.last_status_ = OBSOLETE; |
| 139 |
| 140 AppCacheHost host(1, &mock_frontend_, &service_); |
| 141 host.MarkAsForeignEntry(GURL("http://whatever/"), 22); |
| 142 |
| 143 // We should have received an OnCacheSelected msg for kNoCacheId. |
| 144 EXPECT_EQ(1, mock_frontend_.last_host_id_); |
| 145 EXPECT_EQ(kNoCacheId, mock_frontend_.last_cache_id_); |
| 146 EXPECT_EQ(UNCACHED, mock_frontend_.last_status_); |
| 147 |
| 148 // See that it respond as if there is no cache selected. |
| 149 EXPECT_EQ(1, host.host_id()); |
| 150 EXPECT_EQ(&service_, host.service()); |
| 151 EXPECT_EQ(&mock_frontend_, host.frontend()); |
| 152 EXPECT_EQ(NULL, host.associated_cache()); |
| 153 EXPECT_FALSE(host.is_selection_pending()); |
| 154 } |
| 155 |
| 156 |
| 157 TEST_F(AppCacheHostTest, FailedCacheLoad) { |
| 158 // Reset our mock frontend |
| 159 mock_frontend_.last_cache_id_ = -333; |
| 160 mock_frontend_.last_host_id_ = -333; |
| 161 mock_frontend_.last_status_ = OBSOLETE; |
| 162 |
| 163 AppCacheHost host(1, &mock_frontend_, &service_); |
| 164 EXPECT_FALSE(host.is_selection_pending()); |
| 165 |
| 166 const int kMockCacheId = 333; |
| 167 |
| 168 // Put it in a state where we're waiting on a cache |
| 169 // load prior to finishing cache selection. |
| 170 host.pending_selected_cache_id_ = kMockCacheId; |
| 171 EXPECT_TRUE(host.is_selection_pending()); |
| 172 |
| 173 // The callback should not occur until we finish cache selection. |
| 174 last_status_result_ = OBSOLETE; |
| 175 last_callback_param_ = reinterpret_cast<void*>(-1); |
| 176 host.GetStatusWithCallback(get_status_callback_.get(), |
| 177 reinterpret_cast<void*>(1)); |
| 178 EXPECT_EQ(OBSOLETE, last_status_result_); |
| 179 EXPECT_EQ(reinterpret_cast<void*>(-1), last_callback_param_); |
| 180 |
| 181 // Satisfy the load with NULL, a failure. |
| 182 host.CacheLoadedCallback(NULL, kMockCacheId); |
| 183 |
| 184 // Cache selection should have finished |
| 185 EXPECT_FALSE(host.is_selection_pending()); |
| 186 EXPECT_EQ(1, mock_frontend_.last_host_id_); |
| 187 EXPECT_EQ(kNoCacheId, mock_frontend_.last_cache_id_); |
| 188 EXPECT_EQ(UNCACHED, mock_frontend_.last_status_); |
| 189 |
| 190 // Callback should have fired upon completing the cache load too. |
| 191 EXPECT_EQ(UNCACHED, last_status_result_); |
| 192 EXPECT_EQ(reinterpret_cast<void*>(1), last_callback_param_); |
| 193 } |
| 194 |
| 195 TEST_F(AppCacheHostTest, FailedGroupLoad) { |
| 196 AppCacheHost host(1, &mock_frontend_, &service_); |
| 197 |
| 198 const GURL kMockManifestUrl("http://foo.bar/baz"); |
| 199 |
| 200 // Put it in a state where we're waiting on a cache |
| 201 // load prior to finishing cache selection. |
| 202 host.pending_selected_manifest_url_ = kMockManifestUrl; |
| 203 EXPECT_TRUE(host.is_selection_pending()); |
| 204 |
| 205 // The callback should not occur until we finish cache selection. |
| 206 last_status_result_ = OBSOLETE; |
| 207 last_callback_param_ = reinterpret_cast<void*>(-1); |
| 208 host.GetStatusWithCallback(get_status_callback_.get(), |
| 209 reinterpret_cast<void*>(1)); |
| 210 EXPECT_EQ(OBSOLETE, last_status_result_); |
| 211 EXPECT_EQ(reinterpret_cast<void*>(-1), last_callback_param_); |
| 212 |
| 213 // Satisfy the load will NULL, a failure. |
| 214 host.GroupLoadedCallback(NULL, kMockManifestUrl); |
| 215 |
| 216 // Cache selection should have finished |
| 217 EXPECT_FALSE(host.is_selection_pending()); |
| 218 EXPECT_EQ(1, mock_frontend_.last_host_id_); |
| 219 EXPECT_EQ(kNoCacheId, mock_frontend_.last_cache_id_); |
| 220 EXPECT_EQ(UNCACHED, mock_frontend_.last_status_); |
| 221 |
| 222 // Callback should have fired upon completing the group load. |
| 223 EXPECT_EQ(UNCACHED, last_status_result_); |
| 224 EXPECT_EQ(reinterpret_cast<void*>(1), last_callback_param_); |
| 225 } |
| 226 |
| 227 // TODO(michaeln): Flesh these tests out more. |
| 228 |
| 229 } // namespace appcache |
| 230 |
OLD | NEW |