Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / base / in_process_browser_test_browsertest.cc
bloba438b07b7922fe9ed440a889d70129952fa01c62
1 // Copyright (c) 2011 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 <string.h>
7 #include "base/files/file_util.h"
8 #include "base/path_service.h"
9 #include "chrome/browser/after_startup_task_utils.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_observer.h"
17 #include "net/base/filename_util.h"
18 #include "net/base/net_errors.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 namespace {
23 class InProcessBrowserTestP
24 : public InProcessBrowserTest,
25 public ::testing::WithParamInterface<const char*> {
28 IN_PROC_BROWSER_TEST_P(InProcessBrowserTestP, TestP) {
29 EXPECT_EQ(0, strcmp("foo", GetParam()));
32 INSTANTIATE_TEST_CASE_P(IPBTP,
33 InProcessBrowserTestP,
34 ::testing::Values("foo"));
36 // WebContents observer that can detect provisional load failures.
37 class LoadFailObserver : public content::WebContentsObserver {
38 public:
39 explicit LoadFailObserver(content::WebContents* contents)
40 : content::WebContentsObserver(contents),
41 failed_load_(false),
42 error_code_(net::OK) { }
44 void DidFailProvisionalLoad(
45 content::RenderFrameHost* render_frame_host,
46 const GURL& validated_url,
47 int error_code,
48 const base::string16& error_description,
49 bool was_ignored_by_handler) override {
50 failed_load_ = true;
51 error_code_ = static_cast<net::Error>(error_code);
52 validated_url_ = validated_url;
55 bool failed_load() const { return failed_load_; }
56 net::Error error_code() const { return error_code_; }
57 const GURL& validated_url() const { return validated_url_; }
59 private:
60 bool failed_load_;
61 net::Error error_code_;
62 GURL validated_url_;
64 DISALLOW_COPY_AND_ASSIGN(LoadFailObserver);
67 // Tests that InProcessBrowserTest cannot resolve external host, in this case
68 // "google.com" and "cnn.com". Using external resources is disabled by default
69 // in InProcessBrowserTest because it causes flakiness.
70 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, ExternalConnectionFail) {
71 content::WebContents* contents =
72 browser()->tab_strip_model()->GetActiveWebContents();
74 const char* const kURLs[] = {
75 "http://www.google.com/",
76 "http://www.cnn.com/"
78 for (size_t i = 0; i < arraysize(kURLs); ++i) {
79 GURL url(kURLs[i]);
80 LoadFailObserver observer(contents);
81 ui_test_utils::NavigateToURL(browser(), url);
82 EXPECT_TRUE(observer.failed_load());
83 EXPECT_EQ(net::ERR_NOT_IMPLEMENTED, observer.error_code());
84 EXPECT_EQ(url, observer.validated_url());
88 // Verify that AfterStartupTaskUtils considers startup to be complete
89 // prior to test execution so tasks posted by tests are never deferred.
90 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, AfterStartupTaskUtils) {
91 EXPECT_TRUE(AfterStartupTaskUtils::IsBrowserStartupComplete());
94 // Paths are to very simple HTML files. One is accessible, the other is not.
95 const base::FilePath::CharType kPassHTML[] =
96 FILE_PATH_LITERAL("chrome/test/data/accessibility_pass.html");
97 const base::FilePath::CharType kFailHTML[] =
98 FILE_PATH_LITERAL("chrome/test/data/accessibility_fail.html");
101 * This class is meant as a test for the accessibility audit in the
102 * InProcessBrowserTest. These tests do NOT validate the accessibility audit,
103 * just the ability to run it.
105 class InProcessAccessibilityBrowserTest : public InProcessBrowserTest {
106 protected:
107 // Construct a URL from a file path that can be used to get to a web page.
108 base::FilePath BuildURLToFile(const base::FilePath::CharType* file_path) {
109 base::FilePath source_root;
110 if (!PathService::Get(base::DIR_SOURCE_ROOT, &source_root))
111 return base::FilePath();
112 return source_root.Append(file_path);
115 bool NavigateToURL(const base::FilePath::CharType* address) {
116 GURL url = net::FilePathToFileURL(BuildURLToFile(address));
118 if (!url.is_valid() || url.is_empty() || !browser())
119 return false;
121 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
122 browser(), url, 1);
123 return true;
127 // Test that an accessible page doesn't fail the accessibility audit.
128 IN_PROC_BROWSER_TEST_F(
129 InProcessAccessibilityBrowserTest, DISABLED_VerifyAccessibilityPass) {
130 ASSERT_TRUE(NavigateToURL(kPassHTML));
132 std::string test_result;
133 EXPECT_TRUE(RunAccessibilityChecks(&test_result));
135 // No error message on success.
136 EXPECT_EQ("", test_result);
139 // Test that a page that is not accessible will fail the accessibility audit.
140 IN_PROC_BROWSER_TEST_F(
141 InProcessAccessibilityBrowserTest, VerifyAccessibilityFail) {
142 ASSERT_TRUE(NavigateToURL(kFailHTML));
144 std::string test_result;
145 EXPECT_FALSE(RunAccessibilityChecks(&test_result));
147 // Error should NOT be empty on failure.
148 EXPECT_NE("", test_result);
151 } // namespace