OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/browsing_data_cookie_helper.h" | 5 #include "chrome/browser/browsing_data_cookie_helper.h" |
6 | 6 |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
11 #include "chrome/test/base/testing_profile.h" | 11 #include "chrome/test/base/testing_profile.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 testing_profile_->GetCookieMonster(); | 60 testing_profile_->GetCookieMonster(); |
61 cookie_monster->SetCookieWithOptionsAsync( | 61 cookie_monster->SetCookieWithOptionsAsync( |
62 GURL("http://www.google.com"), "A=1", net::CookieOptions(), | 62 GURL("http://www.google.com"), "A=1", net::CookieOptions(), |
63 net::CookieMonster::SetCookiesCallback()); | 63 net::CookieMonster::SetCookiesCallback()); |
64 cookie_monster->SetCookieWithOptionsAsync( | 64 cookie_monster->SetCookieWithOptionsAsync( |
65 GURL("http://www.gmail.google.com"), "B=1", net::CookieOptions(), | 65 GURL("http://www.gmail.google.com"), "B=1", net::CookieOptions(), |
66 net::CookieMonster::SetCookiesCallback()); | 66 net::CookieMonster::SetCookiesCallback()); |
67 } | 67 } |
68 | 68 |
69 void FetchCallback(const net::CookieList& cookies) { | 69 void FetchCallback(const net::CookieList& cookies) { |
70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
71 ASSERT_EQ(2UL, cookies.size()); | 70 ASSERT_EQ(2UL, cookies.size()); |
72 cookie_list_ = cookies; | 71 cookie_list_ = cookies; |
73 net::CookieList::const_iterator it = cookies.begin(); | 72 net::CookieList::const_iterator it = cookies.begin(); |
74 | 73 |
75 // Correct because fetching cookies will get a sorted cookie list. | 74 // Correct because fetching cookies will get a sorted cookie list. |
76 ASSERT_TRUE(it != cookies.end()); | 75 ASSERT_TRUE(it != cookies.end()); |
77 EXPECT_EQ("www.google.com", it->Domain()); | 76 EXPECT_EQ("www.google.com", it->Domain()); |
78 EXPECT_EQ("A", it->Name()); | 77 EXPECT_EQ("A", it->Name()); |
79 | 78 |
80 ASSERT_TRUE(++it != cookies.end()); | 79 ASSERT_TRUE(++it != cookies.end()); |
81 EXPECT_EQ("www.gmail.google.com", it->Domain()); | 80 EXPECT_EQ("www.gmail.google.com", it->Domain()); |
82 EXPECT_EQ("B", it->Name()); | 81 EXPECT_EQ("B", it->Name()); |
83 | 82 |
84 ASSERT_TRUE(++it == cookies.end()); | 83 ASSERT_TRUE(++it == cookies.end()); |
85 MessageLoop::current()->Quit(); | 84 MessageLoop::current()->Quit(); |
86 } | 85 } |
87 | 86 |
88 void DeleteCallback(const net::CookieList& cookies) { | 87 void DeleteCallback(const net::CookieList& cookies) { |
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
90 ASSERT_EQ(1UL, cookies.size()); | 88 ASSERT_EQ(1UL, cookies.size()); |
91 net::CookieList::const_iterator it = cookies.begin(); | 89 net::CookieList::const_iterator it = cookies.begin(); |
92 | 90 |
93 ASSERT_TRUE(it != cookies.end()); | 91 ASSERT_TRUE(it != cookies.end()); |
94 EXPECT_EQ("www.gmail.google.com", it->Domain()); | 92 EXPECT_EQ("www.gmail.google.com", it->Domain()); |
95 EXPECT_EQ("B", it->Name()); | 93 EXPECT_EQ("B", it->Name()); |
96 | 94 |
97 ASSERT_TRUE(++it == cookies.end()); | 95 ASSERT_TRUE(++it == cookies.end()); |
98 MessageLoop::current()->Quit(); | 96 MessageLoop::current()->Quit(); |
99 } | 97 } |
100 | 98 |
101 void CannedUniqueCallback(const net::CookieList& cookies) { | 99 void CannedUniqueCallback(const net::CookieList& cookies) { |
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
103 ASSERT_EQ(1UL, cookies.size()); | 100 ASSERT_EQ(1UL, cookies.size()); |
104 cookie_list_ = cookies; | 101 cookie_list_ = cookies; |
105 net::CookieList::const_iterator it = cookies.begin(); | 102 net::CookieList::const_iterator it = cookies.begin(); |
106 | 103 |
107 ASSERT_TRUE(it != cookies.end()); | 104 ASSERT_TRUE(it != cookies.end()); |
108 EXPECT_EQ("http://www.google.com/", it->Source()); | 105 EXPECT_EQ("http://www.google.com/", it->Source()); |
109 EXPECT_EQ("A", it->Name()); | 106 EXPECT_EQ("A", it->Name()); |
110 | 107 |
111 ASSERT_TRUE(++it == cookies.end()); | 108 ASSERT_TRUE(++it == cookies.end()); |
112 } | 109 } |
113 | 110 |
| 111 void CannedDifferentFramesCallback(const net::CookieList& cookie_list) { |
| 112 ASSERT_EQ(3U, cookie_list.size()); |
| 113 } |
| 114 |
114 protected: | 115 protected: |
115 MessageLoop message_loop_; | 116 MessageLoop message_loop_; |
116 scoped_ptr<content::TestBrowserThread> ui_thread_; | 117 scoped_ptr<content::TestBrowserThread> ui_thread_; |
117 scoped_ptr<content::TestBrowserThread> io_thread_; | 118 scoped_ptr<content::TestBrowserThread> io_thread_; |
118 scoped_ptr<TestingProfile> testing_profile_; | 119 scoped_ptr<TestingProfile> testing_profile_; |
119 | 120 |
120 net::CookieList cookie_list_; | 121 net::CookieList cookie_list_; |
121 }; | 122 }; |
122 | 123 |
123 TEST_F(BrowsingDataCookieHelperTest, FetchData) { | 124 TEST_F(BrowsingDataCookieHelperTest, FetchData) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 } | 156 } |
156 | 157 |
157 TEST_F(BrowsingDataCookieHelperTest, CannedUnique) { | 158 TEST_F(BrowsingDataCookieHelperTest, CannedUnique) { |
158 const GURL origin("http://www.google.com"); | 159 const GURL origin("http://www.google.com"); |
159 net::CookieList cookie; | 160 net::CookieList cookie; |
160 | 161 |
161 scoped_refptr<CannedBrowsingDataCookieHelper> helper( | 162 scoped_refptr<CannedBrowsingDataCookieHelper> helper( |
162 new CannedBrowsingDataCookieHelper(testing_profile_.get())); | 163 new CannedBrowsingDataCookieHelper(testing_profile_.get())); |
163 | 164 |
164 ASSERT_TRUE(helper->empty()); | 165 ASSERT_TRUE(helper->empty()); |
165 helper->AddChangedCookie(origin, "A=1", net::CookieOptions()); | 166 helper->AddChangedCookie(origin, origin, "A=1", net::CookieOptions()); |
166 helper->AddChangedCookie(origin, "A=1", net::CookieOptions()); | 167 helper->AddChangedCookie(origin, origin, "A=1", net::CookieOptions()); |
167 helper->StartFetching( | 168 helper->StartFetching( |
168 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback, | 169 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback, |
169 base::Unretained(this))); | 170 base::Unretained(this))); |
170 cookie = cookie_list_; | 171 cookie = cookie_list_; |
171 helper->Reset(); | 172 helper->Reset(); |
172 ASSERT_TRUE(helper->empty()); | 173 ASSERT_TRUE(helper->empty()); |
173 | 174 |
174 helper->AddReadCookies(origin, cookie); | 175 helper->AddReadCookies(origin, origin, cookie); |
175 helper->AddReadCookies(origin, cookie); | 176 helper->AddReadCookies(origin, origin, cookie); |
176 helper->StartFetching( | 177 helper->StartFetching( |
177 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback, | 178 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback, |
178 base::Unretained(this))); | 179 base::Unretained(this))); |
179 } | 180 } |
180 | 181 |
181 TEST_F(BrowsingDataCookieHelperTest, CannedEmpty) { | 182 TEST_F(BrowsingDataCookieHelperTest, CannedEmpty) { |
182 const GURL url_google("http://www.google.com"); | 183 const GURL url_google("http://www.google.com"); |
183 | 184 |
184 scoped_refptr<CannedBrowsingDataCookieHelper> helper( | 185 scoped_refptr<CannedBrowsingDataCookieHelper> helper( |
185 new CannedBrowsingDataCookieHelper(testing_profile_.get())); | 186 new CannedBrowsingDataCookieHelper(testing_profile_.get())); |
186 | 187 |
187 ASSERT_TRUE(helper->empty()); | 188 ASSERT_TRUE(helper->empty()); |
188 helper->AddChangedCookie(url_google, "a=1", | 189 helper->AddChangedCookie(url_google, url_google, "a=1", |
189 net::CookieOptions()); | 190 net::CookieOptions()); |
190 ASSERT_FALSE(helper->empty()); | 191 ASSERT_FALSE(helper->empty()); |
191 helper->Reset(); | 192 helper->Reset(); |
192 ASSERT_TRUE(helper->empty()); | 193 ASSERT_TRUE(helper->empty()); |
193 | 194 |
194 net::CookieList cookies; | 195 net::CookieList cookies; |
195 net::CookieMonster::ParsedCookie pc("a=1"); | 196 net::CookieMonster::ParsedCookie pc("a=1"); |
196 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie( | 197 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie( |
197 new net::CookieMonster::CanonicalCookie(url_google, pc)); | 198 new net::CookieMonster::CanonicalCookie(url_google, pc)); |
198 cookies.push_back(*cookie); | 199 cookies.push_back(*cookie); |
199 | 200 |
200 helper->AddReadCookies(url_google, cookies); | 201 helper->AddReadCookies(url_google, url_google, cookies); |
201 ASSERT_FALSE(helper->empty()); | 202 ASSERT_FALSE(helper->empty()); |
202 helper->Reset(); | 203 helper->Reset(); |
203 ASSERT_TRUE(helper->empty()); | 204 ASSERT_TRUE(helper->empty()); |
204 } | 205 } |
205 | 206 |
| 207 TEST_F(BrowsingDataCookieHelperTest, CannedDifferentFrames) { |
| 208 GURL frame1_url("http://www.google.com"); |
| 209 GURL frame2_url("http://www.google.de"); |
| 210 GURL request_url("http://www.google.com"); |
| 211 |
| 212 scoped_refptr<CannedBrowsingDataCookieHelper> helper( |
| 213 new CannedBrowsingDataCookieHelper(testing_profile_.get())); |
| 214 |
| 215 ASSERT_TRUE(helper->empty()); |
| 216 helper->AddChangedCookie(frame1_url, request_url, "a=1", |
| 217 net::CookieOptions()); |
| 218 helper->AddChangedCookie(frame1_url, request_url, "b=1", |
| 219 net::CookieOptions()); |
| 220 helper->AddChangedCookie(frame2_url, request_url, "c=1", |
| 221 net::CookieOptions()); |
| 222 |
| 223 helper->StartFetching( |
| 224 base::Bind(&BrowsingDataCookieHelperTest::CannedDifferentFramesCallback, |
| 225 base::Unretained(this))); |
| 226 } |
| 227 |
206 } // namespace | 228 } // namespace |
OLD | NEW |