| 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/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "content/browser/browser_thread_impl.h" | 10 #include "content/browser/browser_thread_impl.h" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 FilePath appcache_path = temp_dir_.path().Append(kTestingAppCacheDirname); | 119 FilePath appcache_path = temp_dir_.path().Append(kTestingAppCacheDirname); |
| 120 | 120 |
| 121 // Create a ChromeAppCacheService and insert data into it | 121 // Create a ChromeAppCacheService and insert data into it |
| 122 scoped_refptr<ChromeAppCacheService> appcache_service = | 122 scoped_refptr<ChromeAppCacheService> appcache_service = |
| 123 CreateAppCacheService(appcache_path, true); | 123 CreateAppCacheService(appcache_path, true); |
| 124 ASSERT_TRUE(file_util::PathExists(appcache_path)); | 124 ASSERT_TRUE(file_util::PathExists(appcache_path)); |
| 125 ASSERT_TRUE(file_util::PathExists(appcache_path.AppendASCII("Index"))); | 125 ASSERT_TRUE(file_util::PathExists(appcache_path.AppendASCII("Index"))); |
| 126 InsertDataIntoAppCache(appcache_service); | 126 InsertDataIntoAppCache(appcache_service); |
| 127 | 127 |
| 128 // Test: delete the ChromeAppCacheService | 128 // Test: delete the ChromeAppCacheService |
| 129 appcache_service->set_clear_local_state_on_exit(false); | |
| 130 appcache_service = NULL; | 129 appcache_service = NULL; |
| 131 message_loop_.RunAllPending(); | 130 message_loop_.RunAllPending(); |
| 132 | 131 |
| 133 // Recreate the appcache (for reading the data back) | 132 // Recreate the appcache (for reading the data back) |
| 134 appcache_service = CreateAppCacheService(appcache_path, false); | 133 appcache_service = CreateAppCacheService(appcache_path, false); |
| 135 | 134 |
| 136 // The directory is still there | 135 // The directory is still there |
| 137 ASSERT_TRUE(file_util::PathExists(appcache_path)); | 136 ASSERT_TRUE(file_util::PathExists(appcache_path)); |
| 138 | 137 |
| 139 // The appcache data is also there, except the session-only origin. | 138 // The appcache data is also there, except the session-only origin. |
| 140 AppCacheTestHelper appcache_helper; | 139 AppCacheTestHelper appcache_helper; |
| 141 std::set<GURL> origins; | 140 std::set<GURL> origins; |
| 142 appcache_helper.GetOriginsWithCaches(appcache_service, &origins); | 141 appcache_helper.GetOriginsWithCaches(appcache_service, &origins); |
| 143 EXPECT_EQ(2UL, origins.size()); | 142 EXPECT_EQ(2UL, origins.size()); |
| 144 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); | 143 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); |
| 145 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end()); | 144 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end()); |
| 146 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) == | 145 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) == |
| 147 origins.end()); | 146 origins.end()); |
| 148 | 147 |
| 149 // Delete and let cleanup tasks run prior to returning. | 148 // Delete and let cleanup tasks run prior to returning. |
| 150 appcache_service = NULL; | 149 appcache_service = NULL; |
| 151 message_loop_.RunAllPending(); | 150 message_loop_.RunAllPending(); |
| 152 } | 151 } |
| 153 | 152 |
| 154 TEST_F(ChromeAppCacheServiceTest, RemoveOnDestruction) { | |
| 155 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 156 FilePath appcache_path = temp_dir_.path().Append(kTestingAppCacheDirname); | |
| 157 | |
| 158 // Create a ChromeAppCacheService and insert data into it | |
| 159 scoped_refptr<ChromeAppCacheService> appcache_service = | |
| 160 CreateAppCacheService(appcache_path, true); | |
| 161 ASSERT_TRUE(file_util::PathExists(appcache_path)); | |
| 162 ASSERT_TRUE(file_util::PathExists(appcache_path.AppendASCII("Index"))); | |
| 163 InsertDataIntoAppCache(appcache_service); | |
| 164 | |
| 165 // Test: delete the ChromeAppCacheService | |
| 166 appcache_service->set_clear_local_state_on_exit(true); | |
| 167 appcache_service = NULL; | |
| 168 message_loop_.RunAllPending(); | |
| 169 | |
| 170 // Recreate the appcache (for reading the data back) | |
| 171 appcache_service = CreateAppCacheService(appcache_path, false); | |
| 172 | |
| 173 // The directory is still there | |
| 174 ASSERT_TRUE(file_util::PathExists(appcache_path)); | |
| 175 | |
| 176 // The appcache data for the protected origin is there, and the data for the | |
| 177 // unprotected origin was deleted. | |
| 178 AppCacheTestHelper appcache_helper; | |
| 179 std::set<GURL> origins; | |
| 180 appcache_helper.GetOriginsWithCaches(appcache_service, &origins); | |
| 181 EXPECT_EQ(1UL, origins.size()); | |
| 182 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); | |
| 183 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) == origins.end()); | |
| 184 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) == | |
| 185 origins.end()); | |
| 186 | |
| 187 // Delete and let cleanup tasks run prior to returning. | |
| 188 appcache_service = NULL; | |
| 189 message_loop_.RunAllPending(); | |
| 190 } | |
| 191 | |
| 192 TEST_F(ChromeAppCacheServiceTest, SaveSessionState) { | 153 TEST_F(ChromeAppCacheServiceTest, SaveSessionState) { |
| 193 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 154 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 194 FilePath appcache_path = temp_dir_.path().Append(kTestingAppCacheDirname); | 155 FilePath appcache_path = temp_dir_.path().Append(kTestingAppCacheDirname); |
| 195 | 156 |
| 196 // Create a ChromeAppCacheService and insert data into it | 157 // Create a ChromeAppCacheService and insert data into it |
| 197 scoped_refptr<ChromeAppCacheService> appcache_service = | 158 scoped_refptr<ChromeAppCacheService> appcache_service = |
| 198 CreateAppCacheService(appcache_path, true); | 159 CreateAppCacheService(appcache_path, true); |
| 199 ASSERT_TRUE(file_util::PathExists(appcache_path)); | 160 ASSERT_TRUE(file_util::PathExists(appcache_path)); |
| 200 ASSERT_TRUE(file_util::PathExists(appcache_path.AppendASCII("Index"))); | 161 ASSERT_TRUE(file_util::PathExists(appcache_path.AppendASCII("Index"))); |
| 201 InsertDataIntoAppCache(appcache_service); | 162 InsertDataIntoAppCache(appcache_service); |
| 202 | 163 |
| 203 appcache_service->set_clear_local_state_on_exit(true); | |
| 204 // Save session state. This should bypass the destruction-time deletion. | 164 // Save session state. This should bypass the destruction-time deletion. |
| 205 appcache_service->set_save_session_state(true); | 165 appcache_service->set_force_keep_session_state(); |
| 206 | 166 |
| 207 // Test: delete the ChromeAppCacheService | 167 // Test: delete the ChromeAppCacheService |
| 208 appcache_service = NULL; | 168 appcache_service = NULL; |
| 209 message_loop_.RunAllPending(); | 169 message_loop_.RunAllPending(); |
| 210 | 170 |
| 211 // Recreate the appcache (for reading the data back) | 171 // Recreate the appcache (for reading the data back) |
| 212 appcache_service = CreateAppCacheService(appcache_path, false); | 172 appcache_service = CreateAppCacheService(appcache_path, false); |
| 213 | 173 |
| 214 // The directory is still there | 174 // The directory is still there |
| 215 ASSERT_TRUE(file_util::PathExists(appcache_path)); | 175 ASSERT_TRUE(file_util::PathExists(appcache_path)); |
| 216 | 176 |
| 217 // No appcache data was deleted. | 177 // No appcache data was deleted. |
| 218 AppCacheTestHelper appcache_helper; | 178 AppCacheTestHelper appcache_helper; |
| 219 std::set<GURL> origins; | 179 std::set<GURL> origins; |
| 220 appcache_helper.GetOriginsWithCaches(appcache_service, &origins); | 180 appcache_helper.GetOriginsWithCaches(appcache_service, &origins); |
| 221 EXPECT_EQ(3UL, origins.size()); | 181 EXPECT_EQ(3UL, origins.size()); |
| 222 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); | 182 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); |
| 223 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end()); | 183 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end()); |
| 224 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) != | 184 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) != |
| 225 origins.end()); | 185 origins.end()); |
| 226 | 186 |
| 227 // Delete and let cleanup tasks run prior to returning. | 187 // Delete and let cleanup tasks run prior to returning. |
| 228 appcache_service = NULL; | 188 appcache_service = NULL; |
| 229 message_loop_.RunAllPending(); | 189 message_loop_.RunAllPending(); |
| 230 } | 190 } |
| 231 | 191 |
| 232 } // namespace appcache | 192 } // namespace appcache |
| OLD | NEW |