OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "base/synchronization/waitable_event.h" | |
11 #include "chrome/test/base/testing_profile.h" | |
12 #include "content/public/test/test_browser_thread.h" | |
13 #include "net/cookies/canonical_cookie.h" | |
14 #include "net/cookies/parsed_cookie.h" | |
15 #include "net/url_request/url_request_context_getter.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 using content::BrowserThread; | |
19 | |
20 namespace { | |
21 | |
22 class BrowsingDataCookieHelperTest : public testing::Test { | |
23 public: | |
24 void SetUpOnIOThread(base::WaitableEvent* io_setup_complete) { | |
25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
26 // This is a workaround for a bug in the TestingProfile. | |
27 // The URLRequestContext will be created by GetCookieMonster on the UI | |
28 // thread, if it does not already exist. But it must be created on the IO | |
29 // thread or else it will DCHECK upon destruction. | |
30 // Force it to be created here. | |
31 testing_profile_->CreateRequestContext(); | |
32 testing_profile_->GetRequestContext()->GetURLRequestContext(); | |
33 io_setup_complete->Signal(); | |
34 } | |
35 | |
36 virtual void SetUp() { | |
37 ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, | |
38 &message_loop_)); | |
39 // Note: we're starting a real IO thread because parts of the | |
40 // BrowsingDataCookieHelper expect to run on that thread. | |
41 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO)); | |
42 ASSERT_TRUE(io_thread_->Start()); | |
43 testing_profile_.reset(new TestingProfile()); | |
44 base::WaitableEvent io_setup_complete(true, false); | |
45 BrowserThread::PostTask( | |
46 BrowserThread::IO, FROM_HERE, | |
47 base::Bind(&BrowsingDataCookieHelperTest::SetUpOnIOThread, | |
48 base::Unretained(this), &io_setup_complete)); | |
49 io_setup_complete.Wait(); | |
50 } | |
51 | |
52 virtual void TearDown() { | |
53 // This must be reset before the IO thread stops, because the | |
54 // URLRequestContextGetter forces its own deletion to occur on that thread. | |
55 testing_profile_->ResetRequestContext(); | |
56 io_thread_.reset(); | |
57 ui_thread_.reset(); | |
58 } | |
59 | |
60 void CreateCookiesForTest() { | |
61 scoped_refptr<net::CookieMonster> cookie_monster = | |
62 testing_profile_->GetCookieMonster(); | |
63 cookie_monster->SetCookieWithOptionsAsync( | |
64 GURL("http://www.google.com"), "A=1", net::CookieOptions(), | |
65 net::CookieMonster::SetCookiesCallback()); | |
66 cookie_monster->SetCookieWithOptionsAsync( | |
67 GURL("http://www.gmail.google.com"), "B=1", net::CookieOptions(), | |
68 net::CookieMonster::SetCookiesCallback()); | |
69 } | |
70 | |
71 void FetchCallback(const net::CookieList& cookies) { | |
72 ASSERT_EQ(2UL, cookies.size()); | |
73 cookie_list_ = cookies; | |
74 net::CookieList::const_iterator it = cookies.begin(); | |
75 | |
76 // Correct because fetching cookies will get a sorted cookie list. | |
77 ASSERT_TRUE(it != cookies.end()); | |
78 EXPECT_EQ("www.google.com", it->Domain()); | |
79 EXPECT_EQ("A", it->Name()); | |
80 | |
81 ASSERT_TRUE(++it != cookies.end()); | |
82 EXPECT_EQ("www.gmail.google.com", it->Domain()); | |
83 EXPECT_EQ("B", it->Name()); | |
84 | |
85 ASSERT_TRUE(++it == cookies.end()); | |
86 MessageLoop::current()->Quit(); | |
87 } | |
88 | |
89 void DeleteCallback(const net::CookieList& cookies) { | |
90 ASSERT_EQ(1UL, cookies.size()); | |
91 net::CookieList::const_iterator it = cookies.begin(); | |
92 | |
93 ASSERT_TRUE(it != cookies.end()); | |
94 EXPECT_EQ("www.gmail.google.com", it->Domain()); | |
95 EXPECT_EQ("B", it->Name()); | |
96 | |
97 ASSERT_TRUE(++it == cookies.end()); | |
98 MessageLoop::current()->Quit(); | |
99 } | |
100 | |
101 void CannedUniqueCallback(const net::CookieList& cookies) { | |
102 ASSERT_EQ(1UL, cookies.size()); | |
103 cookie_list_ = cookies; | |
104 net::CookieList::const_iterator it = cookies.begin(); | |
105 | |
106 ASSERT_TRUE(it != cookies.end()); | |
107 EXPECT_EQ("http://www.google.com/", it->Source()); | |
108 EXPECT_EQ("A", it->Name()); | |
109 | |
110 ASSERT_TRUE(++it == cookies.end()); | |
111 } | |
112 | |
113 void CannedDifferentFramesCallback(const net::CookieList& cookie_list) { | |
114 ASSERT_EQ(3U, cookie_list.size()); | |
115 } | |
116 | |
117 protected: | |
118 MessageLoop message_loop_; | |
119 scoped_ptr<content::TestBrowserThread> ui_thread_; | |
120 scoped_ptr<content::TestBrowserThread> io_thread_; | |
121 scoped_ptr<TestingProfile> testing_profile_; | |
122 | |
123 net::CookieList cookie_list_; | |
124 }; | |
125 | |
126 TEST_F(BrowsingDataCookieHelperTest, FetchData) { | |
127 CreateCookiesForTest(); | |
128 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( | |
129 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); | |
130 | |
131 cookie_helper->StartFetching( | |
132 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, | |
133 base::Unretained(this))); | |
134 | |
135 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified. | |
136 MessageLoop::current()->Run(); | |
137 } | |
138 | |
139 TEST_F(BrowsingDataCookieHelperTest, DeleteCookie) { | |
140 CreateCookiesForTest(); | |
141 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( | |
142 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); | |
143 | |
144 cookie_helper->StartFetching( | |
145 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, | |
146 base::Unretained(this))); | |
147 | |
148 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified. | |
149 MessageLoop::current()->Run(); | |
150 | |
151 net::CanonicalCookie cookie = cookie_list_[0]; | |
152 cookie_helper->DeleteCookie(cookie); | |
153 | |
154 cookie_helper->StartFetching( | |
155 base::Bind(&BrowsingDataCookieHelperTest::DeleteCallback, | |
156 base::Unretained(this))); | |
157 MessageLoop::current()->Run(); | |
158 } | |
159 | |
160 TEST_F(BrowsingDataCookieHelperTest, CannedUnique) { | |
161 const GURL origin("http://www.google.com"); | |
162 net::CookieList cookie; | |
163 | |
164 scoped_refptr<CannedBrowsingDataCookieHelper> helper( | |
165 new CannedBrowsingDataCookieHelper( | |
166 testing_profile_->GetRequestContext())); | |
167 | |
168 ASSERT_TRUE(helper->empty()); | |
169 helper->AddChangedCookie(origin, origin, "A=1", net::CookieOptions()); | |
170 helper->AddChangedCookie(origin, origin, "A=1", net::CookieOptions()); | |
171 helper->StartFetching( | |
172 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback, | |
173 base::Unretained(this))); | |
174 cookie = cookie_list_; | |
175 helper->Reset(); | |
176 ASSERT_TRUE(helper->empty()); | |
177 | |
178 helper->AddReadCookies(origin, origin, cookie); | |
179 helper->AddReadCookies(origin, origin, cookie); | |
180 helper->StartFetching( | |
181 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback, | |
182 base::Unretained(this))); | |
183 } | |
184 | |
185 TEST_F(BrowsingDataCookieHelperTest, CannedEmpty) { | |
186 const GURL url_google("http://www.google.com"); | |
187 | |
188 scoped_refptr<CannedBrowsingDataCookieHelper> helper( | |
189 new CannedBrowsingDataCookieHelper( | |
190 testing_profile_->GetRequestContext())); | |
191 | |
192 ASSERT_TRUE(helper->empty()); | |
193 helper->AddChangedCookie(url_google, url_google, "a=1", | |
194 net::CookieOptions()); | |
195 ASSERT_FALSE(helper->empty()); | |
196 helper->Reset(); | |
197 ASSERT_TRUE(helper->empty()); | |
198 | |
199 net::CookieList cookies; | |
200 net::ParsedCookie pc("a=1"); | |
201 scoped_ptr<net::CanonicalCookie> cookie( | |
202 new net::CanonicalCookie(url_google, pc)); | |
203 cookies.push_back(*cookie); | |
204 | |
205 helper->AddReadCookies(url_google, url_google, cookies); | |
206 ASSERT_FALSE(helper->empty()); | |
207 helper->Reset(); | |
208 ASSERT_TRUE(helper->empty()); | |
209 } | |
210 | |
211 TEST_F(BrowsingDataCookieHelperTest, CannedDifferentFrames) { | |
212 GURL frame1_url("http://www.google.com"); | |
213 GURL frame2_url("http://www.google.de"); | |
214 GURL request_url("http://www.google.com"); | |
215 | |
216 scoped_refptr<CannedBrowsingDataCookieHelper> helper( | |
217 new CannedBrowsingDataCookieHelper( | |
218 testing_profile_->GetRequestContext())); | |
219 | |
220 ASSERT_TRUE(helper->empty()); | |
221 helper->AddChangedCookie(frame1_url, request_url, "a=1", | |
222 net::CookieOptions()); | |
223 helper->AddChangedCookie(frame1_url, request_url, "b=1", | |
224 net::CookieOptions()); | |
225 helper->AddChangedCookie(frame2_url, request_url, "c=1", | |
226 net::CookieOptions()); | |
227 | |
228 helper->StartFetching( | |
229 base::Bind(&BrowsingDataCookieHelperTest::CannedDifferentFramesCallback, | |
230 base::Unretained(this))); | |
231 } | |
232 | |
233 } // namespace | |
OLD | NEW |