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