1 // Copyright 2015 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.
7 #include "base/path_service.h"
8 #include "chrome/browser/chromeos/login/easy_unlock/bootstrap_user_context_initializer.h"
9 #include "chrome/browser/chromeos/login/session/user_session_manager.h"
10 #include "chrome/browser/chromeos/login/session/user_session_manager_test_api.h"
11 #include "chrome/browser/chromeos/login/test/oobe_base_test.h"
12 #include "chrome/browser/chromeos/login/test/oobe_screen_waiter.h"
13 #include "chrome/browser/chromeos/login/wizard_controller.h"
14 #include "chrome/browser/signin/easy_unlock_service_factory.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chromeos/chromeos_switches.h"
17 #include "components/proximity_auth/switches.h"
18 #include "components/user_manager/user_manager.h"
19 #include "content/public/test/browser_test_utils.h"
20 #include "content/public/test/test_utils.h"
21 #include "google_apis/gaia/gaia_constants.h"
22 #include "google_apis/gaia/gaia_urls.h"
23 #include "net/test/embedded_test_server/embedded_test_server.h"
29 const char kFakeGaiaId
[] = "123456";
30 const char kFakeUser
[] = "test_user@example.com";
31 const char kFakeSid
[] = "fake-sid";
32 const char kFakeLsid
[] = "fake-lsid";
33 const char kFakeRefreshToken
[] = "fake-refresh-token";
34 const char kFakeUserInfoToken
[] = "fake-user-info-token";
36 class ScopedCompleteCallbackForTesting
{
38 explicit ScopedCompleteCallbackForTesting(
39 const BootstrapUserContextInitializer::CompleteCallback
& callback
);
40 ~ScopedCompleteCallbackForTesting();
43 BootstrapUserContextInitializer::CompleteCallback callback_
;
44 DISALLOW_COPY_AND_ASSIGN(ScopedCompleteCallbackForTesting
);
47 ScopedCompleteCallbackForTesting::ScopedCompleteCallbackForTesting(
48 const BootstrapUserContextInitializer::CompleteCallback
& callback
)
49 : callback_(callback
) {
50 BootstrapUserContextInitializer::SetCompleteCallbackForTesting(&callback_
);
53 ScopedCompleteCallbackForTesting::~ScopedCompleteCallbackForTesting() {
54 BootstrapUserContextInitializer::SetCompleteCallbackForTesting(NULL
);
59 class BootstrapTest
: public OobeBaseTest
{
63 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
64 OobeBaseTest::SetUpCommandLine(command_line
);
66 base::CommandLine::ForCurrentProcess()->AppendSwitch(
67 proximity_auth::switches::kForceLoadEasyUnlockAppInTests
);
69 const GURL
& server_url
= embedded_test_server()->base_url();
70 GURL::Replacements replace_host
;
71 replace_host
.SetHostStr("eafe");
72 GURL eafe_url
= server_url
.ReplaceComponents(replace_host
);
74 command_line
->AppendSwitchASCII(switches::kEafeUrl
, eafe_url
.spec());
75 command_line
->AppendSwitchASCII(switches::kEafePath
,
76 "login/easy_unlock/auth_code_login.html");
79 void SetUpOnMainThread() override
{
80 OobeBaseTest::SetUpOnMainThread();
82 GaiaUrls
* gaia_urls
= GaiaUrls::GetInstance();
84 fake_gaia_
->SetFakeMergeSessionParams(kFakeUser
, kFakeSid
, kFakeLsid
);
85 fake_gaia_
->MapEmailToGaiaId(kFakeUser
, kFakeGaiaId
);
87 FakeGaia::AccessTokenInfo userinfo_token_info
;
88 userinfo_token_info
.token
= kFakeUserInfoToken
;
89 userinfo_token_info
.scopes
.insert(GaiaConstants::kGoogleUserInfoEmail
);
90 userinfo_token_info
.scopes
.insert(GaiaConstants::kGoogleUserInfoProfile
);
91 userinfo_token_info
.audience
= gaia_urls
->oauth2_chrome_client_id();
92 userinfo_token_info
.email
= kFakeUser
;
93 fake_gaia_
->IssueOAuthToken(kFakeRefreshToken
, userinfo_token_info
);
96 void SetExpectedCredentials(const UserContext
& user_context
) {
97 test::UserSessionManagerTestApi
session_manager_test_api(
98 UserSessionManager::GetInstance());
99 session_manager_test_api
.InjectStubUserContext(user_context
);
102 bool JsExecute(const std::string
& script
) {
103 return content::ExecuteScript(GetLoginUI()->GetWebContents(), script
);
106 void SetTestEasyUnlockAppPath(const std::string
& relative_path
) {
107 base::FilePath test_data_dir
;
108 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
));
109 EasyUnlockServiceFactory::GetInstance()->set_app_path_for_testing(
110 test_data_dir
.AppendASCII(relative_path
));
113 void StartSignInScreen() {
114 WizardController::SkipPostLoginScreensForTesting();
115 WizardController
* wizard_controller
=
116 WizardController::default_controller();
117 ASSERT_TRUE(wizard_controller
);
118 wizard_controller
->SkipToLoginForTesting(LoginScreenContext());
119 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN
).Wait();
122 void OnBootstrapInitialized(bool success
, const UserContext
& user_context
) {
123 EXPECT_TRUE(success
);
124 SetExpectedCredentials(user_context
);
129 JsExecute("cr.ui.Oobe.handleAccelerator('toggle_easy_bootstrap');");
133 IN_PROC_BROWSER_TEST_F(BootstrapTest
, Basic
) {
134 ScopedCompleteCallbackForTesting
scoped_bootstrap_initialized(base::Bind(
135 &BootstrapTest::OnBootstrapInitialized
, base::Unretained(this)));
137 SetTestEasyUnlockAppPath("login/easy_unlock/auto_pair_success");
141 content::WindowedNotificationObserver(
142 chrome::NOTIFICATION_SESSION_STARTED
,
143 content::NotificationService::AllSources()).Wait();
146 IN_PROC_BROWSER_TEST_F(BootstrapTest
, PRE_CleanUpFailedUser
) {
147 ScopedCompleteCallbackForTesting
scoped_bootstrap_initialized(base::Bind(
148 &BootstrapTest::OnBootstrapInitialized
, base::Unretained(this)));
150 SetTestEasyUnlockAppPath("login/easy_unlock/auto_pair_failure");
154 // Failed bootstrap should attempt exit and get out of this loop.
155 content::RunMessageLoop();
158 IN_PROC_BROWSER_TEST_F(BootstrapTest
, CleanUpFailedUser
) {
159 EXPECT_FALSE(user_manager::UserManager::Get()->IsKnownUser(kFakeUser
));
162 } // namespace chromeos