| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/string_number_conversions.h" | 5 #include "base/string_number_conversions.h" |
| 6 #include "base/utf_string_conversions.h" | 6 #include "base/utf_string_conversions.h" |
| 7 #include "chrome/browser/browser_process.h" |
| 7 #include "chrome/browser/browser_signin.h" | 8 #include "chrome/browser/browser_signin.h" |
| 8 #include "chrome/browser/extensions/extension_browsertest.h" | 9 #include "chrome/browser/extensions/extension_browsertest.h" |
| 9 #include "chrome/browser/extensions/extension_test_message_listener.h" | 10 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 10 #include "chrome/browser/extensions/extension_webstore_private_api.h" | 11 #include "chrome/browser/extensions/extension_webstore_private_api.h" |
| 11 #include "chrome/browser/net/gaia/token_service.h" | 12 #include "chrome/browser/net/gaia/token_service.h" |
| 13 #include "chrome/browser/profile_manager.h" |
| 12 #include "chrome/browser/sync/profile_sync_service.h" | 14 #include "chrome/browser/sync/profile_sync_service.h" |
| 13 #include "chrome/browser/tab_contents/tab_contents.h" | 15 #include "chrome/browser/tab_contents/tab_contents.h" |
| 14 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/net/gaia/gaia_constants.h" | 17 #include "chrome/common/net/gaia/gaia_constants.h" |
| 16 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
| 17 #include "chrome/test/ui_test_utils.h" | 19 #include "chrome/test/ui_test_utils.h" |
| 18 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
| 19 #include "net/base/mock_host_resolver.h" | 21 #include "net/base/mock_host_resolver.h" |
| 20 | 22 |
| 21 using chrome::kHttpScheme; | 23 using chrome::kHttpScheme; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 virtual void RequestSignin(TabContents* tab_contents, | 77 virtual void RequestSignin(TabContents* tab_contents, |
| 76 const string16& preferred_email, | 78 const string16& preferred_email, |
| 77 const string16& message, | 79 const string16& message, |
| 78 SigninDelegate* delegate) { | 80 SigninDelegate* delegate) { |
| 79 if (should_succeed_) { | 81 if (should_succeed_) { |
| 80 // Simulate valid login. | 82 // Simulate valid login. |
| 81 username_ = username_after_login_; | 83 username_ = username_after_login_; |
| 82 delegate->OnLoginSuccess(); | 84 delegate->OnLoginSuccess(); |
| 83 | 85 |
| 84 // Fake a token available notification. | 86 // Fake a token available notification. |
| 85 TokenService* token_service = tab_contents->profile()->GetTokenService(); | 87 Profile* profile = tab_contents->profile(); |
| 88 if (profile->IsOffTheRecord()) { |
| 89 profile = g_browser_process->profile_manager()->GetDefaultProfile(); |
| 90 } |
| 91 TokenService* token_service = profile->GetTokenService(); |
| 86 token_service->IssueAuthTokenForTest(GaiaConstants::kGaiaService, | 92 token_service->IssueAuthTokenForTest(GaiaConstants::kGaiaService, |
| 87 "new_token"); | 93 "new_token"); |
| 88 } else { | 94 } else { |
| 89 delegate->OnLoginFailure(GoogleServiceAuthError( | 95 delegate->OnLoginFailure(GoogleServiceAuthError( |
| 90 GoogleServiceAuthError::REQUEST_CANCELED)); | 96 GoogleServiceAuthError::REQUEST_CANCELED)); |
| 91 } | 97 } |
| 92 } | 98 } |
| 93 | 99 |
| 94 private: | 100 private: |
| 95 bool should_succeed_; | 101 bool should_succeed_; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 115 // command line switch we added in SetupCommandLine). | 121 // command line switch we added in SetupCommandLine). |
| 116 GURL GetUrl(const std::string& relative_path) { | 122 GURL GetUrl(const std::string& relative_path) { |
| 117 GURL base_url = test_server()->GetURL( | 123 GURL base_url = test_server()->GetURL( |
| 118 "files/extensions/webstore_private/" + relative_path); | 124 "files/extensions/webstore_private/" + relative_path); |
| 119 GURL::Replacements replacements; | 125 GURL::Replacements replacements; |
| 120 std::string replacement_host = std::string(kTestUrlHostname); | 126 std::string replacement_host = std::string(kTestUrlHostname); |
| 121 replacements.SetHostStr(replacement_host); | 127 replacements.SetHostStr(replacement_host); |
| 122 return base_url.ReplaceComponents(replacements); | 128 return base_url.ReplaceComponents(replacements); |
| 123 } | 129 } |
| 124 | 130 |
| 125 void RunLoginTest(const std::string& relative_path, | 131 void RunLoginTestImpl(bool incognito, |
| 126 const std::string& initial_login, | 132 const std::string& relative_path, |
| 127 bool login_succeeds, | 133 const std::string& initial_login, |
| 128 const std::string& login_result) { | 134 bool login_succeeds, |
| 135 const std::string& login_result) { |
| 129 // Clear the token service so previous tests don't affect things. | 136 // Clear the token service so previous tests don't affect things. |
| 130 TokenService* token_service = browser()->profile()->GetTokenService(); | 137 TokenService* token_service = browser()->profile()->GetTokenService(); |
| 131 token_service->ResetCredentialsInMemory(); | 138 token_service->ResetCredentialsInMemory(); |
| 132 if (!initial_login.empty()) { | 139 if (!initial_login.empty()) { |
| 133 // Initialize the token service with an existing token. | 140 // Initialize the token service with an existing token. |
| 134 token_service->IssueAuthTokenForTest(GaiaConstants::kGaiaService, | 141 token_service->IssueAuthTokenForTest(GaiaConstants::kGaiaService, |
| 135 "existing_token"); | 142 "existing_token"); |
| 136 } | 143 } |
| 137 | 144 |
| 138 FakeProfileSyncService sync_service; | 145 FakeProfileSyncService sync_service; |
| 139 FakeBrowserSignin signin(login_succeeds, initial_login, login_result); | 146 FakeBrowserSignin signin(login_succeeds, initial_login, login_result); |
| 140 WebstorePrivateApi::SetTestingProfileSyncService(&sync_service); | 147 WebstorePrivateApi::SetTestingProfileSyncService(&sync_service); |
| 141 WebstorePrivateApi::SetTestingBrowserSignin(&signin); | 148 WebstorePrivateApi::SetTestingBrowserSignin(&signin); |
| 142 | 149 |
| 143 ExtensionTestMessageListener listener("success", false); | 150 ExtensionTestMessageListener listener("success", false); |
| 144 GURL url = GetUrl(relative_path); | 151 GURL url = GetUrl(relative_path); |
| 145 ui_test_utils::NavigateToURL(browser(), url); | 152 if (incognito) { |
| 153 ui_test_utils::OpenURLOffTheRecord(browser()->profile(), url); |
| 154 } else { |
| 155 ui_test_utils::NavigateToURL(browser(), url); |
| 156 } |
| 146 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 157 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 147 | 158 |
| 148 WebstorePrivateApi::SetTestingBrowserSignin(NULL); | 159 WebstorePrivateApi::SetTestingBrowserSignin(NULL); |
| 149 WebstorePrivateApi::SetTestingProfileSyncService(NULL); | 160 WebstorePrivateApi::SetTestingProfileSyncService(NULL); |
| 150 } | 161 } |
| 151 | 162 |
| 163 void RunLoginTest(const std::string& relative_path, |
| 164 const std::string& initial_login, |
| 165 bool login_succeeds, |
| 166 const std::string& login_result) { |
| 167 RunLoginTestImpl(true, |
| 168 relative_path, |
| 169 initial_login, |
| 170 login_succeeds, |
| 171 login_result); |
| 172 RunLoginTestImpl(false, |
| 173 relative_path, |
| 174 initial_login, |
| 175 login_succeeds, |
| 176 login_result); |
| 177 } |
| 178 |
| 152 protected: | 179 protected: |
| 153 std::string test_url_base_; | 180 std::string test_url_base_; |
| 154 }; | 181 }; |
| 155 | 182 |
| 156 IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateBrowserTest, BrowserLogin) { | 183 IN_PROC_BROWSER_TEST_F(ExtensionWebstorePrivateBrowserTest, BrowserLogin) { |
| 157 host_resolver()->AddRule(kTestUrlHostname, "127.0.0.1"); | 184 host_resolver()->AddRule(kTestUrlHostname, "127.0.0.1"); |
| 158 ASSERT_TRUE(test_server()->Start()); | 185 ASSERT_TRUE(test_server()->Start()); |
| 159 | 186 |
| 160 RunLoginTest("browser_login/expect_nonempty.html", | 187 RunLoginTest("browser_login/expect_nonempty.html", |
| 161 "foo@bar.com", false, ""); | 188 "foo@bar.com", false, ""); |
| 162 | 189 |
| 163 RunLoginTest("browser_login/prompt_no_preferred.html", "", true, ""); | 190 RunLoginTest("browser_login/prompt_no_preferred.html", "", true, ""); |
| 164 | 191 |
| 165 RunLoginTest("browser_login/prompt_preferred.html", "", true, "foo@bar.com"); | 192 RunLoginTest("browser_login/prompt_preferred.html", "", true, "foo@bar.com"); |
| 166 | 193 |
| 167 RunLoginTest("browser_login/prompt_already_logged_in_error.html", | 194 RunLoginTest("browser_login/prompt_already_logged_in_error.html", |
| 168 "foo@bar.com", false, "foo@bar.com"); | 195 "foo@bar.com", false, "foo@bar.com"); |
| 169 | 196 |
| 170 RunLoginTest("browser_login/prompt_login_fails.html", | 197 RunLoginTest("browser_login/prompt_login_fails.html", |
| 171 "", false, "foo@bar.com"); | 198 "", false, "foo@bar.com"); |
| 172 } | 199 } |
| OLD | NEW |