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

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->SetCookieWithOptionsAsync(
23 GURL("http://www.google.com"), "A=1", net::CookieOptions(),
24 net::CookieMonster::SetCookiesCallback());
25 cookie_monster->SetCookieWithOptionsAsync(
26 GURL("http://www.gmail.google.com"), "B=1", net::CookieOptions(),
27 net::CookieMonster::SetCookiesCallback());
28 MessageLoop message_loop;
erikwright (departed) 2011/07/25 16:50:48 I think you should probably be keeping the Threads
ycxiao 2011/07/26 15:35:38 The BrowserThread seems to be different with Threa
erikwright (departed) 2011/07/26 17:42:00 BrowserThread is a subclass of Thread. You do need
29 BrowserThread ui_thread(BrowserThread::UI, &message_loop);
30 BrowserThread io_thread(BrowserThread::IO);
31 ASSERT_TRUE(io_thread.Start());
32 }
33
34 void FetchCallback(const net::CookieList& cookies) {
35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
36 ASSERT_EQ(2UL, cookies.size());
37 cookie_list_ = cookies;
38 net::CookieList::const_iterator it = cookies.begin();
39
40 // Correct because fetching cookies will get a sorted cookie list.
41 ASSERT_TRUE(it != cookies.end());
42 EXPECT_EQ("www.google.com", it->Domain());
43 EXPECT_EQ("A", it->Name());
44
45 ASSERT_TRUE(++it != cookies.end());
46 EXPECT_EQ("www.gmail.google.com", it->Domain());
47 EXPECT_EQ("B", it->Name());
48
49 ASSERT_TRUE(++it == cookies.end());
50 MessageLoop::current()->Quit();
51 }
52
53 void DeleteCallback(const net::CookieList& cookies) {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
55 ASSERT_EQ(1UL, cookies.size());
56 net::CookieList::const_iterator it = cookies.begin();
57
58 ASSERT_TRUE(it != cookies.end());
59 EXPECT_EQ("www.gmail.google.com", it->Domain());
60 EXPECT_EQ("B", it->Name());
61
62 ASSERT_TRUE(++it == cookies.end());
63 MessageLoop::current()->Quit();
64 }
65
66 void CannedUniqueCallback(const net::CookieList& cookies) {
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
68 ASSERT_EQ(1UL, cookies.size());
69 cookie_list_ = cookies;
70 net::CookieList::const_iterator it = cookies.begin();
71
72 ASSERT_TRUE(it != cookies.end());
73 EXPECT_EQ("http://www.google.com/", it->Source());
74 EXPECT_EQ("A", it->Name());
75
76 ASSERT_TRUE(++it == cookies.end());
77 }
78
79 protected:
80 TestingProfile testing_profile_;
81
82 net::CookieList cookie_list_;
83 };
84
85 TEST_F(BrowsingDataCookieHelperTest, FetchData) {
86 CreateCookiesForTest();
87 scoped_refptr<BrowsingDataCookieHelper> cookie_helper(
88 new BrowsingDataCookieHelper(&testing_profile_));
89
90 cookie_helper->StartFetching(
91 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback,
92 base::Unretained(this)));
93
94 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified.
95 MessageLoop::current()->Run();
96 }
97
98 TEST_F(BrowsingDataCookieHelperTest, DeleteCookie) {
99 CreateCookiesForTest();
100 scoped_refptr<BrowsingDataCookieHelper> cookie_helper(
101 new BrowsingDataCookieHelper(&testing_profile_));
102
103 cookie_helper->StartFetching(
104 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback,
105 base::Unretained(this)));
106
107 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified.
108 MessageLoop::current()->Run();
109
110 net::CookieMonster::CanonicalCookie cookie = cookie_list_[0];
111 cookie_helper->DeleteCookie(cookie);
112
113 cookie_helper->StartFetching(
114 base::Bind(&BrowsingDataCookieHelperTest::DeleteCallback,
115 base::Unretained(this)));
116 MessageLoop::current()->Run();
117 }
118
119 TEST_F(BrowsingDataCookieHelperTest, CannedUnique) {
120 const GURL origin("http://www.google.com");
121 net::CookieList cookie;
122
123 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
124 new CannedBrowsingDataCookieHelper(&testing_profile_));
125 MessageLoop message_loop;
126 BrowserThread ui_thread(BrowserThread::UI, &message_loop);
127
128 ASSERT_TRUE(helper->empty());
129 helper->AddChangeCookie(origin, "A=1", net::CookieOptions());
130 helper->AddChangeCookie(origin, "A=1", net::CookieOptions());
131 helper->StartFetching(
132 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback,
133 base::Unretained(this)));
134 cookie = cookie_list_;
135 helper->Reset();
136 ASSERT_TRUE(helper->empty());
137
138 helper->AddReadCookie(origin, cookie);
139 helper->AddReadCookie(origin, cookie);
140 helper->StartFetching(
141 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback,
142 base::Unretained(this)));
143 }
144
145 TEST(CannedBrowsingDataCookieHelperTest, Empty) {
146 TestingProfile profile;
147
148 const GURL url_google("http://www.google.com");
149
150 scoped_refptr<CannedBrowsingDataCookieHelper> helper(
151 new CannedBrowsingDataCookieHelper(&profile));
152
153 ASSERT_TRUE(helper->empty());
154 helper->AddChangeCookie(url_google, "a=1",
155 net::CookieOptions());
156 ASSERT_FALSE(helper->empty());
157 helper->Reset();
158 ASSERT_TRUE(helper->empty());
159
160 net::CookieList cookies;
161 net::CookieMonster::ParsedCookie pc("a=1");
162 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie(
163 new net::CookieMonster::CanonicalCookie(url_google, pc));
164 cookies.push_back(*cookie);
165
166 helper->AddReadCookie(url_google, cookies);
167 ASSERT_FALSE(helper->empty());
168 helper->Reset();
169 ASSERT_TRUE(helper->empty());
170 }
171
172 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698