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 "base/scoped_temp_dir.h" | 7 #include "base/scoped_temp_dir.h" |
8 #include "base/test/thread_test_helper.h" | 8 #include "base/test/thread_test_helper.h" |
| 9 #include "base/utf_string_conversions.h" |
9 #include "chrome/test/in_process_browser_test.h" | 10 #include "chrome/test/in_process_browser_test.h" |
10 #include "chrome/test/testing_profile.h" | 11 #include "chrome/test/testing_profile.h" |
| 12 #include "content/browser/in_process_webkit/dom_storage_area.h" |
11 #include "content/browser/in_process_webkit/dom_storage_context.h" | 13 #include "content/browser/in_process_webkit/dom_storage_context.h" |
| 14 #include "content/browser/in_process_webkit/dom_storage_message_filter.h" |
| 15 #include "content/browser/in_process_webkit/dom_storage_namespace.h" |
12 #include "content/browser/in_process_webkit/webkit_context.h" | 16 #include "content/browser/in_process_webkit/webkit_context.h" |
13 | 17 |
| 18 namespace base { |
| 19 class MessageLoopProxy; |
| 20 } |
| 21 |
14 typedef InProcessBrowserTest DOMStorageBrowserTest; | 22 typedef InProcessBrowserTest DOMStorageBrowserTest; |
15 | 23 |
| 24 namespace dom_storage_test_helpers { |
| 25 |
| 26 class ChangingSettingsTestHelper : public base::ThreadTestHelper { |
| 27 public: |
| 28 ChangingSettingsTestHelper(base::MessageLoopProxy* target_thread, |
| 29 DOMStorageContext* storage_context) |
| 30 : base::ThreadTestHelper(target_thread), |
| 31 storage_context_(storage_context){ |
| 32 } |
| 33 virtual ~ChangingSettingsTestHelper() { |
| 34 } |
| 35 void RunTest() { |
| 36 DOMStorageNamespace* storage_namespace = |
| 37 storage_context_->GetStorageNamespace(0, true); |
| 38 |
| 39 // After a storage for an origin is created, it can always be retrieved, |
| 40 // even if the content setting for that origin is changed. |
| 41 string16 origin1(ASCIIToUTF16("http://www.someorigin.com")); |
| 42 string16 origin2(ASCIIToUTF16("http://www.someotherorigin.com")); |
| 43 |
| 44 DOMStorageArea* storage_area1 = |
| 45 storage_namespace->GetStorageArea(origin1, true); |
| 46 EXPECT_TRUE(storage_area1 != NULL); |
| 47 EXPECT_TRUE(storage_area1 == |
| 48 storage_namespace->GetStorageArea(origin1, false)); |
| 49 EXPECT_TRUE(storage_area1 == |
| 50 storage_namespace->GetStorageArea(origin1, true)); |
| 51 |
| 52 DOMStorageArea* storage_area2 = |
| 53 storage_namespace->GetStorageArea(origin2, false); |
| 54 EXPECT_TRUE(storage_area2 != NULL); |
| 55 EXPECT_TRUE(storage_area2 != storage_area1); |
| 56 EXPECT_TRUE(storage_area2 == |
| 57 storage_namespace->GetStorageArea(origin2, false)); |
| 58 EXPECT_TRUE(storage_area2 == |
| 59 storage_namespace->GetStorageArea(origin2, true)); |
| 60 set_test_result(true); |
| 61 } |
| 62 DOMStorageContext* storage_context_; |
| 63 }; |
| 64 |
| 65 class SessionOnlyTestHelper : public base::ThreadTestHelper { |
| 66 public: |
| 67 SessionOnlyTestHelper(base::MessageLoopProxy* target_thread, |
| 68 DOMStorageContext* storage_context, |
| 69 WebKitContext* webkit_context) |
| 70 : base::ThreadTestHelper(target_thread), |
| 71 storage_context_(storage_context), |
| 72 webkit_context_(webkit_context) { |
| 73 } |
| 74 virtual ~SessionOnlyTestHelper() { |
| 75 } |
| 76 void RunTest() { |
| 77 DOMStorageNamespace* storage_namespace = |
| 78 storage_context_->GetStorageNamespace(0, true); |
| 79 |
| 80 // Write data into the session-only localStorage and the permanent |
| 81 // localStorage. |
| 82 string16 session_only_origin(ASCIIToUTF16("http://www.sessiononly.com")); |
| 83 string16 permanent_origin(ASCIIToUTF16("http://www.permanent.com")); |
| 84 |
| 85 DOMStorageArea* session_only_storage_area = |
| 86 storage_namespace->GetStorageArea(session_only_origin, false); |
| 87 EXPECT_TRUE(session_only_storage_area != NULL); |
| 88 DOMStorageArea* permanent_storage_area = |
| 89 storage_namespace->GetStorageArea(permanent_origin, true); |
| 90 EXPECT_TRUE(permanent_storage_area != NULL); |
| 91 |
| 92 string16 key(ASCIIToUTF16("Key")); |
| 93 string16 value(ASCIIToUTF16("Value")); |
| 94 WebKit::WebStorageArea::Result result; |
| 95 NullableString16 old_value; |
| 96 scoped_refptr<DOMStorageMessageFilter> filter = |
| 97 new DOMStorageMessageFilter(0, webkit_context_); |
| 98 |
| 99 // Invoke DOMStorageArea::SetItem with this indirection, to have the message |
| 100 // filters set up correctly. |
| 101 filter->OnSetItem(session_only_storage_area->id(), key, value, GURL(""), |
| 102 &result, &old_value); |
| 103 EXPECT_TRUE(result == WebKit::WebStorageArea::ResultOK); |
| 104 filter->OnSetItem(permanent_storage_area->id(), key, value, GURL(""), |
| 105 &result, &old_value); |
| 106 EXPECT_TRUE(result == WebKit::WebStorageArea::ResultOK); |
| 107 |
| 108 set_test_result(true); |
| 109 } |
| 110 DOMStorageContext* storage_context_; |
| 111 WebKitContext* webkit_context_; |
| 112 }; |
| 113 |
| 114 } // namespace dom_storage_test_helpers |
| 115 |
16 // In proc browser test is needed here because ClearLocalState indirectly calls | 116 // In proc browser test is needed here because ClearLocalState indirectly calls |
17 // WebKit's isMainThread through WebSecurityOrigin->SecurityOrigin. | 117 // WebKit's isMainThread through WebSecurityOrigin->SecurityOrigin. |
18 IN_PROC_BROWSER_TEST_F(DOMStorageBrowserTest, ClearLocalState) { | 118 IN_PROC_BROWSER_TEST_F(DOMStorageBrowserTest, ClearLocalState) { |
19 // Create test files. | 119 // Create test files. |
20 ScopedTempDir temp_dir; | 120 ScopedTempDir temp_dir; |
21 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 121 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
22 FilePath domstorage_dir = temp_dir.path().Append( | 122 FilePath domstorage_dir = temp_dir.path().Append( |
23 DOMStorageContext::kLocalStorageDirectory); | 123 DOMStorageContext::kLocalStorageDirectory); |
24 ASSERT_TRUE(file_util::CreateDirectory(domstorage_dir)); | 124 ASSERT_TRUE(file_util::CreateDirectory(domstorage_dir)); |
25 | 125 |
(...skipping 19 matching lines...) Expand all Loading... |
45 scoped_refptr<base::ThreadTestHelper> helper( | 145 scoped_refptr<base::ThreadTestHelper> helper( |
46 new base::ThreadTestHelper( | 146 new base::ThreadTestHelper( |
47 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::WEBKIT))); | 147 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::WEBKIT))); |
48 ASSERT_TRUE(helper->Run()); | 148 ASSERT_TRUE(helper->Run()); |
49 | 149 |
50 // Because we specified https for scheme to be skipped the second file | 150 // Because we specified https for scheme to be skipped the second file |
51 // should survive and the first go into vanity. | 151 // should survive and the first go into vanity. |
52 ASSERT_FALSE(file_util::PathExists(temp_file_path_1)); | 152 ASSERT_FALSE(file_util::PathExists(temp_file_path_1)); |
53 ASSERT_TRUE(file_util::PathExists(temp_file_path_2)); | 153 ASSERT_TRUE(file_util::PathExists(temp_file_path_2)); |
54 } | 154 } |
| 155 |
| 156 IN_PROC_BROWSER_TEST_F(DOMStorageBrowserTest, ChangingSettings) { |
| 157 ScopedTempDir temp_dir; |
| 158 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 159 FilePath domstorage_dir = temp_dir.path().Append( |
| 160 DOMStorageContext::kLocalStorageDirectory); |
| 161 ASSERT_TRUE(file_util::CreateDirectory(domstorage_dir)); |
| 162 |
| 163 TestingProfile profile; |
| 164 DOMStorageContext* storage_context = |
| 165 profile.GetWebKitContext()->dom_storage_context(); |
| 166 |
| 167 scoped_refptr<dom_storage_test_helpers::ChangingSettingsTestHelper> helper( |
| 168 new dom_storage_test_helpers::ChangingSettingsTestHelper( |
| 169 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::WEBKIT), |
| 170 storage_context)); |
| 171 ASSERT_TRUE(helper->Run()); |
| 172 } |
| 173 |
| 174 IN_PROC_BROWSER_TEST_F(DOMStorageBrowserTest, SessionOnly) { |
| 175 ScopedTempDir temp_dir; |
| 176 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 177 FilePath domstorage_dir = temp_dir.path().Append( |
| 178 DOMStorageContext::kLocalStorageDirectory); |
| 179 ASSERT_TRUE(file_util::CreateDirectory(domstorage_dir)); |
| 180 |
| 181 // Create the scope which will ensure we run the destructor of the webkit |
| 182 // context which should trigger the clean up. |
| 183 { |
| 184 TestingProfile profile; |
| 185 DOMStorageContext* storage_context = |
| 186 profile.GetWebKitContext()->dom_storage_context(); |
| 187 storage_context->set_data_path(temp_dir.path()); |
| 188 |
| 189 scoped_refptr<dom_storage_test_helpers::SessionOnlyTestHelper> helper( |
| 190 new dom_storage_test_helpers::SessionOnlyTestHelper( |
| 191 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::WEBKIT), |
| 192 storage_context, |
| 193 profile.GetWebKitContext())); |
| 194 ASSERT_TRUE(helper->Run()); |
| 195 } |
| 196 // Make sure we wait until the destructor has run. |
| 197 scoped_refptr<base::ThreadTestHelper> helper( |
| 198 new base::ThreadTestHelper( |
| 199 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::WEBKIT))); |
| 200 ASSERT_TRUE(helper->Run()); |
| 201 |
| 202 // Expected result: the database file for the permanent storage was created, |
| 203 // but the database for the session only storage was not created. |
| 204 FilePath::StringType session_only_database( |
| 205 FILE_PATH_LITERAL("http_www.sessiononly.com_0")); |
| 206 FilePath::StringType permanent_database( |
| 207 FILE_PATH_LITERAL("http_www.permanent.com_0")); |
| 208 session_only_database.append(DOMStorageContext::kLocalStorageExtension); |
| 209 permanent_database.append(DOMStorageContext::kLocalStorageExtension); |
| 210 |
| 211 FilePath session_only_database_path = |
| 212 domstorage_dir.Append(session_only_database); |
| 213 FilePath permanent_database_path = |
| 214 domstorage_dir.Append(permanent_database); |
| 215 |
| 216 EXPECT_FALSE(file_util::PathExists(session_only_database_path)); |
| 217 EXPECT_TRUE(file_util::PathExists(permanent_database_path)); |
| 218 } |
OLD | NEW |