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

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: Rebased 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/bind_helpers.h"
8 #include "base/file_path.h" 9 #include "base/file_path.h"
9 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/values.h"
10 #include "base/scoped_temp_dir.h" 12 #include "base/scoped_temp_dir.h"
11 #include "base/string16.h" 13 #include "base/string16.h"
14 #include "base/test/test_file_util.h"
12 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
13 #include "base/test/test_file_util.h"
14 #include "chrome/app/chrome_command_ids.h" 16 #include "chrome/app/chrome_command_ids.h"
15 #include "chrome/browser/autocomplete/autocomplete_controller.h" 17 #include "chrome/browser/autocomplete/autocomplete_controller.h"
16 #include "chrome/browser/browser_process.h" 18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/download/download_prefs.h"
17 #include "chrome/browser/infobars/infobar_tab_helper.h" 20 #include "chrome/browser/infobars/infobar_tab_helper.h"
18 #include "chrome/browser/net/url_request_mock_util.h" 21 #include "chrome/browser/net/url_request_mock_util.h"
19 #include "chrome/browser/policy/browser_policy_connector.h" 22 #include "chrome/browser/policy/browser_policy_connector.h"
20 #include "chrome/browser/policy/mock_configuration_policy_provider.h" 23 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
21 #include "chrome/browser/policy/policy_map.h" 24 #include "chrome/browser/policy/policy_map.h"
22 #include "chrome/browser/prefs/pref_service.h" 25 #include "chrome/browser/prefs/pref_service.h"
23 #include "chrome/browser/profiles/profile.h" 26 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/net/url_request_mock_util.h" 27 #include "chrome/browser/net/url_request_mock_util.h"
25 #include "chrome/browser/search_engines/template_url.h" 28 #include "chrome/browser/search_engines/template_url.h"
26 #include "chrome/browser/search_engines/template_url_service.h" 29 #include "chrome/browser/search_engines/template_url_service.h"
(...skipping 11 matching lines...) Expand all
38 #include "chrome/browser/ui/tab_contents/tab_contents.h" 41 #include "chrome/browser/ui/tab_contents/tab_contents.h"
39 #include "chrome/common/chrome_notification_types.h" 42 #include "chrome/common/chrome_notification_types.h"
40 #include "chrome/common/pref_names.h" 43 #include "chrome/common/pref_names.h"
41 #include "chrome/test/base/in_process_browser_test.h" 44 #include "chrome/test/base/in_process_browser_test.h"
42 #include "chrome/test/base/ui_test_utils.h" 45 #include "chrome/test/base/ui_test_utils.h"
43 #include "content/public/browser/browser_context.h" 46 #include "content/public/browser/browser_context.h"
44 #include "content/public/browser/browser_thread.h" 47 #include "content/public/browser/browser_thread.h"
45 #include "content/public/browser/download_item.h" 48 #include "content/public/browser/download_item.h"
46 #include "content/public/browser/download_manager.h" 49 #include "content/public/browser/download_manager.h"
47 #include "content/public/browser/notification_service.h" 50 #include "content/public/browser/notification_service.h"
51 #include "content/public/browser/notification_source.h"
48 #include "content/public/browser/notification_types.h" 52 #include "content/public/browser/notification_types.h"
49 #include "content/public/browser/web_contents.h" 53 #include "content/public/browser/web_contents.h"
50 #include "content/public/test/browser_test_utils.h" 54 #include "content/public/test/browser_test_utils.h"
51 #include "content/public/test/download_test_observer.h" 55 #include "content/public/test/download_test_observer.h"
52 #include "content/public/test/test_utils.h" 56 #include "content/public/test/test_utils.h"
53 #include "content/test/net/url_request_mock_http_job.h" 57 #include "content/test/net/url_request_mock_http_job.h"
54 #include "googleurl/src/gurl.h" 58 #include "googleurl/src/gurl.h"
55 #include "net/base/net_util.h" 59 #include "net/base/net_util.h"
56 #include "net/url_request/url_request.h" 60 #include "net/url_request/url_request.h"
57 #include "net/url_request/url_request_filter.h" 61 #include "net/url_request/url_request_filter.h"
58 #include "policy/policy_constants.h" 62 #include "policy/policy_constants.h"
59 #include "testing/gmock/include/gmock/gmock.h" 63 #include "testing/gmock/include/gmock/gmock.h"
60 #include "testing/gtest/include/gtest/gtest.h" 64 #include "testing/gtest/include/gtest/gtest.h"
61 65
66 #if defined(OS_CHROMEOS)
67 #include "ash/accelerators/accelerator_controller.h"
68 #include "ash/accelerators/accelerator_table.h"
69 #include "ash/shell.h"
70 #include "ash/shell_delegate.h"
71 #endif
72
62 using content::BrowserThread; 73 using content::BrowserThread;
63 using testing::Return; 74 using testing::Return;
64 75
65 namespace policy { 76 namespace policy {
66 77
67 namespace { 78 namespace {
68 79
69 const char kURL[] = "http://example.com"; 80 const char kURL[] = "http://example.com";
70 const char kCookieValue[] = "converted=true"; 81 const char kCookieValue[] = "converted=true";
71 // Assigned to Philip J. Fry to fix eventually. 82 // Assigned to Philip J. Fry to fix eventually.
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 observer.WaitForFinished(); 146 observer.WaitForFinished();
136 EXPECT_EQ( 147 EXPECT_EQ(
137 1u, observer.NumDownloadsSeenInState(content::DownloadItem::COMPLETE)); 148 1u, observer.NumDownloadsSeenInState(content::DownloadItem::COMPLETE));
138 EXPECT_TRUE(file_util::PathExists(downloaded)); 149 EXPECT_TRUE(file_util::PathExists(downloaded));
139 file_util::FileEnumerator enumerator( 150 file_util::FileEnumerator enumerator(
140 dir, false, file_util::FileEnumerator::FILES); 151 dir, false, file_util::FileEnumerator::FILES);
141 EXPECT_EQ(file, enumerator.Next().BaseName()); 152 EXPECT_EQ(file, enumerator.Next().BaseName());
142 EXPECT_EQ(FilePath(), enumerator.Next()); 153 EXPECT_EQ(FilePath(), enumerator.Next());
143 } 154 }
144 155
156 #if defined(OS_CHROMEOS)
157 int CountScreenshots() {
158 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(
159 ash::Shell::GetInstance()->delegate()->GetCurrentBrowserContext());
160 file_util::FileEnumerator enumerator(download_prefs->DownloadPath(),
161 false, file_util::FileEnumerator::FILES,
162 "Screenshot*");
163 int count = 0;
164 while (!enumerator.Next().empty())
165 count++;
166 return count;
167 }
168 #endif
169
145 } // namespace 170 } // namespace
146 171
147 class PolicyTest : public InProcessBrowserTest { 172 class PolicyTest : public InProcessBrowserTest {
148 protected: 173 protected:
149 PolicyTest() {} 174 PolicyTest() {}
150 175
151 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { 176 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
152 EXPECT_CALL(provider_, IsInitializationComplete()) 177 EXPECT_CALL(provider_, IsInitializationComplete())
153 .WillRepeatedly(Return(true)); 178 .WillRepeatedly(Return(true));
154 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); 179 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
155 } 180 }
156 181
157 virtual void SetUpOnMainThread() OVERRIDE { 182 virtual void SetUpOnMainThread() OVERRIDE {
158 BrowserThread::PostTask( 183 BrowserThread::PostTask(
159 BrowserThread::IO, FROM_HERE, 184 BrowserThread::IO, FROM_HERE,
160 base::Bind(chrome_browser_net::SetUrlRequestMocksEnabled, true)); 185 base::Bind(chrome_browser_net::SetUrlRequestMocksEnabled, true));
161 } 186 }
162 187
188 void SetScreenshotPolicy(bool enabled) {
189 PolicyMap policies;
190 policies.Set(key::kDisableScreenshots, POLICY_LEVEL_MANDATORY,
191 POLICY_SCOPE_USER, base::Value::CreateBooleanValue(!enabled));
192 provider_.UpdateChromePolicy(policies);
193 }
194
195 void TestScreenshotFeedback(bool enabled) {
196 SetScreenshotPolicy(enabled);
197
198 // Wait for feedback page to load.
199 content::WindowedNotificationObserver observer(
200 content::NOTIFICATION_LOAD_STOP,
201 content::NotificationService::AllSources());
202 EXPECT_TRUE(chrome::ExecuteCommand(browser(), IDC_FEEDBACK));
203 observer.Wait();
204 content::WebContents* web_contents =
205 static_cast<content::Source<content::NavigationController> >(
206 observer.source())->GetWebContents();
207
208 // Wait for feedback page to fully initialize.
209 // setupCurrentScreenshot is called when feedback page loads and (among
210 // other things) adds current-screenshots-thumbnailDiv-0-image element.
211 // The code below executes either before setupCurrentScreenshot was called
212 // (setupCurrentScreenshot is replaced with our hook) or after it has
213 // completed (in that case send result immediately).
214 bool result = false;
215 EXPECT_TRUE(content::ExecuteJavaScriptAndExtractBool(
216 web_contents->GetRenderViewHost(),
217 std::wstring(),
218 L"function btest_initCompleted(url) {"
219 L" var img = new Image();"
220 L" img.src = url;"
221 L" img.onload = function() {"
222 L" domAutomationController.send(img.width * img.height > 0);"
223 L" };"
224 L" img.onerror = function() {"
225 L" domAutomationController.send(false);"
226 L" };"
227 L"}"
228 L"function setupCurrentScreenshot(url) {"
229 L" btest_initCompleted(url);"
230 L"}"
231 L"var img = document.getElementById("
232 L" 'current-screenshots-thumbnailDiv-0-image');"
233 L"if (img)"
234 L" btest_initCompleted(img.src);",
235 &result));
236 EXPECT_EQ(enabled, result);
237
238 // Feedback page is a singleton page, so close so future calls to this
239 // function work as expected.
240 web_contents->Close();
241 }
242
243 #if defined(OS_CHROMEOS)
244 void TestScreenshotFile(bool enabled) {
245 SetScreenshotPolicy(enabled);
246 ash::Shell::GetInstance()->accelerator_controller()->PerformAction(
247 ash::TAKE_SCREENSHOT, ui::Accelerator());
248
249 // TAKE_SCREENSHOT handler posts write file task on success, wait for it.
250 BrowserThread::PostTaskAndReply(
251 BrowserThread::IO,
252 FROM_HERE,
253 base::Bind(base::DoNothing),
254 MessageLoop::QuitClosure());
255 content::RunMessageLoop();
256 }
257 #endif
258
163 MockConfigurationPolicyProvider provider_; 259 MockConfigurationPolicyProvider provider_;
164 }; 260 };
165 261
166 IN_PROC_BROWSER_TEST_F(PolicyTest, BookmarkBarEnabled) { 262 IN_PROC_BROWSER_TEST_F(PolicyTest, BookmarkBarEnabled) {
167 // Verifies that the bookmarks bar can be forced to always or never show up. 263 // Verifies that the bookmarks bar can be forced to always or never show up.
168 264
169 // Test starts in about:blank. 265 // Test starts in about:blank.
170 PrefService* prefs = browser()->profile()->GetPrefs(); 266 PrefService* prefs = browser()->profile()->GetPrefs();
171 EXPECT_FALSE(prefs->IsManagedPreference(prefs::kShowBookmarkBar)); 267 EXPECT_FALSE(prefs->IsManagedPreference(prefs::kShowBookmarkBar));
172 EXPECT_FALSE(prefs->GetBoolean(prefs::kShowBookmarkBar)); 268 EXPECT_FALSE(prefs->GetBoolean(prefs::kShowBookmarkBar));
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 whitelist.Append(base::Value::CreateStringValue("bbb.com/policy")); 555 whitelist.Append(base::Value::CreateStringValue("bbb.com/policy"));
460 policies.Set(key::kURLWhitelist, POLICY_LEVEL_MANDATORY, 556 policies.Set(key::kURLWhitelist, POLICY_LEVEL_MANDATORY,
461 POLICY_SCOPE_USER, whitelist.DeepCopy()); 557 POLICY_SCOPE_USER, whitelist.DeepCopy());
462 provider_.UpdateChromePolicy(policies); 558 provider_.UpdateChromePolicy(policies);
463 CheckCanOpenURL(browser(), kAAA); 559 CheckCanOpenURL(browser(), kAAA);
464 CheckURLIsBlocked(browser(), kBBB); 560 CheckURLIsBlocked(browser(), kBBB);
465 CheckCanOpenURL(browser(), kSUB_BBB); 561 CheckCanOpenURL(browser(), kSUB_BBB);
466 CheckCanOpenURL(browser(), kBBB_PATH); 562 CheckCanOpenURL(browser(), kBBB_PATH);
467 } 563 }
468 564
565 IN_PROC_BROWSER_TEST_F(PolicyTest, DisableScreenshotsFeedback) {
566 // Make sure current screenshot can be taken and displayed on feedback page.
567 TestScreenshotFeedback(true);
568
569 // Check if banning screenshots disabled feedback page's ability to grab a
570 // screenshot.
571 TestScreenshotFeedback(false);
572 }
573
574 #if defined(OS_CHROMEOS)
575 IN_PROC_BROWSER_TEST_F(PolicyTest, DisableScreenshotsFile) {
576 int screenshot_count = CountScreenshots();
577
578 // Make sure screenshots are counted correctly.
579 TestScreenshotFile(true);
580 ASSERT_EQ(CountScreenshots(), screenshot_count + 1);
581
582 // Check if trying to take a screenshot fails when disabled by policy.
583 TestScreenshotFile(false);
584 ASSERT_EQ(CountScreenshots(), screenshot_count + 1);
585 }
586 #endif
587
469 } // namespace policy 588 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698