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 "base/prefs/scoped_user_pref_update.h"
      8 #include "chrome/browser/browser_process.h"
      9 #include "chrome/browser/chrome_notification_types.h"
     10 #include "chrome/browser/chromeos/login/auth/key.h"
     11 #include "chrome/browser/chromeos/login/auth/user_context.h"
     12 #include "chrome/browser/chromeos/login/existing_user_controller.h"
     13 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
     14 #include "chrome/browser/chromeos/login/ui/webui_login_view.h"
     15 #include "chrome/browser/chromeos/login/users/user.h"
     16 #include "chrome/browser/chromeos/login/users/user_manager.h"
     17 #include "chromeos/chromeos_switches.h"
     18 #include "content/public/browser/notification_service.h"
     19 #include "content/public/browser/web_contents.h"
     20 #include "content/public/test/browser_test_utils.h"
     21 #include "content/public/test/test_utils.h"
     22 #include "testing/gtest/include/gtest/gtest.h"
     23 
     24 namespace chromeos {
     25 
     26 LoginManagerTest::LoginManagerTest(bool should_launch_browser)
     27     : should_launch_browser_(should_launch_browser),
     28       web_contents_(NULL) {
     29   set_exit_when_last_browser_closes(false);
     30 }
     31 
     32 void LoginManagerTest::CleanUpOnMainThread() {
     33   if (LoginDisplayHostImpl::default_host())
     34     LoginDisplayHostImpl::default_host()->Finalize();
     35   base::MessageLoop::current()->RunUntilIdle();
     36 }
     37 
     38 void LoginManagerTest::SetUpCommandLine(CommandLine* command_line) {
     39   command_line->AppendSwitch(chromeos::switches::kLoginManager);
     40   command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
     41 }
     42 
     43 void LoginManagerTest::SetUpInProcessBrowserTestFixture() {
     44   mock_login_utils_ = new testing::NiceMock<MockLoginUtils>();
     45   mock_login_utils_->DelegateToFake();
     46   mock_login_utils_->GetFakeLoginUtils()->set_should_launch_browser(
     47       should_launch_browser_);
     48   LoginUtils::Set(mock_login_utils_);
     49 }
     50 
     51 void LoginManagerTest::SetUpOnMainThread() {
     52   content::WindowedNotificationObserver(
     53       chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
     54       content::NotificationService::AllSources()).Wait();
     55   InitializeWebContents();
     56 }
     57 
     58 void LoginManagerTest::RegisterUser(const std::string& user_id) {
     59   ListPrefUpdate users_pref(g_browser_process->local_state(), "LoggedInUsers");
     60   users_pref->AppendIfNotPresent(new base::StringValue(user_id));
     61 }
     62 
     63 void LoginManagerTest::SetExpectedCredentials(const UserContext& user_context) {
     64   login_utils().GetFakeLoginUtils()->SetExpectedCredentials(user_context);
     65 }
     66 
     67 bool LoginManagerTest::TryToLogin(const UserContext& user_context) {
     68   if (!AddUserToSession(user_context))
     69     return false;
     70   if (const User* active_user = UserManager::Get()->GetActiveUser())
     71     return active_user->email() == user_context.GetUserID();
     72   return false;
     73 }
     74 
     75 bool LoginManagerTest::AddUserToSession(const UserContext& user_context) {
     76   ExistingUserController* controller =
     77       ExistingUserController::current_controller();
     78   if (!controller) {
     79     ADD_FAILURE();
     80     return false;
     81   }
     82   controller->Login(user_context);
     83   content::WindowedNotificationObserver(
     84       chrome::NOTIFICATION_SESSION_STARTED,
     85       content::NotificationService::AllSources()).Wait();
     86   const UserList& logged_users = UserManager::Get()->GetLoggedInUsers();
     87   for (UserList::const_iterator it = logged_users.begin();
     88        it != logged_users.end(); ++it) {
     89     if ((*it)->email() == user_context.GetUserID())
     90       return true;
     91   }
     92   return false;
     93 }
     94 
     95 void LoginManagerTest::LoginUser(const std::string& user_id) {
     96   UserContext user_context(user_id);
     97   user_context.SetKey(Key("password"));
     98   SetExpectedCredentials(user_context);
     99   EXPECT_TRUE(TryToLogin(user_context));
    100 }
    101 
    102 void LoginManagerTest::AddUser(const std::string& user_id) {
    103   UserContext user_context(user_id);
    104   user_context.SetKey(Key("password"));
    105   SetExpectedCredentials(user_context);
    106   EXPECT_TRUE(AddUserToSession(user_context));
    107 }
    108 
    109 void LoginManagerTest::JSExpect(const std::string& expression) {
    110   js_checker_.ExpectTrue(expression);
    111 }
    112 
    113 void LoginManagerTest::InitializeWebContents() {
    114     LoginDisplayHost* host = LoginDisplayHostImpl::default_host();
    115     EXPECT_TRUE(host != NULL);
    116 
    117     content::WebContents* web_contents =
    118         host->GetWebUILoginView()->GetWebContents();
    119     EXPECT_TRUE(web_contents != NULL);
    120     set_web_contents(web_contents);
    121     js_checker_.set_web_contents(web_contents);
    122   }
    123 
    124 }  // namespace chromeos
    125