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_path.h" | 5 #include "base/file_path.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "content/browser/browser_thread_impl.h" | 7 #include "content/browser/browser_thread_impl.h" |
8 #include "content/browser/in_process_webkit/dom_storage_context_impl.h" | 8 #include "content/browser/in_process_webkit/dom_storage_context_impl.h" |
9 #include "content/test/test_browser_context.h" | 9 #include "content/test/test_browser_context.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "webkit/dom_storage/dom_storage_types.h" | 11 #include "webkit/dom_storage/dom_storage_types.h" |
12 #include "webkit/quota/mock_special_storage_policy.h" | 12 #include "webkit/quota/mock_special_storage_policy.h" |
13 | 13 |
14 #ifdef ENABLE_NEW_DOM_STORAGE_BACKEND | 14 #ifdef ENABLE_NEW_DOM_STORAGE_BACKEND |
15 // No longer applicable. | 15 // No longer applicable. |
16 #else | 16 #else |
17 | 17 |
18 using content::BrowserContext; | 18 using content::BrowserContext; |
19 using content::BrowserThread; | 19 using content::BrowserThread; |
20 using content::BrowserThreadImpl; | 20 using content::BrowserThreadImpl; |
21 | 21 |
22 class DOMStorageTest : public testing::Test { | 22 class DOMStorageTest : public testing::Test { |
23 public: | 23 public: |
24 DOMStorageTest() | 24 DOMStorageTest() |
25 : message_loop_(MessageLoop::TYPE_IO), | 25 : message_loop_(MessageLoop::TYPE_IO), |
26 webkit_thread_(BrowserThread::WEBKIT_DEPRECATED, &message_loop_) { | 26 webkit_thread_(BrowserThread::WEBKIT_DEPRECATED, &message_loop_), |
| 27 file_thread_(BrowserThread::FILE_USER_BLOCKING, &message_loop_), |
| 28 io_thread_(BrowserThread::IO, &message_loop_) { |
27 } | 29 } |
28 | 30 |
29 protected: | 31 protected: |
30 MessageLoop message_loop_; | 32 MessageLoop message_loop_; |
31 | 33 |
32 private: | 34 private: |
33 BrowserThreadImpl webkit_thread_; | 35 BrowserThreadImpl webkit_thread_; |
| 36 BrowserThreadImpl file_thread_; |
| 37 BrowserThreadImpl io_thread_; |
34 }; | 38 }; |
35 | 39 |
36 TEST_F(DOMStorageTest, SessionOnly) { | 40 TEST_F(DOMStorageTest, SessionOnly) { |
37 GURL session_only_origin("http://www.sessiononly.com"); | 41 GURL session_only_origin("http://www.sessiononly.com"); |
38 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = | 42 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = |
39 new quota::MockSpecialStoragePolicy; | 43 new quota::MockSpecialStoragePolicy; |
40 special_storage_policy->AddSessionOnly(session_only_origin); | 44 special_storage_policy->AddSessionOnly(session_only_origin); |
41 | 45 |
42 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext); | 46 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext); |
43 | 47 |
(...skipping 19 matching lines...) Expand all Loading... |
63 // Inject MockSpecialStoragePolicy into DOMStorageContext. | 67 // Inject MockSpecialStoragePolicy into DOMStorageContext. |
64 DOMStorageContextImpl* dom_storage_context = | 68 DOMStorageContextImpl* dom_storage_context = |
65 static_cast<DOMStorageContextImpl*>( | 69 static_cast<DOMStorageContextImpl*>( |
66 BrowserContext::GetDOMStorageContext(browser_context.get())); | 70 BrowserContext::GetDOMStorageContext(browser_context.get())); |
67 dom_storage_context->special_storage_policy_ = special_storage_policy; | 71 dom_storage_context->special_storage_policy_ = special_storage_policy; |
68 | 72 |
69 // Delete the TestBrowserContext but own the temp dir. This way the | 73 // Delete the TestBrowserContext but own the temp dir. This way the |
70 // temporary data directory stays alive long enough to conduct the test. | 74 // temporary data directory stays alive long enough to conduct the test. |
71 ScopedTempDir temp_dir; | 75 ScopedTempDir temp_dir; |
72 ignore_result(temp_dir.Set(browser_context->TakePath())); | 76 ignore_result(temp_dir.Set(browser_context->TakePath())); |
| 77 message_loop_.RunAllPending(); |
73 browser_context.reset(); | 78 browser_context.reset(); |
74 // Run the message loop to ensure that DOMStorageContext gets destroyed. | 79 // Run the message loop to ensure that DOMStorageContext gets destroyed. |
75 message_loop_.RunAllPending(); | 80 message_loop_.RunAllPending(); |
76 | 81 |
77 // Expected result: the database file for the permanent storage remains but | 82 // Expected result: the database file for the permanent storage remains but |
78 // the database for the session only storage was deleted. | 83 // the database for the session only storage was deleted. |
79 EXPECT_FALSE(file_util::PathExists(session_only_database_path)); | 84 EXPECT_FALSE(file_util::PathExists(session_only_database_path)); |
80 EXPECT_TRUE(file_util::PathExists(permanent_database_path)); | 85 EXPECT_TRUE(file_util::PathExists(permanent_database_path)); |
81 } | 86 } |
82 | 87 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 | 120 |
116 dom_storage_context->SetClearLocalState(true); | 121 dom_storage_context->SetClearLocalState(true); |
117 | 122 |
118 // Save session state. This should bypass the destruction-time deletion. | 123 // Save session state. This should bypass the destruction-time deletion. |
119 dom_storage_context->SaveSessionState(); | 124 dom_storage_context->SaveSessionState(); |
120 | 125 |
121 // Delete the TestBrowserContext but own the temp dir. This way the | 126 // Delete the TestBrowserContext but own the temp dir. This way the |
122 // temporary data directory stays alive long enough to conduct the test. | 127 // temporary data directory stays alive long enough to conduct the test. |
123 ScopedTempDir temp_dir; | 128 ScopedTempDir temp_dir; |
124 ignore_result(temp_dir.Set(browser_context->TakePath())); | 129 ignore_result(temp_dir.Set(browser_context->TakePath())); |
| 130 message_loop_.RunAllPending(); |
125 browser_context.reset(); | 131 browser_context.reset(); |
126 // Run the message loop to ensure that DOMStorageContext gets destroyed. | 132 // Run the message loop to ensure that DOMStorageContext gets destroyed. |
127 message_loop_.RunAllPending(); | 133 message_loop_.RunAllPending(); |
128 | 134 |
129 // Expected result: no database files were deleted because of | 135 // Expected result: no database files were deleted because of |
130 // SaveSessionState. | 136 // SaveSessionState. |
131 EXPECT_TRUE(file_util::PathExists(session_only_database_path)); | 137 EXPECT_TRUE(file_util::PathExists(session_only_database_path)); |
132 EXPECT_TRUE(file_util::PathExists(permanent_database_path)); | 138 EXPECT_TRUE(file_util::PathExists(permanent_database_path)); |
133 } | 139 } |
134 | 140 |
| 141 TEST_F(DOMStorageTest, ClearLocalState) { |
| 142 // Create test files. |
| 143 ScopedTempDir temp_dir; |
| 144 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 145 FilePath domstorage_dir = temp_dir.path().Append( |
| 146 DOMStorageContextImpl::kLocalStorageDirectory); |
| 147 ASSERT_TRUE(file_util::CreateDirectory(domstorage_dir)); |
| 148 |
| 149 FilePath::StringType file_name_1(FILE_PATH_LITERAL("http_foo_0")); |
| 150 file_name_1.append(DOMStorageContextImpl::kLocalStorageExtension); |
| 151 FilePath::StringType file_name_2(FILE_PATH_LITERAL("chrome-devtools_foo_0")); |
| 152 file_name_2.append(DOMStorageContextImpl::kLocalStorageExtension); |
| 153 FilePath temp_file_path_1 = domstorage_dir.Append(file_name_1); |
| 154 FilePath temp_file_path_2 = domstorage_dir.Append(file_name_2); |
| 155 |
| 156 ASSERT_EQ(1, file_util::WriteFile(temp_file_path_1, ".", 1)); |
| 157 ASSERT_EQ(1, file_util::WriteFile(temp_file_path_2, "o", 1)); |
| 158 |
| 159 // Create the scope which will ensure we run the destructor of the webkit |
| 160 // context which should trigger the clean up. |
| 161 { |
| 162 TestBrowserContext browser_context; |
| 163 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = |
| 164 new quota::MockSpecialStoragePolicy; |
| 165 special_storage_policy->AddProtected(GURL("chrome-devtools://")); |
| 166 browser_context.SetSpecialStoragePolicy(special_storage_policy); |
| 167 DOMStorageContextImpl* dom_storage_context = |
| 168 static_cast<DOMStorageContextImpl*>( |
| 169 BrowserContext::GetDOMStorageContext(&browser_context)); |
| 170 dom_storage_context->set_data_path_for_testing(temp_dir.path()); |
| 171 dom_storage_context->SetClearLocalState(true); |
| 172 message_loop_.RunAllPending(); |
| 173 } |
| 174 message_loop_.RunAllPending(); |
| 175 |
| 176 // Because we specified https for scheme to be skipped the second file |
| 177 // should survive and the first go into vanity. |
| 178 ASSERT_FALSE(file_util::PathExists(temp_file_path_1)); |
| 179 ASSERT_TRUE(file_util::PathExists(temp_file_path_2)); |
| 180 } |
| 181 |
135 #endif // ENABLE_NEW_DOM_STORAGE_BACKEND | 182 #endif // ENABLE_NEW_DOM_STORAGE_BACKEND |
OLD | NEW |