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/webkit_context.h" | 8 #include "content/browser/in_process_webkit/webkit_context.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" |
(...skipping 15 matching lines...) Expand all Loading... |
26 private: | 26 private: |
27 BrowserThreadImpl webkit_thread_; | 27 BrowserThreadImpl webkit_thread_; |
28 }; | 28 }; |
29 | 29 |
30 TEST_F(DOMStorageTest, SessionOnly) { | 30 TEST_F(DOMStorageTest, SessionOnly) { |
31 GURL session_only_origin("http://www.sessiononly.com"); | 31 GURL session_only_origin("http://www.sessiononly.com"); |
32 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = | 32 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = |
33 new quota::MockSpecialStoragePolicy; | 33 new quota::MockSpecialStoragePolicy; |
34 special_storage_policy->AddSessionOnly(session_only_origin); | 34 special_storage_policy->AddSessionOnly(session_only_origin); |
35 | 35 |
36 // A scope for deleting TestBrowserContext early enough. | 36 TestBrowserContext browser_context; |
37 { | |
38 TestBrowserContext browser_context; | |
39 | 37 |
40 // Create databases for permanent and session-only origins. | 38 // Create databases for permanent and session-only origins. |
41 FilePath domstorage_dir = browser_context.GetPath().Append( | 39 FilePath domstorage_dir = browser_context.GetPath().Append( |
42 DOMStorageContext::kLocalStorageDirectory); | 40 DOMStorageContext::kLocalStorageDirectory); |
43 FilePath::StringType session_only_database( | 41 FilePath::StringType session_only_database( |
44 FILE_PATH_LITERAL("http_www.sessiononly.com_0")); | 42 FILE_PATH_LITERAL("http_www.sessiononly.com_0")); |
45 FilePath::StringType permanent_database( | 43 FilePath::StringType permanent_database( |
46 FILE_PATH_LITERAL("http_www.permanent.com_0")); | 44 FILE_PATH_LITERAL("http_www.permanent.com_0")); |
47 session_only_database.append(DOMStorageContext::kLocalStorageExtension); | 45 session_only_database.append(DOMStorageContext::kLocalStorageExtension); |
48 permanent_database.append(DOMStorageContext::kLocalStorageExtension); | 46 permanent_database.append(DOMStorageContext::kLocalStorageExtension); |
49 FilePath session_only_database_path = | 47 FilePath session_only_database_path = |
50 domstorage_dir.Append(session_only_database); | 48 domstorage_dir.Append(session_only_database); |
51 FilePath permanent_database_path = | 49 FilePath permanent_database_path = |
52 domstorage_dir.Append(permanent_database); | 50 domstorage_dir.Append(permanent_database); |
53 | 51 |
54 ASSERT_TRUE(file_util::CreateDirectory(domstorage_dir)); | 52 ASSERT_TRUE(file_util::CreateDirectory(domstorage_dir)); |
55 | 53 |
56 ASSERT_EQ(1, file_util::WriteFile(session_only_database_path, ".", 1)); | 54 ASSERT_EQ(1, file_util::WriteFile(session_only_database_path, ".", 1)); |
57 ASSERT_EQ(1, file_util::WriteFile(permanent_database_path, ".", 1)); | 55 ASSERT_EQ(1, file_util::WriteFile(permanent_database_path, ".", 1)); |
58 | 56 |
59 // Inject MockSpecialStoragePolicy into DOMStorageContext. | 57 // Inject MockSpecialStoragePolicy into DOMStorageContext. |
60 browser_context.GetWebKitContext()->dom_storage_context()-> | 58 browser_context.GetWebKitContext()->dom_storage_context()-> |
61 special_storage_policy_ = special_storage_policy; | 59 special_storage_policy_ = special_storage_policy; |
62 | 60 |
63 // Tell the WebKitContext explicitly do clean up the session-only | 61 // Delete the WebKitContext before destroying TestBrowserContext. This way the |
64 // data. TestBrowserContext doesn't do it automatically | 62 // temporary data directory stays alive long enough to conduct the test. |
65 // when destructed. The deletion is done immediately since we're on the | 63 browser_context.webkit_context_ = NULL; |
66 // WEBKIT thread (created by the test). | |
67 browser_context.GetWebKitContext()->DeleteSessionOnlyData(); | |
68 | |
69 // Expected result: the database file for the permanent storage remains but | |
70 // the database for the session only storage was deleted. | |
71 EXPECT_FALSE(file_util::PathExists(session_only_database_path)); | |
72 EXPECT_TRUE(file_util::PathExists(permanent_database_path)); | |
73 } | |
74 // Run the message loop to ensure that DOMStorageContext gets destroyed. | 64 // Run the message loop to ensure that DOMStorageContext gets destroyed. |
75 message_loop_.RunAllPending(); | 65 message_loop_.RunAllPending(); |
| 66 |
| 67 // Expected result: the database file for the permanent storage remains but |
| 68 // the database for the session only storage was deleted. |
| 69 EXPECT_FALSE(file_util::PathExists(session_only_database_path)); |
| 70 EXPECT_TRUE(file_util::PathExists(permanent_database_path)); |
76 } | 71 } |
| 72 |
| 73 TEST_F(DOMStorageTest, SaveSessionState) { |
| 74 GURL session_only_origin("http://www.sessiononly.com"); |
| 75 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = |
| 76 new quota::MockSpecialStoragePolicy; |
| 77 special_storage_policy->AddSessionOnly(session_only_origin); |
| 78 |
| 79 TestBrowserContext browser_context; |
| 80 |
| 81 // Create databases for permanent and session-only origins. |
| 82 FilePath domstorage_dir = browser_context.GetPath().Append( |
| 83 DOMStorageContext::kLocalStorageDirectory); |
| 84 FilePath::StringType session_only_database( |
| 85 FILE_PATH_LITERAL("http_www.sessiononly.com_0")); |
| 86 FilePath::StringType permanent_database( |
| 87 FILE_PATH_LITERAL("http_www.permanent.com_0")); |
| 88 session_only_database.append(DOMStorageContext::kLocalStorageExtension); |
| 89 permanent_database.append(DOMStorageContext::kLocalStorageExtension); |
| 90 FilePath session_only_database_path = |
| 91 domstorage_dir.Append(session_only_database); |
| 92 FilePath permanent_database_path = |
| 93 domstorage_dir.Append(permanent_database); |
| 94 |
| 95 ASSERT_TRUE(file_util::CreateDirectory(domstorage_dir)); |
| 96 |
| 97 ASSERT_EQ(1, file_util::WriteFile(session_only_database_path, ".", 1)); |
| 98 ASSERT_EQ(1, file_util::WriteFile(permanent_database_path, ".", 1)); |
| 99 |
| 100 // Inject MockSpecialStoragePolicy into DOMStorageContext. |
| 101 DOMStorageContext* dom_storage_context = |
| 102 browser_context.GetWebKitContext()->dom_storage_context(); |
| 103 dom_storage_context->special_storage_policy_ = special_storage_policy; |
| 104 |
| 105 dom_storage_context->set_clear_local_state_on_exit_(true); |
| 106 |
| 107 // Save session state. This should bypass the destruction-time deletion. |
| 108 dom_storage_context->SaveSessionState(); |
| 109 |
| 110 // Delete the WebKitContext before destroying TestBrowserContext. This way the |
| 111 // temporary data directory stays alive long enough to conduct the test. |
| 112 browser_context.webkit_context_ = NULL; |
| 113 // Run the message loop to ensure that DOMStorageContext gets destroyed. |
| 114 message_loop_.RunAllPending(); |
| 115 |
| 116 // Expected result: no database files were deleted because of |
| 117 // SaveSessionState. |
| 118 EXPECT_TRUE(file_util::PathExists(session_only_database_path)); |
| 119 EXPECT_TRUE(file_util::PathExists(permanent_database_path)); |
| 120 } |
OLD | NEW |