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

Side by Side Diff: chrome/test/base/in_process_browser_test.cc

Issue 2013573007: Catch CSP violations in InProcessBrowserTest Base URL: https://chromium.googlesource.com/a/chromium/src.git@master
Patch Set: double check test is run Created 4 years, 4 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 | « chrome/test/base/in_process_browser_test.h ('k') | no next file » | 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 "chrome/test/base/in_process_browser_test.h" 5 #include "chrome/test/base/in_process_browser_test.h"
6 6
7 #include "ash/common/ash_switches.h" 7 #include "ash/common/ash_switches.h"
8 #include "base/auto_reset.h" 8 #include "base/auto_reset.h"
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 "var error = '';" 125 "var error = '';"
126 "for (var i = 0; i < result.length; ++i) {" 126 "for (var i = 0; i < result.length; ++i) {"
127 " if (result[i].result == axs.constants.AuditResult.FAIL) {" 127 " if (result[i].result == axs.constants.AuditResult.FAIL) {"
128 " error = axs.Audit.createReport(result);" 128 " error = axs.Audit.createReport(result);"
129 " break;" 129 " break;"
130 " }" 130 " }"
131 "}" 131 "}"
132 "domAutomationController.send(error);"; 132 "domAutomationController.send(error);";
133 133
134 InProcessBrowserTest::InProcessBrowserTest() 134 InProcessBrowserTest::InProcessBrowserTest()
135 : browser_(NULL), 135 : browser_(nullptr),
136 exit_when_last_browser_closes_(true), 136 exit_when_last_browser_closes_(true),
137 open_about_blank_on_browser_launch_(true), 137 open_about_blank_on_browser_launch_(true),
138 expect_csp_messages_(false),
139 console_delegate_(nullptr),
138 run_accessibility_checks_for_test_case_(false) 140 run_accessibility_checks_for_test_case_(false)
139 #if defined(OS_MACOSX) 141 #if defined(OS_MACOSX)
140 , autorelease_pool_(NULL) 142 , autorelease_pool_(nullptr)
141 #endif // OS_MACOSX 143 #endif // OS_MACOSX
142 { 144 {
143 #if defined(OS_MACOSX) 145 #if defined(OS_MACOSX)
144 // TODO(phajdan.jr): Make browser_tests self-contained on Mac, remove this. 146 // TODO(phajdan.jr): Make browser_tests self-contained on Mac, remove this.
145 // Before we run the browser, we have to hack the path to the exe to match 147 // Before we run the browser, we have to hack the path to the exe to match
146 // what it would be if Chrome was running, because it is used to fork renderer 148 // what it would be if Chrome was running, because it is used to fork renderer
147 // processes, on Linux at least (failure to do so will cause a browser_test to 149 // processes, on Linux at least (failure to do so will cause a browser_test to
148 // be run instead of a renderer). 150 // be run instead of a renderer).
149 base::FilePath chrome_path; 151 base::FilePath chrome_path;
150 CHECK(PathService::Get(base::FILE_EXE, &chrome_path)); 152 CHECK(PathService::Get(base::FILE_EXE, &chrome_path));
(...skipping 15 matching lines...) Expand all
166 #if defined(OS_MACOSX) 168 #if defined(OS_MACOSX)
167 bundle_swizzler_.reset(new ScopedBundleSwizzlerMac); 169 bundle_swizzler_.reset(new ScopedBundleSwizzlerMac);
168 #endif 170 #endif
169 171
170 #if defined(USE_ASH) 172 #if defined(USE_ASH)
171 DefaultAshEventGeneratorDelegate::GetInstance(); 173 DefaultAshEventGeneratorDelegate::GetInstance();
172 #endif 174 #endif
173 } 175 }
174 176
175 InProcessBrowserTest::~InProcessBrowserTest() { 177 InProcessBrowserTest::~InProcessBrowserTest() {
178 // Make sure console_delegate_ is active in a function that's guaranteed
179 // to be called. RunTestOnMainThreadLoop() could be overriden.
180 CHECK(console_delegate_);
181 delete console_delegate_;
176 } 182 }
177 183
178 void InProcessBrowserTest::SetUp() { 184 void InProcessBrowserTest::SetUp() {
179 // Browser tests will create their own g_browser_process later. 185 // Browser tests will create their own g_browser_process later.
180 DCHECK(!g_browser_process); 186 DCHECK(!g_browser_process);
181 187
182 // Clear the FeatureList instance from base/test/test_suite.cc. Since this is 188 // Clear the FeatureList instance from base/test/test_suite.cc. Since this is
183 // a browser test, a FeatureList will be registered as part of normal browser 189 // a browser test, a FeatureList will be registered as part of normal browser
184 // start up in ChromeBrowserMainParts::SetupMetricsAndFieldTrials(). 190 // start up in ChromeBrowserMainParts::SetupMetricsAndFieldTrials().
185 base::FeatureList::ClearInstanceForTesting(); 191 base::FeatureList::ClearInstanceForTesting();
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 int index, 402 int index,
397 const GURL& url, 403 const GURL& url,
398 ui::PageTransition transition) { 404 ui::PageTransition transition) {
399 AddTabAtIndexToBrowser(browser(), index, url, transition, true); 405 AddTabAtIndexToBrowser(browser(), index, url, transition, true);
400 } 406 }
401 407
402 bool InProcessBrowserTest::SetUpUserDataDirectory() { 408 bool InProcessBrowserTest::SetUpUserDataDirectory() {
403 return true; 409 return true;
404 } 410 }
405 411
412 void InProcessBrowserTest::SetExpectCSPErrorMessages(bool expect) {
413 expect_csp_messages_ = expect;
414 }
415
406 #if !defined(OS_MACOSX) 416 #if !defined(OS_MACOSX)
407 void InProcessBrowserTest::OpenDevToolsWindow( 417 void InProcessBrowserTest::OpenDevToolsWindow(
408 content::WebContents* web_contents) { 418 content::WebContents* web_contents) {
409 ASSERT_FALSE(content::DevToolsAgentHost::HasFor(web_contents)); 419 ASSERT_FALSE(content::DevToolsAgentHost::HasFor(web_contents));
410 DevToolsWindow::OpenDevToolsWindow(web_contents); 420 DevToolsWindow::OpenDevToolsWindow(web_contents);
411 ASSERT_TRUE(content::DevToolsAgentHost::HasFor(web_contents)); 421 ASSERT_TRUE(content::DevToolsAgentHost::HasFor(web_contents));
412 } 422 }
413 423
414 Browser* InProcessBrowserTest::OpenURLOffTheRecord(Profile* profile, 424 Browser* InProcessBrowserTest::OpenURLOffTheRecord(Profile* profile,
415 const GURL& url) { 425 const GURL& url) {
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 538
529 // Pump any pending events that were created as a result of creating a 539 // Pump any pending events that were created as a result of creating a
530 // browser. 540 // browser.
531 content::RunAllPendingInMessageLoop(); 541 content::RunAllPendingInMessageLoop();
532 542
533 // run_accessibility_checks_for_test_case_ must be set before calling 543 // run_accessibility_checks_for_test_case_ must be set before calling
534 // SetUpOnMainThread or RunTestOnMainThread so that one or all tests can 544 // SetUpOnMainThread or RunTestOnMainThread so that one or all tests can
535 // enable/disable the accessibility audit. 545 // enable/disable the accessibility audit.
536 run_accessibility_checks_for_test_case_ = false; 546 run_accessibility_checks_for_test_case_ = false;
537 547
548 // Set up console message listener to look for CSP error messages.
549 content::WebContents* web_contents =
550 browser()->tab_strip_model()->GetActiveWebContents();
551 console_delegate_ = new content::ConsoleObserverDelegate(
552 web_contents,
553 "*Content Security Policy*");
554 web_contents->SetDelegate(console_delegate_);
555
538 SetUpOnMainThread(); 556 SetUpOnMainThread();
539 #if defined(OS_MACOSX) 557 #if defined(OS_MACOSX)
540 autorelease_pool_->Recycle(); 558 autorelease_pool_->Recycle();
541 #endif 559 #endif
542 560
543 if (!HasFatalFailure()) 561 if (!HasFatalFailure())
544 RunTestOnMainThread(); 562 RunTestOnMainThread();
545 #if defined(OS_MACOSX) 563 #if defined(OS_MACOSX)
546 autorelease_pool_->Recycle(); 564 autorelease_pool_->Recycle();
547 #endif 565 #endif
548 566
549 if (run_accessibility_checks_for_test_case_) { 567 if (run_accessibility_checks_for_test_case_) {
550 std::string error_message; 568 std::string error_message;
551 EXPECT_TRUE(RunAccessibilityChecks(&error_message)); 569 EXPECT_TRUE(RunAccessibilityChecks(&error_message));
552 EXPECT_EQ("", error_message); 570 EXPECT_EQ("", error_message);
553 } 571 }
554 572
555 // Invoke cleanup and quit even if there are failures. This is similar to 573 // Invoke cleanup and quit even if there are failures. This is similar to
556 // gtest in that it invokes TearDown even if Setup fails. 574 // gtest in that it invokes TearDown even if Setup fails.
557 TearDownOnMainThread(); 575 TearDownOnMainThread();
558 #if defined(OS_MACOSX) 576 #if defined(OS_MACOSX)
559 autorelease_pool_->Recycle(); 577 autorelease_pool_->Recycle();
560 #endif 578 #endif
561 579
580 // Check CSP error messages.
581 if (expect_csp_messages_) {
582 EXPECT_NE("", console_delegate_->message());
583 } else {
584 EXPECT_EQ("", console_delegate_->message());
585 }
586
562 // Sometimes tests leave Quit tasks in the MessageLoop (for shame), so let's 587 // Sometimes tests leave Quit tasks in the MessageLoop (for shame), so let's
563 // run all pending messages here to avoid preempting the QuitBrowsers tasks. 588 // run all pending messages here to avoid preempting the QuitBrowsers tasks.
564 // TODO(jbates) Once crbug.com/134753 is fixed, this can be removed because it 589 // TODO(jbates) Once crbug.com/134753 is fixed, this can be removed because it
565 // will not be possible to post Quit tasks. 590 // will not be possible to post Quit tasks.
566 content::RunAllPendingInMessageLoop(); 591 content::RunAllPendingInMessageLoop();
567 592
568 QuitBrowsers(); 593 QuitBrowsers();
569 // BrowserList should be empty at this point. 594 // BrowserList should be empty at this point.
570 CHECK(BrowserList::GetInstance()->empty()); 595 CHECK(BrowserList::GetInstance()->empty());
571 } 596 }
(...skipping 16 matching lines...) Expand all
588 // their tab contents. The last tab contents being removed triggers closing of 613 // their tab contents. The last tab contents being removed triggers closing of
589 // the browser window. 614 // the browser window.
590 // 615 //
591 // On the Mac, this eventually reaches 616 // On the Mac, this eventually reaches
592 // -[BrowserWindowController windowWillClose:], which will post a deferred 617 // -[BrowserWindowController windowWillClose:], which will post a deferred
593 // -autorelease on itself to ultimately destroy the Browser object. The line 618 // -autorelease on itself to ultimately destroy the Browser object. The line
594 // below is necessary to pump these pending messages to ensure all Browsers 619 // below is necessary to pump these pending messages to ensure all Browsers
595 // get deleted. 620 // get deleted.
596 content::RunAllPendingInMessageLoop(); 621 content::RunAllPendingInMessageLoop();
597 delete autorelease_pool_; 622 delete autorelease_pool_;
598 autorelease_pool_ = NULL; 623 autorelease_pool_ = nullptr;
599 #endif 624 #endif
600 } 625 }
OLDNEW
« no previous file with comments | « chrome/test/base/in_process_browser_test.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698