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