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