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"
24 #include "testing/gtest/include/gtest/gtest.h"
30 const char kFakeGaiaId
[] = "123456";
31 const char kFakeUser
[] = "test_user@example.com";
32 const char kFakeSid
[] = "fake-sid";
33 const char kFakeLsid
[] = "fake-lsid";
34 const char kFakeRefreshToken
[] = "fake-refresh-token";
35 const char kFakeUserInfoToken
[] = "fake-user-info-token";
37 class ScopedCompleteCallbackForTesting
{
39 explicit ScopedCompleteCallbackForTesting(
40 const BootstrapUserContextInitializer::CompleteCallback
& callback
);
41 ~ScopedCompleteCallbackForTesting();
44 BootstrapUserContextInitializer::CompleteCallback callback_
;
45 DISALLOW_COPY_AND_ASSIGN(ScopedCompleteCallbackForTesting
);
48 ScopedCompleteCallbackForTesting::ScopedCompleteCallbackForTesting(
49 const BootstrapUserContextInitializer::CompleteCallback
& callback
)
50 : callback_(callback
) {
51 BootstrapUserContextInitializer::SetCompleteCallbackForTesting(&callback_
);
54 ScopedCompleteCallbackForTesting::~ScopedCompleteCallbackForTesting() {
55 BootstrapUserContextInitializer::SetCompleteCallbackForTesting(NULL
);
60 // Boolean parameter is used to run this test for webview (true) and for
61 // iframe (false) GAIA sign in.
62 class BootstrapTest
: public OobeBaseTest
,
63 public testing::WithParamInterface
<bool> {
65 BootstrapTest() { set_use_webview(GetParam()); }
67 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
68 OobeBaseTest::SetUpCommandLine(command_line
);
70 base::CommandLine::ForCurrentProcess()->AppendSwitch(
71 proximity_auth::switches::kForceLoadEasyUnlockAppInTests
);
73 const GURL
& server_url
= embedded_test_server()->base_url();
74 GURL::Replacements replace_host
;
75 replace_host
.SetHostStr("eafe");
76 GURL eafe_url
= server_url
.ReplaceComponents(replace_host
);
78 command_line
->AppendSwitchASCII(switches::kEafeUrl
, eafe_url
.spec());
79 command_line
->AppendSwitchASCII(switches::kEafePath
,
80 "login/easy_unlock/auth_code_login.html");
83 void SetUpOnMainThread() override
{
84 OobeBaseTest::SetUpOnMainThread();
86 GaiaUrls
* gaia_urls
= GaiaUrls::GetInstance();
88 fake_gaia_
->SetFakeMergeSessionParams(kFakeUser
, kFakeSid
, kFakeLsid
);
89 fake_gaia_
->MapEmailToGaiaId(kFakeUser
, kFakeGaiaId
);
91 FakeGaia::AccessTokenInfo userinfo_token_info
;
92 userinfo_token_info
.token
= kFakeUserInfoToken
;
93 userinfo_token_info
.scopes
.insert(GaiaConstants::kGoogleUserInfoEmail
);
94 userinfo_token_info
.scopes
.insert(GaiaConstants::kGoogleUserInfoProfile
);
95 userinfo_token_info
.audience
= gaia_urls
->oauth2_chrome_client_id();
96 userinfo_token_info
.email
= kFakeUser
;
97 fake_gaia_
->IssueOAuthToken(kFakeRefreshToken
, userinfo_token_info
);
100 void SetExpectedCredentials(const UserContext
& user_context
) {
101 test::UserSessionManagerTestApi
session_manager_test_api(
102 UserSessionManager::GetInstance());
103 session_manager_test_api
.InjectStubUserContext(user_context
);
106 bool JsExecute(const std::string
& script
) {
107 return content::ExecuteScript(GetLoginUI()->GetWebContents(), script
);
110 void SetTestEasyUnlockAppPath(const std::string
& relative_path
) {
111 base::FilePath test_data_dir
;
112 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
));
113 EasyUnlockServiceFactory::GetInstance()->set_app_path_for_testing(
114 test_data_dir
.AppendASCII(relative_path
));
117 void StartSignInScreen() {
118 WizardController::SkipPostLoginScreensForTesting();
119 WizardController
* wizard_controller
=
120 WizardController::default_controller();
121 ASSERT_TRUE(wizard_controller
);
122 wizard_controller
->SkipToLoginForTesting(LoginScreenContext());
123 OobeScreenWaiter(OobeDisplay::SCREEN_GAIA_SIGNIN
).Wait();
126 void OnBootstrapInitialized(bool success
, const UserContext
& user_context
) {
127 EXPECT_TRUE(success
);
128 SetExpectedCredentials(user_context
);
133 JsExecute("cr.ui.Oobe.handleAccelerator('toggle_easy_bootstrap');");
137 IN_PROC_BROWSER_TEST_P(BootstrapTest
, Basic
) {
138 ScopedCompleteCallbackForTesting
scoped_bootstrap_initialized(base::Bind(
139 &BootstrapTest::OnBootstrapInitialized
, base::Unretained(this)));
141 SetTestEasyUnlockAppPath("login/easy_unlock/auto_pair_success");
145 content::WindowedNotificationObserver(
146 chrome::NOTIFICATION_SESSION_STARTED
,
147 content::NotificationService::AllSources()).Wait();
150 IN_PROC_BROWSER_TEST_P(BootstrapTest
, PRE_CleanUpFailedUser
) {
151 ScopedCompleteCallbackForTesting
scoped_bootstrap_initialized(base::Bind(
152 &BootstrapTest::OnBootstrapInitialized
, base::Unretained(this)));
154 SetTestEasyUnlockAppPath("login/easy_unlock/auto_pair_failure");
158 // Failed bootstrap should attempt exit and get out of this loop.
159 content::RunMessageLoop();
162 IN_PROC_BROWSER_TEST_P(BootstrapTest
, CleanUpFailedUser
) {
163 EXPECT_FALSE(user_manager::UserManager::Get()->IsKnownUser(kFakeUser
));
166 // TODO(nkostylev): Fix this test for webview. http://crbug.com/477402
167 INSTANTIATE_TEST_CASE_P(BootstrapTestSuite
,
169 testing::Values(false));
171 } // namespace chromeos