OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/scoped_observer.h" | |
Nicolas Zea
2014/11/07 23:55:48
You should probably include <string> here, since y
shadi
2014/11/08 01:37:57
I added it in .h file.
| |
6 #include "chrome/browser/profiles/profile.h" | |
7 #include "chrome/browser/signin/signin_manager_factory.h" | |
8 #include "chrome/browser/signin/signin_promo.h" | |
9 #include "chrome/browser/ui/browser.h" | |
10 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
11 #include "chrome/browser/ui/webui/signin/inline_login_ui.h" | |
12 #include "chrome/test/base/ui_test_utils.h" | |
13 #include "components/signin/core/browser/signin_manager.h" | |
14 #include "content/public/browser/notification_service.h" | |
15 #include "content/public/browser/notification_types.h" | |
16 #include "content/public/test/browser_test_utils.h" | |
17 | |
18 using content::MessageLoopRunner; | |
19 | |
20 class SignInWaiter : public SigninManagerBase::Observer { | |
pval...(no longer on Chromium)
2014/11/07 23:54:14
I believe this class (and all other util method fo
Nicolas Zea
2014/11/07 23:55:48
nit: put all this helper logic anonymous namespace
shadi
2014/11/08 01:37:57
Done.
shadi
2014/11/08 01:37:57
Done.
| |
21 public: | |
22 explicit SignInWaiter(SigninManagerBase* signin_manager) | |
23 : seen_(false), | |
24 running_(false), | |
25 signed_in_(false), | |
26 scoped_observer_(this) { | |
27 scoped_observer_.Add(signin_manager); | |
28 } | |
29 virtual ~SignInWaiter() {} | |
30 | |
31 bool DidSignIn() { | |
Nicolas Zea
2014/11/07 23:55:48
Comments for these methods?
shadi
2014/11/08 01:37:56
Done.
| |
32 return signed_in_; | |
33 } | |
34 | |
35 void Wait() { | |
36 if (seen_) | |
37 return; | |
38 | |
39 running_ = true; | |
40 message_loop_runner_ = new MessageLoopRunner; | |
41 message_loop_runner_->Run(); | |
42 EXPECT_TRUE(seen_); | |
43 } | |
44 | |
45 virtual void GoogleSigninFailed( | |
46 const GoogleServiceAuthError& error) override { | |
47 VLOG(1) << "Google signin failed."; | |
48 seen_ = true; | |
49 if (!running_) | |
50 return; | |
51 message_loop_runner_->Quit(); | |
52 running_ = false; | |
53 } | |
54 | |
55 virtual void GoogleSigninSucceeded(const std::string& account_id, | |
56 const std::string& username, | |
57 const std::string& password) override { | |
58 VLOG(1) << "Google signin succeeded for " << username; | |
59 seen_ = true; | |
60 signed_in_ = true; | |
61 if (!running_) | |
62 return; | |
63 message_loop_runner_->Quit(); | |
64 running_ = false; | |
65 } | |
66 | |
67 private: | |
68 bool seen_; | |
Nicolas Zea
2014/11/07 23:55:48
comments for these members?
shadi
2014/11/08 01:37:57
Done.
| |
69 bool running_; | |
70 bool signed_in_; | |
71 ScopedObserver<SigninManagerBase, SignInWaiter> scoped_observer_; | |
72 scoped_refptr<MessageLoopRunner> message_loop_runner_; | |
73 }; | |
74 | |
75 // static | |
Nicolas Zea
2014/11/07 23:55:48
This isn't actually static (here and below)
shadi
2014/11/08 01:37:57
Done.
| |
76 bool HtmlElementExistsInSigninFrame(content::WebContents* web_contents, | |
77 const std::string& name) { | |
78 bool result; | |
79 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | |
80 InlineLoginUI::GetAuthIframe(web_contents, GURL(), "signin-frame"), | |
81 "window.domAutomationController.send(" | |
82 "document.getElementById(\"" + name + "\") != null);", | |
83 &result)); | |
84 | |
85 return result; | |
86 } | |
87 | |
88 // static | |
89 // Executes JavaScript code in the auth iframe hosted by gaia_auth extension. | |
90 void ExecuteScriptAndWaitForAnyPageLoad(content::WebContents* web_contents, | |
91 const std::string& script) { | |
92 content::WindowedNotificationObserver observer( | |
93 content::NOTIFICATION_LOAD_STOP, | |
94 content::Source<content::NavigationController>( | |
95 &web_contents->GetController())); | |
96 ASSERT_TRUE(content::ExecuteScript(InlineLoginUI::GetAuthIframe( | |
97 web_contents, GURL(), "signin-frame"), script)); | |
98 observer.Wait(); | |
99 } | |
100 | |
101 void WaitUntilUIReady(content::WebContents* web_contents) { | |
102 content::DOMMessageQueue message_queue; | |
103 ASSERT_TRUE(content::ExecuteScript( | |
104 web_contents, | |
105 "if (!inline.login.getAuthExtHost())" | |
106 " inline.login.initialize();" | |
107 "var handler = function() {" | |
108 " window.domAutomationController.setAutomationId(0);" | |
109 " window.domAutomationController.send('ready');" | |
110 "};" | |
111 "if (inline.login.isAuthReady())" | |
112 " handler();" | |
113 "else" | |
114 " inline.login.getAuthExtHost().addEventListener('ready', handler);")); | |
115 | |
116 std::string message; | |
117 do { | |
118 ASSERT_TRUE(message_queue.WaitForMessage(&message)); | |
119 } while (message != "\"ready\""); | |
120 } | |
121 | |
122 // static | |
123 bool SignInToGAIA(Browser* browser, | |
124 const std::string& username, | |
125 const std::string& password) { | |
126 SigninManager* signinManager = | |
127 SigninManagerFactory::GetForProfile(browser->profile()); | |
128 SignInWaiter sign_in_waiter(signinManager); | |
129 | |
130 GURL signinURL = signin::GetPromoURL(signin::SOURCE_START_PAGE, false); | |
131 VLOG(1) << "Navigating to " << signinURL; | |
132 content::WindowedNotificationObserver observer( | |
133 content::NOTIFICATION_LOAD_STOP, | |
134 content::NotificationService::AllSources()); | |
135 ui_test_utils::NavigateToURL(browser, signinURL); | |
136 observer.Wait(); | |
137 content::WebContents* web_contents = | |
138 browser->tab_strip_model()->GetActiveWebContents(); | |
139 WaitUntilUIReady(web_contents); | |
140 VLOG(1) << "LOGIN UI READY"; | |
141 // The active tab should have the "Google Accounts" login page loaded. | |
142 if (!HtmlElementExistsInSigninFrame(web_contents, "Email") || | |
143 !HtmlElementExistsInSigninFrame(web_contents, "Passwd")) { | |
144 VLOG(1) << "Signin URL loaded but there are no email/password fields."; | |
145 return false; | |
146 } | |
147 | |
148 std::string js = | |
149 "document.getElementById('Email').value = '" + username + "';" | |
150 "document.getElementById('Passwd').value = '" + password + "';" | |
151 "document.getElementById('signIn').click();"; | |
152 ExecuteScriptAndWaitForAnyPageLoad(web_contents, js); | |
153 sign_in_waiter.Wait(); | |
154 return sign_in_waiter.DidSignIn(); | |
155 } | |
OLD | NEW |