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