Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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/profile_resetter/profile_resetter.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "chrome/browser/profile_resetter/profile_resetter_test_base.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/browser/ui/browser.h" | |
| 11 #include "chrome/test/base/in_process_browser_test.h" | |
| 12 #include "content/public/test/test_utils.h" | |
| 13 #include "net/cookies/cookie_store.h" | |
| 14 #include "net/url_request/url_request_context.h" | |
| 15 #include "net/url_request/url_request_context_getter.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 using content::BrowserThread; | |
| 20 | |
| 21 class RemoveCookieTester { | |
|
battre
2013/06/20 06:15:57
highlevel class comment?
vasilii
2013/06/20 13:08:22
Done.
| |
| 22 public: | |
| 23 explicit RemoveCookieTester(Profile* profile); | |
| 24 ~RemoveCookieTester(); | |
| 25 | |
| 26 bool ContainsCookie(); | |
| 27 void AddCookie(); | |
| 28 | |
| 29 private: | |
| 30 void GetCookieOnIOThread(net::URLRequestContextGetter* rq_context); | |
| 31 void SetCookieOnIOThread(net::URLRequestContextGetter* rq_context); | |
| 32 void GetCookieCallback(const std::string& cookies); | |
| 33 void SetCookieCallback(bool result); | |
| 34 | |
| 35 void BlockUntilNotified(); | |
| 36 void Notify(); | |
| 37 | |
| 38 bool get_cookie_success_; | |
| 39 bool waiting_callback_; | |
| 40 Profile* profile_; | |
| 41 scoped_refptr<content::MessageLoopRunner> runner_; | |
| 42 | |
| 43 DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester); | |
| 44 }; | |
| 45 | |
| 46 RemoveCookieTester::RemoveCookieTester(Profile* profile) | |
| 47 : get_cookie_success_(false), | |
| 48 waiting_callback_(false), | |
| 49 profile_(profile) { | |
| 50 } | |
| 51 | |
| 52 RemoveCookieTester::~RemoveCookieTester() {} | |
| 53 | |
| 54 // Returns true, if the given cookie exists in the cookie store. | |
| 55 bool RemoveCookieTester::ContainsCookie() { | |
| 56 get_cookie_success_ = false; | |
| 57 waiting_callback_ = true; | |
| 58 BrowserThread::PostTask( | |
| 59 BrowserThread::IO, FROM_HERE, | |
| 60 base::Bind(&RemoveCookieTester::GetCookieOnIOThread, | |
| 61 base::Unretained(this), | |
| 62 base::Unretained(profile_->GetRequestContext()))); | |
|
battre
2013/06/20 06:15:57
with this suggestions below, this function would b
vasilii
2013/06/20 13:08:22
Done.
| |
| 63 BlockUntilNotified(); | |
| 64 return get_cookie_success_; | |
| 65 } | |
| 66 | |
| 67 void RemoveCookieTester::AddCookie() { | |
| 68 waiting_callback_ = true; | |
| 69 BrowserThread::PostTask( | |
| 70 BrowserThread::IO, FROM_HERE, | |
| 71 base::Bind(&RemoveCookieTester::SetCookieOnIOThread, | |
| 72 base::Unretained(this), | |
| 73 base::Unretained(profile_->GetRequestContext()))); | |
| 74 BlockUntilNotified(); | |
| 75 } | |
| 76 | |
| 77 void RemoveCookieTester::GetCookieOnIOThread( | |
| 78 net::URLRequestContextGetter* rq_context) { | |
|
battre
2013/06/20 06:15:57
nit: We try not to use abbreviations in variable n
vasilii
2013/06/20 13:08:22
Done.
| |
| 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 80 net::CookieStore* cookie_store = rq_context-> | |
| 81 GetURLRequestContext()->cookie_store(); | |
| 82 cookie_store->GetCookiesWithOptionsAsync( | |
| 83 GURL("http://host1/"), net::CookieOptions(), | |
|
battre
2013/06/20 06:15:57
nit: -2 indentation
battre
2013/06/20 06:15:57
What do you think of passing the host name as a pa
battre
2013/06/20 06:15:57
can you move this string literal and also the A=1
vasilii
2013/06/20 13:08:22
Done.
vasilii
2013/06/20 13:08:22
Done.
vasilii
2013/06/20 13:08:22
Done.
| |
| 84 base::Bind(&RemoveCookieTester::GetCookieCallback, | |
| 85 base::Unretained(this))); | |
| 86 } | |
| 87 | |
| 88 void RemoveCookieTester::SetCookieOnIOThread( | |
| 89 net::URLRequestContextGetter* rq_context) { | |
| 90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 91 net::CookieStore* cookie_store = rq_context-> | |
| 92 GetURLRequestContext()->cookie_store(); | |
| 93 cookie_store->SetCookieWithOptionsAsync( | |
| 94 GURL("http://host1/"), "A=1", net::CookieOptions(), | |
|
battre
2013/06/20 06:15:57
what do you think of passing the hostname and cook
vasilii
2013/06/20 13:08:22
Done.
| |
| 95 base::Bind(&RemoveCookieTester::SetCookieCallback, | |
| 96 base::Unretained(this))); | |
| 97 } | |
| 98 | |
| 99 void RemoveCookieTester::GetCookieCallback(const std::string& cookies) { | |
| 100 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 101 BrowserThread::PostTask( | |
| 102 BrowserThread::UI, FROM_HERE, | |
| 103 base::Bind(&RemoveCookieTester::GetCookieCallback, | |
| 104 base::Unretained(this), cookies)); | |
| 105 return; | |
| 106 } | |
| 107 if (cookies == "A=1") { | |
| 108 get_cookie_success_ = true; | |
|
battre
2013/06/20 06:15:57
opt: What do you think of just storing cookies int
vasilii
2013/06/20 13:08:22
Done.
| |
| 109 } else { | |
| 110 EXPECT_EQ("", cookies); | |
| 111 get_cookie_success_ = false; | |
| 112 } | |
| 113 Notify(); | |
| 114 } | |
| 115 | |
| 116 void RemoveCookieTester::SetCookieCallback(bool result) { | |
| 117 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 118 BrowserThread::PostTask( | |
| 119 BrowserThread::UI, FROM_HERE, | |
| 120 base::Bind(&RemoveCookieTester::SetCookieCallback, | |
| 121 base::Unretained(this), result)); | |
| 122 return; | |
| 123 } | |
| 124 ASSERT_TRUE(result); | |
| 125 Notify(); | |
| 126 } | |
| 127 | |
| 128 void RemoveCookieTester::BlockUntilNotified() { | |
| 129 DCHECK(!runner_); | |
| 130 if (waiting_callback_) { | |
| 131 runner_ = new content::MessageLoopRunner; | |
| 132 runner_->Run(); | |
| 133 runner_ = NULL; | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 void RemoveCookieTester::Notify() { | |
| 138 DCHECK(waiting_callback_); | |
| 139 waiting_callback_ = false; | |
| 140 if (runner_) | |
| 141 runner_->Quit(); | |
| 142 } | |
| 143 | |
| 144 class ProfileResetTest : public InProcessBrowserTest, | |
| 145 public ProfileResetterTestBase { | |
| 146 protected: | |
| 147 virtual void SetUpOnMainThread() OVERRIDE { | |
| 148 resetter_.reset(new ProfileResetter(browser()->profile())); | |
| 149 } | |
| 150 }; | |
| 151 | |
| 152 | |
| 153 IN_PROC_BROWSER_TEST_F(ProfileResetTest, ResetCookiesAndSiteData) { | |
| 154 RemoveCookieTester tester(browser()->profile()); | |
| 155 tester.AddCookie(); | |
|
battre
2013/06/20 06:15:57
how about making this call
tester.AddCookie(kCooki
vasilii
2013/06/20 13:08:22
Done.
| |
| 156 ASSERT_TRUE(tester.ContainsCookie()); | |
| 157 | |
| 158 resetter_->Reset(ProfileResetter::COOKIES_AND_SITE_DATA, | |
| 159 base::Bind(&ProfileResetterMockObject::StopLoop, | |
| 160 base::Unretained(&mock_object_))); | |
| 161 mock_object_.RunLoop(); | |
| 162 | |
| 163 EXPECT_FALSE(tester.ContainsCookie()); | |
|
battre
2013/06/20 06:15:57
and making this here:
EXPECT_EQ(kCookieDefinition,
vasilii
2013/06/20 13:08:22
Done.
| |
| 164 } | |
| 165 | |
| 166 } // namespace | |
| OLD | NEW |