Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(415)

Side by Side Diff: content/browser/in_process_webkit/dom_storage_unittest.cc

Issue 9419033: Move creation of BrowserContext objects that live in content to content, instead of depending on th… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Fix memory leaks in tests Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
11 #include "webkit/quota/mock_special_storage_policy.h" 11 #include "webkit/quota/mock_special_storage_policy.h"
12 12
13 using content::BrowserContext;
13 using content::BrowserThread; 14 using content::BrowserThread;
14 using content::BrowserThreadImpl; 15 using content::BrowserThreadImpl;
15 16
16 class DOMStorageTest : public testing::Test { 17 class DOMStorageTest : public testing::Test {
17 public: 18 public:
18 DOMStorageTest() 19 DOMStorageTest()
19 : message_loop_(MessageLoop::TYPE_IO), 20 : message_loop_(MessageLoop::TYPE_IO),
20 webkit_thread_(BrowserThread::WEBKIT_DEPRECATED, &message_loop_) { 21 webkit_thread_(BrowserThread::WEBKIT_DEPRECATED, &message_loop_) {
21 } 22 }
22 23
23 protected: 24 protected:
24 MessageLoop message_loop_; 25 MessageLoop message_loop_;
25 26
26 private: 27 private:
27 BrowserThreadImpl webkit_thread_; 28 BrowserThreadImpl webkit_thread_;
28 }; 29 };
29 30
30 TEST_F(DOMStorageTest, SessionOnly) { 31 TEST_F(DOMStorageTest, SessionOnly) {
31 GURL session_only_origin("http://www.sessiononly.com"); 32 GURL session_only_origin("http://www.sessiononly.com");
32 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = 33 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy =
33 new quota::MockSpecialStoragePolicy; 34 new quota::MockSpecialStoragePolicy;
34 special_storage_policy->AddSessionOnly(session_only_origin); 35 special_storage_policy->AddSessionOnly(session_only_origin);
35 36
36 TestBrowserContext browser_context; 37 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext);
37 38
38 // Create databases for permanent and session-only origins. 39 // Create databases for permanent and session-only origins.
39 FilePath domstorage_dir = browser_context.GetPath().Append( 40 FilePath domstorage_dir = browser_context->GetPath().Append(
40 DOMStorageContext::kLocalStorageDirectory); 41 DOMStorageContext::kLocalStorageDirectory);
41 FilePath::StringType session_only_database( 42 FilePath::StringType session_only_database(
42 FILE_PATH_LITERAL("http_www.sessiononly.com_0")); 43 FILE_PATH_LITERAL("http_www.sessiononly.com_0"));
43 FilePath::StringType permanent_database( 44 FilePath::StringType permanent_database(
44 FILE_PATH_LITERAL("http_www.permanent.com_0")); 45 FILE_PATH_LITERAL("http_www.permanent.com_0"));
45 session_only_database.append(DOMStorageContext::kLocalStorageExtension); 46 session_only_database.append(DOMStorageContext::kLocalStorageExtension);
46 permanent_database.append(DOMStorageContext::kLocalStorageExtension); 47 permanent_database.append(DOMStorageContext::kLocalStorageExtension);
47 FilePath session_only_database_path = 48 FilePath session_only_database_path =
48 domstorage_dir.Append(session_only_database); 49 domstorage_dir.Append(session_only_database);
49 FilePath permanent_database_path = 50 FilePath permanent_database_path =
50 domstorage_dir.Append(permanent_database); 51 domstorage_dir.Append(permanent_database);
51 52
52 ASSERT_TRUE(file_util::CreateDirectory(domstorage_dir)); 53 ASSERT_TRUE(file_util::CreateDirectory(domstorage_dir));
53 54
54 ASSERT_EQ(1, file_util::WriteFile(session_only_database_path, ".", 1)); 55 ASSERT_EQ(1, file_util::WriteFile(session_only_database_path, ".", 1));
55 ASSERT_EQ(1, file_util::WriteFile(permanent_database_path, ".", 1)); 56 ASSERT_EQ(1, file_util::WriteFile(permanent_database_path, ".", 1));
56 57
57 // Inject MockSpecialStoragePolicy into DOMStorageContext. 58 // Inject MockSpecialStoragePolicy into DOMStorageContext.
58 browser_context.GetWebKitContext()->dom_storage_context()-> 59 BrowserContext::GetWebKitContext(browser_context.get())->
59 special_storage_policy_ = special_storage_policy; 60 dom_storage_context()->special_storage_policy_ = special_storage_policy;
60 61
61 // Delete the WebKitContext before destroying TestBrowserContext. This way the 62 // Delete the TestBrowserContext but own the temp dir. This way the
62 // temporary data directory stays alive long enough to conduct the test. 63 // temporary data directory stays alive long enough to conduct the test.
63 browser_context.webkit_context_ = NULL; 64 ScopedTempDir temp_dir;
65 ignore_result(temp_dir.Set(browser_context->TakePath()));
66 browser_context.reset();
64 // Run the message loop to ensure that DOMStorageContext gets destroyed. 67 // Run the message loop to ensure that DOMStorageContext gets destroyed.
65 message_loop_.RunAllPending(); 68 message_loop_.RunAllPending();
66 69
67 // Expected result: the database file for the permanent storage remains but 70 // Expected result: the database file for the permanent storage remains but
68 // the database for the session only storage was deleted. 71 // the database for the session only storage was deleted.
69 EXPECT_FALSE(file_util::PathExists(session_only_database_path)); 72 EXPECT_FALSE(file_util::PathExists(session_only_database_path));
70 EXPECT_TRUE(file_util::PathExists(permanent_database_path)); 73 EXPECT_TRUE(file_util::PathExists(permanent_database_path));
71 } 74 }
72 75
73 TEST_F(DOMStorageTest, SaveSessionState) { 76 TEST_F(DOMStorageTest, SaveSessionState) {
74 GURL session_only_origin("http://www.sessiononly.com"); 77 GURL session_only_origin("http://www.sessiononly.com");
75 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = 78 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy =
76 new quota::MockSpecialStoragePolicy; 79 new quota::MockSpecialStoragePolicy;
77 special_storage_policy->AddSessionOnly(session_only_origin); 80 special_storage_policy->AddSessionOnly(session_only_origin);
78 81
79 TestBrowserContext browser_context; 82 scoped_ptr<TestBrowserContext> browser_context(new TestBrowserContext);
80 83
81 // Create databases for permanent and session-only origins. 84 // Create databases for permanent and session-only origins.
82 FilePath domstorage_dir = browser_context.GetPath().Append( 85 FilePath domstorage_dir = browser_context->GetPath().Append(
83 DOMStorageContext::kLocalStorageDirectory); 86 DOMStorageContext::kLocalStorageDirectory);
84 FilePath::StringType session_only_database( 87 FilePath::StringType session_only_database(
85 FILE_PATH_LITERAL("http_www.sessiononly.com_0")); 88 FILE_PATH_LITERAL("http_www.sessiononly.com_0"));
86 FilePath::StringType permanent_database( 89 FilePath::StringType permanent_database(
87 FILE_PATH_LITERAL("http_www.permanent.com_0")); 90 FILE_PATH_LITERAL("http_www.permanent.com_0"));
88 session_only_database.append(DOMStorageContext::kLocalStorageExtension); 91 session_only_database.append(DOMStorageContext::kLocalStorageExtension);
89 permanent_database.append(DOMStorageContext::kLocalStorageExtension); 92 permanent_database.append(DOMStorageContext::kLocalStorageExtension);
90 FilePath session_only_database_path = 93 FilePath session_only_database_path =
91 domstorage_dir.Append(session_only_database); 94 domstorage_dir.Append(session_only_database);
92 FilePath permanent_database_path = 95 FilePath permanent_database_path =
93 domstorage_dir.Append(permanent_database); 96 domstorage_dir.Append(permanent_database);
94 97
95 ASSERT_TRUE(file_util::CreateDirectory(domstorage_dir)); 98 ASSERT_TRUE(file_util::CreateDirectory(domstorage_dir));
96 99
97 ASSERT_EQ(1, file_util::WriteFile(session_only_database_path, ".", 1)); 100 ASSERT_EQ(1, file_util::WriteFile(session_only_database_path, ".", 1));
98 ASSERT_EQ(1, file_util::WriteFile(permanent_database_path, ".", 1)); 101 ASSERT_EQ(1, file_util::WriteFile(permanent_database_path, ".", 1));
99 102
100 // Inject MockSpecialStoragePolicy into DOMStorageContext. 103 // Inject MockSpecialStoragePolicy into DOMStorageContext.
101 DOMStorageContext* dom_storage_context = 104 DOMStorageContext* dom_storage_context =
102 browser_context.GetWebKitContext()->dom_storage_context(); 105 BrowserContext::GetWebKitContext(browser_context.get())->
106 dom_storage_context();
103 dom_storage_context->special_storage_policy_ = special_storage_policy; 107 dom_storage_context->special_storage_policy_ = special_storage_policy;
104 108
105 dom_storage_context->set_clear_local_state_on_exit_(true); 109 dom_storage_context->set_clear_local_state_on_exit_(true);
106 110
107 // Save session state. This should bypass the destruction-time deletion. 111 // Save session state. This should bypass the destruction-time deletion.
108 dom_storage_context->SaveSessionState(); 112 dom_storage_context->SaveSessionState();
109 113
110 // Delete the WebKitContext before destroying TestBrowserContext. This way the 114 // Delete the TestBrowserContext but own the temp dir. This way the
111 // temporary data directory stays alive long enough to conduct the test. 115 // temporary data directory stays alive long enough to conduct the test.
112 browser_context.webkit_context_ = NULL; 116 ScopedTempDir temp_dir;
117 ignore_result(temp_dir.Set(browser_context->TakePath()));
118 browser_context.reset();
113 // Run the message loop to ensure that DOMStorageContext gets destroyed. 119 // Run the message loop to ensure that DOMStorageContext gets destroyed.
114 message_loop_.RunAllPending(); 120 message_loop_.RunAllPending();
115 121
116 // Expected result: no database files were deleted because of 122 // Expected result: no database files were deleted because of
117 // SaveSessionState. 123 // SaveSessionState.
118 EXPECT_TRUE(file_util::PathExists(session_only_database_path)); 124 EXPECT_TRUE(file_util::PathExists(session_only_database_path));
119 EXPECT_TRUE(file_util::PathExists(permanent_database_path)); 125 EXPECT_TRUE(file_util::PathExists(permanent_database_path));
120 } 126 }
OLDNEW
« no previous file with comments | « content/browser/in_process_webkit/dom_storage_context.cc ('k') | content/browser/in_process_webkit/indexed_db_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698