| 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/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/scoped_temp_dir.h" | 8 #include "base/scoped_temp_dir.h" |
| 9 #include "content/browser/appcache/chrome_appcache_service.h" | 9 #include "content/browser/appcache/chrome_appcache_service.h" |
| 10 #include "content/browser/browser_thread.h" | 10 #include "content/browser/browser_thread.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "webkit/appcache/appcache_database.h" | 12 #include "webkit/appcache/appcache_database.h" |
| 13 #include "webkit/appcache/appcache_storage_impl.h" | 13 #include "webkit/appcache/appcache_storage_impl.h" |
| 14 #include "webkit/appcache/appcache_test_helper.h" | 14 #include "webkit/appcache/appcache_test_helper.h" |
| 15 #include "webkit/quota/mock_special_storage_policy.h" | 15 #include "webkit/quota/mock_special_storage_policy.h" |
| 16 | 16 |
| 17 #include <set> | 17 #include <set> |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 const FilePath::CharType kTestingAppCacheDirname[] = | 20 const FilePath::CharType kTestingAppCacheDirname[] = |
| 21 FILE_PATH_LITERAL("Application Cache"); | 21 FILE_PATH_LITERAL("Application Cache"); |
| 22 | 22 |
| 23 // Examples of a protected and an unprotected origin, to be used througout the | 23 // Examples of a protected and an unprotected origin, to be used througout the |
| 24 // test. | 24 // test. |
| 25 const char kProtectedManifest[] = "http://www.protected.com/cache.manifest"; | 25 const char kProtectedManifest[] = "http://www.protected.com/cache.manifest"; |
| 26 const char kNormalManifest[] = "http://www.normal.com/cache.manifest"; | 26 const char kNormalManifest[] = "http://www.normal.com/cache.manifest"; |
| 27 const char kSessionOnlyManifest[] = "http://www.sessiononly.com/cache.manifest"; |
| 27 | 28 |
| 28 } // namespace | 29 } // namespace |
| 29 | 30 |
| 30 namespace appcache { | 31 namespace appcache { |
| 31 | 32 |
| 32 class ChromeAppCacheServiceTest : public testing::Test { | 33 class ChromeAppCacheServiceTest : public testing::Test { |
| 33 public: | 34 public: |
| 34 ChromeAppCacheServiceTest() | 35 ChromeAppCacheServiceTest() |
| 35 : message_loop_(MessageLoop::TYPE_IO), | 36 : message_loop_(MessageLoop::TYPE_IO), |
| 36 kProtectedManifestURL(kProtectedManifest), | 37 kProtectedManifestURL(kProtectedManifest), |
| 37 kNormalManifestURL(kNormalManifest), | 38 kNormalManifestURL(kNormalManifest), |
| 39 kSessionOnlyManifestURL(kSessionOnlyManifest), |
| 38 db_thread_(BrowserThread::DB, &message_loop_), | 40 db_thread_(BrowserThread::DB, &message_loop_), |
| 39 file_thread_(BrowserThread::FILE, &message_loop_), | 41 file_thread_(BrowserThread::FILE, &message_loop_), |
| 40 cache_thread_(BrowserThread::CACHE, &message_loop_), | 42 cache_thread_(BrowserThread::CACHE, &message_loop_), |
| 41 io_thread_(BrowserThread::IO, &message_loop_) { | 43 io_thread_(BrowserThread::IO, &message_loop_) { |
| 42 } | 44 } |
| 43 | 45 |
| 44 protected: | 46 protected: |
| 45 scoped_refptr<ChromeAppCacheService> CreateAppCacheService( | 47 scoped_refptr<ChromeAppCacheService> CreateAppCacheService( |
| 46 const FilePath& appcache_path, | 48 const FilePath& appcache_path, |
| 47 bool init_storage); | 49 bool init_storage); |
| 48 void InsertDataIntoAppCache(ChromeAppCacheService* appcache_service); | 50 void InsertDataIntoAppCache(ChromeAppCacheService* appcache_service); |
| 49 | 51 |
| 50 MessageLoop message_loop_; | 52 MessageLoop message_loop_; |
| 51 ScopedTempDir temp_dir_; | 53 ScopedTempDir temp_dir_; |
| 52 const GURL kProtectedManifestURL; | 54 const GURL kProtectedManifestURL; |
| 53 const GURL kNormalManifestURL; | 55 const GURL kNormalManifestURL; |
| 56 const GURL kSessionOnlyManifestURL; |
| 54 | 57 |
| 55 private: | 58 private: |
| 56 BrowserThread db_thread_; | 59 BrowserThread db_thread_; |
| 57 BrowserThread file_thread_; | 60 BrowserThread file_thread_; |
| 58 BrowserThread cache_thread_; | 61 BrowserThread cache_thread_; |
| 59 BrowserThread io_thread_; | 62 BrowserThread io_thread_; |
| 60 }; | 63 }; |
| 61 | 64 |
| 62 scoped_refptr<ChromeAppCacheService> | 65 scoped_refptr<ChromeAppCacheService> |
| 63 ChromeAppCacheServiceTest::CreateAppCacheService( | 66 ChromeAppCacheServiceTest::CreateAppCacheService( |
| 64 const FilePath& appcache_path, | 67 const FilePath& appcache_path, |
| 65 bool init_storage) { | 68 bool init_storage) { |
| 66 scoped_refptr<ChromeAppCacheService> appcache_service = | 69 scoped_refptr<ChromeAppCacheService> appcache_service = |
| 67 new ChromeAppCacheService(NULL); | 70 new ChromeAppCacheService(NULL); |
| 68 const content::ResourceContext* resource_context = NULL; | 71 const content::ResourceContext* resource_context = NULL; |
| 69 scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy = | 72 scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy = |
| 70 new quota::MockSpecialStoragePolicy; | 73 new quota::MockSpecialStoragePolicy; |
| 71 mock_policy->AddProtected(kProtectedManifestURL.GetOrigin()); | 74 mock_policy->AddProtected(kProtectedManifestURL.GetOrigin()); |
| 75 mock_policy->AddSessionOnly(kSessionOnlyManifestURL.GetOrigin()); |
| 72 BrowserThread::PostTask( | 76 BrowserThread::PostTask( |
| 73 BrowserThread::IO, FROM_HERE, | 77 BrowserThread::IO, FROM_HERE, |
| 74 NewRunnableMethod(appcache_service.get(), | 78 NewRunnableMethod(appcache_service.get(), |
| 75 &ChromeAppCacheService::InitializeOnIOThread, | 79 &ChromeAppCacheService::InitializeOnIOThread, |
| 76 appcache_path, | 80 appcache_path, |
| 77 resource_context, | 81 resource_context, |
| 78 mock_policy)); | 82 mock_policy)); |
| 79 // Steps needed to initialize the storage of AppCache data. | 83 // Steps needed to initialize the storage of AppCache data. |
| 80 message_loop_.RunAllPending(); | 84 message_loop_.RunAllPending(); |
| 81 if (init_storage) { | 85 if (init_storage) { |
| 82 appcache::AppCacheStorageImpl* storage = | 86 appcache::AppCacheStorageImpl* storage = |
| 83 static_cast<appcache::AppCacheStorageImpl*>( | 87 static_cast<appcache::AppCacheStorageImpl*>( |
| 84 appcache_service->storage()); | 88 appcache_service->storage()); |
| 85 storage->database_->db_connection(); | 89 storage->database_->db_connection(); |
| 86 storage->disk_cache(); | 90 storage->disk_cache(); |
| 87 message_loop_.RunAllPending(); | 91 message_loop_.RunAllPending(); |
| 88 } | 92 } |
| 89 return appcache_service; | 93 return appcache_service; |
| 90 } | 94 } |
| 91 | 95 |
| 92 void ChromeAppCacheServiceTest::InsertDataIntoAppCache( | 96 void ChromeAppCacheServiceTest::InsertDataIntoAppCache( |
| 93 ChromeAppCacheService* appcache_service) { | 97 ChromeAppCacheService* appcache_service) { |
| 94 AppCacheTestHelper appcache_helper; | 98 AppCacheTestHelper appcache_helper; |
| 95 appcache_helper.AddGroupAndCache(appcache_service, kNormalManifestURL); | 99 appcache_helper.AddGroupAndCache(appcache_service, kNormalManifestURL); |
| 96 appcache_helper.AddGroupAndCache(appcache_service, kProtectedManifestURL); | 100 appcache_helper.AddGroupAndCache(appcache_service, kProtectedManifestURL); |
| 101 appcache_helper.AddGroupAndCache(appcache_service, kSessionOnlyManifestURL); |
| 97 | 102 |
| 98 // Verify that adding the data succeeded | 103 // Verify that adding the data succeeded |
| 99 std::set<GURL> origins; | 104 std::set<GURL> origins; |
| 100 appcache_helper.GetOriginsWithCaches(appcache_service, &origins); | 105 appcache_helper.GetOriginsWithCaches(appcache_service, &origins); |
| 101 ASSERT_EQ(2UL, origins.size()); | 106 ASSERT_EQ(3UL, origins.size()); |
| 102 ASSERT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); | 107 ASSERT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); |
| 103 ASSERT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end()); | 108 ASSERT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end()); |
| 109 ASSERT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) != |
| 110 origins.end()); |
| 104 } | 111 } |
| 105 | 112 |
| 106 TEST_F(ChromeAppCacheServiceTest, KeepOnDestruction) { | 113 TEST_F(ChromeAppCacheServiceTest, KeepOnDestruction) { |
| 107 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 114 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 108 FilePath appcache_path = temp_dir_.path().Append(kTestingAppCacheDirname); | 115 FilePath appcache_path = temp_dir_.path().Append(kTestingAppCacheDirname); |
| 109 | 116 |
| 110 // Create a ChromeAppCacheService and insert data into it | 117 // Create a ChromeAppCacheService and insert data into it |
| 111 scoped_refptr<ChromeAppCacheService> appcache_service = | 118 scoped_refptr<ChromeAppCacheService> appcache_service = |
| 112 CreateAppCacheService(appcache_path, true); | 119 CreateAppCacheService(appcache_path, true); |
| 113 ASSERT_TRUE(file_util::PathExists(appcache_path)); | 120 ASSERT_TRUE(file_util::PathExists(appcache_path)); |
| 114 ASSERT_TRUE(file_util::PathExists(appcache_path.AppendASCII("Index"))); | 121 ASSERT_TRUE(file_util::PathExists(appcache_path.AppendASCII("Index"))); |
| 115 InsertDataIntoAppCache(appcache_service); | 122 InsertDataIntoAppCache(appcache_service); |
| 116 | 123 |
| 117 // Test: delete the ChromeAppCacheService | 124 // Test: delete the ChromeAppCacheService |
| 118 appcache_service->set_clear_local_state_on_exit(false); | 125 appcache_service->set_clear_local_state_on_exit(false); |
| 119 appcache_service = NULL; | 126 appcache_service = NULL; |
| 120 message_loop_.RunAllPending(); | 127 message_loop_.RunAllPending(); |
| 121 | 128 |
| 122 // Recreate the appcache (for reading the data back) | 129 // Recreate the appcache (for reading the data back) |
| 123 appcache_service = CreateAppCacheService(appcache_path, false); | 130 appcache_service = CreateAppCacheService(appcache_path, false); |
| 124 | 131 |
| 125 // The directory is still there | 132 // The directory is still there |
| 126 ASSERT_TRUE(file_util::PathExists(appcache_path)); | 133 ASSERT_TRUE(file_util::PathExists(appcache_path)); |
| 127 | 134 |
| 128 // The appcache data is also there | 135 // The appcache data is also there, except the session-only origin. |
| 129 AppCacheTestHelper appcache_helper; | 136 AppCacheTestHelper appcache_helper; |
| 130 std::set<GURL> origins; | 137 std::set<GURL> origins; |
| 131 appcache_helper.GetOriginsWithCaches(appcache_service, &origins); | 138 appcache_helper.GetOriginsWithCaches(appcache_service, &origins); |
| 132 EXPECT_EQ(2UL, origins.size()); | 139 EXPECT_EQ(2UL, origins.size()); |
| 133 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); | 140 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); |
| 134 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end()); | 141 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) != origins.end()); |
| 142 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) == |
| 143 origins.end()); |
| 135 | 144 |
| 136 // Delete and let cleanup tasks run prior to returning. | 145 // Delete and let cleanup tasks run prior to returning. |
| 137 appcache_service = NULL; | 146 appcache_service = NULL; |
| 138 message_loop_.RunAllPending(); | 147 message_loop_.RunAllPending(); |
| 139 } | 148 } |
| 140 | 149 |
| 141 TEST_F(ChromeAppCacheServiceTest, RemoveOnDestruction) { | 150 TEST_F(ChromeAppCacheServiceTest, RemoveOnDestruction) { |
| 142 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 151 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 143 FilePath appcache_path = temp_dir_.path().Append(kTestingAppCacheDirname); | 152 FilePath appcache_path = temp_dir_.path().Append(kTestingAppCacheDirname); |
| 144 | 153 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 161 ASSERT_TRUE(file_util::PathExists(appcache_path)); | 170 ASSERT_TRUE(file_util::PathExists(appcache_path)); |
| 162 | 171 |
| 163 // The appcache data for the protected origin is there, and the data for the | 172 // The appcache data for the protected origin is there, and the data for the |
| 164 // unprotected origin was deleted. | 173 // unprotected origin was deleted. |
| 165 AppCacheTestHelper appcache_helper; | 174 AppCacheTestHelper appcache_helper; |
| 166 std::set<GURL> origins; | 175 std::set<GURL> origins; |
| 167 appcache_helper.GetOriginsWithCaches(appcache_service, &origins); | 176 appcache_helper.GetOriginsWithCaches(appcache_service, &origins); |
| 168 EXPECT_EQ(1UL, origins.size()); | 177 EXPECT_EQ(1UL, origins.size()); |
| 169 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); | 178 EXPECT_TRUE(origins.find(kProtectedManifestURL.GetOrigin()) != origins.end()); |
| 170 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) == origins.end()); | 179 EXPECT_TRUE(origins.find(kNormalManifestURL.GetOrigin()) == origins.end()); |
| 180 EXPECT_TRUE(origins.find(kSessionOnlyManifestURL.GetOrigin()) == |
| 181 origins.end()); |
| 171 | 182 |
| 172 // Delete and let cleanup tasks run prior to returning. | 183 // Delete and let cleanup tasks run prior to returning. |
| 173 appcache_service = NULL; | 184 appcache_service = NULL; |
| 174 message_loop_.RunAllPending(); | 185 message_loop_.RunAllPending(); |
| 175 } | 186 } |
| 176 | 187 |
| 177 } // namespace appcache | 188 } // namespace appcache |
| OLD | NEW |