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.
7 #include "base/files/file_util.h"
8 #include "base/path_service.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/test/base/in_process_browser_test.h"
12 #include "chrome/test/base/ui_test_utils.h"
13 #include "content/public/browser/render_view_host.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #include "net/base/filename_util.h"
17 #include "net/base/net_errors.h"
18 #include "testing/gtest/include/gtest/gtest.h"
22 class InProcessBrowserTestP
23 : public InProcessBrowserTest
,
24 public ::testing::WithParamInterface
<const char*> {
27 IN_PROC_BROWSER_TEST_P(InProcessBrowserTestP
, TestP
) {
28 EXPECT_EQ(0, strcmp("foo", GetParam()));
31 INSTANTIATE_TEST_CASE_P(IPBTP
,
32 InProcessBrowserTestP
,
33 ::testing::Values("foo"));
35 // WebContents observer that can detect provisional load failures.
36 class LoadFailObserver
: public content::WebContentsObserver
{
38 explicit LoadFailObserver(content::WebContents
* contents
)
39 : content::WebContentsObserver(contents
),
41 error_code_(net::OK
) { }
43 void DidFailProvisionalLoad(
44 content::RenderFrameHost
* render_frame_host
,
45 const GURL
& validated_url
,
47 const base::string16
& error_description
,
48 bool was_ignored_by_handler
) override
{
50 error_code_
= static_cast<net::Error
>(error_code
);
51 validated_url_
= validated_url
;
54 bool failed_load() const { return failed_load_
; }
55 net::Error
error_code() const { return error_code_
; }
56 const GURL
& validated_url() const { return validated_url_
; }
60 net::Error error_code_
;
63 DISALLOW_COPY_AND_ASSIGN(LoadFailObserver
);
66 // Tests that InProcessBrowserTest cannot resolve external host, in this case
67 // "google.com" and "cnn.com". Using external resources is disabled by default
68 // in InProcessBrowserTest because it causes flakiness.
69 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest
, ExternalConnectionFail
) {
70 content::WebContents
* contents
=
71 browser()->tab_strip_model()->GetActiveWebContents();
73 const char* const kURLs
[] = {
74 "http://www.google.com/",
77 for (size_t i
= 0; i
< arraysize(kURLs
); ++i
) {
79 LoadFailObserver
observer(contents
);
80 ui_test_utils::NavigateToURL(browser(), url
);
81 EXPECT_TRUE(observer
.failed_load());
82 EXPECT_EQ(net::ERR_NOT_IMPLEMENTED
, observer
.error_code());
83 EXPECT_EQ(url
, observer
.validated_url());
87 // Paths are to very simple HTML files. One is accessible, the other is not.
88 const base::FilePath::CharType kPassHTML
[] =
89 FILE_PATH_LITERAL("chrome/test/data/accessibility_pass.html");
90 const base::FilePath::CharType kFailHTML
[] =
91 FILE_PATH_LITERAL("chrome/test/data/accessibility_fail.html");
94 * This class is meant as a test for the accessibility audit in the
95 * InProcessBrowserTest. These tests do NOT validate the accessibility audit,
96 * just the ability to run it.
98 class InProcessAccessibilityBrowserTest
: public InProcessBrowserTest
{
100 // Construct a URL from a file path that can be used to get to a web page.
101 base::FilePath
BuildURLToFile(const base::FilePath::CharType
* file_path
) {
102 base::FilePath source_root
;
103 if (!PathService::Get(base::DIR_SOURCE_ROOT
, &source_root
))
104 return base::FilePath();
105 return source_root
.Append(file_path
);
108 bool NavigateToURL(const base::FilePath::CharType
* address
) {
109 GURL url
= net::FilePathToFileURL(BuildURLToFile(address
));
111 if (!url
.is_valid() || url
.is_empty() || !browser())
114 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
120 // Test that an accessible page doesn't fail the accessibility audit.
121 IN_PROC_BROWSER_TEST_F(
122 InProcessAccessibilityBrowserTest
, DISABLED_VerifyAccessibilityPass
) {
123 ASSERT_TRUE(NavigateToURL(kPassHTML
));
125 std::string test_result
;
126 EXPECT_TRUE(RunAccessibilityChecks(&test_result
));
128 // No error message on success.
129 EXPECT_EQ("", test_result
);
132 // Test that a page that is not accessible will fail the accessibility audit.
133 IN_PROC_BROWSER_TEST_F(
134 InProcessAccessibilityBrowserTest
, VerifyAccessibilityFail
) {
135 ASSERT_TRUE(NavigateToURL(kFailHTML
));
137 std::string test_result
;
138 EXPECT_FALSE(RunAccessibilityChecks(&test_result
));
140 // Error should NOT be empty on failure.
141 EXPECT_NE("", test_result
);