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 "base/bind.h" |
| 6 #include "chrome/browser/browsing_data_cookie_helper.h" |
| 7 #include "chrome/test/in_process_browser_test.h" |
| 8 #include "chrome/test/testing_profile.h" |
| 9 #include "chrome/test/ui_test_utils.h" |
| 10 #include "content/browser/browser_thread.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 class BrowsingDataCookieHelperTest : public InProcessBrowserTest { |
| 15 public: |
| 16 void CreateCookiesForTest() { |
| 17 testing_profile_.CreateRequestContext(); |
| 18 scoped_refptr<net::CookieMonster> cookie_monster = |
| 19 testing_profile_.GetCookieMonster(); |
| 20 cookie_monster->SetCookieWithOptions( |
| 21 GURL("http://www.google.com"), "A=1", net::CookieOptions()); |
| 22 cookie_monster->SetCookieWithOptions( |
| 23 GURL("http://www.gmail.google.com"), "B=1", net::CookieOptions()); |
| 24 } |
| 25 |
| 26 void FetchCallback(const net::CookieList& cookies) { |
| 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 28 ASSERT_EQ(2UL, cookies.size()); |
| 29 cookie_list_ = cookies; |
| 30 net::CookieList::const_iterator it = cookies.begin(); |
| 31 |
| 32 // Correct because fetching cookies will get a sorted cookie list. |
| 33 ASSERT_TRUE(it != cookies.end()); |
| 34 EXPECT_EQ("www.google.com", it->Domain()); |
| 35 EXPECT_EQ("A", it->Name()); |
| 36 |
| 37 ASSERT_TRUE(++it != cookies.end()); |
| 38 EXPECT_EQ("www.gmail.google.com", it->Domain()); |
| 39 EXPECT_EQ("B", it->Name()); |
| 40 |
| 41 ASSERT_TRUE(++it == cookies.end()); |
| 42 MessageLoop::current()->Quit(); |
| 43 } |
| 44 |
| 45 void DeleteCallback(const net::CookieList& cookies) { |
| 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 47 ASSERT_EQ(1UL, cookies.size()); |
| 48 net::CookieList::const_iterator it = cookies.begin(); |
| 49 |
| 50 ASSERT_TRUE(it != cookies.end()); |
| 51 EXPECT_EQ("www.gmail.google.com", it->Domain()); |
| 52 EXPECT_EQ("B", it->Name()); |
| 53 |
| 54 ASSERT_TRUE(++it == cookies.end()); |
| 55 MessageLoop::current()->Quit(); |
| 56 } |
| 57 |
| 58 void CannedUniqueCallback(const net::CookieList& cookies) { |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 60 ASSERT_EQ(1UL, cookies.size()); |
| 61 cookie_list_ = cookies; |
| 62 net::CookieList::const_iterator it = cookies.begin(); |
| 63 |
| 64 ASSERT_TRUE(it != cookies.end()); |
| 65 EXPECT_EQ("http://www.google.com/", it->Source()); |
| 66 EXPECT_EQ("A", it->Name()); |
| 67 |
| 68 ASSERT_TRUE(++it == cookies.end()); |
| 69 } |
| 70 |
| 71 protected: |
| 72 TestingProfile testing_profile_; |
| 73 |
| 74 net::CookieList cookie_list_; |
| 75 }; |
| 76 |
| 77 IN_PROC_BROWSER_TEST_F(BrowsingDataCookieHelperTest, FetchData) { |
| 78 CreateCookiesForTest(); |
| 79 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( |
| 80 new BrowsingDataCookieHelper(&testing_profile_)); |
| 81 |
| 82 cookie_helper->StartFetching( |
| 83 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, |
| 84 base::Unretained(this))); |
| 85 |
| 86 // Blocks until BrowsingDataCookieHelperTest::Callback is notified. |
| 87 ui_test_utils::RunMessageLoop(); |
| 88 } |
| 89 |
| 90 IN_PROC_BROWSER_TEST_F(BrowsingDataCookieHelperTest, DeleteCookie) { |
| 91 CreateCookiesForTest(); |
| 92 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( |
| 93 new BrowsingDataCookieHelper(&testing_profile_)); |
| 94 |
| 95 cookie_helper->StartFetching( |
| 96 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, |
| 97 base::Unretained(this))); |
| 98 |
| 99 // Blocks until BrowsingDataCookieHelperTest::Callback is notified. |
| 100 ui_test_utils::RunMessageLoop(); |
| 101 |
| 102 net::CookieMonster::CanonicalCookie cookie = cookie_list_[0]; |
| 103 cookie_helper->DeleteCookie(cookie); |
| 104 |
| 105 cookie_helper->StartFetching( |
| 106 base::Bind(&BrowsingDataCookieHelperTest::DeleteCallback, |
| 107 base::Unretained(this))); |
| 108 ui_test_utils::RunMessageLoop(); |
| 109 } |
| 110 |
| 111 IN_PROC_BROWSER_TEST_F(BrowsingDataCookieHelperTest, CannedUnique) { |
| 112 const GURL origin("http://www.google.com"); |
| 113 net::CookieList cookie; |
| 114 |
| 115 scoped_refptr<CannedBrowsingDataCookieHelper> helper( |
| 116 new CannedBrowsingDataCookieHelper(&testing_profile_)); |
| 117 |
| 118 ASSERT_TRUE(helper->empty()); |
| 119 helper->AddChangeCookie(origin, "A=1", net::CookieOptions()); |
| 120 helper->AddChangeCookie(origin, "A=1", net::CookieOptions()); |
| 121 helper->StartFetching( |
| 122 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback, |
| 123 base::Unretained(this))); |
| 124 cookie = cookie_list_; |
| 125 helper->Reset(); |
| 126 ASSERT_TRUE(helper->empty()); |
| 127 |
| 128 helper->AddReadCookie(origin, cookie); |
| 129 helper->AddReadCookie(origin, cookie); |
| 130 helper->StartFetching( |
| 131 base::Bind(&BrowsingDataCookieHelperTest::CannedUniqueCallback, |
| 132 base::Unretained(this))); |
| 133 } |
| 134 |
| 135 } // namespace |
OLD | NEW |