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

Side by Side Diff: chrome/browser/net/sqlite_persistent_cookie_store_unittest.cc

Issue 5430004: Refactored cookies persistent store clean-up on shutdown. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Style fixes. Created 10 years 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
(Empty)
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/file_util.h"
6 #include "base/message_loop.h"
7 #include "base/ref_counted.h"
8 #include "base/scoped_temp_dir.h"
9 #include "base/stl_util-inl.h"
10 #include "base/time.h"
11 #include "chrome/browser/browser_thread.h"
12 #include "chrome/browser/net/sqlite_persistent_cookie_store.h"
13 #include "chrome/common/chrome_constants.h"
14 #include "chrome/test/thread_test_helper.h"
15 #include "googleurl/src/gurl.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 class SQLitePersistentCookieStoreTest : public testing::Test {
19 public:
20 SQLitePersistentCookieStoreTest()
21 : message_loop_(MessageLoop::TYPE_UI),
22 ui_thread_(BrowserThread::UI, &message_loop_),
23 db_thread_(BrowserThread::DB) {}
jochen (gone - plz use gerrit) 2010/12/02 16:48:44 nit. closing bracket on new line
pastarmovj 2010/12/03 16:43:02 Done.
24
25 protected:
26 virtual void SetUp() {
27 db_thread_.Start();
jochen (gone - plz use gerrit) 2010/12/02 16:48:44 I meant that you can start the db_thread just like
pastarmovj 2010/12/03 16:43:02 Done.
28 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
29 ASSERT_TRUE(store_ = new SQLitePersistentCookieStore(
jochen (gone - plz use gerrit) 2010/12/02 16:48:44 either make it ASSERT(store_) in the next line and
pastarmovj 2010/12/03 16:43:02 Done.
30 temp_dir_.path().Append(chrome::kCookieFilename)));
31 ASSERT_TRUE(store_->Load(&cookies_));
32 ASSERT_TRUE(0 == cookies_.size());
jochen (gone - plz use gerrit) 2010/12/02 16:48:44 ASSERT_EQ I think it's good ok to have the store
pastarmovj 2010/12/03 16:43:02 However I can't use the store if don't call the Lo
pastarmovj 2010/12/03 16:46:40 Sorry I posted an obsoleted comment here ignore it
33 }
34
35 void ClearCookies() {
36 STLDeleteContainerPointers(cookies_.begin(), cookies_.end());
37 cookies_.clear();
38 }
39
40 MessageLoop message_loop_;
41
jochen (gone - plz use gerrit) 2010/12/02 16:48:44 no newline
pastarmovj 2010/12/03 16:43:02 Done.
42 BrowserThread ui_thread_;
43 BrowserThread db_thread_;
44 scoped_refptr<SQLitePersistentCookieStore> store_;
45 ScopedTempDir temp_dir_;
46 std::vector<net::CookieMonster::CanonicalCookie*> cookies_;
47 };
48
49 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) {
50 // Make sure the store gets written at least once.
51 store_->AddCookie(
52 net::CookieMonster::CanonicalCookie("A", "B", "http://foo.bar", "/",
53 false, false,
54 base::Time(), base::Time(),
55 true, base::Time()));
56 store_->SetClearLocalStateOnExit(false);
57 // Replace the store effectively destroying the current one and forcing it
58 // to call it's destructor. We can't just NULL it so that our TearDown code
59 // won't crash.
60 store_ = new SQLitePersistentCookieStore(
jochen (gone - plz use gerrit) 2010/12/02 16:48:44 store_ = NULL (so it's symmetrical to the test bel
pastarmovj 2010/12/03 16:43:02 Done.
61 temp_dir_.path().Append(chrome::kCookieFilename));
62 // Make sure we wait until the destructor has run.
63 scoped_refptr<ThreadTestHelper> helper(
64 new ThreadTestHelper(BrowserThread::DB));
65 ASSERT_TRUE(helper->Run());
66
67 ASSERT_TRUE(file_util::PathExists(
68 temp_dir_.path().Append(chrome::kCookieFilename)));
69 }
70
71 TEST_F(SQLitePersistentCookieStoreTest, RemoveOnDestruction) {
72 // Make sure the store gets written at least once.
73 store_->AddCookie(
74 net::CookieMonster::CanonicalCookie("A", "B", "http://foo.bar", "/",
75 false, false,
76 base::Time(), base::Time(),
77 true, base::Time()));
78 store_->SetClearLocalStateOnExit(true);
79 store_ = NULL;
80 // Make sure we wait until the destructor has run.
81 scoped_refptr<ThreadTestHelper> helper(
82 new ThreadTestHelper(BrowserThread::DB));
83 ASSERT_TRUE(helper->Run());
84
85 ASSERT_FALSE(file_util::PathExists(
86 temp_dir_.path().Append(chrome::kCookieFilename)));
87 }
88
89 // Test if data is stored as expected in the SQLite database.
90 TEST_F(SQLitePersistentCookieStoreTest, TestPersistance) {
91 // Make sure the store gets written at least once.
92 store_->AddCookie(
93 net::CookieMonster::CanonicalCookie("A", "B", "http://foo.bar", "/",
94 false, false,
95 base::Time::Now(), base::Time::Now(),
96 true, base::Time::Now()));
97 // Replace the store effectively destroying the current one and forcing it
98 // to write it's data to disk. Then we can see if after loading it again it
99 // is still there.
100 store_ = NULL;
101 scoped_refptr<ThreadTestHelper> helper(
102 new ThreadTestHelper(BrowserThread::DB));
103 // Make sure we wait until the destructor has run.
104 ASSERT_TRUE(helper->Run());
105 ClearCookies();
jochen (gone - plz use gerrit) 2010/12/02 16:48:44 cookies_ should be empty here. no need to clear
pastarmovj 2010/12/03 16:43:02 Done.
106 store_ = new SQLitePersistentCookieStore(
107 temp_dir_.path().Append(chrome::kCookieFilename));
108
109 // Reload and test for persistence
110 ASSERT_TRUE(store_->Load(&cookies_));
111 ASSERT_EQ(1U, cookies_.size());
112 ASSERT_STREQ("http://foo.bar", cookies_[0]->Domain().c_str());
113 ASSERT_STREQ("A", cookies_[0]->Name().c_str());
114 ASSERT_STREQ("B", cookies_[0]->Value().c_str());
115
116 // Now delete the cookie and check persistence again.
117 store_->DeleteCookie(*cookies_[0]);
118 store_ = NULL;
119 // Make sure we wait until the destructor has run.
120 ASSERT_TRUE(helper->Run());
121 ClearCookies();
122 store_ = new SQLitePersistentCookieStore(
123 temp_dir_.path().Append(chrome::kCookieFilename));
124
125 // Reload and check if the cookie has been removed.
126 ASSERT_TRUE(store_->Load(&cookies_));
127 ASSERT_EQ(0U, cookies_.size());
128 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698