chromium/chrome/browser/ui/passwords/credential_manager_dialog_controller_impl_unittest.cc

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/passwords/credential_manager_dialog_controller_impl.h"

#include <memory>
#include <utility>
#include <vector>

#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/ui/passwords/password_dialog_prompts.h"
#include "chrome/browser/ui/passwords/passwords_model_delegate_mock.h"
#include "chrome/test/base/testing_profile.h"
#include "components/password_manager/core/browser/mock_password_feature_manager.h"
#include "components/password_manager/core/browser/password_bubble_experiment.h"
#include "components/password_manager/core/browser/password_form.h"
#include "components/password_manager/core/browser/password_manager_metrics_util.h"
#include "components/password_manager/core/common/password_manager_pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

ElementsAre;
Pointee;
StrictMock;

const char16_t kUsername[] =;
const char16_t kUsername2[] =;

class MockPasswordPrompt : public AccountChooserPrompt,
                           public AutoSigninFirstRunPrompt {};

password_manager::PasswordForm GetLocalForm() {}

class CredentialManagerDialogControllerTest : public testing::Test {};

TEST_F(CredentialManagerDialogControllerTest, ShowAccountChooser) {}

TEST_F(CredentialManagerDialogControllerTest, ShowAccountChooserAndSignIn) {}

TEST_F(CredentialManagerDialogControllerTest, AccountChooserClosed) {}

TEST_F(CredentialManagerDialogControllerTest, AutoSigninPromo) {}

TEST_F(CredentialManagerDialogControllerTest, AutoSigninPromoOkGotIt) {}

TEST_F(CredentialManagerDialogControllerTest, AutoSigninPromoTurnOff) {}

#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
TEST_F(CredentialManagerDialogControllerTest, SignInBiometricsEnabled) {
  StrictMock<MockPasswordPrompt> prompt;
  password_manager::PasswordForm local_form = GetLocalForm();
  std::vector<std::unique_ptr<password_manager::PasswordForm>> locals;
  locals.push_back(
      std::make_unique<password_manager::PasswordForm>(local_form));

  EXPECT_CALL(prompt, ShowAccountChooser);
  controller().ShowAccountChooser(&prompt, std::move(locals));

  EXPECT_CALL(feature_manager(), IsBiometricAuthenticationBeforeFillingEnabled)
      .WillOnce(testing::Return(true));
  EXPECT_CALL(ui_controller_mock(), AuthenticateUserWithMessage)
      .WillOnce(testing::WithArg<1>([](auto callback) {
        // Simulate successful authentication.
        std::move(callback).Run(true);
      }));
  EXPECT_CALL(prompt, ControllerGone);
  EXPECT_CALL(ui_controller_mock(),
              ChooseCredential(
                  local_form,
                  password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD));
  controller().OnSignInClicked();
}

TEST_F(CredentialManagerDialogControllerTest,
       SignInBiometricsEnabledButFailed) {
  StrictMock<MockPasswordPrompt> prompt;
  password_manager::PasswordForm local_form = GetLocalForm();
  std::vector<std::unique_ptr<password_manager::PasswordForm>> locals;
  locals.push_back(
      std::make_unique<password_manager::PasswordForm>(local_form));

  EXPECT_CALL(prompt, ShowAccountChooser);
  controller().ShowAccountChooser(&prompt, std::move(locals));

  EXPECT_CALL(feature_manager(), IsBiometricAuthenticationBeforeFillingEnabled)
      .WillOnce(testing::Return(true));
  EXPECT_CALL(ui_controller_mock(), AuthenticateUserWithMessage)
      .WillOnce(testing::WithArg<1>([](auto callback) {
        // Simulate failed authentication.
        std::move(callback).Run(false);
      }));
  EXPECT_CALL(prompt, ControllerGone).Times(0);
  EXPECT_CALL(ui_controller_mock(), ChooseCredential).Times(0);
  controller().OnSignInClicked();

  testing::Mock::VerifyAndClearExpectations(&prompt);
  testing::Mock::VerifyAndClearExpectations(&ui_controller_mock());

  controller().OnCloseDialog();
}

TEST_F(CredentialManagerDialogControllerTest,
       OnChooseCredentialsBiometricsEnabled) {
  StrictMock<MockPasswordPrompt> prompt;

  EXPECT_CALL(prompt, ShowAccountChooser);
  std::vector<std::unique_ptr<password_manager::PasswordForm>> locals;
  locals.push_back(
      std::make_unique<password_manager::PasswordForm>(GetLocalForm()));
  locals.push_back(
      std::make_unique<password_manager::PasswordForm>(GetLocalForm()));
  controller().ShowAccountChooser(&prompt, std::move(locals));

  EXPECT_CALL(feature_manager(), IsBiometricAuthenticationBeforeFillingEnabled)
      .WillOnce(testing::Return(true));
  EXPECT_CALL(ui_controller_mock(), AuthenticateUserWithMessage)
      .WillOnce(testing::WithArg<1>([](auto callback) {
        // Simulate successful authentication.
        std::move(callback).Run(true);
      }));
  EXPECT_CALL(prompt, ControllerGone);
  EXPECT_CALL(ui_controller_mock(),
              ChooseCredential(
                  GetLocalForm(),
                  password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD));
  controller().OnChooseCredentials(
      GetLocalForm(),
      password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD);
}

TEST_F(CredentialManagerDialogControllerTest,
       OnChooseCredentialsBiometricsEnabledButFailed) {
  StrictMock<MockPasswordPrompt> prompt;

  EXPECT_CALL(prompt, ShowAccountChooser);
  std::vector<std::unique_ptr<password_manager::PasswordForm>> locals;
  locals.push_back(
      std::make_unique<password_manager::PasswordForm>(GetLocalForm()));
  locals.push_back(
      std::make_unique<password_manager::PasswordForm>(GetLocalForm()));
  controller().ShowAccountChooser(&prompt, std::move(locals));

  EXPECT_CALL(feature_manager(), IsBiometricAuthenticationBeforeFillingEnabled)
      .WillOnce(testing::Return(true));
  EXPECT_CALL(ui_controller_mock(), AuthenticateUserWithMessage)
      .WillOnce(testing::WithArg<1>([](auto callback) {
        // Simulate failed authentication.
        std::move(callback).Run(false);
      }));
  EXPECT_CALL(prompt, ControllerGone).Times(0);
  EXPECT_CALL(ui_controller_mock(), ChooseCredential).Times(0);
  controller().OnChooseCredentials(
      GetLocalForm(),
      password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD);

  testing::Mock::VerifyAndClearExpectations(&prompt);
  testing::Mock::VerifyAndClearExpectations(&ui_controller_mock());

  controller().OnCloseDialog();
}
#endif

}  // namespace