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 virtual void SetUp() { |
| 19 ui_thread_.reset(new BrowserThread(BrowserThread::UI, &message_loop_)); |
| 20 // Note: we're starting a real IO thread to make sure our DCHECKs that |
| 21 // verify which thread is running are actually tested. |
| 22 io_thread_.reset(new BrowserThread(BrowserThread::IO)); |
| 23 ASSERT_TRUE(io_thread_->Start()); |
| 24 } |
| 25 |
| 26 virtual void TearDown() { |
| 27 message_loop_.RunAllPending(); |
| 28 io_thread_.reset(); |
| 29 ui_thread_.reset(); |
| 30 } |
| 31 |
| 32 void CreateCookiesForTest() { |
| 33 testing_profile_.CreateRequestContext(); |
| 34 scoped_refptr<net::CookieMonster> cookie_monster = |
| 35 testing_profile_.GetCookieMonster(); |
| 36 cookie_monster->SetCookieWithOptionsAsync( |
| 37 GURL("http://www.google.com"), "A=1", net::CookieOptions(), |
| 38 net::CookieMonster::SetCookiesCallback()); |
| 39 cookie_monster->SetCookieWithOptionsAsync( |
| 40 GURL("http://www.gmail.google.com"), "B=1", net::CookieOptions(), |
| 41 net::CookieMonster::SetCookiesCallback()); |
| 42 } |
| 43 |
| 44 void FetchCallback(const net::CookieList& cookies) { |
| 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 46 ASSERT_EQ(2UL, cookies.size()); |
| 47 cookie_list_ = cookies; |
| 48 net::CookieList::const_iterator it = cookies.begin(); |
| 49 |
| 50 // Correct because fetching cookies will get a sorted cookie list. |
| 51 ASSERT_TRUE(it != cookies.end()); |
| 52 EXPECT_EQ("www.google.com", it->Domain()); |
| 53 EXPECT_EQ("A", it->Name()); |
| 54 |
| 55 ASSERT_TRUE(++it != cookies.end()); |
| 56 EXPECT_EQ("www.gmail.google.com", it->Domain()); |
| 57 EXPECT_EQ("B", it->Name()); |
| 58 |
| 59 ASSERT_TRUE(++it == cookies.end()); |
| 60 MessageLoop::current()->Quit(); |
| 61 } |
| 62 |
| 63 void DeleteCallback(const net::CookieList& cookies) { |
| 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 65 ASSERT_EQ(1UL, cookies.size()); |
| 66 net::CookieList::const_iterator it = cookies.begin(); |
| 67 |
| 68 ASSERT_TRUE(it != cookies.end()); |
| 69 EXPECT_EQ("www.gmail.google.com", it->Domain()); |
| 70 EXPECT_EQ("B", it->Name()); |
| 71 |
| 72 ASSERT_TRUE(++it == cookies.end()); |
| 73 MessageLoop::current()->Quit(); |
| 74 } |
| 75 |
| 76 void CannedUniqueCallback(const net::CookieList& cookies) { |
| 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 78 ASSERT_EQ(1UL, cookies.size()); |
| 79 cookie_list_ = cookies; |
| 80 net::CookieList::const_iterator it = cookies.begin(); |
| 81 |
| 82 ASSERT_TRUE(it != cookies.end()); |
| 83 EXPECT_EQ("http://www.google.com/", it->Source()); |
| 84 EXPECT_EQ("A", it->Name()); |
| 85 |
| 86 ASSERT_TRUE(++it == cookies.end()); |
| 87 } |
| 88 |
| 89 protected: |
| 90 MessageLoop message_loop_; |
| 91 scoped_ptr<BrowserThread> ui_thread_; |
| 92 scoped_ptr<BrowserThread> io_thread_; |
| 93 TestingProfile testing_profile_; |
| 94 |
| 95 net::CookieList cookie_list_; |
| 96 }; |
| 97 |
| 98 TEST_F(BrowsingDataCookieHelperTest, FetchData) { |
| 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 |
| 111 TEST_F(BrowsingDataCookieHelperTest, DeleteCookie) { |
| 112 CreateCookiesForTest(); |
| 113 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( |
| 114 new BrowsingDataCookieHelper(&testing_profile_)); |
| 115 |
| 116 cookie_helper->StartFetching( |
| 117 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, |
| 118 base::Unretained(this))); |
| 119 |
| 120 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified. |
| 121 MessageLoop::current()->Run(); |
| 122 |
| 123 net::CookieMonster::CanonicalCookie cookie = cookie_list_[0]; |
| 124 cookie_helper->DeleteCookie(cookie); |
| 125 |
| 126 cookie_helper->StartFetching( |
| 127 base::Bind(&BrowsingDataCookieHelperTest::DeleteCallback, |
| 128 base::Unretained(this))); |
| 129 MessageLoop::current()->Run(); |
| 130 } |
| 131 |
| 132 TEST_F(BrowsingDataCookieHelperTest, CannedUnique) { |
| 133 const GURL origin("http://www.google.com"); |
| 134 net::CookieList cookie; |
| 135 |
| 136 scoped_refptr<CannedBrowsingDataCookieHelper> helper( |
| 137 new CannedBrowsingDataCookieHelper(&testing_profile_)); |
| 138 |
| 139 ASSERT_TRUE(helper->empty()); |
| 140 helper->AddChangeCookie(origin, "A=1", net::CookieOptions()); |
| 141 helper->AddChangeCookie(origin, "A=1", net::CookieOptions()); |
| 142 helper->StartFetching( |
| 143 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback, |
| 144 base::Unretained(this))); |
| 145 cookie = cookie_list_; |
| 146 helper->Reset(); |
| 147 ASSERT_TRUE(helper->empty()); |
| 148 |
| 149 helper->AddReadCookie(origin, cookie); |
| 150 helper->AddReadCookie(origin, cookie); |
| 151 helper->StartFetching( |
| 152 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback, |
| 153 base::Unretained(this))); |
| 154 } |
| 155 |
| 156 TEST_F(BrowsingDataCookieHelperTest, CannedEmpty) { |
| 157 const GURL url_google("http://www.google.com"); |
| 158 |
| 159 scoped_refptr<CannedBrowsingDataCookieHelper> helper( |
| 160 new CannedBrowsingDataCookieHelper(&testing_profile_)); |
| 161 |
| 162 ASSERT_TRUE(helper->empty()); |
| 163 helper->AddChangeCookie(url_google, "a=1", |
| 164 net::CookieOptions()); |
| 165 ASSERT_FALSE(helper->empty()); |
| 166 helper->Reset(); |
| 167 ASSERT_TRUE(helper->empty()); |
| 168 |
| 169 net::CookieList cookies; |
| 170 net::CookieMonster::ParsedCookie pc("a=1"); |
| 171 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie( |
| 172 new net::CookieMonster::CanonicalCookie(url_google, pc)); |
| 173 cookies.push_back(*cookie); |
| 174 |
| 175 helper->AddReadCookie(url_google, cookies); |
| 176 ASSERT_FALSE(helper->empty()); |
| 177 helper->Reset(); |
| 178 ASSERT_TRUE(helper->empty()); |
| 179 } |
| 180 |
| 181 } // namespace |
OLD | NEW |