| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 #ifndef CHROME_BROWSER_SIGNIN_SIGNIN_BROWSERTEST_H_ | |
| 6 #define CHROME_BROWSER_SIGNIN_SIGNIN_BROWSERTEST_H_ | |
| 7 | |
| 8 #include "base/command_line.h" | |
| 9 #include "chrome/browser/signin/chrome_signin_client.h" | |
| 10 #include "chrome/browser/signin/chrome_signin_client_factory.h" | |
| 11 #include "chrome/browser/signin/signin_promo.h" | |
| 12 #include "chrome/browser/ui/browser.h" | |
| 13 #include "chrome/browser/ui/singleton_tabs.h" | |
| 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 15 #include "chrome/browser/ui/webui/signin/login_ui_service.h" | |
| 16 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" | |
| 17 #include "chrome/common/chrome_switches.h" | |
| 18 #include "chrome/common/url_constants.h" | |
| 19 #include "chrome/test/base/in_process_browser_test.h" | |
| 20 #include "chrome/test/base/ui_test_utils.h" | |
| 21 #include "components/signin/core/common/profile_management_switches.h" | |
| 22 #include "content/public/browser/notification_service.h" | |
| 23 #include "content/public/browser/notification_types.h" | |
| 24 #include "content/public/browser/render_process_host.h" | |
| 25 #include "content/public/browser/render_view_host.h" | |
| 26 #include "content/public/browser/web_contents.h" | |
| 27 #include "content/public/browser/web_contents_observer.h" | |
| 28 #include "content/public/common/content_switches.h" | |
| 29 #include "google_apis/gaia/gaia_urls.h" | |
| 30 #include "net/http/http_status_code.h" | |
| 31 #include "net/url_request/test_url_fetcher_factory.h" | |
| 32 #include "net/url_request/url_request_status.h" | |
| 33 | |
| 34 namespace { | |
| 35 const char kNonSigninURL[] = "http://www.google.com"; | |
| 36 } | |
| 37 | |
| 38 class SigninBrowserTest : public InProcessBrowserTest { | |
| 39 public: | |
| 40 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 41 https_server_.reset(new net::SpawnedTestServer( | |
| 42 net::SpawnedTestServer::TYPE_HTTPS, | |
| 43 net::SpawnedTestServer::kLocalhost, | |
| 44 base::FilePath(FILE_PATH_LITERAL("chrome/test/data")))); | |
| 45 ASSERT_TRUE(https_server_->Start()); | |
| 46 | |
| 47 // Add a host resolver rule to map all outgoing requests to the test server. | |
| 48 // This allows us to use "real" hostnames in URLs, which we can use to | |
| 49 // create arbitrary SiteInstances. | |
| 50 command_line->AppendSwitchASCII( | |
| 51 switches::kHostResolverRules, | |
| 52 "MAP * " + https_server_->host_port_pair().ToString() + | |
| 53 ",EXCLUDE localhost"); | |
| 54 command_line->AppendSwitch(switches::kIgnoreCertificateErrors); | |
| 55 // All tests in this file are for the web based sign in flows. | |
| 56 // TODO(guohui): fix tests for inline sign in flows. | |
| 57 command_line->AppendSwitch(switches::kEnableWebBasedSignin); | |
| 58 } | |
| 59 | |
| 60 void SetUp() override { | |
| 61 factory_.reset(new net::URLFetcherImplFactory()); | |
| 62 fake_factory_.reset(new net::FakeURLFetcherFactory(factory_.get())); | |
| 63 fake_factory_->SetFakeResponse( | |
| 64 GaiaUrls::GetInstance()->service_login_url(), std::string(), | |
| 65 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | |
| 66 fake_factory_->SetFakeResponse( | |
| 67 GURL(kNonSigninURL), std::string(), net::HTTP_OK, | |
| 68 net::URLRequestStatus::SUCCESS); | |
| 69 // Yield control back to the InProcessBrowserTest framework. | |
| 70 InProcessBrowserTest::SetUp(); | |
| 71 } | |
| 72 | |
| 73 void TearDown() override { | |
| 74 if (fake_factory_.get()) { | |
| 75 fake_factory_->ClearFakeResponses(); | |
| 76 fake_factory_.reset(); | |
| 77 } | |
| 78 | |
| 79 // Cancel any outstanding URL fetches and destroy the URLFetcherImplFactory | |
| 80 // we created. | |
| 81 net::URLFetcher::CancelAll(); | |
| 82 factory_.reset(); | |
| 83 InProcessBrowserTest::TearDown(); | |
| 84 } | |
| 85 | |
| 86 private: | |
| 87 // Fake URLFetcher factory used to mock out GAIA signin. | |
| 88 scoped_ptr<net::FakeURLFetcherFactory> fake_factory_; | |
| 89 | |
| 90 // The URLFetcherImplFactory instance used to instantiate |fake_factory_|. | |
| 91 scoped_ptr<net::URLFetcherImplFactory> factory_; | |
| 92 | |
| 93 scoped_ptr<net::SpawnedTestServer> https_server_; | |
| 94 }; | |
| 95 | |
| 96 // If the one-click-signin feature is not enabled (e.g Chrome OS), we | |
| 97 // never grant signin privileges to any renderer processes. | |
| 98 #if defined(ENABLE_ONE_CLICK_SIGNIN) | |
| 99 const bool kOneClickSigninEnabled = true; | |
| 100 #else | |
| 101 const bool kOneClickSigninEnabled = false; | |
| 102 #endif | |
| 103 | |
| 104 // Disabled on Windows due to flakiness. http://crbug.com/249055 | |
| 105 #if defined(OS_WIN) | |
| 106 #define MAYBE_ProcessIsolation DISABLED_ProcessIsolation | |
| 107 #else | |
| 108 #define MAYBE_ProcessIsolation ProcessIsolation | |
| 109 #endif | |
| 110 IN_PROC_BROWSER_TEST_F(SigninBrowserTest, MAYBE_ProcessIsolation) { | |
| 111 // This test is not needed for the webview based sign-in code. | |
| 112 if (switches::IsEnableWebviewBasedSignin()) | |
| 113 return; | |
| 114 | |
| 115 SigninClient* signin = | |
| 116 ChromeSigninClientFactory::GetForProfile(browser()->profile()); | |
| 117 EXPECT_FALSE(signin->HasSigninProcess()); | |
| 118 | |
| 119 ui_test_utils::NavigateToURL(browser(), signin::GetPromoURL( | |
| 120 signin_metrics::SOURCE_NTP_LINK, true)); | |
| 121 EXPECT_EQ(kOneClickSigninEnabled, signin->HasSigninProcess()); | |
| 122 | |
| 123 // Navigating away should change the process. | |
| 124 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIOmniboxURL)); | |
| 125 EXPECT_FALSE(signin->HasSigninProcess()); | |
| 126 | |
| 127 ui_test_utils::NavigateToURL(browser(), signin::GetPromoURL( | |
| 128 signin_metrics::SOURCE_NTP_LINK, true)); | |
| 129 EXPECT_EQ(kOneClickSigninEnabled, signin->HasSigninProcess()); | |
| 130 | |
| 131 content::WebContents* active_tab = | |
| 132 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 133 int active_tab_process_id = | |
| 134 active_tab->GetRenderProcessHost()->GetID(); | |
| 135 EXPECT_EQ(kOneClickSigninEnabled, | |
| 136 signin->IsSigninProcess(active_tab_process_id)); | |
| 137 EXPECT_EQ(0, active_tab->GetRenderViewHost()->GetEnabledBindings()); | |
| 138 | |
| 139 // Entry points to signin request "SINGLETON_TAB" mode, so a new request | |
| 140 // shouldn't change anything. | |
| 141 chrome::NavigateParams params(chrome::GetSingletonTabNavigateParams( | |
| 142 browser(), | |
| 143 GURL(signin::GetPromoURL(signin_metrics::SOURCE_NTP_LINK, false)))); | |
| 144 params.path_behavior = chrome::NavigateParams::IGNORE_AND_NAVIGATE; | |
| 145 ShowSingletonTabOverwritingNTP(browser(), params); | |
| 146 EXPECT_EQ(active_tab, browser()->tab_strip_model()->GetActiveWebContents()); | |
| 147 EXPECT_EQ(kOneClickSigninEnabled, | |
| 148 signin->IsSigninProcess(active_tab_process_id)); | |
| 149 | |
| 150 // Navigating away should change the process. | |
| 151 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL)); | |
| 152 EXPECT_FALSE(signin->IsSigninProcess( | |
| 153 active_tab->GetRenderProcessHost()->GetID())); | |
| 154 } | |
| 155 | |
| 156 #if defined (OS_MACOSX) | |
| 157 // crbug.com/375197 | |
| 158 #define MAYBE_NotTrustedAfterRedirect DISABLED_NotTrustedAfterRedirect | |
| 159 #else | |
| 160 #define MAYBE_NotTrustedAfterRedirect NotTrustedAfterRedirect | |
| 161 #endif | |
| 162 | |
| 163 IN_PROC_BROWSER_TEST_F(SigninBrowserTest, MAYBE_NotTrustedAfterRedirect) { | |
| 164 // This test is not needed for the webview based sign-in code. | |
| 165 if (switches::IsEnableWebviewBasedSignin()) | |
| 166 return; | |
| 167 | |
| 168 SigninClient* signin = | |
| 169 ChromeSigninClientFactory::GetForProfile(browser()->profile()); | |
| 170 EXPECT_FALSE(signin->HasSigninProcess()); | |
| 171 | |
| 172 GURL url = signin::GetPromoURL(signin_metrics::SOURCE_NTP_LINK, true); | |
| 173 ui_test_utils::NavigateToURL(browser(), url); | |
| 174 EXPECT_EQ(kOneClickSigninEnabled, signin->HasSigninProcess()); | |
| 175 | |
| 176 // Navigating in a different tab should not affect the sign-in process. | |
| 177 ui_test_utils::NavigateToURLWithDisposition( | |
| 178 browser(), GURL(kNonSigninURL), NEW_BACKGROUND_TAB, | |
| 179 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
| 180 EXPECT_EQ(kOneClickSigninEnabled, signin->HasSigninProcess()); | |
| 181 | |
| 182 // Navigating away should clear the sign-in process. | |
| 183 GURL redirect_url("https://accounts.google.com/server-redirect?" | |
| 184 "https://foo.com?service=chromiumsync"); | |
| 185 ui_test_utils::NavigateToURL(browser(), redirect_url); | |
| 186 EXPECT_FALSE(signin->HasSigninProcess()); | |
| 187 } | |
| 188 | |
| 189 class BackOnNTPCommitObserver : public content::WebContentsObserver { | |
| 190 public: | |
| 191 explicit BackOnNTPCommitObserver(content::WebContents* web_contents) | |
| 192 : content::WebContentsObserver(web_contents) { | |
| 193 } | |
| 194 | |
| 195 void DidCommitProvisionalLoadForFrame( | |
| 196 content::RenderFrameHost* render_frame_host, | |
| 197 const GURL& url, | |
| 198 ui::PageTransition transition_type) override { | |
| 199 if (url == GURL(chrome::kChromeUINewTabURL) || | |
| 200 url == GURL(chrome::kChromeSearchLocalNtpUrl)) { | |
| 201 content::WindowedNotificationObserver observer( | |
| 202 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
| 203 content::NotificationService::AllSources()); | |
| 204 web_contents()->GetController().GoBack(); | |
| 205 observer.Wait(); | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 private: | |
| 210 DISALLOW_COPY_AND_ASSIGN(BackOnNTPCommitObserver); | |
| 211 }; | |
| 212 | |
| 213 // This is a test for http://crbug.com/257277. It simulates the navigations | |
| 214 // that occur if the user clicks on the "Skip for now" link at the signin page | |
| 215 // and initiates a back navigation between the point of Commit and | |
| 216 // DidStopLoading of the NTP. | |
| 217 IN_PROC_BROWSER_TEST_F(SigninBrowserTest, SigninSkipForNowAndGoBack) { | |
| 218 // This test is not needed for the webview based sign-in code. | |
| 219 // OneClickSigninHelper is not used. | |
| 220 if (switches::IsEnableWebviewBasedSignin()) | |
| 221 return; | |
| 222 | |
| 223 GURL ntp_url(chrome::kChromeUINewTabURL); | |
| 224 GURL start_url = signin::GetPromoURL( | |
| 225 signin_metrics::SOURCE_START_PAGE, false); | |
| 226 GURL skip_url = signin::GetLandingURL("ntp", 1); | |
| 227 | |
| 228 SigninClient* signin = | |
| 229 ChromeSigninClientFactory::GetForProfile(browser()->profile()); | |
| 230 EXPECT_FALSE(signin->HasSigninProcess()); | |
| 231 | |
| 232 ui_test_utils::NavigateToURL(browser(), start_url); | |
| 233 EXPECT_EQ(kOneClickSigninEnabled, signin->HasSigninProcess()); | |
| 234 | |
| 235 content::WebContents* web_contents = | |
| 236 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 237 | |
| 238 // Simulate clicking on the Skip for now link. It's important to have a | |
| 239 // link transition so that OneClickSigninHelper removes the blank page | |
| 240 // from the history. | |
| 241 chrome::NavigateParams navigate_params(browser(), | |
| 242 skip_url, | |
| 243 ui::PAGE_TRANSITION_LINK); | |
| 244 ui_test_utils::NavigateToURL(&navigate_params); | |
| 245 | |
| 246 // Register an observer that will navigate back immediately on the commit of | |
| 247 // the NTP. This will allow us to hit the race condition of navigating back | |
| 248 // before the DidStopLoading message of NTP gets delivered. This must be | |
| 249 // created after the navigation to the skip_url has finished loading, | |
| 250 // otherwise this observer will navigate back, before the history cleaner | |
| 251 // has had a chance to remove the navigation entry. | |
| 252 BackOnNTPCommitObserver commit_observer(web_contents); | |
| 253 | |
| 254 // Since OneClickSigninHelper aborts redirect to NTP, thus we expect the | |
| 255 // visible URL to be the starting URL. | |
| 256 EXPECT_EQ(skip_url, web_contents->GetLastCommittedURL()); | |
| 257 EXPECT_EQ(start_url, web_contents->GetVisibleURL()); | |
| 258 | |
| 259 content::WindowedNotificationObserver observer( | |
| 260 content::NOTIFICATION_LOAD_STOP, | |
| 261 content::NotificationService::AllSources()); | |
| 262 observer.Wait(); | |
| 263 EXPECT_EQ(start_url, web_contents->GetLastCommittedURL()); | |
| 264 } | |
| 265 #endif // CHROME_BROWSER_SIGNIN_SIGNIN_BROWSERTEST_H_ | |
| OLD | NEW |