Converted policy.PolicyTest.ClearSiteDataOnExit pyauto test to a browser_test.

BUG=143637


Review URL: https://chromiumcodereview.appspot.com/10873082

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@153529 0039d316-1c4b-4281-b951-d872f2087c98
parent c6e8c574
......@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "chrome/browser/browser_process.h"
#include "chrome/browser/policy/browser_policy_connector.h"
#include "chrome/browser/policy/mock_configuration_policy_provider.h"
......@@ -13,6 +15,7 @@
#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/test/browser_test_utils.h"
#include "googleurl/src/gurl.h"
#include "policy/policy_constants.h"
#include "testing/gmock/include/gmock/gmock.h"
......@@ -22,6 +25,15 @@ 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";
} // namespace
class PolicyTest : public InProcessBrowserTest {
protected:
PolicyTest() {}
......@@ -70,4 +82,31 @@ IN_PROC_BROWSER_TEST_F(PolicyTest, BookmarkBarEnabled) {
EXPECT_EQ(BookmarkBar::DETACHED, browser()->bookmark_bar_state());
}
IN_PROC_BROWSER_TEST_F(PolicyTest, PRE_PRE_ClearSiteDataOnExit) {
Profile* profile = browser()->profile();
GURL url(kURL);
// No cookies at startup.
EXPECT_TRUE(content::GetCookies(profile, url).empty());
// Set a cookie now.
std::string value = std::string(kCookieValue) + std::string(kCookieOptions);
EXPECT_TRUE(content::SetCookie(profile, url, value));
// Verify it was set.
EXPECT_EQ(kCookieValue, GetCookies(profile, url));
}
IN_PROC_BROWSER_TEST_F(PolicyTest, PRE_ClearSiteDataOnExit) {
// Verify that the cookie persists across restarts.
EXPECT_EQ(kCookieValue, GetCookies(browser()->profile(), GURL(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(GetCookies(browser()->profile(), GURL(kURL)).empty());
}
} // namespace policy
......@@ -628,32 +628,6 @@ class PolicyTest(policy_base.PolicyTestBase):
expect_retval=True),
msg='The force install extension was never installed.')
def testClearSiteDataOnExit(self):
"""Verify the ClearSiteDataOnExit policy is taking effect.
Install a cookie and make sure the cookie gets removed on browser restart
when the policy is set.
"""
cookie_url = 'http://example.com'
cookie_val = 'ponies=unicorns'
self.SetCookie(pyauto.GURL(cookie_url),
cookie_val + ';expires=Wed Jan 01 3000 00:00:00 GMT')
# Cookie should be kept over restarts.
self.RestartBrowser(clear_profile=False)
self.assertEqual(
cookie_val, self.GetCookie(pyauto.GURL(cookie_url)),
msg='Cookie on ' + cookie_url + ' does not match ' + cookie_val + '.');
# With the policy set, the cookie should be gone after a restart.
self.SetUserPolicy({
'ClearSiteDataOnExit': True
})
self.RestartBrowser(clear_profile=False)
self.assertFalse(
self.GetCookie(pyauto.GURL(cookie_url)),
msg='Cookie present on ' + cookie_url + '.');
if __name__ == '__main__':
pyauto_functional.Main()
......@@ -166,8 +166,26 @@ void GetCookiesOnIOThread(const GURL& url,
context_getter->GetURLRequestContext()->cookie_store();
cookie_store->GetCookiesWithOptionsAsync(
url, net::CookieOptions(),
base::Bind(&GetCookiesCallback,
base::Unretained(cookies), base::Unretained(event)));
base::Bind(&GetCookiesCallback, cookies, event));
}
void SetCookieCallback(bool* result,
base::WaitableEvent* event,
bool success) {
*result = success;
event->Signal();
}
void SetCookieOnIOThread(const GURL& url,
const std::string& value,
net::URLRequestContextGetter* context_getter,
base::WaitableEvent* event,
bool* result) {
net::CookieStore* cookie_store =
context_getter->GetURLRequestContext()->cookie_store();
cookie_store->SetCookieWithOptionsAsync(
url, value, net::CookieOptions(),
base::Bind(&SetCookieCallback, result, event));
}
} // namespace
......@@ -319,6 +337,22 @@ std::string GetCookies(BrowserContext* browser_context, const GURL& url) {
return cookies;
}
bool SetCookie(BrowserContext* browser_context,
const GURL& url,
const std::string& value) {
bool result = false;
base::WaitableEvent event(true, false);
net::URLRequestContextGetter* context_getter =
browser_context->GetRequestContext();
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&SetCookieOnIOThread, url, value,
make_scoped_refptr(context_getter), &event, &result));
event.Wait();
return result;
}
TitleWatcher::TitleWatcher(WebContents* web_contents,
const string16& expected_title)
: web_contents_(web_contents),
......
......@@ -5,6 +5,7 @@
#ifndef CONTENT_PUBLIC_TEST_BROWSER_TEST_UTILS_H_
#define CONTENT_PUBLIC_TEST_BROWSER_TEST_UTILS_H_
#include <string>
#include <vector>
#include "base/callback_forward.h"
......@@ -101,9 +102,14 @@ bool ExecuteJavaScriptAndExtractString(
const std::wstring& script,
std::string* result) WARN_UNUSED_RESULT;
// Returns the cookies for the given url. Runs a nested message loop.
// Returns the cookies for the given url.
std::string GetCookies(BrowserContext* browser_context, const GURL& url);
// Sets a cookie for the given url. Returns true on success.
bool SetCookie(BrowserContext* browser_context,
const GURL& url,
const std::string& value);
// Watches title changes on a tab, blocking until an expected title is set.
class TitleWatcher : public NotificationObserver {
public:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment