Home | History | Annotate | Download | only in login
      1 // Copyright (c) 2011 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 "base/memory/scoped_ptr.h"
      6 #include "base/message_loop.h"
      7 #include "chrome/browser/chromeos/cros/cros_mock.h"
      8 #include "chrome/browser/chromeos/cros/cros_in_process_browser_test.h"
      9 #include "chrome/browser/chromeos/cros/mock_cryptohome_library.h"
     10 #include "chrome/browser/chromeos/cros/mock_login_library.h"
     11 #include "chrome/browser/chromeos/cros/mock_network_library.h"
     12 #include "chrome/browser/chromeos/login/existing_user_controller.h"
     13 #include "chrome/browser/chromeos/login/helper.h"
     14 #include "chrome/browser/chromeos/login/login_display.h"
     15 #include "chrome/browser/chromeos/login/login_display_host.h"
     16 #include "chrome/browser/chromeos/login/login_performer.h"
     17 #include "chrome/browser/chromeos/login/login_utils.h"
     18 #include "chrome/browser/chromeos/login/mock_authenticator.h"
     19 #include "chrome/browser/chromeos/login/wizard_controller.h"
     20 #include "grit/generated_resources.h"
     21 #include "testing/gmock/include/gmock/gmock.h"
     22 #include "testing/gtest/include/gtest/gtest.h"
     23 #include "ui/base/l10n/l10n_util.h"
     24 
     25 namespace chromeos {
     26 
     27 using ::testing::AnyNumber;
     28 using ::testing::InvokeWithoutArgs;
     29 using ::testing::Return;
     30 using ::testing::ReturnNull;
     31 
     32 const char kUsername[] = "test_user (at) gmail.com";
     33 const char kPassword[] = "test_password";
     34 
     35 class MockLoginDisplay : public LoginDisplay {
     36  public:
     37   MockLoginDisplay()
     38       : LoginDisplay(NULL, gfx::Rect()) {
     39   }
     40 
     41   MOCK_METHOD3(Init, void(const UserVector&, bool, bool));
     42   MOCK_METHOD1(OnUserImageChanged, void(UserManager::User*));
     43   MOCK_METHOD0(OnFadeOut, void(void));
     44   MOCK_METHOD1(SetUIEnabled, void(bool));
     45   MOCK_METHOD3(ShowError, void(int, int, HelpAppLauncher::HelpTopic));
     46   MOCK_METHOD1(OnBeforeUserRemoved, void(const std::string&));
     47   MOCK_METHOD1(OnUserRemoved, void(const std::string&));
     48 
     49  private:
     50   DISALLOW_COPY_AND_ASSIGN(MockLoginDisplay);
     51 };
     52 
     53 class MockLoginDisplayHost : public LoginDisplayHost {
     54  public:
     55   MockLoginDisplayHost() {
     56   }
     57 
     58   MOCK_CONST_METHOD1(CreateLoginDisplay,
     59                      LoginDisplay*(LoginDisplay::Delegate*));
     60   MOCK_CONST_METHOD0(GetNativeWindow, gfx::NativeWindow(void));
     61   MOCK_METHOD0(OnSessionStart, void(void));
     62   MOCK_METHOD1(SetOobeProgress, void(BackgroundView::LoginStep));
     63   MOCK_METHOD1(SetOobeProgressBarVisible, void(bool));
     64   MOCK_METHOD1(SetShutdownButtonEnabled, void(bool));
     65   MOCK_METHOD1(SetStatusAreaEnabled, void(bool));
     66   MOCK_METHOD1(SetStatusAreaVisible, void(bool));
     67   MOCK_METHOD0(ShowBackground, void(void));
     68   MOCK_METHOD2(StartWizard, void(const std::string&,
     69                                  const GURL&));
     70   MOCK_METHOD0(StartSignInScreen, void(void));
     71 
     72  private:
     73  DISALLOW_COPY_AND_ASSIGN(MockLoginDisplayHost);
     74 };
     75 
     76 class MockLoginPerformerDelegate : public LoginPerformer::Delegate {
     77  public:
     78   explicit MockLoginPerformerDelegate(ExistingUserController* controller)
     79       : controller_(controller) {
     80   }
     81 
     82   void OnLoginSuccess(const std::string&,
     83                       const std::string&,
     84                       const GaiaAuthConsumer::ClientLoginResult&,
     85                       bool) {
     86     WizardController::MarkDeviceRegistered();
     87     LoginPerformer* login_performer = controller_->login_performer_.release();
     88     login_performer = NULL;
     89     controller_->OnProfilePrepared(NULL);
     90   }
     91 
     92   MOCK_METHOD1(OnLoginFailure, void(const LoginFailure&));
     93   MOCK_METHOD1(WhiteListCheckFailed, void(const std::string&));
     94 
     95  private:
     96   ExistingUserController* controller_;
     97 
     98   DISALLOW_COPY_AND_ASSIGN(MockLoginPerformerDelegate);
     99 };
    100 
    101 class ExistingUserControllerTest : public CrosInProcessBrowserTest {
    102  protected:
    103   ExistingUserControllerTest()
    104       : mock_cryptohome_library_(NULL),
    105         mock_login_library_(NULL),
    106         mock_network_library_(NULL),
    107         mock_login_display_(NULL),
    108         mock_login_display_host_(NULL) {
    109   }
    110 
    111   ExistingUserController* existing_user_controller() {
    112     return ExistingUserController::current_controller();
    113   }
    114 
    115   virtual void SetUpInProcessBrowserTestFixture() {
    116     CrosInProcessBrowserTest::SetUpInProcessBrowserTestFixture();
    117     cros_mock_->InitStatusAreaMocks();
    118     cros_mock_->SetStatusAreaMocksExpectations();
    119 
    120     mock_network_library_ = cros_mock_->mock_network_library();
    121     mock_login_library_ = new MockLoginLibrary();
    122     EXPECT_CALL(*mock_login_library_, EmitLoginPromptReady())
    123         .Times(1);
    124     EXPECT_CALL(*mock_login_library_, RequestRetrieveProperty(_, _, _))
    125         .Times(AnyNumber());
    126     cros_mock_->test_api()->SetLoginLibrary(mock_login_library_, true);
    127 
    128     cros_mock_->InitMockCryptohomeLibrary();
    129     mock_cryptohome_library_ = cros_mock_->mock_cryptohome_library();
    130     EXPECT_CALL(*mock_cryptohome_library_, IsMounted())
    131         .Times(AnyNumber())
    132         .WillRepeatedly(Return(true));
    133     EXPECT_CALL(*mock_cryptohome_library_,
    134                 AsyncDoAutomaticFreeDiskSpaceControl(_))
    135         .Times(1)
    136         .WillOnce(Return(true));
    137     LoginUtils::Set(new MockLoginUtils(kUsername, kPassword));
    138 
    139     mock_login_display_.reset(new MockLoginDisplay());
    140     mock_login_display_host_.reset(new MockLoginDisplayHost());
    141 
    142     EXPECT_CALL(*mock_login_display_host_.get(), CreateLoginDisplay(_))
    143         .Times(1)
    144         .WillOnce(Return(mock_login_display_.get()));
    145     EXPECT_CALL(*mock_login_display_host_.get(), GetNativeWindow())
    146         .Times(1)
    147         .WillOnce(ReturnNull());
    148     EXPECT_CALL(*mock_login_display_.get(), Init(_, false, true))
    149         .Times(1);
    150   }
    151 
    152   virtual void SetUpOnMainThread() {
    153     ExistingUserController* controller =
    154         new ExistingUserController(mock_login_display_host_.get());
    155     controller->Init(UserVector());
    156     MockLoginPerformerDelegate* mock_delegate =
    157           new MockLoginPerformerDelegate(controller);
    158     existing_user_controller()->set_login_performer_delegate(mock_delegate);
    159   }
    160 
    161   virtual void TearDownInProcessBrowserTestFixture() {
    162     CrosInProcessBrowserTest::TearDownInProcessBrowserTestFixture();
    163     cros_mock_->test_api()->SetLoginLibrary(NULL, false);
    164   }
    165 
    166   // These mocks are owned by CrosLibrary class.
    167   MockCryptohomeLibrary* mock_cryptohome_library_;
    168   MockLoginLibrary* mock_login_library_;
    169   MockNetworkLibrary* mock_network_library_;
    170 
    171   scoped_ptr<MockLoginDisplay> mock_login_display_;
    172   scoped_ptr<MockLoginDisplayHost> mock_login_display_host_;
    173 
    174  private:
    175   DISALLOW_COPY_AND_ASSIGN(ExistingUserControllerTest);
    176 };
    177 
    178 IN_PROC_BROWSER_TEST_F(ExistingUserControllerTest, NewUserLogin) {
    179   EXPECT_CALL(*mock_login_display_host_, SetStatusAreaEnabled(false))
    180       .Times(1);
    181   EXPECT_CALL(*mock_login_display_, SetUIEnabled(false))
    182       .Times(1);
    183   EXPECT_CALL(*mock_login_display_host_,
    184               StartWizard(WizardController::kUserImageScreenName,
    185                           GURL()))
    186       .Times(1);
    187   EXPECT_CALL(*mock_login_display_, OnFadeOut())
    188       .Times(1);
    189   existing_user_controller()->Login(kUsername, kPassword);
    190 }
    191 
    192 }  // namespace chromeos
    193