Home | History | Annotate | Download | only in login
      1 // Copyright 2013 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 "chrome/browser/chromeos/login/login_manager_test.h"
      6 
      7 #include "chrome/browser/browser_process.h"
      8 #include "chrome/browser/chrome_notification_types.h"
      9 #include "chrome/browser/chromeos/login/existing_user_controller.h"
     10 #include "chrome/browser/chromeos/login/user_manager.h"
     11 #include "chrome/browser/prefs/scoped_user_pref_update.h"
     12 #include "chrome/common/chrome_switches.h"
     13 #include "chromeos/chromeos_switches.h"
     14 #include "content/public/browser/notification_service.h"
     15 #include "content/public/test/test_utils.h"
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 
     18 namespace chromeos {
     19 
     20 LoginManagerTest::LoginManagerTest(bool should_launch_browser)
     21     : should_launch_browser_(should_launch_browser) {
     22   set_exit_when_last_browser_closes(false);
     23 }
     24 
     25 void LoginManagerTest::SetUpCommandLine(CommandLine* command_line) {
     26   command_line->AppendSwitch(chromeos::switches::kLoginManager);
     27   command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
     28   command_line->AppendSwitch(::switches::kMultiProfiles);
     29 }
     30 
     31 void LoginManagerTest::SetUpInProcessBrowserTestFixture() {
     32   mock_login_utils_ = new testing::NiceMock<MockLoginUtils>();
     33   mock_login_utils_->DelegateToFake();
     34   mock_login_utils_->GetFakeLoginUtils()->set_should_launch_browser(
     35       should_launch_browser_);
     36   LoginUtils::Set(mock_login_utils_);
     37 }
     38 
     39 void LoginManagerTest::RegisterUser(const std::string& username) {
     40   ListPrefUpdate users_pref(g_browser_process->local_state(), "LoggedInUsers");
     41   users_pref->AppendIfNotPresent(new base::StringValue(username));
     42 }
     43 
     44 void LoginManagerTest::SetExpectedCredentials(const std::string& username,
     45                                               const std::string& password) {
     46   login_utils().GetFakeLoginUtils()->SetExpectedCredentials(username, password);
     47 }
     48 
     49 bool LoginManagerTest::TryToLogin(const std::string& username,
     50                                   const std::string& password) {
     51   ExistingUserController* controller =
     52       ExistingUserController::current_controller();
     53   EXPECT_TRUE(controller != NULL);
     54   controller->Login(UserContext(username, password, std::string()));
     55   content::WindowedNotificationObserver(
     56       chrome::NOTIFICATION_SESSION_STARTED,
     57       content::NotificationService::AllSources()).Wait();
     58   if (const User* active_user = UserManager::Get()->GetActiveUser())
     59     return active_user->email() == username;
     60   return false;
     61 }
     62 
     63 void LoginManagerTest::LoginUser(const std::string& username) {
     64   SetExpectedCredentials(username, "password");
     65   EXPECT_TRUE(TryToLogin(username, "password"));
     66 }
     67 
     68 }  // namespace chromeos
     69