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

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: Added unit test and moved flag to the persistence store itself. 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 "googleurl/src/gurl.h"
jochen (gone - plz use gerrit) 2010/12/02 15:09:51 alphabetical ordering
pastarmovj 2010/12/02 16:29:07 You got me on that one :). Done!
12 #include "chrome/browser/browser_thread.h"
13 #include "chrome/browser/net/sqlite_persistent_cookie_store.h"
14 #include "chrome/common/chrome_constants.h"
15 #include "chrome/test/thread_test_helper.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 15:09:51 why no message loop for the db thread?
pastarmovj 2010/12/02 16:29:07 I start the thread myself in SetUp. A message loop
24
25 protected:
26 virtual void SetUp() {
27 db_thread_.Start();
jochen (gone - plz use gerrit) 2010/12/02 15:09:51 that shouldn't be required
pastarmovj 2010/12/02 16:29:07 See previous comment.
28 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
29 ASSERT_TRUE(store_ = new SQLitePersistentCookieStore(
30 temp_dir_.path().Append(chrome::kCookieFilename)));
31 ASSERT_TRUE(store_->Load(&cookies_));
jochen (gone - plz use gerrit) 2010/12/02 15:09:51 in a new directory, there cannot be any cookies
pastarmovj 2010/12/02 16:29:07 The method returns true whenever the database has
32 ASSERT_TRUE(0 == cookies_.size());
33 }
34
35 virtual void TearDown() {
36 // Enforce deleting the store file upon destruction.
37 store_->SetClearLocalStateOnExit(true);
jochen (gone - plz use gerrit) 2010/12/02 15:09:51 please don't add additional tests in tear down
pastarmovj 2010/12/02 16:29:07 Done. Moved it to a separate test.
38 store_ = NULL;
39 // Make sure we wait until the destructor has run.
40 scoped_refptr<ThreadTestHelper> helper(
41 new ThreadTestHelper(BrowserThread::DB));
42 ASSERT_TRUE(helper->Run());
43
44 ASSERT_FALSE(file_util::PathExists(
45 temp_dir_.path().Append(chrome::kCookieFilename)));
46 }
47
48 void ClearCookies() {
49 STLDeleteContainerPointers(cookies_.begin(), cookies_.end());
50 cookies_.clear();
51 }
52
53 MessageLoop message_loop_;
54
55 BrowserThread ui_thread_;
56 BrowserThread db_thread_;
57 scoped_refptr<SQLitePersistentCookieStore> store_;
jochen (gone - plz use gerrit) 2010/12/02 15:09:51 i think you should move the store into the single
pastarmovj 2010/12/02 16:29:07 Well still share this between all 2 (now 3) tests
58 ScopedTempDir temp_dir_;
59 std::vector<net::CookieMonster::CanonicalCookie*> cookies_;
jochen (gone - plz use gerrit) 2010/12/02 15:09:51 this is only used in one test, so I'd move this va
pastarmovj 2010/12/02 16:29:07 Actually we need it in the initialization call of
60 };
61
62 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) {
63 // Make sure the store gets written at least once.
64 store_->AddCookie(
65 net::CookieMonster::CanonicalCookie("A", "B", "http://foo.bar", "/",
66 false, false,
67 base::Time(), base::Time(),
68 true, base::Time()));
69 store_->SetClearLocalStateOnExit(false);
70 // Replace the store effectively destroying the current one and forcing it
71 // to call it's destructor. We can't just NULL it so that our TearDown code
72 // won't crash.
73 store_ = new SQLitePersistentCookieStore(
74 temp_dir_.path().Append(chrome::kCookieFilename));
75 // Make sure we wait until the destructor has run.
76 scoped_refptr<ThreadTestHelper> helper(
77 new ThreadTestHelper(BrowserThread::DB));
78 ASSERT_TRUE(helper->Run());
79
80 ASSERT_TRUE(file_util::PathExists(
81 temp_dir_.path().Append(chrome::kCookieFilename)));
82 }
83
84 // Test if data is stored as expected in the SQLite database.
85 TEST_F(SQLitePersistentCookieStoreTest, TestPersistance) {
86 // Make sure the store gets written at least once.
87 store_->AddCookie(
88 net::CookieMonster::CanonicalCookie("A", "B", "http://foo.bar", "/",
89 false, false,
90 base::Time::Now(), base::Time::Now(),
91 true, base::Time::Now()));
92 // Replace the store effectively destroying the current one and forcing it
93 // to write it's data to disk. Then we can see if after loading it again it
94 // is still there.
95 store_ = NULL;
96 scoped_refptr<ThreadTestHelper> helper(
97 new ThreadTestHelper(BrowserThread::DB));
98 // Make sure we wait until the destructor has run.
99 ASSERT_TRUE(helper->Run());
100 ClearCookies();
101 store_ = new SQLitePersistentCookieStore(
102 temp_dir_.path().Append(chrome::kCookieFilename));
103
104 // Reload and test for persistence
105 ASSERT_TRUE(store_->Load(&cookies_));
106 ASSERT_TRUE(1 == cookies_.size());
jochen (gone - plz use gerrit) 2010/12/02 15:09:51 ASSERT_EQ
pastarmovj 2010/12/02 16:29:07 Done.
107 ASSERT_EQ("http://foo.bar", cookies_[0]->Domain());
108 ASSERT_EQ("A", cookies_[0]->Name());
jochen (gone - plz use gerrit) 2010/12/02 15:09:51 ASSERT_STREQ
pastarmovj 2010/12/02 16:29:07 Done.
109 ASSERT_EQ("B", cookies_[0]->Value());
110
111 // Now delete the cookie and check persistence again.
112 store_->DeleteCookie(*cookies_[0]);
113 store_ = NULL;
114 // Make sure we wait until the destructor has run.
115 ASSERT_TRUE(helper->Run());
116 ClearCookies();
117 store_ = new SQLitePersistentCookieStore(
118 temp_dir_.path().Append(chrome::kCookieFilename));
119
120 // Reload and check if the cookie has been removed.
121 ASSERT_TRUE(store_->Load(&cookies_));
122 ASSERT_TRUE(0 == cookies_.size());
jochen (gone - plz use gerrit) 2010/12/02 15:09:51 ASSERT_EQ
pastarmovj 2010/12/02 16:29:07 Done.
123 }
124
125
jochen (gone - plz use gerrit) 2010/12/02 15:09:51 no empty lines please
pastarmovj 2010/12/02 16:29:07 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698