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