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

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

Issue 10784009: screenshot disabling policy tests (Closed) Base URL: http://git.chromium.org/chromium/src.git@disable_screenshots
Patch Set: Added missing CrOS guards 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
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> 5 #include <string>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_path.h" 8 #include "base/file_path.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/scoped_temp_dir.h" 10 #include "base/scoped_temp_dir.h"
11 #include "base/string16.h" 11 #include "base/string16.h"
12 #include "base/test/test_file_util.h"
12 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
13 #include "base/test/test_file_util.h"
14 #include "chrome/app/chrome_command_ids.h" 14 #include "chrome/app/chrome_command_ids.h"
15 #include "chrome/browser/autocomplete/autocomplete_controller.h" 15 #include "chrome/browser/autocomplete/autocomplete_controller.h"
16 #include "chrome/browser/browser_process.h" 16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/history/history.h" 17 #include "chrome/browser/history/history.h"
18 #include "chrome/browser/history/history_service_factory.h" 18 #include "chrome/browser/history/history_service_factory.h"
19 #include "chrome/browser/infobars/infobar_tab_helper.h" 19 #include "chrome/browser/infobars/infobar_tab_helper.h"
20 #include "chrome/browser/net/url_request_mock_util.h" 20 #include "chrome/browser/net/url_request_mock_util.h"
21 #include "chrome/browser/policy/browser_policy_connector.h" 21 #include "chrome/browser/policy/browser_policy_connector.h"
22 #include "chrome/browser/policy/mock_configuration_policy_provider.h" 22 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
23 #include "chrome/browser/policy/policy_map.h" 23 #include "chrome/browser/policy/policy_map.h"
(...skipping 16 matching lines...) Expand all
40 #include "chrome/browser/ui/tab_contents/tab_contents.h" 40 #include "chrome/browser/ui/tab_contents/tab_contents.h"
41 #include "chrome/common/chrome_notification_types.h" 41 #include "chrome/common/chrome_notification_types.h"
42 #include "chrome/common/pref_names.h" 42 #include "chrome/common/pref_names.h"
43 #include "chrome/test/base/in_process_browser_test.h" 43 #include "chrome/test/base/in_process_browser_test.h"
44 #include "chrome/test/base/ui_test_utils.h" 44 #include "chrome/test/base/ui_test_utils.h"
45 #include "content/public/browser/browser_context.h" 45 #include "content/public/browser/browser_context.h"
46 #include "content/public/browser/browser_thread.h" 46 #include "content/public/browser/browser_thread.h"
47 #include "content/public/browser/download_item.h" 47 #include "content/public/browser/download_item.h"
48 #include "content/public/browser/download_manager.h" 48 #include "content/public/browser/download_manager.h"
49 #include "content/public/browser/notification_service.h" 49 #include "content/public/browser/notification_service.h"
50 #include "content/public/browser/notification_source.h"
50 #include "content/public/browser/notification_types.h" 51 #include "content/public/browser/notification_types.h"
51 #include "content/public/browser/web_contents.h" 52 #include "content/public/browser/web_contents.h"
52 #include "content/public/test/browser_test_utils.h" 53 #include "content/public/test/browser_test_utils.h"
53 #include "content/public/test/download_test_observer.h" 54 #include "content/public/test/download_test_observer.h"
54 #include "content/public/test/test_utils.h" 55 #include "content/public/test/test_utils.h"
55 #include "content/test/net/url_request_mock_http_job.h" 56 #include "content/test/net/url_request_mock_http_job.h"
56 #include "googleurl/src/gurl.h" 57 #include "googleurl/src/gurl.h"
57 #include "net/base/net_util.h" 58 #include "net/base/net_util.h"
58 #include "net/url_request/url_request.h" 59 #include "net/url_request/url_request.h"
59 #include "net/url_request/url_request_filter.h" 60 #include "net/url_request/url_request_filter.h"
60 #include "policy/policy_constants.h" 61 #include "policy/policy_constants.h"
61 #include "testing/gmock/include/gmock/gmock.h" 62 #include "testing/gmock/include/gmock/gmock.h"
62 #include "testing/gtest/include/gtest/gtest.h" 63 #include "testing/gtest/include/gtest/gtest.h"
63 64
65 #if defined(OS_CHROMEOS)
66 #include "ash/accelerators/accelerator_controller.h"
67 #include "ash/accelerators/accelerator_table.h"
68 #include "ash/shell.h"
69 #include "ash/shell_delegate.h"
70 #include "chrome/browser/download/download_prefs.h"
71 #endif
72
64 using content::BrowserThread; 73 using content::BrowserThread;
65 using testing::Return; 74 using testing::Return;
66 75
67 namespace policy { 76 namespace policy {
68 77
69 namespace { 78 namespace {
70 79
71 const char kURL[] = "http://example.com"; 80 const char kURL[] = "http://example.com";
72 const char kCookieValue[] = "converted=true"; 81 const char kCookieValue[] = "converted=true";
73 // Assigned to Philip J. Fry to fix eventually. 82 // Assigned to Philip J. Fry to fix eventually.
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 observer.WaitForFinished(); 159 observer.WaitForFinished();
151 EXPECT_EQ( 160 EXPECT_EQ(
152 1u, observer.NumDownloadsSeenInState(content::DownloadItem::COMPLETE)); 161 1u, observer.NumDownloadsSeenInState(content::DownloadItem::COMPLETE));
153 EXPECT_TRUE(file_util::PathExists(downloaded)); 162 EXPECT_TRUE(file_util::PathExists(downloaded));
154 file_util::FileEnumerator enumerator( 163 file_util::FileEnumerator enumerator(
155 dir, false, file_util::FileEnumerator::FILES); 164 dir, false, file_util::FileEnumerator::FILES);
156 EXPECT_EQ(file, enumerator.Next().BaseName()); 165 EXPECT_EQ(file, enumerator.Next().BaseName());
157 EXPECT_EQ(FilePath(), enumerator.Next()); 166 EXPECT_EQ(FilePath(), enumerator.Next());
158 } 167 }
159 168
169 class LoadObserver : public content::NotificationObserver {
bartfab (slow) 2012/08/31 15:50:13 #include "content/public/browser/notification_obse
Joao da Silva 2012/09/03 09:01:04 This is essentially a WindowedNotificationObserver
qfel 2012/09/03 11:31:21 Seems I just missed its existence.
170 public:
171 LoadObserver()
172 : source_(content::NotificationService::AllSources()),
173 message_loop_runner_(new content::MessageLoopRunner) {
174 }
175
176 content::Source<content::NavigationController> Wait() {
177 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
178 content::NotificationService::AllSources());
179 message_loop_runner_->Run();
180 return source_;
181 }
182
183 void Observe(int type,
184 const content::NotificationSource& source,
185 const content::NotificationDetails& details) {
186 CHECK_EQ(type, content::NOTIFICATION_LOAD_STOP);
bartfab (slow) 2012/08/31 15:50:13 Why CHECK_EQ, not ASSERT_EQ?
qfel 2012/09/03 11:31:21 I wonder myself.. Seems I just happened to see sim
187 source_ = static_cast<content::Source<content::NavigationController> >(
188 source);
189 message_loop_runner_->Quit();
190 }
191
192 private:
193 content::NotificationRegistrar registrar_;
194 content::Source<content::NavigationController> source_;
195 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
196 };
197
198 #if defined(OS_CHROMEOS)
199 int CountScreenshots() {
200 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(
bartfab (slow) 2012/08/31 15:50:13 #include "chrome/browser/download/download_prefs.h
qfel 2012/09/03 11:31:21 It's in the OS_CHROMEOS guarded include section. M
201 ash::Shell::GetInstance()->delegate()->GetCurrentBrowserContext());
202 file_util::FileEnumerator enumerator(download_prefs->DownloadPath(),
203 false, file_util::FileEnumerator::FILES,
204 "Screenshot*");
205 int count = 0;
206 while (!enumerator.Next().empty())
207 count++;
208 return count;
209 }
210
211 void NoOp() {
212 // As the name suggests, do absolutely nothing and chill out. Used as a
213 // callback to wait for message loop to process previous messages.
bartfab (slow) 2012/08/31 15:50:13 This is what base::DoNothing is for.
qfel 2012/09/03 11:31:21 Done.
214 }
215 #endif
216
160 } // namespace 217 } // namespace
161 218
162 class PolicyTest : public InProcessBrowserTest { 219 class PolicyTest : public InProcessBrowserTest {
163 protected: 220 protected:
164 PolicyTest() {} 221 PolicyTest() {}
165 222
166 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { 223 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
167 EXPECT_CALL(provider_, IsInitializationComplete()) 224 EXPECT_CALL(provider_, IsInitializationComplete())
168 .WillRepeatedly(Return(true)); 225 .WillRepeatedly(Return(true));
169 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); 226 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
170 } 227 }
171 228
172 virtual void SetUpOnMainThread() OVERRIDE { 229 virtual void SetUpOnMainThread() OVERRIDE {
173 BrowserThread::PostTask( 230 BrowserThread::PostTask(
174 BrowserThread::IO, FROM_HERE, 231 BrowserThread::IO, FROM_HERE,
175 base::Bind(chrome_browser_net::SetUrlRequestMocksEnabled, true)); 232 base::Bind(chrome_browser_net::SetUrlRequestMocksEnabled, true));
176 } 233 }
177 234
235 void SetScreenshotPolicy(bool enabled) {
236 PolicyMap policies;
237 policies.Set(key::kDisableScreenshots, POLICY_LEVEL_MANDATORY,
238 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(!enabled));
bartfab (slow) 2012/08/31 15:50:13 #include "base/values.h" is missing for this.
qfel 2012/09/03 11:31:21 Done.
239 provider_.UpdateChromePolicy(policies);
240 }
241
242 void TestScreenshotFeedback(bool enabled) {
243 SetScreenshotPolicy(enabled);
244
245 // Wait for feedback page to load.
246 LoadObserver observer;
247 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_FEEDBACK));
248 content::WebContents* web_contents = observer.Wait()->GetWebContents();
249
250 // Wait for feedback page to fully initialize.
251 bool result = false;
252 EXPECT_TRUE(content::ExecuteJavaScriptAndExtractBool(
253 web_contents->GetRenderViewHost(),
254 std::wstring(),
bartfab (slow) 2012/08/31 15:50:13 L"" would work just as well.
qfel 2012/09/03 11:31:21 The code where I found ExecuteJavaScriptAndExtract
255 L"function btest_initCompleted(url) {"
bartfab (slow) 2012/08/31 15:50:13 Indentation throughout this JS code should be two
qfel 2012/09/03 11:31:21 Done.
256 L" var img = new Image();"
257 L" img.src = url;"
258 L" img.onload = function() {"
259 L" domAutomationController.send(img.width * img.height > 0);"
260 L" };"
261 L" img.onerror = function() {"
262 L" domAutomationController.send(false);"
263 L" };"
264 L"}"
265 L"function setupCurrentScreenshot(url) {"
266 L" btest_initCompleted(url);"
267 L"}"
268 L"var img = document.getElementById("
269 L" 'current-screenshots-thumbnailDiv-0-image');"
270 L"if (img)"
271 L" btest_initCompleted(img.src);",
Joao da Silva 2012/09/03 09:01:04 else domAutomationController.send(false);
qfel 2012/09/03 11:31:21 No. If !img, then setupCurrentScreenshot is yet to
Joao da Silva 2012/09/03 11:37:55 Right. The comment makes that clear now :-)
272 &result));
273 EXPECT_TRUE(result == enabled);
Joao da Silva 2012/09/03 09:01:04 EXPECT_EQ
qfel 2012/09/03 11:31:21 Done.
274
275 // Feedback page is a singleton page, so close so future calls to this
276 // function work as expected.
277 web_contents->Close();
278 }
279
280 #if defined(OS_CHROMEOS)
281 void TestScreenshotFile(bool enabled) {
282 SetScreenshotPolicy(enabled);
283 ash::Shell::GetInstance()->accelerator_controller()->PerformAction(
284 ash::TAKE_SCREENSHOT, ui::Accelerator());
Joao da Silva 2012/09/03 09:01:04 indent
qfel 2012/09/03 11:31:21 Done.
285
286 // TAKE_SCREENSHOT handler posts write file task on success, wait for it.
287 BrowserThread::PostTaskAndReply(
288 BrowserThread::IO,
289 FROM_HERE,
290 base::Bind(NoOp),
291 MessageLoop::QuitClosure());
292 content::RunMessageLoop();
293 }
294 #endif
295
178 MockConfigurationPolicyProvider provider_; 296 MockConfigurationPolicyProvider provider_;
179 }; 297 };
180 298
181 IN_PROC_BROWSER_TEST_F(PolicyTest, BookmarkBarEnabled) { 299 IN_PROC_BROWSER_TEST_F(PolicyTest, BookmarkBarEnabled) {
182 // Verifies that the bookmarks bar can be forced to always or never show up. 300 // Verifies that the bookmarks bar can be forced to always or never show up.
183 301
184 // Test starts in about:blank. 302 // Test starts in about:blank.
185 PrefService* prefs = browser()->profile()->GetPrefs(); 303 PrefService* prefs = browser()->profile()->GetPrefs();
186 EXPECT_FALSE(prefs->IsManagedPreference(prefs::kShowBookmarkBar)); 304 EXPECT_FALSE(prefs->IsManagedPreference(prefs::kShowBookmarkBar));
187 EXPECT_FALSE(prefs->GetBoolean(prefs::kShowBookmarkBar)); 305 EXPECT_FALSE(prefs->GetBoolean(prefs::kShowBookmarkBar));
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 whitelist.Append(base::Value::CreateStringValue("bbb.com/policy")); 596 whitelist.Append(base::Value::CreateStringValue("bbb.com/policy"));
479 policies.Set(key::kURLWhitelist, POLICY_LEVEL_MANDATORY, 597 policies.Set(key::kURLWhitelist, POLICY_LEVEL_MANDATORY,
480 POLICY_SCOPE_USER, whitelist.DeepCopy()); 598 POLICY_SCOPE_USER, whitelist.DeepCopy());
481 provider_.UpdateChromePolicy(policies); 599 provider_.UpdateChromePolicy(policies);
482 CheckCanOpenURL(browser(), kAAA); 600 CheckCanOpenURL(browser(), kAAA);
483 CheckURLIsBlocked(browser(), kBBB); 601 CheckURLIsBlocked(browser(), kBBB);
484 CheckCanOpenURL(browser(), kSUB_BBB); 602 CheckCanOpenURL(browser(), kSUB_BBB);
485 CheckCanOpenURL(browser(), kBBB_PATH); 603 CheckCanOpenURL(browser(), kBBB_PATH);
486 } 604 }
487 605
606 IN_PROC_BROWSER_TEST_F(PolicyTest, DisableScreenshotsFeedback) {
607 // Make sure current screenshot can be taken and displayed on feedback page.
608 TestScreenshotFeedback(true);
609
610 // Check if banning screenshots disabled feedback page's ability to grab a
611 // screenshot.
612 TestScreenshotFeedback(false);
613 }
614
615 #if defined(OS_CHROMEOS)
616 IN_PROC_BROWSER_TEST_F(PolicyTest, DisableScreenshotsFile) {
617 int screenshot_count = CountScreenshots();
618
619 // Make sure screenshots are counted correctly.
620 TestScreenshotFile(true);
621 ASSERT_EQ(CountScreenshots(), screenshot_count + 1);
622
623 // Check if trying to take a screenshot fails when disabled by policy.
624 TestScreenshotFile(false);
625 ASSERT_EQ(CountScreenshots(), screenshot_count + 1);
626 }
627 #endif
628
488 } // namespace policy 629 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698