login_utils_browsertest.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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. 4 5#include <string> 6 7#include "base/bind.h" 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" 30 31namespace chromeos { 32 33namespace { 34 35#if defined(ENABLE_RLZ) 36void 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)); 40} 41#endif 42 43} // namespace 44 45class LoginUtilsTest : public InProcessBrowserTest { 46 public: 47 LoginUtilsTest() {} 48 49 virtual void SetUpCommandLine(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()); 53 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, 59 "gaia_auth"); 60 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()); 66 } 67 68 virtual void SetUpOnMainThread() OVERRIDE { 69 fake_gaia_.Initialize(); 70 embedded_test_server()->RegisterRequestHandler( 71 base::Bind(&FakeGaia::HandleRequest, base::Unretained(&fake_gaia_))); 72 } 73 74 virtual void TearDownOnMainThread() OVERRIDE { 75 RunUntilIdle(); 76 EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete()); 77 } 78 79 void RunUntilIdle() { 80 base::RunLoop().RunUntilIdle(); 81 } 82 83 PrefService* local_state() { 84 return g_browser_process->local_state(); 85 } 86 87 void Login(const std::string& username) { 88 content::WindowedNotificationObserver session_started_observer( 89 chrome::NOTIFICATION_SESSION_STARTED, 90 content::NotificationService::AllSources()); 91 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); 98 99 login_display->ShowSigninScreenForCreds(username, "password"); 100 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(); 104 } 105 106 private: 107 FakeGaia fake_gaia_; 108 109 DISALLOW_COPY_AND_ASSIGN(LoginUtilsTest); 110}; 111 112#if defined(ENABLE_RLZ) 113IN_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()); 119 120 content::WindowedNotificationObserver( 121 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, 122 content::NotificationService::AllSources()).Wait(); 123 RunUntilIdle(); 124 125 // No RLZ brand code set initially. 126 EXPECT_FALSE(local_state()->HasPrefPath(prefs::kRLZBrand)); 127 128 // Wait for blocking RLZ tasks to complete. 129 { 130 base::RunLoop loop; 131 PrefChangeRegistrar registrar; 132 registrar.Init(local_state()); 133 registrar.Add(prefs::kRLZBrand, loop.QuitClosure()); 134 Login("username"); 135 loop.Run(); 136 } 137 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)); 141 142 // RLZ value for homepage access point should have been initialized. 143 // This value must be obtained in a background thread. 144 { 145 base::RunLoop loop; 146 base::string16 rlz_string; 147 content::BrowserThread::PostBlockingPoolTaskAndReply( 148 FROM_HERE, 149 base::Bind(&GetAccessPointRlzInBackgroundThread, 150 RLZTracker::ChromeHomePage(), 151 &rlz_string), 152 loop.QuitClosure()); 153 loop.Run(); 154 EXPECT_EQ(base::string16(), rlz_string); 155 } 156} 157#endif 158 159} // namespace chromeos 160