1 // Copyright (c) 2012 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.
8 #include "base/command_line.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/run_loop.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/chromeos/login/existing_user_controller.h"
14 #include "chrome/browser/chromeos/login/ui/webui_login_display.h"
15 #include "chrome/browser/chromeos/login/wizard_controller.h"
16 #include "chrome/browser/rlz/rlz.h"
17 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/test/base/in_process_browser_test.h"
21 #include "chromeos/chromeos_switches.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/test/test_utils.h"
24 #include "google_apis/gaia/fake_gaia.h"
25 #include "google_apis/gaia/gaia_switches.h"
26 #include "google_apis/gaia/gaia_urls.h"
27 #include "net/test/embedded_test_server/embedded_test_server.h"
28 #include "net/test/embedded_test_server/http_response.h"
29 #include "testing/gtest/include/gtest/gtest.h"
35 #if defined(ENABLE_RLZ)
36 void GetAccessPointRlzInBackgroundThread(rlz_lib::AccessPoint point
,
37 base::string16
* rlz
) {
38 ASSERT_FALSE(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
39 ASSERT_TRUE(RLZTracker::GetAccessPointRlz(point
, rlz
));
45 class LoginUtilsTest
: public InProcessBrowserTest
{
49 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
50 // Initialize the test server early, so that we can use its base url for
51 // the command line flags.
52 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
54 // Use the login manager screens and the gaia auth extension.
55 command_line
->AppendSwitch(switches::kLoginManager
);
56 command_line
->AppendSwitch(switches::kForceLoginManagerInTests
);
57 command_line
->AppendSwitchASCII(switches::kLoginProfile
, "user");
58 command_line
->AppendSwitchASCII(::switches::kAuthExtensionPath
,
61 // Redirect requests to gaia and the policy server to the test server.
62 command_line
->AppendSwitchASCII(::switches::kGaiaUrl
,
63 embedded_test_server()->base_url().spec());
64 command_line
->AppendSwitchASCII(::switches::kLsoUrl
,
65 embedded_test_server()->base_url().spec());
68 void SetUpOnMainThread() override
{
69 fake_gaia_
.Initialize();
70 embedded_test_server()->RegisterRequestHandler(
71 base::Bind(&FakeGaia::HandleRequest
, base::Unretained(&fake_gaia_
)));
74 void TearDownOnMainThread() override
{
76 EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
80 base::RunLoop().RunUntilIdle();
83 PrefService
* local_state() {
84 return g_browser_process
->local_state();
87 void Login(const std::string
& username
) {
88 content::WindowedNotificationObserver
session_started_observer(
89 chrome::NOTIFICATION_SESSION_STARTED
,
90 content::NotificationService::AllSources());
92 ExistingUserController
* controller
=
93 ExistingUserController::current_controller();
94 ASSERT_TRUE(controller
);
95 WebUILoginDisplay
* login_display
=
96 static_cast<WebUILoginDisplay
*>(controller
->login_display());
97 ASSERT_TRUE(login_display
);
99 login_display
->ShowSigninScreenForCreds(username
, "password");
101 // Wait for the session to start after submitting the credentials. This
102 // will wait until all the background requests are done.
103 session_started_observer
.Wait();
109 DISALLOW_COPY_AND_ASSIGN(LoginUtilsTest
);
112 #if defined(ENABLE_RLZ)
113 IN_PROC_BROWSER_TEST_F(LoginUtilsTest
, RlzInitialized
) {
114 // Skip to the signin screen.
115 WizardController::SkipPostLoginScreensForTesting();
116 WizardController
* wizard_controller
= WizardController::default_controller();
117 ASSERT_TRUE(wizard_controller
);
118 wizard_controller
->SkipToLoginForTesting(LoginScreenContext());
120 content::WindowedNotificationObserver(
121 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
,
122 content::NotificationService::AllSources()).Wait();
125 // No RLZ brand code set initially.
126 EXPECT_FALSE(local_state()->HasPrefPath(prefs::kRLZBrand
));
128 // Wait for blocking RLZ tasks to complete.
131 PrefChangeRegistrar registrar
;
132 registrar
.Init(local_state());
133 registrar
.Add(prefs::kRLZBrand
, loop
.QuitClosure());
138 // RLZ brand code has been set to empty string.
139 EXPECT_TRUE(local_state()->HasPrefPath(prefs::kRLZBrand
));
140 EXPECT_EQ(std::string(), local_state()->GetString(prefs::kRLZBrand
));
142 // RLZ value for homepage access point should have been initialized.
143 // This value must be obtained in a background thread.
146 base::string16 rlz_string
;
147 content::BrowserThread::PostBlockingPoolTaskAndReply(
149 base::Bind(&GetAccessPointRlzInBackgroundThread
,
150 RLZTracker::ChromeHomePage(),
154 EXPECT_EQ(base::string16(), rlz_string
);
159 } // namespace chromeos