Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: chrome/browser/policy/policy_browsertest.cc

Issue 10873082: Converted policy.PolicyTest.ClearSiteDataOnExit pyauto test to a browser_test. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/test/functional/policy.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <string>
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/run_loop.h"
5 #include "chrome/browser/browser_process.h" 11 #include "chrome/browser/browser_process.h"
6 #include "chrome/browser/policy/browser_policy_connector.h" 12 #include "chrome/browser/policy/browser_policy_connector.h"
7 #include "chrome/browser/policy/mock_configuration_policy_provider.h" 13 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
8 #include "chrome/browser/policy/policy_map.h" 14 #include "chrome/browser/policy/policy_map.h"
9 #include "chrome/browser/prefs/pref_service.h" 15 #include "chrome/browser/prefs/pref_service.h"
10 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/bookmarks/bookmark_bar.h" 17 #include "chrome/browser/ui/bookmarks/bookmark_bar.h"
12 #include "chrome/browser/ui/browser.h" 18 #include "chrome/browser/ui/browser.h"
13 #include "chrome/common/pref_names.h" 19 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/in_process_browser_test.h" 20 #include "chrome/test/base/in_process_browser_test.h"
15 #include "chrome/test/base/ui_test_utils.h" 21 #include "chrome/test/base/ui_test_utils.h"
22 #include "content/public/browser/browser_thread.h"
16 #include "googleurl/src/gurl.h" 23 #include "googleurl/src/gurl.h"
24 #include "net/cookies/cookie_options.h"
25 #include "net/cookies/cookie_store.h"
26 #include "net/url_request/url_request_context.h"
27 #include "net/url_request/url_request_context_getter.h"
17 #include "policy/policy_constants.h" 28 #include "policy/policy_constants.h"
18 #include "testing/gmock/include/gmock/gmock.h" 29 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
20 31
32 using content::BrowserThread;
21 using testing::Return; 33 using testing::Return;
22 34
23 namespace policy { 35 namespace policy {
24 36
37 namespace {
38
39 const char kURL[] = "http://example.com";
40 const char kCookieValue[] = "converted=true";
41 // Assigned to Philip J. Fry to fix eventually.
42 const char kCookieOptions[] = ";expires=Wed Jan 01 3000 00:00:00 GMT";
43
44 void SetCookieCallback(
45 bool* result,
46 const base::Closure& callback,
47 bool success) {
48 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
49 *result = success;
50 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback);
51 }
52
53 void SetCookieOnIOThread(
54 const GURL& url,
55 const std::string& value,
56 const scoped_refptr<net::URLRequestContextGetter>& context,
57 bool* result,
58 const base::Closure& callback) {
59 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
60 context->GetURLRequestContext()->cookie_store()->SetCookieWithOptionsAsync(
61 url, value, net::CookieOptions(),
62 base::Bind(&SetCookieCallback, result, callback));
63 }
64
65 void GetCookieCallback(
66 std::string* value,
67 const base::Closure& callback,
68 const std::string& cookies) {
69 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
70 *value = cookies;
71 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback);
72 }
73
74 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.
75 const GURL& url,
76 const scoped_refptr<net::URLRequestContextGetter>& context,
77 std::string* value,
78 const base::Closure& callback) {
79 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
80 context->GetURLRequestContext()->cookie_store()->GetCookiesWithOptionsAsync(
81 url, net::CookieOptions(),
82 base::Bind(&GetCookieCallback, value, callback));
83 }
84
85 } // namespace
86
25 class PolicyTest : public InProcessBrowserTest { 87 class PolicyTest : public InProcessBrowserTest {
26 protected: 88 protected:
27 PolicyTest() {} 89 PolicyTest() {}
28 90
29 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { 91 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
30 EXPECT_CALL(provider_, IsInitializationComplete()) 92 EXPECT_CALL(provider_, IsInitializationComplete())
31 .WillRepeatedly(Return(true)); 93 .WillRepeatedly(Return(true));
32 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); 94 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
33 } 95 }
34 96
97 bool SetCookie(const std::string& url, const std::string& value) {
98 scoped_refptr<net::URLRequestContextGetter> context(
99 browser()->profile()->GetRequestContext());
100 bool result = false;
101 base::RunLoop run_loop;
102 BrowserThread::PostTask(
103 BrowserThread::IO, FROM_HERE,
104 base::Bind(&SetCookieOnIOThread, GURL(url), value, context, &result,
105 run_loop.QuitClosure()));
106 run_loop.Run();
107 return result;
108 }
109
110 std::string GetCookie(const std::string& url) {
111 scoped_refptr<net::URLRequestContextGetter> context(
112 browser()->profile()->GetRequestContext());
113 std::string cookie;
114 base::RunLoop run_loop;
115 BrowserThread::PostTask(
116 BrowserThread::IO, FROM_HERE,
117 base::Bind(&GetCookieOnIOThread, GURL(url), context, &cookie,
118 run_loop.QuitClosure()));
119 run_loop.Run();
120 return cookie;
121 }
122
35 MockConfigurationPolicyProvider provider_; 123 MockConfigurationPolicyProvider provider_;
36 }; 124 };
37 125
38 IN_PROC_BROWSER_TEST_F(PolicyTest, BookmarkBarEnabled) { 126 IN_PROC_BROWSER_TEST_F(PolicyTest, BookmarkBarEnabled) {
39 // Test starts in about:blank. 127 // Test starts in about:blank.
40 PrefService* prefs = browser()->profile()->GetPrefs(); 128 PrefService* prefs = browser()->profile()->GetPrefs();
41 EXPECT_FALSE(prefs->IsManagedPreference(prefs::kShowBookmarkBar)); 129 EXPECT_FALSE(prefs->IsManagedPreference(prefs::kShowBookmarkBar));
42 EXPECT_FALSE(prefs->GetBoolean(prefs::kShowBookmarkBar)); 130 EXPECT_FALSE(prefs->GetBoolean(prefs::kShowBookmarkBar));
43 EXPECT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state()); 131 EXPECT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
44 132
(...skipping 18 matching lines...) Expand all
63 EXPECT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state()); 151 EXPECT_EQ(BookmarkBar::HIDDEN, browser()->bookmark_bar_state());
64 152
65 policies.Clear(); 153 policies.Clear();
66 provider_.UpdateChromePolicy(policies); 154 provider_.UpdateChromePolicy(policies);
67 EXPECT_FALSE(prefs->IsManagedPreference(prefs::kShowBookmarkBar)); 155 EXPECT_FALSE(prefs->IsManagedPreference(prefs::kShowBookmarkBar));
68 EXPECT_FALSE(prefs->GetBoolean(prefs::kShowBookmarkBar)); 156 EXPECT_FALSE(prefs->GetBoolean(prefs::kShowBookmarkBar));
69 // The bookmark bar is shown detached in the NTP, when disabled by prefs only. 157 // The bookmark bar is shown detached in the NTP, when disabled by prefs only.
70 EXPECT_EQ(BookmarkBar::DETACHED, browser()->bookmark_bar_state()); 158 EXPECT_EQ(BookmarkBar::DETACHED, browser()->bookmark_bar_state());
71 } 159 }
72 160
161 IN_PROC_BROWSER_TEST_F(PolicyTest, PRE_PRE_ClearSiteDataOnExit) {
162 // No cookies at startup.
163 EXPECT_TRUE(GetCookie(kURL).empty());
164 // Set a cookie now.
165 std::string value = std::string(kCookieValue) + std::string(kCookieOptions);
166 EXPECT_TRUE(SetCookie(kURL, value));
167 // Verify it was set.
168 EXPECT_EQ(kCookieValue, GetCookie(kURL));
169 }
170
171 IN_PROC_BROWSER_TEST_F(PolicyTest, PRE_ClearSiteDataOnExit) {
172 // Verify that the cookie persists across restarts.
173 EXPECT_EQ(kCookieValue, GetCookie(kURL));
174 // Now set the policy and the cookie should be gone after another restart.
175 PolicyMap policies;
176 policies.Set(key::kClearSiteDataOnExit, POLICY_LEVEL_MANDATORY,
177 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(true));
178 provider_.UpdateChromePolicy(policies);
179 }
180
181 IN_PROC_BROWSER_TEST_F(PolicyTest, ClearSiteDataOnExit) {
182 // Verify that the cookie is gone.
183 EXPECT_TRUE(GetCookie(kURL).empty());
184 }
185
73 } // namespace policy 186 } // namespace policy
OLDNEW
« no previous file with comments | « no previous file | chrome/test/functional/policy.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698