Chromium Code Reviews| Index: chrome/browser/policy/policy_browsertest.cc |
| diff --git a/chrome/browser/policy/policy_browsertest.cc b/chrome/browser/policy/policy_browsertest.cc |
| index ec924de954bf72f7a09a90154ebd6ac16fd0db68..c19c4bbb1e6c1487b53a6a8dbe8a9b4595e7fdd7 100644 |
| --- a/chrome/browser/policy/policy_browsertest.cc |
| +++ b/chrome/browser/policy/policy_browsertest.cc |
| @@ -2,6 +2,12 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include <string> |
| + |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/run_loop.h" |
| #include "chrome/browser/browser_process.h" |
| #include "chrome/browser/policy/browser_policy_connector.h" |
| #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| @@ -13,15 +19,71 @@ |
| #include "chrome/common/pref_names.h" |
| #include "chrome/test/base/in_process_browser_test.h" |
| #include "chrome/test/base/ui_test_utils.h" |
| +#include "content/public/browser/browser_thread.h" |
| #include "googleurl/src/gurl.h" |
| +#include "net/cookies/cookie_options.h" |
| +#include "net/cookies/cookie_store.h" |
| +#include "net/url_request/url_request_context.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| #include "policy/policy_constants.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| +using content::BrowserThread; |
| using testing::Return; |
| namespace policy { |
| +namespace { |
| + |
| +const char kURL[] = "http://example.com"; |
| +const char kCookieValue[] = "converted=true"; |
| +// Assigned to Philip J. Fry to fix eventually. |
| +const char kCookieOptions[] = ";expires=Wed Jan 01 3000 00:00:00 GMT"; |
| + |
| +void SetCookieCallback( |
| + bool* result, |
| + const base::Closure& callback, |
| + bool success) { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + *result = success; |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); |
| +} |
| + |
| +void SetCookieOnIOThread( |
| + const GURL& url, |
| + const std::string& value, |
| + const scoped_refptr<net::URLRequestContextGetter>& context, |
| + bool* result, |
| + const base::Closure& callback) { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + context->GetURLRequestContext()->cookie_store()->SetCookieWithOptionsAsync( |
| + url, value, net::CookieOptions(), |
| + base::Bind(&SetCookieCallback, result, callback)); |
| +} |
| + |
| +void GetCookieCallback( |
| + std::string* value, |
| + const base::Closure& callback, |
| + const std::string& cookies) { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + *value = cookies; |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); |
| +} |
| + |
| +void GetCookieOnIOThread( |
|
jam
2012/08/27 17:02:04
see r153484 which added a common method for gettin
Joao da Silva
2012/08/27 17:39:43
Thanks for the pointer, done.
|
| + const GURL& url, |
| + const scoped_refptr<net::URLRequestContextGetter>& context, |
| + std::string* value, |
| + const base::Closure& callback) { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + context->GetURLRequestContext()->cookie_store()->GetCookiesWithOptionsAsync( |
| + url, net::CookieOptions(), |
| + base::Bind(&GetCookieCallback, value, callback)); |
| +} |
| + |
| +} // namespace |
| + |
| class PolicyTest : public InProcessBrowserTest { |
| protected: |
| PolicyTest() {} |
| @@ -32,6 +94,32 @@ class PolicyTest : public InProcessBrowserTest { |
| BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); |
| } |
| + bool SetCookie(const std::string& url, const std::string& value) { |
| + scoped_refptr<net::URLRequestContextGetter> context( |
| + browser()->profile()->GetRequestContext()); |
| + bool result = false; |
| + base::RunLoop run_loop; |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&SetCookieOnIOThread, GURL(url), value, context, &result, |
| + run_loop.QuitClosure())); |
| + run_loop.Run(); |
| + return result; |
| + } |
| + |
| + std::string GetCookie(const std::string& url) { |
| + scoped_refptr<net::URLRequestContextGetter> context( |
| + browser()->profile()->GetRequestContext()); |
| + std::string cookie; |
| + base::RunLoop run_loop; |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&GetCookieOnIOThread, GURL(url), context, &cookie, |
| + run_loop.QuitClosure())); |
| + run_loop.Run(); |
| + return cookie; |
| + } |
| + |
| MockConfigurationPolicyProvider provider_; |
| }; |
| @@ -70,4 +158,29 @@ IN_PROC_BROWSER_TEST_F(PolicyTest, BookmarkBarEnabled) { |
| EXPECT_EQ(BookmarkBar::DETACHED, browser()->bookmark_bar_state()); |
| } |
| +IN_PROC_BROWSER_TEST_F(PolicyTest, PRE_PRE_ClearSiteDataOnExit) { |
| + // No cookies at startup. |
| + EXPECT_TRUE(GetCookie(kURL).empty()); |
| + // Set a cookie now. |
| + std::string value = std::string(kCookieValue) + std::string(kCookieOptions); |
| + EXPECT_TRUE(SetCookie(kURL, value)); |
| + // Verify it was set. |
| + EXPECT_EQ(kCookieValue, GetCookie(kURL)); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(PolicyTest, PRE_ClearSiteDataOnExit) { |
| + // Verify that the cookie persists across restarts. |
| + EXPECT_EQ(kCookieValue, GetCookie(kURL)); |
| + // Now set the policy and the cookie should be gone after another restart. |
| + PolicyMap policies; |
| + policies.Set(key::kClearSiteDataOnExit, POLICY_LEVEL_MANDATORY, |
| + POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true)); |
| + provider_.UpdateChromePolicy(policies); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(PolicyTest, ClearSiteDataOnExit) { |
| + // Verify that the cookie is gone. |
| + EXPECT_TRUE(GetCookie(kURL).empty()); |
| +} |
| + |
| } // namespace policy |