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