login_browsertest.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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 "ash/shell.h" 6#include "base/command_line.h" 7#include "chrome/browser/chrome_browser_main.h" 8#include "chrome/browser/chrome_browser_main_extra_parts.h" 9#include "chrome/browser/chrome_content_browser_client.h" 10#include "chrome/browser/chrome_notification_types.h" 11#include "chrome/browser/chromeos/cros/cros_in_process_browser_test.h" 12#include "chrome/browser/chromeos/login/login_display_host_impl.h" 13#include "chrome/browser/chromeos/login/login_wizard.h" 14#include "chrome/browser/chromeos/login/wizard_controller.h" 15#include "chrome/browser/extensions/extension_system.h" 16#include "chrome/browser/profiles/profile_manager.h" 17#include "chrome/browser/ui/browser.h" 18#include "chrome/common/chrome_switches.h" 19#include "chrome/test/base/in_process_browser_test.h" 20#include "chrome/test/base/interactive_test_utils.h" 21#include "chrome/test/base/ui_test_utils.h" 22#include "chromeos/chromeos_switches.h" 23#include "chromeos/cryptohome/mock_cryptohome_library.h" 24#include "content/public/browser/notification_observer.h" 25#include "content/public/browser/notification_registrar.h" 26#include "content/public/browser/notification_service.h" 27#include "content/public/test/test_utils.h" 28#include "testing/gmock/include/gmock/gmock.h" 29#include "testing/gtest/include/gtest/gtest.h" 30 31using ::testing::_; 32using ::testing::AnyNumber; 33using ::testing::Return; 34 35namespace { 36 37class LoginTestBase : public chromeos::CrosInProcessBrowserTest { 38 public: 39 LoginTestBase() {} 40 41 protected: 42 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { 43 CrosInProcessBrowserTest::SetUpInProcessBrowserTestFixture(); 44 45 mock_cryptohome_library_.reset(new chromeos::MockCryptohomeLibrary()); 46 EXPECT_CALL(*(mock_cryptohome_library_.get()), GetSystemSalt()) 47 .WillRepeatedly(Return(std::string("stub_system_salt"))); 48 EXPECT_CALL(*(mock_cryptohome_library_.get()), InstallAttributesIsReady()) 49 .WillRepeatedly(Return(false)); 50 } 51 52 scoped_ptr<chromeos::MockCryptohomeLibrary> mock_cryptohome_library_; 53 54 private: 55 DISALLOW_COPY_AND_ASSIGN(LoginTestBase); 56}; 57 58class LoginUserTest : public LoginTestBase { 59 protected: 60 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { 61 LoginTestBase::SetUpInProcessBrowserTestFixture(); 62 } 63 64 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 65 command_line->AppendSwitchASCII( 66 chromeos::switches::kLoginUser, "TestUser@gmail.com"); 67 command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user"); 68 command_line->AppendSwitch(::switches::kNoFirstRun); 69 } 70}; 71 72class LoginGuestTest : public LoginTestBase { 73 protected: 74 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 75 command_line->AppendSwitch(chromeos::switches::kGuestSession); 76 command_line->AppendSwitch(::switches::kIncognito); 77 command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user"); 78 command_line->AppendSwitch(::switches::kNoFirstRun); 79 } 80}; 81 82class LoginCursorTest : public LoginTestBase { 83 protected: 84 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 85 command_line->AppendSwitch(chromeos::switches::kLoginManager); 86 } 87}; 88 89// Used to add an observer to NotificationService after it's created. 90class TestBrowserMainExtraParts 91 : public ChromeBrowserMainExtraParts, 92 public content::NotificationObserver { 93 public: 94 TestBrowserMainExtraParts() {} 95 virtual ~TestBrowserMainExtraParts() {} 96 97 // ChromeBrowserMainExtraParts implementation. 98 virtual void PreEarlyInitialization() OVERRIDE { 99 registrar_.Add(this, chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE, 100 content::NotificationService::AllSources()); 101 } 102 103 void set_quit_task(const base::Closure& quit_task) { quit_task_ = quit_task; } 104 105 private: 106 // Overridden from content::NotificationObserver: 107 virtual void Observe(int type, 108 const content::NotificationSource& source, 109 const content::NotificationDetails& details) OVERRIDE { 110 quit_task_.Run(); 111 } 112 113 content::NotificationRegistrar registrar_; 114 base::Closure quit_task_; 115 116 DISALLOW_COPY_AND_ASSIGN(TestBrowserMainExtraParts); 117}; 118 119class TestContentBrowserClient : public chrome::ChromeContentBrowserClient { 120 public: 121 TestContentBrowserClient() {} 122 virtual ~TestContentBrowserClient() {} 123 124 virtual content::BrowserMainParts* CreateBrowserMainParts( 125 const content::MainFunctionParams& parameters) OVERRIDE { 126 ChromeBrowserMainParts* main_parts = static_cast<ChromeBrowserMainParts*>( 127 ChromeContentBrowserClient::CreateBrowserMainParts(parameters)); 128 129 browser_main_extra_parts_ = new TestBrowserMainExtraParts(); 130 main_parts->AddParts(browser_main_extra_parts_); 131 return main_parts; 132 } 133 134 TestBrowserMainExtraParts* browser_main_extra_parts_; 135 136 private: 137 DISALLOW_COPY_AND_ASSIGN(TestContentBrowserClient); 138}; 139 140 141class LoginSigninTest : public chromeos::CrosInProcessBrowserTest { 142 protected: 143 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 144 command_line->AppendSwitch(chromeos::switches::kLoginManager); 145 command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests); 146 } 147 148 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { 149 content_browser_client_.reset(new TestContentBrowserClient()); 150 original_content_browser_client_ = content::SetBrowserClientForTesting( 151 content_browser_client_.get()); 152 } 153 154 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE { 155 content::SetBrowserClientForTesting(original_content_browser_client_); 156 } 157 158 scoped_ptr<TestContentBrowserClient> content_browser_client_; 159 content::ContentBrowserClient* original_content_browser_client_; 160}; 161 162// After a chrome crash, the session manager will restart chrome with 163// the -login-user flag indicating that the user is already logged in. 164// This profile should NOT be an OTR profile. 165IN_PROC_BROWSER_TEST_F(LoginUserTest, UserPassed) { 166 Profile* profile = browser()->profile(); 167 EXPECT_EQ("user", profile->GetPath().BaseName().value()); 168 EXPECT_FALSE(profile->IsOffTheRecord()); 169} 170 171// Verifies the cursor is not hidden at startup when user is logged in. 172IN_PROC_BROWSER_TEST_F(LoginUserTest, CursorShown) { 173 EXPECT_TRUE(ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible()); 174} 175 176// After a guest login, we should get the OTR default profile. 177IN_PROC_BROWSER_TEST_F(LoginGuestTest, GuestIsOTR) { 178 Profile* profile = browser()->profile(); 179 EXPECT_EQ("Default", profile->GetPath().BaseName().value()); 180 EXPECT_TRUE(profile->IsOffTheRecord()); 181 // Ensure there's extension service for this profile. 182 EXPECT_TRUE(extensions::ExtensionSystem::Get(profile)->extension_service()); 183} 184 185// Verifies the cursor is not hidden at startup when running guest session. 186IN_PROC_BROWSER_TEST_F(LoginGuestTest, CursorShown) { 187 EXPECT_TRUE(ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible()); 188} 189 190// Verifies the cursor is hidden at startup on login screen. 191IN_PROC_BROWSER_TEST_F(LoginCursorTest, CursorHidden) { 192 // Login screen needs to be shown explicitly when running test. 193 chromeos::ShowLoginWizard(chromeos::WizardController::kLoginScreenName); 194 195 // Cursor should be hidden at startup 196 EXPECT_FALSE(ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible()); 197 198 // Cursor should be shown after cursor is moved. 199 EXPECT_TRUE(ui_test_utils::SendMouseMoveSync(gfx::Point())); 200 EXPECT_TRUE(ash::Shell::GetInstance()->cursor_manager()->IsCursorVisible()); 201 202 base::MessageLoop::current()->DeleteSoon( 203 FROM_HERE, chromeos::LoginDisplayHostImpl::default_host()); 204} 205 206// Verifies that the webui for login comes up successfully. 207IN_PROC_BROWSER_TEST_F(LoginSigninTest, WebUIVisible) { 208 scoped_refptr<content::MessageLoopRunner> runner = 209 new content::MessageLoopRunner; 210 content_browser_client_->browser_main_extra_parts_->set_quit_task( 211 runner->QuitClosure()); 212 runner->Run(); 213} 214 215} // namespace 216