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. 4 5 #include <string.h> 6 7 #include "chrome/browser/ui/browser.h" 8 #include "chrome/browser/ui/tabs/tab_strip_model.h" 9 #include "chrome/test/base/in_process_browser_test.h" 10 #include "chrome/test/base/ui_test_utils.h" 11 #include "content/public/browser/render_view_host.h" 12 #include "content/public/browser/web_contents.h" 13 #include "content/public/browser/web_contents_observer.h" 14 #include "net/base/net_errors.h" 15 #include "testing/gtest/include/gtest/gtest.h" 16 17 namespace { 18 19 class InProcessBrowserTestP 20 : public InProcessBrowserTest, 21 public ::testing::WithParamInterface<const char*> { 22 }; 23 24 IN_PROC_BROWSER_TEST_P(InProcessBrowserTestP, TestP) { 25 EXPECT_EQ(0, strcmp("foo", GetParam())); 26 } 27 28 INSTANTIATE_TEST_CASE_P(IPBTP, 29 InProcessBrowserTestP, 30 ::testing::Values("foo")); 31 32 // WebContents observer that can detect provisional load failures. 33 class LoadFailObserver : public content::WebContentsObserver { 34 public: 35 explicit LoadFailObserver(content::WebContents* contents) 36 : content::WebContentsObserver(contents), 37 failed_load_(false), 38 error_code_(net::OK) { } 39 40 virtual void DidFailProvisionalLoad( 41 int64 frame_id, 42 const base::string16& frame_unique_name, 43 bool is_main_frame, 44 const GURL& validated_url, 45 int error_code, 46 const base::string16& error_description, 47 content::RenderViewHost* render_view_host) OVERRIDE { 48 failed_load_ = true; 49 error_code_ = static_cast<net::Error>(error_code); 50 validated_url_ = validated_url; 51 } 52 53 bool failed_load() const { return failed_load_; } 54 net::Error error_code() const { return error_code_; } 55 const GURL& validated_url() const { return validated_url_; } 56 57 private: 58 bool failed_load_; 59 net::Error error_code_; 60 GURL validated_url_; 61 62 DISALLOW_COPY_AND_ASSIGN(LoadFailObserver); 63 }; 64 65 // Tests that InProcessBrowserTest cannot resolve external host, in this case 66 // "google.com" and "cnn.com". Using external resources is disabled by default 67 // in InProcessBrowserTest because it causes flakiness. 68 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, ExternalConnectionFail) { 69 content::WebContents* contents = 70 browser()->tab_strip_model()->GetActiveWebContents(); 71 72 const char* const kURLs[] = { 73 "http://www.google.com/", 74 "http://www.cnn.com/" 75 }; 76 for (size_t i = 0; i < arraysize(kURLs); ++i) { 77 GURL url(kURLs[i]); 78 LoadFailObserver observer(contents); 79 ui_test_utils::NavigateToURL(browser(), url); 80 EXPECT_TRUE(observer.failed_load()); 81 EXPECT_EQ(net::ERR_NOT_IMPLEMENTED, observer.error_code()); 82 EXPECT_EQ(url, observer.validated_url()); 83 } 84 } 85 86 } // namespace 87