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 CreatCookiesForTest() { |
| 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 protected: |
| 27 TestingProfile testing_profile_; |
| 28 }; |
| 29 |
| 30 // Called back by BrowsingDataCookieHelper on the UI thread once the cookie |
| 31 // information has been retrieved. |
| 32 class StopTestOnCallback { |
| 33 public: |
| 34 explicit StopTestOnCallback(BrowsingDataCookieHelper* cookie_helper) |
| 35 : cookie_helper_(cookie_helper) { |
| 36 DCHECK(cookie_helper_); |
| 37 } |
| 38 |
| 39 void FetchCallback(const net::CookieList& cookies) { |
| 40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 41 ASSERT_EQ(2UL, cookies.size()); |
| 42 net::CookieList::const_iterator it = cookies.begin(); |
| 43 |
| 44 ASSERT_TRUE(it != cookies.end()); |
| 45 EXPECT_EQ("www.google.com", it->Domain()); |
| 46 EXPECT_EQ("A", it->Name()); |
| 47 |
| 48 ASSERT_TRUE(++it != cookies.end()); |
| 49 EXPECT_EQ("www.gmail.google.com", it->Domain()); |
| 50 EXPECT_EQ("B", it->Name()); |
| 51 |
| 52 ASSERT_TRUE(++it == cookies.end()); |
| 53 MessageLoop::current()->Quit(); |
| 54 } |
| 55 |
| 56 void DeleteCallback(const net::CookieList& cookies) { |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 58 ASSERT_EQ(1UL, cookies.size()); |
| 59 net::CookieList::const_iterator it = cookies.begin(); |
| 60 |
| 61 ASSERT_TRUE(it != cookies.end()); |
| 62 EXPECT_EQ("www.gmail.google.com", it->Domain()); |
| 63 EXPECT_EQ("B", it->Name()); |
| 64 |
| 65 ASSERT_TRUE(++it == cookies.end()); |
| 66 MessageLoop::current()->Quit(); |
| 67 } |
| 68 |
| 69 void CannedUniqueCallback(const net::CookieList& cookies) { |
| 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 71 ASSERT_EQ(1UL, cookies.size()); |
| 72 net::CookieList::const_iterator it = cookies.begin(); |
| 73 |
| 74 ASSERT_TRUE(it != cookies.end()); |
| 75 EXPECT_EQ("http://www.google.com/", it->Source()); |
| 76 EXPECT_EQ("A", it->Name()); |
| 77 |
| 78 ASSERT_TRUE(++it == cookies.end()); |
| 79 } |
| 80 |
| 81 private: |
| 82 BrowsingDataCookieHelper* cookie_helper_; |
| 83 }; |
| 84 |
| 85 IN_PROC_BROWSER_TEST_F(BrowsingDataCookieHelperTest, FetchData) { |
| 86 CreatCookiesForTest(); |
| 87 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( |
| 88 new BrowsingDataCookieHelper(&testing_profile_)); |
| 89 StopTestOnCallback stop_test_on_callback(cookie_helper); |
| 90 |
| 91 cookie_helper->StartFetching( |
| 92 base::Bind(&StopTestOnCallback::FetchCallback, |
| 93 base::Unretained(&stop_test_on_callback))); |
| 94 |
| 95 // Blocks until StopTestOnCallback::Callback is notified. |
| 96 ui_test_utils::RunMessageLoop(); |
| 97 } |
| 98 |
| 99 IN_PROC_BROWSER_TEST_F(BrowsingDataCookieHelperTest, DeleteCookie) { |
| 100 CreatCookiesForTest(); |
| 101 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( |
| 102 new BrowsingDataCookieHelper(&testing_profile_)); |
| 103 StopTestOnCallback stop_test_on_callback(cookie_helper); |
| 104 |
| 105 cookie_helper->StartFetching( |
| 106 base::Bind(&StopTestOnCallback::FetchCallback, |
| 107 base::Unretained(&stop_test_on_callback))); |
| 108 |
| 109 // Blocks until StopTestOnCallback::Callback is notified. |
| 110 ui_test_utils::RunMessageLoop(); |
| 111 |
| 112 net::CookieMonster::CanonicalCookie cookie = |
| 113 cookie_helper->cookie_list()[0]; |
| 114 cookie_helper->DeleteCookie(cookie); |
| 115 |
| 116 cookie_helper->StartFetching( |
| 117 base::Bind(&StopTestOnCallback::DeleteCallback, |
| 118 base::Unretained(&stop_test_on_callback))); |
| 119 ui_test_utils::RunMessageLoop(); |
| 120 } |
| 121 |
| 122 IN_PROC_BROWSER_TEST_F(BrowsingDataCookieHelperTest, CannedUnique) { |
| 123 const GURL origin("http://www.google.com"); |
| 124 net::CookieList cookie; |
| 125 |
| 126 scoped_refptr<CannedBrowsingDataCookieHelper> helper( |
| 127 new CannedBrowsingDataCookieHelper(&testing_profile_)); |
| 128 StopTestOnCallback callback(helper); |
| 129 |
| 130 ASSERT_TRUE(helper->empty()); |
| 131 helper->AddChangeCookie(origin, "A=1", net::CookieOptions()); |
| 132 helper->AddChangeCookie(origin, "A=1", net::CookieOptions()); |
| 133 helper->StartFetching( |
| 134 base::Bind(&StopTestOnCallback::CannedUniqueCallback, |
| 135 base::Unretained(&callback))); |
| 136 cookie = helper->cookie_list(); |
| 137 helper->Reset(); |
| 138 ASSERT_TRUE(helper->empty()); |
| 139 |
| 140 helper->AddReadCookie(origin, cookie); |
| 141 helper->AddReadCookie(origin, cookie); |
| 142 helper->StartFetching( |
| 143 base::Bind(&StopTestOnCallback::CannedUniqueCallback, |
| 144 base::Unretained(&callback))); |
| 145 } |
| 146 |
| 147 } // namespace |
OLD | NEW |