Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(468)

Side by Side Diff: chrome/browser/ui/webui/signin/login_ui_test_utils.cc

Issue 1433613002: Customize login test utils to wait for cookies to be set. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit: docs Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/ui/webui/signin/login_ui_test_utils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "chrome/browser/signin/signin_promo.h" 5 #include "chrome/browser/signin/signin_promo.h"
6 #include "chrome/browser/signin/signin_tracker_factory.h" 6 #include "chrome/browser/signin/signin_tracker_factory.h"
7 #include "chrome/browser/ui/browser.h" 7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/tabs/tab_strip_model.h" 8 #include "chrome/browser/ui/tabs/tab_strip_model.h"
9 #include "chrome/browser/ui/webui/signin/inline_login_ui.h" 9 #include "chrome/browser/ui/webui/signin/inline_login_ui.h"
10 #include "chrome/test/base/ui_test_utils.h" 10 #include "chrome/test/base/ui_test_utils.h"
11 #include "content/public/browser/notification_service.h" 11 #include "content/public/browser/notification_service.h"
12 #include "content/public/browser/notification_types.h" 12 #include "content/public/browser/notification_types.h"
13 #include "content/public/browser/web_contents.h" 13 #include "content/public/browser/web_contents.h"
14 #include "content/public/test/browser_test_utils.h" 14 #include "content/public/test/browser_test_utils.h"
15 15
16 using content::MessageLoopRunner; 16 using content::MessageLoopRunner;
17 17
18 // anonymous namespace for signin with UI helper functions. 18 // anonymous namespace for signin with UI helper functions.
19 namespace { 19 namespace {
20 20
21 // The SignInObserver observes the signin manager and blocks until a 21 // The SignInObserver observes the signin manager and blocks until a
22 // GoogleSigninSucceeded or a GoogleSigninFailed notification is fired. 22 // GoogleSigninSucceeded or a GoogleSigninFailed notification is fired.
23 class SignInObserver : public SigninTracker::Observer { 23 class SignInObserver : public SigninTracker::Observer {
24 public: 24 public:
25 SignInObserver() 25 explicit SignInObserver(bool wait_for_account_cookies)
26 : seen_(false), 26 : seen_(false),
27 running_(false), 27 running_(false),
28 signed_in_(false) {} 28 signed_in_(false),
29 wait_for_account_cookies_(wait_for_account_cookies) {}
29 30
30 virtual ~SignInObserver() {} 31 virtual ~SignInObserver() {}
31 32
32 // Returns whether a GoogleSigninSucceeded event has happened. 33 // Returns whether a GoogleSigninSucceeded event has happened.
33 bool DidSignIn() { 34 bool DidSignIn() {
34 return signed_in_; 35 return signed_in_;
35 } 36 }
36 37
37 // Blocks and waits until the user signs in. Wait() does not block if a 38 // Blocks and waits until the user signs in. Wait() does not block if a
38 // GoogleSigninSucceeded or a GoogleSigninFailed has already occurred. 39 // GoogleSigninSucceeded or a GoogleSigninFailed has already occurred.
39 void Wait() { 40 void Wait() {
40 if (seen_) 41 if (seen_)
41 return; 42 return;
42 43
43 running_ = true; 44 running_ = true;
44 message_loop_runner_ = new MessageLoopRunner; 45 message_loop_runner_ = new MessageLoopRunner;
45 message_loop_runner_->Run(); 46 message_loop_runner_->Run();
46 EXPECT_TRUE(seen_); 47 EXPECT_TRUE(seen_);
47 } 48 }
48 49
49 void SigninFailed(const GoogleServiceAuthError& error) override { 50 void SigninFailed(const GoogleServiceAuthError& error) override {
50 DVLOG(1) << "Google signin failed."; 51 DVLOG(1) << "Google signin failed.";
52 QuitLoopRunner();
53 }
54
55 void AccountAddedToCookie(const GoogleServiceAuthError& error) override {
56 if (!wait_for_account_cookies_)
57 return;
58 if (error.state() != GoogleServiceAuthError::NONE) {
59 DVLOG(1) << "Error signing the account, error " << error.state();
60 } else {
61 DVLOG(1) << "Account cookies are added to cookie jar.";
62 signed_in_ = true;
63 }
64 QuitLoopRunner();
65 }
66
67 void SigninSuccess() override {
68 DVLOG(1) << "Google signin succeeded.";
69 if (wait_for_account_cookies_)
70 return;
71 signed_in_ = true;
72 QuitLoopRunner();
73 }
74
75 void QuitLoopRunner() {
51 seen_ = true; 76 seen_ = true;
52 if (!running_) 77 if (!running_)
53 return; 78 return;
54 message_loop_runner_->Quit(); 79 message_loop_runner_->Quit();
55 running_ = false;
56 }
57
58 void AccountAddedToCookie(const GoogleServiceAuthError& error) override {}
59
60 void SigninSuccess() override {
61 DVLOG(1) << "Google signin succeeded.";
62 seen_ = true;
63 signed_in_ = true;
64 if (!running_)
65 return;
66 message_loop_runner_->Quit();
67 running_ = false; 80 running_ = false;
68 } 81 }
69 82
70 private: 83 private:
71 // Bool to mark an observed event as seen prior to calling Wait(), used to 84 // Bool to mark an observed event as seen prior to calling Wait(), used to
72 // prevent the observer from blocking. 85 // prevent the observer from blocking.
73 bool seen_; 86 bool seen_;
74 // True is the message loop runner is running. 87 // True is the message loop runner is running.
75 bool running_; 88 bool running_;
76 // True if a GoogleSigninSucceeded event has been observed. 89 // True if a GoogleSigninSucceeded event has been observed.
77 bool signed_in_; 90 bool signed_in_;
91 // Whether we should block until the account cookies are added or not.
92 // If false, we only wait until SigninSuccess event is fired which happens
93 // prior to adding account to cookie.
94 bool wait_for_account_cookies_;
78 scoped_refptr<MessageLoopRunner> message_loop_runner_; 95 scoped_refptr<MessageLoopRunner> message_loop_runner_;
79 }; 96 };
80 97
81 } // anonymous namespace 98 } // anonymous namespace
82 99
83 100
84 namespace login_ui_test_utils { 101 namespace login_ui_test_utils {
85 102
86 void WaitUntilUIReady(Browser* browser) { 103 void WaitUntilUIReady(Browser* browser) {
87 std::string message; 104 std::string message;
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 const std::string& password) { 197 const std::string& password) {
181 WaitUntilElementExistsInSigninFrame(browser, "Email"); 198 WaitUntilElementExistsInSigninFrame(browser, "Email");
182 if (ElementExistsInSigninFrame(browser, "next")) 199 if (ElementExistsInSigninFrame(browser, "next"))
183 SigninInNewGaiaFlow(browser, email, password); 200 SigninInNewGaiaFlow(browser, email, password);
184 else 201 else
185 SigninInOldGaiaFlow(browser, email, password); 202 SigninInOldGaiaFlow(browser, email, password);
186 } 203 }
187 204
188 bool SignInWithUI(Browser* browser, 205 bool SignInWithUI(Browser* browser,
189 const std::string& username, 206 const std::string& username,
190 const std::string& password) { 207 const std::string& password,
191 208 bool wait_for_account_cookies,
192 SignInObserver signin_observer; 209 signin_metrics::Source signin_source) {
210 SignInObserver signin_observer(wait_for_account_cookies);
193 scoped_ptr<SigninTracker> tracker = 211 scoped_ptr<SigninTracker> tracker =
194 SigninTrackerFactory::CreateForProfile(browser->profile(), 212 SigninTrackerFactory::CreateForProfile(browser->profile(),
195 &signin_observer); 213 &signin_observer);
196 214
197 GURL signin_url = signin::GetPromoURL( 215 GURL signin_url = signin::GetPromoURL(signin_source, false);
198 signin_metrics::SOURCE_START_PAGE, false);
199 DVLOG(1) << "Navigating to " << signin_url; 216 DVLOG(1) << "Navigating to " << signin_url;
200 // For some tests, the window is not shown yet and this might be the first tab 217 // For some tests, the window is not shown yet and this might be the first tab
201 // navigation, so GetActiveWebContents() for CURRENT_TAB is NULL. That's why 218 // navigation, so GetActiveWebContents() for CURRENT_TAB is NULL. That's why
202 // we use NEW_FOREGROUND_TAB rather than the CURRENT_TAB used by default in 219 // we use NEW_FOREGROUND_TAB rather than the CURRENT_TAB used by default in
203 // ui_test_utils::NavigateToURL(). 220 // ui_test_utils::NavigateToURL().
204 ui_test_utils::NavigateToURLWithDisposition( 221 ui_test_utils::NavigateToURLWithDisposition(
205 browser, 222 browser,
206 signin_url, 223 signin_url,
207 NEW_FOREGROUND_TAB, 224 NEW_FOREGROUND_TAB,
208 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); 225 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
209 226
210 DVLOG(1) << "Wait for login UI to be ready."; 227 DVLOG(1) << "Wait for login UI to be ready.";
211 WaitUntilUIReady(browser); 228 WaitUntilUIReady(browser);
212 DVLOG(1) << "Sign in user: " << username; 229 DVLOG(1) << "Sign in user: " << username;
213 ExecuteJsToSigninInSigninFrame(browser, username, password); 230 ExecuteJsToSigninInSigninFrame(browser, username, password);
214 signin_observer.Wait(); 231 signin_observer.Wait();
215 return signin_observer.DidSignIn(); 232 return signin_observer.DidSignIn();
216 } 233 }
217 234
235 bool SignInWithUI(Browser* browser,
236 const std::string& username,
237 const std::string& password) {
238 return SignInWithUI(browser, username, password,
239 false /* wait_for_account_cookies */,
240 signin_metrics::SOURCE_START_PAGE);
241 }
242
218 } // namespace login_ui_test_utils 243 } // namespace login_ui_test_utils
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/signin/login_ui_test_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698