| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "content/browser/appcache/chrome_appcache_service.h" | 10 #include "content/browser/appcache/chrome_appcache_service.h" |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/browser/browser_thread_impl.h" |
| 12 #include "content/public/browser/resource_context.h" | 12 #include "content/public/browser/resource_context.h" |
| 13 #include "content/public/test/test_browser_context.h" | 13 #include "content/public/test/test_browser_context.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | |
| 15 #include "net/url_request/url_request_context_getter.h" | 14 #include "net/url_request/url_request_context_getter.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "webkit/browser/appcache/appcache_database.h" | 16 #include "webkit/browser/appcache/appcache_database.h" |
| 18 #include "webkit/browser/appcache/appcache_storage_impl.h" | 17 #include "webkit/browser/appcache/appcache_storage_impl.h" |
| 19 #include "webkit/browser/appcache/appcache_test_helper.h" | 18 #include "webkit/browser/appcache/appcache_test_helper.h" |
| 20 #include "webkit/browser/quota/mock_special_storage_policy.h" | 19 #include "webkit/browser/quota/mock_special_storage_policy.h" |
| 21 | 20 |
| 22 #include <set> | 21 #include <set> |
| 23 | 22 |
| 24 using appcache::AppCacheTestHelper; | 23 using appcache::AppCacheTestHelper; |
| 25 | 24 |
| 26 namespace content { | 25 namespace content { |
| 27 namespace { | 26 namespace { |
| 28 const base::FilePath::CharType kTestingAppCacheDirname[] = | 27 const base::FilePath::CharType kTestingAppCacheDirname[] = |
| 29 FILE_PATH_LITERAL("Application Cache"); | 28 FILE_PATH_LITERAL("Application Cache"); |
| 30 | 29 |
| 31 // Examples of a protected and an unprotected origin, to be used througout the | 30 // Examples of a protected and an unprotected origin, to be used througout the |
| 32 // test. | 31 // test. |
| 33 const char kProtectedManifest[] = "http://www.protected.com/cache.manifest"; | 32 const char kProtectedManifest[] = "http://www.protected.com/cache.manifest"; |
| 34 const char kNormalManifest[] = "http://www.normal.com/cache.manifest"; | 33 const char kNormalManifest[] = "http://www.normal.com/cache.manifest"; |
| 35 const char kSessionOnlyManifest[] = "http://www.sessiononly.com/cache.manifest"; | 34 const char kSessionOnlyManifest[] = "http://www.sessiononly.com/cache.manifest"; |
| 36 | 35 |
| 36 class MockURLRequestContextGetter : public net::URLRequestContextGetter { |
| 37 public: |
| 38 MockURLRequestContextGetter( |
| 39 net::URLRequestContext* context, |
| 40 base::MessageLoopProxy* message_loop_proxy) |
| 41 : context_(context), message_loop_proxy_(message_loop_proxy) { |
| 42 } |
| 43 |
| 44 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE { |
| 45 return context_; |
| 46 } |
| 47 |
| 48 virtual scoped_refptr<base::SingleThreadTaskRunner> |
| 49 GetNetworkTaskRunner() const OVERRIDE { |
| 50 return message_loop_proxy_; |
| 51 } |
| 52 |
| 53 protected: |
| 54 virtual ~MockURLRequestContextGetter() {} |
| 55 |
| 56 private: |
| 57 net::URLRequestContext* context_; |
| 58 scoped_refptr<base::SingleThreadTaskRunner> message_loop_proxy_; |
| 59 }; |
| 60 |
| 37 } // namespace | 61 } // namespace |
| 38 | 62 |
| 39 class ChromeAppCacheServiceTest : public testing::Test { | 63 class ChromeAppCacheServiceTest : public testing::Test { |
| 40 protected: | 64 public: |
| 41 ChromeAppCacheServiceTest() | 65 ChromeAppCacheServiceTest() |
| 42 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 66 : message_loop_(base::MessageLoop::TYPE_IO), |
| 43 kProtectedManifestURL(kProtectedManifest), | 67 kProtectedManifestURL(kProtectedManifest), |
| 44 kNormalManifestURL(kNormalManifest), | 68 kNormalManifestURL(kNormalManifest), |
| 45 kSessionOnlyManifestURL(kSessionOnlyManifest) { | 69 kSessionOnlyManifestURL(kSessionOnlyManifest), |
| 46 } | 70 file_thread_(BrowserThread::FILE, &message_loop_), |
| 71 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING, |
| 72 &message_loop_), |
| 73 cache_thread_(BrowserThread::CACHE, &message_loop_), |
| 74 io_thread_(BrowserThread::IO, &message_loop_) {} |
| 47 | 75 |
| 76 protected: |
| 48 scoped_refptr<ChromeAppCacheService> CreateAppCacheService( | 77 scoped_refptr<ChromeAppCacheService> CreateAppCacheService( |
| 49 const base::FilePath& appcache_path, | 78 const base::FilePath& appcache_path, |
| 50 bool init_storage); | 79 bool init_storage); |
| 51 void InsertDataIntoAppCache(ChromeAppCacheService* appcache_service); | 80 void InsertDataIntoAppCache(ChromeAppCacheService* appcache_service); |
| 52 | 81 |
| 53 TestBrowserThreadBundle thread_bundle_; | 82 base::MessageLoop message_loop_; |
| 54 base::ScopedTempDir temp_dir_; | 83 base::ScopedTempDir temp_dir_; |
| 55 const GURL kProtectedManifestURL; | 84 const GURL kProtectedManifestURL; |
| 56 const GURL kNormalManifestURL; | 85 const GURL kNormalManifestURL; |
| 57 const GURL kSessionOnlyManifestURL; | 86 const GURL kSessionOnlyManifestURL; |
| 58 | 87 |
| 59 private: | 88 private: |
| 89 BrowserThreadImpl file_thread_; |
| 90 BrowserThreadImpl file_user_blocking_thread_; |
| 91 BrowserThreadImpl cache_thread_; |
| 92 BrowserThreadImpl io_thread_; |
| 60 TestBrowserContext browser_context_; | 93 TestBrowserContext browser_context_; |
| 61 }; | 94 }; |
| 62 | 95 |
| 63 scoped_refptr<ChromeAppCacheService> | 96 scoped_refptr<ChromeAppCacheService> |
| 64 ChromeAppCacheServiceTest::CreateAppCacheService( | 97 ChromeAppCacheServiceTest::CreateAppCacheService( |
| 65 const base::FilePath& appcache_path, | 98 const base::FilePath& appcache_path, |
| 66 bool init_storage) { | 99 bool init_storage) { |
| 67 scoped_refptr<ChromeAppCacheService> appcache_service = | 100 scoped_refptr<ChromeAppCacheService> appcache_service = |
| 68 new ChromeAppCacheService(NULL); | 101 new ChromeAppCacheService(NULL); |
| 69 scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy = | 102 scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy = |
| 70 new quota::MockSpecialStoragePolicy; | 103 new quota::MockSpecialStoragePolicy; |
| 71 mock_policy->AddProtected(kProtectedManifestURL.GetOrigin()); | 104 mock_policy->AddProtected(kProtectedManifestURL.GetOrigin()); |
| 72 mock_policy->AddSessionOnly(kSessionOnlyManifestURL.GetOrigin()); | 105 mock_policy->AddSessionOnly(kSessionOnlyManifestURL.GetOrigin()); |
| 106 scoped_refptr<MockURLRequestContextGetter> mock_request_context_getter = |
| 107 new MockURLRequestContextGetter( |
| 108 browser_context_.GetResourceContext()->GetRequestContext(), |
| 109 message_loop_.message_loop_proxy().get()); |
| 73 BrowserThread::PostTask( | 110 BrowserThread::PostTask( |
| 74 BrowserThread::IO, | 111 BrowserThread::IO, |
| 75 FROM_HERE, | 112 FROM_HERE, |
| 76 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, | 113 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, |
| 77 appcache_service.get(), | 114 appcache_service.get(), |
| 78 appcache_path, | 115 appcache_path, |
| 79 browser_context_.GetResourceContext(), | 116 browser_context_.GetResourceContext(), |
| 80 make_scoped_refptr(browser_context_.GetRequestContext()), | 117 mock_request_context_getter, |
| 81 mock_policy)); | 118 mock_policy)); |
| 82 // Steps needed to initialize the storage of AppCache data. | 119 // Steps needed to initialize the storage of AppCache data. |
| 83 base::RunLoop().RunUntilIdle(); | 120 message_loop_.RunUntilIdle(); |
| 84 if (init_storage) { | 121 if (init_storage) { |
| 85 appcache::AppCacheStorageImpl* storage = | 122 appcache::AppCacheStorageImpl* storage = |
| 86 static_cast<appcache::AppCacheStorageImpl*>( | 123 static_cast<appcache::AppCacheStorageImpl*>( |
| 87 appcache_service->storage()); | 124 appcache_service->storage()); |
| 88 storage->database_->db_connection(); | 125 storage->database_->db_connection(); |
| 89 storage->disk_cache(); | 126 storage->disk_cache(); |
| 90 base::RunLoop().RunUntilIdle(); | 127 message_loop_.RunUntilIdle(); |
| 91 } | 128 } |
| 92 return appcache_service; | 129 return appcache_service; |
| 93 } | 130 } |
| 94 | 131 |
| 95 void ChromeAppCacheServiceTest::InsertDataIntoAppCache( | 132 void ChromeAppCacheServiceTest::InsertDataIntoAppCache( |
| 96 ChromeAppCacheService* appcache_service) { | 133 ChromeAppCacheService* appcache_service) { |
| 97 AppCacheTestHelper appcache_helper; | 134 AppCacheTestHelper appcache_helper; |
| 98 appcache_helper.AddGroupAndCache(appcache_service, kNormalManifestURL); | 135 appcache_helper.AddGroupAndCache(appcache_service, kNormalManifestURL); |
| 99 appcache_helper.AddGroupAndCache(appcache_service, kProtectedManifestURL); | 136 appcache_helper.AddGroupAndCache(appcache_service, kProtectedManifestURL); |
| 100 appcache_helper.AddGroupAndCache(appcache_service, kSessionOnlyManifestURL); | 137 appcache_helper.AddGroupAndCache(appcache_service, kSessionOnlyManifestURL); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 116 | 153 |
| 117 // Create a ChromeAppCacheService and insert data into it | 154 // Create a ChromeAppCacheService and insert data into it |
| 118 scoped_refptr<ChromeAppCacheService> appcache_service = | 155 scoped_refptr<ChromeAppCacheService> appcache_service = |
| 119 CreateAppCacheService(appcache_path, true); | 156 CreateAppCacheService(appcache_path, true); |
| 120 ASSERT_TRUE(base::PathExists(appcache_path)); | 157 ASSERT_TRUE(base::PathExists(appcache_path)); |
| 121 ASSERT_TRUE(base::PathExists(appcache_path.AppendASCII("Index"))); | 158 ASSERT_TRUE(base::PathExists(appcache_path.AppendASCII("Index"))); |
| 122 InsertDataIntoAppCache(appcache_service.get()); | 159 InsertDataIntoAppCache(appcache_service.get()); |
| 123 | 160 |
| 124 // Test: delete the ChromeAppCacheService | 161 // Test: delete the ChromeAppCacheService |
| 125 appcache_service = NULL; | 162 appcache_service = NULL; |
| 126 base::RunLoop().RunUntilIdle(); | 163 message_loop_.RunUntilIdle(); |
| 127 | 164 |
| 128 // Recreate the appcache (for reading the data back) | 165 // Recreate the appcache (for reading the data back) |
| 129 appcache_service = CreateAppCacheService(appcache_path, false); | 166 appcache_service = CreateAppCacheService(appcache_path, false); |
| 130 | 167 |
| 131 // The directory is still there | 168 // The directory is still there |
| 132 ASSERT_TRUE(base::PathExists(appcache_path)); | 169 ASSERT_TRUE(base::PathExists(appcache_path)); |
| 133 | 170 |
| 134 // The appcache data is also there, except the session-only origin. | 171 // The appcache data is also there, except the session-only origin. |
| 135 AppCacheTestHelper appcache_helper; | 172 AppCacheTestHelper appcache_helper; |
| 136 std::set<GURL> origins; | 173 std::set<GURL> origins; |
| 137 appcache_helper.GetOriginsWithCaches(appcache_service.get(), &origins); | 174 appcache_helper.GetOriginsWithCaches(appcache_service.get(), &origins); |
| 138 EXPECT_EQ(2UL, origins.size()); | 175 EXPECT_EQ(2UL, origins.size()); |
| 139 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); | 176 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); |
| 140 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end()); | 177 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end()); |
| 141 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) == | 178 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) == |
| 142 origins.end()); | 179 origins.end()); |
| 143 | 180 |
| 144 // Delete and let cleanup tasks run prior to returning. | 181 // Delete and let cleanup tasks run prior to returning. |
| 145 appcache_service = NULL; | 182 appcache_service = NULL; |
| 146 base::RunLoop().RunUntilIdle(); | 183 message_loop_.RunUntilIdle(); |
| 147 } | 184 } |
| 148 | 185 |
| 149 TEST_F(ChromeAppCacheServiceTest, SaveSessionState) { | 186 TEST_F(ChromeAppCacheServiceTest, SaveSessionState) { |
| 150 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 187 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 151 base::FilePath appcache_path = | 188 base::FilePath appcache_path = |
| 152 temp_dir_.path().Append(kTestingAppCacheDirname); | 189 temp_dir_.path().Append(kTestingAppCacheDirname); |
| 153 | 190 |
| 154 // Create a ChromeAppCacheService and insert data into it | 191 // Create a ChromeAppCacheService and insert data into it |
| 155 scoped_refptr<ChromeAppCacheService> appcache_service = | 192 scoped_refptr<ChromeAppCacheService> appcache_service = |
| 156 CreateAppCacheService(appcache_path, true); | 193 CreateAppCacheService(appcache_path, true); |
| 157 ASSERT_TRUE(base::PathExists(appcache_path)); | 194 ASSERT_TRUE(base::PathExists(appcache_path)); |
| 158 ASSERT_TRUE(base::PathExists(appcache_path.AppendASCII("Index"))); | 195 ASSERT_TRUE(base::PathExists(appcache_path.AppendASCII("Index"))); |
| 159 InsertDataIntoAppCache(appcache_service.get()); | 196 InsertDataIntoAppCache(appcache_service.get()); |
| 160 | 197 |
| 161 // Save session state. This should bypass the destruction-time deletion. | 198 // Save session state. This should bypass the destruction-time deletion. |
| 162 appcache_service->set_force_keep_session_state(); | 199 appcache_service->set_force_keep_session_state(); |
| 163 | 200 |
| 164 // Test: delete the ChromeAppCacheService | 201 // Test: delete the ChromeAppCacheService |
| 165 appcache_service = NULL; | 202 appcache_service = NULL; |
| 166 base::RunLoop().RunUntilIdle(); | 203 message_loop_.RunUntilIdle(); |
| 167 | 204 |
| 168 // Recreate the appcache (for reading the data back) | 205 // Recreate the appcache (for reading the data back) |
| 169 appcache_service = CreateAppCacheService(appcache_path, false); | 206 appcache_service = CreateAppCacheService(appcache_path, false); |
| 170 | 207 |
| 171 // The directory is still there | 208 // The directory is still there |
| 172 ASSERT_TRUE(base::PathExists(appcache_path)); | 209 ASSERT_TRUE(base::PathExists(appcache_path)); |
| 173 | 210 |
| 174 // No appcache data was deleted. | 211 // No appcache data was deleted. |
| 175 AppCacheTestHelper appcache_helper; | 212 AppCacheTestHelper appcache_helper; |
| 176 std::set<GURL> origins; | 213 std::set<GURL> origins; |
| 177 appcache_helper.GetOriginsWithCaches(appcache_service.get(), &origins); | 214 appcache_helper.GetOriginsWithCaches(appcache_service.get(), &origins); |
| 178 EXPECT_EQ(3UL, origins.size()); | 215 EXPECT_EQ(3UL, origins.size()); |
| 179 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); | 216 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); |
| 180 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end()); | 217 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end()); |
| 181 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) != | 218 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) != |
| 182 origins.end()); | 219 origins.end()); |
| 183 | 220 |
| 184 // Delete and let cleanup tasks run prior to returning. | 221 // Delete and let cleanup tasks run prior to returning. |
| 185 appcache_service = NULL; | 222 appcache_service = NULL; |
| 186 base::RunLoop().RunUntilIdle(); | 223 message_loop_.RunUntilIdle(); |
| 187 } | 224 } |
| 188 | 225 |
| 189 } // namespace content | 226 } // namespace content |
| OLD | NEW |