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

Side by Side Diff: chrome/browser/browsing_data_cookie_helper_unittest.cc

Issue 7355025: Creat BrowsingDataCookieHelper and CannedBrowsingDataCookieHelper for logging cookies at UI thread. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years, 5 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/browsing_data_cookie_helper.h"
6
7
8 #include "base/bind.h"
9 #include "base/message_loop.h"
10 #include "chrome/test/testing_profile.h"
11 #include "content/browser/browser_thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace {
15
16 class BrowsingDataCookieHelperTest : public testing::Test {
17 public:
18 void CreateCookiesForTest() {
19 testing_profile_.CreateRequestContext();
20 scoped_refptr<net::CookieMonster> cookie_monster =
21 testing_profile_.GetCookieMonster();
22 cookie_monster->SetCookieWithOptions(
erikwright (departed) 2011/07/21 19:24:36 Don't use the synchronous methods, since you are g
ycxiao 2011/07/21 21:17:15 Done.
23 GURL("http://www.google.com"), "A=1", net::CookieOptions());
24 cookie_monster->SetCookieWithOptions(
25 GURL("http://www.gmail.google.com"), "B=1", net::CookieOptions());
26 }
27
28 void FetchCallback(const net::CookieList& cookies) {
29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
30 ASSERT_EQ(2UL, cookies.size());
31 cookie_list_ = cookies;
32 net::CookieList::const_iterator it = cookies.begin();
33
34 // Correct because fetching cookies will get a sorted cookie list.
35 ASSERT_TRUE(it != cookies.end());
36 EXPECT_EQ("www.google.com", it->Domain());
37 EXPECT_EQ("A", it->Name());
38
39 ASSERT_TRUE(++it != cookies.end());
40 EXPECT_EQ("www.gmail.google.com", it->Domain());
41 EXPECT_EQ("B", it->Name());
42
43 ASSERT_TRUE(++it == cookies.end());
44 MessageLoop::current()->Quit();
45 }
46
47 void DeleteCallback(const net::CookieList& cookies) {
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
49 ASSERT_EQ(1UL, cookies.size());
50 net::CookieList::const_iterator it = cookies.begin();
51
52 ASSERT_TRUE(it != cookies.end());
53 EXPECT_EQ("www.gmail.google.com", it->Domain());
54 EXPECT_EQ("B", it->Name());
55
56 ASSERT_TRUE(++it == cookies.end());
57 MessageLoop::current()->Quit();
58 }
59
60 void CannedUniqueCallback(const net::CookieList& cookies) {
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
62 ASSERT_EQ(1UL, cookies.size());
63 cookie_list_ = cookies;
64 net::CookieList::const_iterator it = cookies.begin();
65
66 ASSERT_TRUE(it != cookies.end());
67 EXPECT_EQ("http://www.google.com/", it->Source());
68 EXPECT_EQ("A", it->Name());
69
70 ASSERT_TRUE(++it == cookies.end());
71 }
72
73 protected:
74 TestingProfile testing_profile_;
75
76 net::CookieList cookie_list_;
77 };
78
79 TEST_F(BrowsingDataCookieHelperTest, FetchData) {
80 CreateCookiesForTest();
81 scoped_refptr<BrowsingDataCookieHelper> cookie_helper(
82 new BrowsingDataCookieHelper(&testing_profile_));
83 MessageLoop message_loop;
84 BrowserThread ui_thread(BrowserThread::UI, &message_loop);
85 BrowserThread io_thread(BrowserThread::IO);
86 ASSERT_TRUE(io_thread.Start());
87
88 cookie_helper->StartFetching(
89 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback,
90 base::Unretained(this)));
91
92 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified.
93 MessageLoop::current()->Run();
94 }
95
96 TEST_F(BrowsingDataCookieHelperTest, DeleteCookie) {
97 CreateCookiesForTest();
98 scoped_refptr<BrowsingDataCookieHelper> cookie_helper(
99 new BrowsingDataCookieHelper(&testing_profile_));
100 MessageLoop message_loop;
101 BrowserThread ui_thread(BrowserThread::UI, &message_loop);
102 BrowserThread io_thread(BrowserThread::IO);
103 ASSERT_TRUE(io_thread.Start());
104
105 cookie_helper->StartFetching(
106 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback,
107 base::Unretained(this)));
108
109 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified.
110 MessageLoop::current()->Run();
111
112 net::CookieMonster::CanonicalCookie cookie = cookie_list_[0];
113 cookie_helper->DeleteCookie(cookie);
114
115 cookie_helper->StartFetching(
116 base::Bind(&BrowsingDataCookieHelperTest::DeleteCallback,
117 base::Unretained(this)));
118 MessageLoop::current()->Run();
119 }
120
121 TEST_F(BrowsingDataCookieHelperTest, CannedUnique) {
122 const GURL origin("http://www.google.com");
123 net::CookieList cookie;
124
125 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
126 new CannedBrowsingDataCookieHelper(&testing_profile_));
127 MessageLoop message_loop;
128 BrowserThread ui_thread(BrowserThread::UI, &message_loop);
129
130 ASSERT_TRUE(helper->empty());
131 helper->AddChangeCookie(origin, "A=1", net::CookieOptions());
132 helper->AddChangeCookie(origin, "A=1", net::CookieOptions());
133 helper->StartFetching(
134 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback,
135 base::Unretained(this)));
136 cookie = cookie_list_;
137 helper->Reset();
138 ASSERT_TRUE(helper->empty());
139
140 helper->AddReadCookie(origin, cookie);
141 helper->AddReadCookie(origin, cookie);
142 helper->StartFetching(
143 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback,
144 base::Unretained(this)));
145 }
146
147 TEST(CannedBrowsingDataCookieHelperTest, Empty) {
148 TestingProfile profile;
149
150 const GURL url_google("http://www.google.com");
151
152 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
153 new CannedBrowsingDataCookieHelper(&profile));
154
155 ASSERT_TRUE(helper->empty());
156 helper->AddChangeCookie(url_google, "a=1",
157 net::CookieOptions());
158 ASSERT_FALSE(helper->empty());
159 helper->Reset();
160 ASSERT_TRUE(helper->empty());
161
162 net::CookieList cookies;
163 net::CookieMonster::ParsedCookie pc("a=1");
164 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie(
165 new net::CookieMonster::CanonicalCookie(url_google, pc));
166 cookies.push_back(*cookie);
167
168 helper->AddReadCookie(url_google, cookies);
169 ASSERT_FALSE(helper->empty());
170 helper->Reset();
171 ASSERT_TRUE(helper->empty());
172 }
173
174 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698