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