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