Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / safe_browsing / safe_browsing_blocking_page_unittest.cc
blobda5a3cade179a00d3e31c3dac0fa6394d91d84cf
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
3 // found in the LICENSE file.
5 #include <list>
7 #include "base/prefs/pref_service.h"
8 #include "base/run_loop.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/safe_browsing/malware_details.h"
11 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h"
12 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
13 #include "chrome/browser/safe_browsing/ui_manager.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
16 #include "content/public/browser/interstitial_page.h"
17 #include "content/public/browser/navigation_entry.h"
18 #include "content/public/browser/render_process_host.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/test/web_contents_tester.h"
22 using content::InterstitialPage;
23 using content::NavigationEntry;
24 using content::WebContents;
25 using content::WebContentsTester;
27 static const char* kGoogleURL = "http://www.google.com/";
28 static const char* kGoodURL = "http://www.goodguys.com/";
29 static const char* kBadURL = "http://www.badguys.com/";
30 static const char* kBadURL2 = "http://www.badguys2.com/";
31 static const char* kBadURL3 = "http://www.badguys3.com/";
33 namespace {
35 // A SafeBrowingBlockingPage class that does not create windows.
36 class TestSafeBrowsingBlockingPageV1 : public SafeBrowsingBlockingPageV1 {
37 public:
38 TestSafeBrowsingBlockingPageV1(SafeBrowsingUIManager* manager,
39 WebContents* web_contents,
40 const UnsafeResourceList& unsafe_resources)
41 : SafeBrowsingBlockingPageV1(manager, web_contents, unsafe_resources) {
42 // Don't delay details at all for the unittest.
43 malware_details_proceed_delay_ms_ = 0;
45 // Don't create a view.
46 interstitial_page()->DontCreateViewForTesting();
50 // A SafeBrowingBlockingPage class that does not create windows.
51 class TestSafeBrowsingBlockingPageV2 : public SafeBrowsingBlockingPageV2 {
52 public:
53 TestSafeBrowsingBlockingPageV2(SafeBrowsingUIManager* manager,
54 WebContents* web_contents,
55 const UnsafeResourceList& unsafe_resources)
56 : SafeBrowsingBlockingPageV2(manager, web_contents, unsafe_resources) {
57 // Don't delay details at all for the unittest.
58 malware_details_proceed_delay_ms_ = 0;
60 // Don't create a view.
61 interstitial_page()->DontCreateViewForTesting();
65 class TestSafeBrowsingUIManager: public SafeBrowsingUIManager {
66 public:
67 explicit TestSafeBrowsingUIManager(SafeBrowsingService* service)
68 : SafeBrowsingUIManager(service) {
71 virtual void SendSerializedMalwareDetails(
72 const std::string& serialized) OVERRIDE {
73 details_.push_back(serialized);
76 std::list<std::string>* GetDetails() {
77 return &details_;
80 private:
81 virtual ~TestSafeBrowsingUIManager() {}
83 std::list<std::string> details_;
86 class TestSafeBrowsingBlockingPageFactory
87 : public SafeBrowsingBlockingPageFactory {
88 public:
89 TestSafeBrowsingBlockingPageFactory() { }
90 virtual ~TestSafeBrowsingBlockingPageFactory() { }
92 virtual SafeBrowsingBlockingPage* CreateSafeBrowsingPage(
93 SafeBrowsingUIManager* manager,
94 WebContents* web_contents,
95 const SafeBrowsingBlockingPage::UnsafeResourceList& unsafe_resources)
96 OVERRIDE {
97 // TODO(mattm): remove this when SafeBrowsingBlockingPageV2 supports
98 // multi-threat warnings.
99 if (unsafe_resources.size() == 1 &&
100 (unsafe_resources[0].threat_type == SB_THREAT_TYPE_URL_MALWARE ||
101 unsafe_resources[0].threat_type == SB_THREAT_TYPE_URL_PHISHING)) {
102 return new TestSafeBrowsingBlockingPageV2(manager, web_contents,
103 unsafe_resources);
105 return new TestSafeBrowsingBlockingPageV1(manager, web_contents,
106 unsafe_resources);
110 } // namespace
112 class SafeBrowsingBlockingPageTest : public ChromeRenderViewHostTestHarness {
113 public:
114 // The decision the user made.
115 enum UserResponse {
116 PENDING,
118 CANCEL
121 SafeBrowsingBlockingPageTest() {
122 ResetUserResponse();
123 // The safe browsing UI manager does not need a service for this test.
124 ui_manager_ = new TestSafeBrowsingUIManager(NULL);
127 virtual void SetUp() OVERRIDE {
128 ChromeRenderViewHostTestHarness::SetUp();
129 SafeBrowsingBlockingPage::RegisterFactory(&factory_);
130 ResetUserResponse();
133 virtual void TearDown() OVERRIDE {
134 // Release the UI manager before the BrowserThreads are destroyed.
135 ui_manager_ = NULL;
136 SafeBrowsingBlockingPage::RegisterFactory(NULL);
137 // Clean up singleton reference (crbug.com/110594).
138 MalwareDetails::RegisterFactory(NULL);
139 ChromeRenderViewHostTestHarness::TearDown();
142 void OnBlockingPageComplete(bool proceed) {
143 if (proceed)
144 user_response_ = OK;
145 else
146 user_response_ = CANCEL;
149 void Navigate(const char* url, int page_id) {
150 WebContentsTester::For(web_contents())->TestDidNavigate(
151 web_contents()->GetRenderViewHost(), page_id, GURL(url),
152 content::PAGE_TRANSITION_TYPED);
155 void GoBack(bool is_cross_site) {
156 NavigationEntry* entry =
157 web_contents()->GetController().GetEntryAtOffset(-1);
158 ASSERT_TRUE(entry);
159 web_contents()->GetController().GoBack();
161 // The pending RVH should commit for cross-site navigations.
162 content::RenderViewHost* rvh = is_cross_site ?
163 WebContentsTester::For(web_contents())->GetPendingRenderViewHost() :
164 web_contents()->GetRenderViewHost();
165 WebContentsTester::For(web_contents())->TestDidNavigate(
166 rvh,
167 entry->GetPageID(),
168 GURL(entry->GetURL()),
169 content::PAGE_TRANSITION_TYPED);
172 void ShowInterstitial(bool is_subresource, const char* url) {
173 SafeBrowsingUIManager::UnsafeResource resource;
174 InitResource(&resource, is_subresource, GURL(url));
175 SafeBrowsingBlockingPage::ShowBlockingPage(ui_manager_.get(), resource);
178 // Returns the SafeBrowsingBlockingPage currently showing or NULL if none is
179 // showing.
180 SafeBrowsingBlockingPage* GetSafeBrowsingBlockingPage() {
181 InterstitialPage* interstitial =
182 InterstitialPage::GetInterstitialPage(web_contents());
183 if (!interstitial)
184 return NULL;
185 return static_cast<SafeBrowsingBlockingPage*>(
186 interstitial->GetDelegateForTesting());
189 UserResponse user_response() const { return user_response_; }
190 void ResetUserResponse() { user_response_ = PENDING; }
192 static void ProceedThroughInterstitial(
193 SafeBrowsingBlockingPage* sb_interstitial) {
194 sb_interstitial->interstitial_page_->Proceed();
195 // Proceed() posts a task to update the SafeBrowsingService::Client.
196 base::RunLoop().RunUntilIdle();
199 static void DontProceedThroughInterstitial(
200 SafeBrowsingBlockingPage* sb_interstitial) {
201 sb_interstitial->interstitial_page_->DontProceed();
202 // DontProceed() posts a task to update the SafeBrowsingService::Client.
203 base::RunLoop().RunUntilIdle();
206 void DontProceedThroughSubresourceInterstitial(
207 SafeBrowsingBlockingPage* sb_interstitial) {
208 // CommandReceived(kTakeMeBackCommand) does a back navigation for
209 // subresource interstitials.
210 GoBack(false);
211 // DontProceed() posts a task to update the SafeBrowsingService::Client.
212 base::RunLoop().RunUntilIdle();
215 scoped_refptr<TestSafeBrowsingUIManager> ui_manager_;
217 private:
218 void InitResource(SafeBrowsingUIManager::UnsafeResource* resource,
219 bool is_subresource,
220 const GURL& url) {
221 resource->callback =
222 base::Bind(&SafeBrowsingBlockingPageTest::OnBlockingPageComplete,
223 base::Unretained(this));
224 resource->url = url;
225 resource->is_subresource = is_subresource;
226 resource->threat_type = SB_THREAT_TYPE_URL_MALWARE;
227 resource->render_process_host_id =
228 web_contents()->GetRenderProcessHost()->GetID();
229 resource->render_view_id =
230 web_contents()->GetRenderViewHost()->GetRoutingID();
233 UserResponse user_response_;
234 TestSafeBrowsingBlockingPageFactory factory_;
237 // Tests showing a blocking page for a malware page and not proceeding.
238 TEST_F(SafeBrowsingBlockingPageTest, MalwarePageDontProceed) {
239 // Enable malware details.
240 Profile* profile = Profile::FromBrowserContext(
241 web_contents()->GetBrowserContext());
242 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
244 // Start a load.
245 controller().LoadURL(GURL(kBadURL), content::Referrer(),
246 content::PAGE_TRANSITION_TYPED, std::string());
249 // Simulate the load causing a safe browsing interstitial to be shown.
250 ShowInterstitial(false, kBadURL);
251 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
252 ASSERT_TRUE(sb_interstitial);
254 base::RunLoop().RunUntilIdle();
256 // Simulate the user clicking "don't proceed".
257 DontProceedThroughInterstitial(sb_interstitial);
259 // The interstitial should be gone.
260 EXPECT_EQ(CANCEL, user_response());
261 EXPECT_FALSE(GetSafeBrowsingBlockingPage());
263 // We did not proceed, the pending entry should be gone.
264 EXPECT_FALSE(controller().GetPendingEntry());
266 // A report should have been sent.
267 EXPECT_EQ(1u, ui_manager_->GetDetails()->size());
268 ui_manager_->GetDetails()->clear();
271 // Tests showing a blocking page for a malware page and then proceeding.
272 TEST_F(SafeBrowsingBlockingPageTest, MalwarePageProceed) {
273 // Enable malware reports.
274 Profile* profile = Profile::FromBrowserContext(
275 web_contents()->GetBrowserContext());
276 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
278 // Start a load.
279 controller().LoadURL(GURL(kBadURL), content::Referrer(),
280 content::PAGE_TRANSITION_TYPED, std::string());
282 // Simulate the load causing a safe browsing interstitial to be shown.
283 ShowInterstitial(false, kBadURL);
284 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
285 ASSERT_TRUE(sb_interstitial);
287 // Simulate the user clicking "proceed".
288 ProceedThroughInterstitial(sb_interstitial);
290 // The interstitial is shown until the navigation commits.
291 ASSERT_TRUE(InterstitialPage::GetInterstitialPage(web_contents()));
292 // Commit the navigation.
293 Navigate(kBadURL, 1);
294 // The interstitial should be gone now.
295 ASSERT_FALSE(InterstitialPage::GetInterstitialPage(web_contents()));
297 // A report should have been sent.
298 EXPECT_EQ(1u, ui_manager_->GetDetails()->size());
299 ui_manager_->GetDetails()->clear();
302 // Tests showing a blocking page for a page that contains malware subresources
303 // and not proceeding.
304 TEST_F(SafeBrowsingBlockingPageTest, PageWithMalwareResourceDontProceed) {
305 // Enable malware reports.
306 Profile* profile = Profile::FromBrowserContext(
307 web_contents()->GetBrowserContext());
308 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
310 // Navigate somewhere.
311 Navigate(kGoogleURL, 1);
313 // Navigate somewhere else.
314 Navigate(kGoodURL, 2);
316 // Simulate that page loading a bad-resource triggering an interstitial.
317 ShowInterstitial(true, kBadURL);
319 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
320 ASSERT_TRUE(sb_interstitial);
322 // Simulate the user clicking "don't proceed".
323 DontProceedThroughSubresourceInterstitial(sb_interstitial);
324 EXPECT_EQ(CANCEL, user_response());
325 EXPECT_FALSE(GetSafeBrowsingBlockingPage());
327 // We did not proceed, we should be back to the first page, the 2nd one should
328 // have been removed from the navigation controller.
329 ASSERT_EQ(1, controller().GetEntryCount());
330 EXPECT_EQ(kGoogleURL, controller().GetActiveEntry()->GetURL().spec());
332 // A report should have been sent.
333 EXPECT_EQ(1u, ui_manager_->GetDetails()->size());
334 ui_manager_->GetDetails()->clear();
337 // Tests showing a blocking page for a page that contains malware subresources
338 // and proceeding.
339 TEST_F(SafeBrowsingBlockingPageTest, PageWithMalwareResourceProceed) {
340 // Enable malware reports.
341 Profile* profile = Profile::FromBrowserContext(
342 web_contents()->GetBrowserContext());
343 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
345 // Navigate somewhere.
346 Navigate(kGoodURL, 1);
348 // Simulate that page loading a bad-resource triggering an interstitial.
349 ShowInterstitial(true, kBadURL);
351 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
352 ASSERT_TRUE(sb_interstitial);
354 // Simulate the user clicking "proceed".
355 ProceedThroughInterstitial(sb_interstitial);
356 EXPECT_EQ(OK, user_response());
357 EXPECT_FALSE(GetSafeBrowsingBlockingPage());
359 // We did proceed, we should be back to showing the page.
360 ASSERT_EQ(1, controller().GetEntryCount());
361 EXPECT_EQ(kGoodURL, controller().GetActiveEntry()->GetURL().spec());
363 // A report should have been sent.
364 EXPECT_EQ(1u, ui_manager_->GetDetails()->size());
365 ui_manager_->GetDetails()->clear();
368 // Tests showing a blocking page for a page that contains multiple malware
369 // subresources and not proceeding. This just tests that the extra malware
370 // subresources (which trigger queued interstitial pages) do not break anything.
371 TEST_F(SafeBrowsingBlockingPageTest,
372 PageWithMultipleMalwareResourceDontProceed) {
373 // Enable malware reports.
374 Profile* profile = Profile::FromBrowserContext(
375 web_contents()->GetBrowserContext());
376 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
378 // Navigate somewhere.
379 Navigate(kGoogleURL, 1);
381 // Navigate somewhere else.
382 Navigate(kGoodURL, 2);
384 // Simulate that page loading a bad-resource triggering an interstitial.
385 ShowInterstitial(true, kBadURL);
387 // More bad resources loading causing more interstitials. The new
388 // interstitials should be queued.
389 ShowInterstitial(true, kBadURL2);
390 ShowInterstitial(true, kBadURL3);
392 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
393 ASSERT_TRUE(sb_interstitial);
395 // Simulate the user clicking "don't proceed".
396 DontProceedThroughSubresourceInterstitial(sb_interstitial);
397 EXPECT_EQ(CANCEL, user_response());
398 EXPECT_FALSE(GetSafeBrowsingBlockingPage());
400 // We did not proceed, we should be back to the first page, the 2nd one should
401 // have been removed from the navigation controller.
402 ASSERT_EQ(1, controller().GetEntryCount());
403 EXPECT_EQ(kGoogleURL, controller().GetActiveEntry()->GetURL().spec());
405 // A report should have been sent.
406 EXPECT_EQ(1u, ui_manager_->GetDetails()->size());
407 ui_manager_->GetDetails()->clear();
410 // Tests showing a blocking page for a page that contains multiple malware
411 // subresources and proceeding through the first interstitial, but not the next.
412 TEST_F(SafeBrowsingBlockingPageTest,
413 PageWithMultipleMalwareResourceProceedThenDontProceed) {
414 // Enable malware reports.
415 Profile* profile = Profile::FromBrowserContext(
416 web_contents()->GetBrowserContext());
417 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
419 // Navigate somewhere.
420 Navigate(kGoogleURL, 1);
422 // Navigate somewhere else.
423 Navigate(kGoodURL, 2);
425 // Simulate that page loading a bad-resource triggering an interstitial.
426 ShowInterstitial(true, kBadURL);
428 // More bad resources loading causing more interstitials. The new
429 // interstitials should be queued.
430 ShowInterstitial(true, kBadURL2);
431 ShowInterstitial(true, kBadURL3);
433 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
434 ASSERT_TRUE(sb_interstitial);
436 // Proceed through the 1st interstitial.
437 ProceedThroughInterstitial(sb_interstitial);
438 EXPECT_EQ(OK, user_response());
440 // A report should have been sent.
441 EXPECT_EQ(1u, ui_manager_->GetDetails()->size());
442 ui_manager_->GetDetails()->clear();
444 ResetUserResponse();
446 // We should land to a 2nd interstitial (aggregating all the malware resources
447 // loaded while the 1st interstitial was showing).
448 sb_interstitial = GetSafeBrowsingBlockingPage();
449 ASSERT_TRUE(sb_interstitial);
451 // Don't proceed through the 2nd interstitial.
452 DontProceedThroughSubresourceInterstitial(sb_interstitial);
453 EXPECT_EQ(CANCEL, user_response());
454 EXPECT_FALSE(GetSafeBrowsingBlockingPage());
456 // We did not proceed, we should be back to the first page, the 2nd one should
457 // have been removed from the navigation controller.
458 ASSERT_EQ(1, controller().GetEntryCount());
459 EXPECT_EQ(kGoogleURL, controller().GetActiveEntry()->GetURL().spec());
461 // No report should have been sent -- we don't create a report the
462 // second time.
463 EXPECT_EQ(0u, ui_manager_->GetDetails()->size());
464 ui_manager_->GetDetails()->clear();
467 // Tests showing a blocking page for a page that contains multiple malware
468 // subresources and proceeding through the multiple interstitials.
469 TEST_F(SafeBrowsingBlockingPageTest, PageWithMultipleMalwareResourceProceed) {
470 // Enable malware reports.
471 Profile* profile = Profile::FromBrowserContext(
472 web_contents()->GetBrowserContext());
473 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
475 // Navigate somewhere else.
476 Navigate(kGoodURL, 1);
478 // Simulate that page loading a bad-resource triggering an interstitial.
479 ShowInterstitial(true, kBadURL);
481 // More bad resources loading causing more interstitials. The new
482 // interstitials should be queued.
483 ShowInterstitial(true, kBadURL2);
484 ShowInterstitial(true, kBadURL3);
486 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
487 ASSERT_TRUE(sb_interstitial);
489 // Proceed through the 1st interstitial.
490 ProceedThroughInterstitial(sb_interstitial);
491 EXPECT_EQ(OK, user_response());
493 // A report should have been sent.
494 EXPECT_EQ(1u, ui_manager_->GetDetails()->size());
495 ui_manager_->GetDetails()->clear();
497 ResetUserResponse();
499 // We should land to a 2nd interstitial (aggregating all the malware resources
500 // loaded while the 1st interstitial was showing).
501 sb_interstitial = GetSafeBrowsingBlockingPage();
502 ASSERT_TRUE(sb_interstitial);
504 // Proceed through the 2nd interstitial.
505 ProceedThroughInterstitial(sb_interstitial);
506 EXPECT_EQ(OK, user_response());
508 // We did proceed, we should be back to the initial page.
509 ASSERT_EQ(1, controller().GetEntryCount());
510 EXPECT_EQ(kGoodURL, controller().GetActiveEntry()->GetURL().spec());
512 // No report should have been sent -- we don't create a report the
513 // second time.
514 EXPECT_EQ(0u, ui_manager_->GetDetails()->size());
515 ui_manager_->GetDetails()->clear();
518 // Tests showing a blocking page then navigating back and forth to make sure the
519 // controller entries are OK. http://crbug.com/17627
520 TEST_F(SafeBrowsingBlockingPageTest, NavigatingBackAndForth) {
521 // Enable malware reports.
522 Profile* profile = Profile::FromBrowserContext(
523 web_contents()->GetBrowserContext());
524 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
526 // Navigate somewhere.
527 Navigate(kGoodURL, 1);
529 // Now navigate to a bad page triggerring an interstitial.
530 controller().LoadURL(GURL(kBadURL), content::Referrer(),
531 content::PAGE_TRANSITION_TYPED, std::string());
532 ShowInterstitial(false, kBadURL);
533 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
534 ASSERT_TRUE(sb_interstitial);
536 // Proceed, then navigate back.
537 ProceedThroughInterstitial(sb_interstitial);
538 Navigate(kBadURL, 2); // Commit the navigation.
539 GoBack(true);
541 // We are back on the good page.
542 sb_interstitial = GetSafeBrowsingBlockingPage();
543 ASSERT_FALSE(sb_interstitial);
544 ASSERT_EQ(2, controller().GetEntryCount());
545 EXPECT_EQ(kGoodURL, controller().GetActiveEntry()->GetURL().spec());
547 // Navigate forward to the malware URL.
548 web_contents()->GetController().GoForward();
549 ShowInterstitial(false, kBadURL);
550 sb_interstitial = GetSafeBrowsingBlockingPage();
551 ASSERT_TRUE(sb_interstitial);
553 // Let's proceed and make sure everything is OK (bug 17627).
554 ProceedThroughInterstitial(sb_interstitial);
555 Navigate(kBadURL, 2); // Commit the navigation.
556 sb_interstitial = GetSafeBrowsingBlockingPage();
557 ASSERT_FALSE(sb_interstitial);
558 ASSERT_EQ(2, controller().GetEntryCount());
559 EXPECT_EQ(kBadURL, controller().GetActiveEntry()->GetURL().spec());
561 // Two reports should have been sent.
562 EXPECT_EQ(2u, ui_manager_->GetDetails()->size());
563 ui_manager_->GetDetails()->clear();
566 // Tests that calling "don't proceed" after "proceed" has been called doesn't
567 // cause problems. http://crbug.com/30079
568 TEST_F(SafeBrowsingBlockingPageTest, ProceedThenDontProceed) {
569 // Enable malware reports.
570 Profile* profile = Profile::FromBrowserContext(
571 web_contents()->GetBrowserContext());
572 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, true);
574 // Start a load.
575 controller().LoadURL(GURL(kBadURL), content::Referrer(),
576 content::PAGE_TRANSITION_TYPED, std::string());
578 // Simulate the load causing a safe browsing interstitial to be shown.
579 ShowInterstitial(false, kBadURL);
580 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
581 ASSERT_TRUE(sb_interstitial);
583 base::RunLoop().RunUntilIdle();
585 // Simulate the user clicking "proceed" then "don't proceed" (before the
586 // interstitial is shown).
587 sb_interstitial->interstitial_page_->Proceed();
588 sb_interstitial->interstitial_page_->DontProceed();
589 // Proceed() and DontProceed() post a task to update the
590 // SafeBrowsingService::Client.
591 base::RunLoop().RunUntilIdle();
593 // The interstitial should be gone.
594 EXPECT_EQ(OK, user_response());
595 EXPECT_FALSE(GetSafeBrowsingBlockingPage());
597 // Only one report should have been sent.
598 EXPECT_EQ(1u, ui_manager_->GetDetails()->size());
599 ui_manager_->GetDetails()->clear();
602 // Tests showing a blocking page for a malware page with reports disabled.
603 TEST_F(SafeBrowsingBlockingPageTest, MalwareReportsDisabled) {
604 // Disable malware reports.
605 Profile* profile = Profile::FromBrowserContext(
606 web_contents()->GetBrowserContext());
607 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, false);
609 // Start a load.
610 controller().LoadURL(GURL(kBadURL), content::Referrer(),
611 content::PAGE_TRANSITION_TYPED, std::string());
613 // Simulate the load causing a safe browsing interstitial to be shown.
614 ShowInterstitial(false, kBadURL);
615 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
616 ASSERT_TRUE(sb_interstitial);
618 base::RunLoop().RunUntilIdle();
620 // Simulate the user clicking "don't proceed".
621 DontProceedThroughInterstitial(sb_interstitial);
623 // The interstitial should be gone.
624 EXPECT_EQ(CANCEL, user_response());
625 EXPECT_FALSE(GetSafeBrowsingBlockingPage());
627 // We did not proceed, the pending entry should be gone.
628 EXPECT_FALSE(controller().GetPendingEntry());
630 // No report should have been sent.
631 EXPECT_EQ(0u, ui_manager_->GetDetails()->size());
632 ui_manager_->GetDetails()->clear();
635 // Test setting the malware report preferance
636 TEST_F(SafeBrowsingBlockingPageTest, MalwareReports) {
637 // Disable malware reports.
638 Profile* profile = Profile::FromBrowserContext(
639 web_contents()->GetBrowserContext());
640 profile->GetPrefs()->SetBoolean(prefs::kSafeBrowsingReportingEnabled, false);
642 // Start a load.
643 controller().LoadURL(GURL(kBadURL), content::Referrer(),
644 content::PAGE_TRANSITION_TYPED, std::string());
646 // Simulate the load causing a safe browsing interstitial to be shown.
647 ShowInterstitial(false, kBadURL);
648 SafeBrowsingBlockingPage* sb_interstitial = GetSafeBrowsingBlockingPage();
649 ASSERT_TRUE(sb_interstitial);
651 base::RunLoop().RunUntilIdle();
653 EXPECT_FALSE(profile->GetPrefs()->GetBoolean(
654 prefs::kSafeBrowsingReportingEnabled));
656 // Simulate the user check the report agreement checkbox.
657 sb_interstitial->SetReportingPreference(true);
659 EXPECT_TRUE(profile->GetPrefs()->GetBoolean(
660 prefs::kSafeBrowsingReportingEnabled));
662 // Simulate the user uncheck the report agreement checkbox.
663 sb_interstitial->SetReportingPreference(false);
665 EXPECT_FALSE(profile->GetPrefs()->GetBoolean(
666 prefs::kSafeBrowsingReportingEnabled));