Commit b1d57f8b authored by Victor Hugo Vianna Silva's avatar Victor Hugo Vianna Silva Committed by Commit Bot

Introduce PasswordBubbleViewTestBase

Move some of the logic in PasswordSaveUpdateWithAccountStoreViewTest to
a dedicated class so that it can be used by other bubble unit tests.

Change-Id: Ib1a1eb5f769ccfdf6abff8d8b31d374a8dd36438
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2129530
Commit-Queue: Victor Vianna <victorvianna@google.com>
Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#755412}
parent 62e7e987
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/passwords/password_bubble_view_test_base.h"
#include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::ReturnRef;
class TestManagePasswordsUIController : public ManagePasswordsUIController {
public:
explicit TestManagePasswordsUIController(
content::WebContents* web_contents,
base::WeakPtr<PasswordsModelDelegate> model_delegate);
base::WeakPtr<PasswordsModelDelegate> GetModelDelegateProxy() override {
return model_delegate_;
}
private:
base::WeakPtr<PasswordsModelDelegate> model_delegate_;
};
TestManagePasswordsUIController::TestManagePasswordsUIController(
content::WebContents* web_contents,
base::WeakPtr<PasswordsModelDelegate> model_delegate)
: ManagePasswordsUIController(web_contents),
model_delegate_(std::move(model_delegate)) {
DCHECK(model_delegate_);
// Do not silently replace an existing ManagePasswordsUIController
// because it unregisters itself in WebContentsDestroyed().
EXPECT_FALSE(web_contents->GetUserData(UserDataKey()));
web_contents->SetUserData(UserDataKey(), base::WrapUnique(this));
}
} // namespace
PasswordBubbleViewTestBase::PasswordBubbleViewTestBase()
: profile_(IdentityTestEnvironmentProfileAdaptor::
CreateProfileForIdentityTestEnvironment({})),
identity_test_env_profile_adaptor_(
std::make_unique<IdentityTestEnvironmentProfileAdaptor>(
profile_.get())),
test_web_contents_(
content::WebContentsTester::CreateTestWebContents(profile_.get(),
nullptr)),
model_delegate_weak_ptr_factory_(&model_delegate_mock_) {
ON_CALL(model_delegate_mock_, GetWebContents)
.WillByDefault(Return(web_contents()));
ON_CALL(model_delegate_mock_, GetPasswordFeatureManager)
.WillByDefault(Return(feature_manager_mock()));
// Create the test UIController here so that it's bound to
// |test_web_contents_|, and will be retrieved correctly via
// ManagePasswordsUIController::FromWebContents in
// PasswordsModelDelegateFromWebContents().
new TestManagePasswordsUIController(
test_web_contents_.get(), model_delegate_weak_ptr_factory_.GetWeakPtr());
}
PasswordBubbleViewTestBase::~PasswordBubbleViewTestBase() = default;
void PasswordBubbleViewTestBase::CreateAnchorViewAndShow() {
anchor_widget_ = CreateTestWidget(views::Widget::InitParams::TYPE_WINDOW);
anchor_widget_->Show();
}
void PasswordBubbleViewTestBase::TearDown() {
anchor_widget_.reset();
ChromeViewsTestBase::TearDown();
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_VIEWS_PASSWORDS_PASSWORD_BUBBLE_VIEW_TEST_BASE_H_
#define CHROME_BROWSER_UI_VIEWS_PASSWORDS_PASSWORD_BUBBLE_VIEW_TEST_BASE_H_
#include <memory>
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/browser/ui/passwords/passwords_model_delegate_mock.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/views/chrome_views_test_base.h"
#include "components/password_manager/core/browser/mock_password_feature_manager.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "content/public/test/web_contents_tester.h"
#include "ui/views/widget/widget.h"
// Base class for testing password bubble views, which can be constructed
// passing web_contents() and anchor_view(). Mock behaviors can be set for
// model_delegate_mock() and feature_manager_mock().
class PasswordBubbleViewTestBase : public ChromeViewsTestBase {
public:
PasswordBubbleViewTestBase();
~PasswordBubbleViewTestBase() override;
// Should be called before showing the child bubble view.
void CreateAnchorViewAndShow();
TestingProfile* profile() { return profile_.get(); }
signin::IdentityTestEnvironment* identity_test_env() {
return identity_test_env_profile_adaptor_->identity_test_env();
}
content::WebContents* web_contents() { return test_web_contents_.get(); }
views::View* anchor_view() { return anchor_widget_->GetContentsView(); }
PasswordsModelDelegateMock* model_delegate_mock() {
return &model_delegate_mock_;
}
password_manager::MockPasswordFeatureManager* feature_manager_mock() {
return &feature_manager_mock_;
}
void TearDown() override;
private:
std::unique_ptr<TestingProfile> profile_;
std::unique_ptr<IdentityTestEnvironmentProfileAdaptor>
identity_test_env_profile_adaptor_;
std::unique_ptr<content::WebContents> test_web_contents_;
std::unique_ptr<views::Widget> anchor_widget_;
testing::NiceMock<password_manager::MockPasswordFeatureManager>
feature_manager_mock_;
testing::NiceMock<PasswordsModelDelegateMock> model_delegate_mock_;
base::WeakPtrFactory<PasswordsModelDelegate> model_delegate_weak_ptr_factory_;
};
#endif // CHROME_BROWSER_UI_VIEWS_PASSWORDS_PASSWORD_BUBBLE_VIEW_TEST_BASE_H_
...@@ -4,66 +4,24 @@ ...@@ -4,66 +4,24 @@
#include "chrome/browser/ui/views/passwords/password_save_update_with_account_store_view.h" #include "chrome/browser/ui/views/passwords/password_save_update_with_account_store_view.h"
#include <memory>
#include <vector>
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "chrome/browser/password_manager/password_store_factory.h" #include "chrome/browser/password_manager/password_store_factory.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h" #include "chrome/browser/ui/views/passwords/password_bubble_view_test_base.h"
#include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
#include "chrome/browser/ui/passwords/passwords_model_delegate_mock.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/views/chrome_views_test_base.h"
#include "components/password_manager/core/browser/mock_password_feature_manager.h" #include "components/password_manager/core/browser/mock_password_feature_manager.h"
#include "components/password_manager/core/browser/mock_password_store.h" #include "components/password_manager/core/browser/mock_password_store.h"
#include "components/password_manager/core/browser/password_manager_test_utils.h" #include "components/password_manager/core/browser/password_manager_test_utils.h"
#include "components/signin/public/identity_manager/identity_test_environment.h" #include "testing/gmock/include/gmock/gmock.h"
#include "content/public/test/web_contents_tester.h" #include "testing/gtest/include/gtest/gtest.h"
#include "ui/views/controls/combobox/combobox.h" #include "ui/views/controls/combobox/combobox.h"
using ::testing::NiceMock;
using ::testing::Return; using ::testing::Return;
using ::testing::ReturnRef; using ::testing::ReturnRef;
class TestManagePasswordsUIController : public ManagePasswordsUIController { class PasswordSaveUpdateWithAccountStoreViewTest
public: : public PasswordBubbleViewTestBase {
explicit TestManagePasswordsUIController(
content::WebContents* web_contents,
password_manager::PasswordFeatureManager* feature_manager);
base::WeakPtr<PasswordsModelDelegate> GetModelDelegateProxy() override {
return weak_ptr_factory_.GetWeakPtr();
}
private:
NiceMock<PasswordsModelDelegateMock> model_delegate_mock_;
base::WeakPtrFactory<PasswordsModelDelegate> weak_ptr_factory_;
autofill::PasswordForm pending_password_;
std::vector<std::unique_ptr<autofill::PasswordForm>> current_forms_;
};
TestManagePasswordsUIController::TestManagePasswordsUIController(
content::WebContents* web_contents,
password_manager::PasswordFeatureManager* feature_manager)
: ManagePasswordsUIController(web_contents),
weak_ptr_factory_(&model_delegate_mock_) {
// Do not silently replace an existing ManagePasswordsUIController
// because it unregisters itself in WebContentsDestroyed().
EXPECT_FALSE(web_contents->GetUserData(UserDataKey()));
web_contents->SetUserData(UserDataKey(), base::WrapUnique(this));
ON_CALL(model_delegate_mock_, GetOrigin)
.WillByDefault(ReturnRef(pending_password_.origin));
ON_CALL(model_delegate_mock_, GetState)
.WillByDefault(Return(password_manager::ui::PENDING_PASSWORD_STATE));
ON_CALL(model_delegate_mock_, GetPendingPassword)
.WillByDefault(ReturnRef(pending_password_));
ON_CALL(model_delegate_mock_, GetCurrentForms)
.WillByDefault(ReturnRef(current_forms_));
ON_CALL(model_delegate_mock_, GetWebContents)
.WillByDefault(Return(web_contents));
ON_CALL(model_delegate_mock_, GetPasswordFeatureManager)
.WillByDefault(Return(feature_manager));
}
class PasswordSaveUpdateWithAccountStoreViewTest : public ChromeViewsTestBase {
public: public:
PasswordSaveUpdateWithAccountStoreViewTest(); PasswordSaveUpdateWithAccountStoreViewTest();
~PasswordSaveUpdateWithAccountStoreViewTest() override = default; ~PasswordSaveUpdateWithAccountStoreViewTest() override = default;
...@@ -73,66 +31,47 @@ class PasswordSaveUpdateWithAccountStoreViewTest : public ChromeViewsTestBase { ...@@ -73,66 +31,47 @@ class PasswordSaveUpdateWithAccountStoreViewTest : public ChromeViewsTestBase {
void TearDown() override { void TearDown() override {
view_->GetWidget()->CloseWithReason( view_->GetWidget()->CloseWithReason(
views::Widget::ClosedReason::kCloseButtonClicked); views::Widget::ClosedReason::kCloseButtonClicked);
anchor_widget_.reset();
ChromeViewsTestBase::TearDown(); PasswordBubbleViewTestBase::TearDown();
} }
PasswordSaveUpdateWithAccountStoreView* view() { return view_; } PasswordSaveUpdateWithAccountStoreView* view() { return view_; }
views::Combobox* account_picker() { views::Combobox* account_picker() {
return view_->DestinationDropdownForTesting(); return view_->DestinationDropdownForTesting();
} }
password_manager::MockPasswordFeatureManager* feature_manager() {
return &feature_manager_;
}
signin::IdentityTestEnvironment* identity_test_env() {
return identity_test_env_profile_adaptor_->identity_test_env();
}
private: private:
NiceMock<password_manager::MockPasswordFeatureManager> feature_manager_;
std::unique_ptr<TestingProfile> profile_;
std::unique_ptr<content::WebContents> test_web_contents_;
std::unique_ptr<views::Widget> anchor_widget_;
PasswordSaveUpdateWithAccountStoreView* view_; PasswordSaveUpdateWithAccountStoreView* view_;
std::unique_ptr<IdentityTestEnvironmentProfileAdaptor> autofill::PasswordForm pending_password_;
identity_test_env_profile_adaptor_; std::vector<std::unique_ptr<autofill::PasswordForm>> current_forms_;
}; };
PasswordSaveUpdateWithAccountStoreViewTest:: PasswordSaveUpdateWithAccountStoreViewTest::
PasswordSaveUpdateWithAccountStoreViewTest() { PasswordSaveUpdateWithAccountStoreViewTest() {
ON_CALL(feature_manager_, GetDefaultPasswordStore) ON_CALL(*feature_manager_mock(), GetDefaultPasswordStore)
.WillByDefault(Return(autofill::PasswordForm::Store::kAccountStore)); .WillByDefault(Return(autofill::PasswordForm::Store::kAccountStore));
ON_CALL(*model_delegate_mock(), GetOrigin)
profile_ = IdentityTestEnvironmentProfileAdaptor:: .WillByDefault(ReturnRef(pending_password_.origin));
CreateProfileForIdentityTestEnvironment({}); ON_CALL(*model_delegate_mock(), GetState)
identity_test_env_profile_adaptor_ = .WillByDefault(Return(password_manager::ui::PENDING_PASSWORD_STATE));
std::make_unique<IdentityTestEnvironmentProfileAdaptor>(profile_.get()); ON_CALL(*model_delegate_mock(), GetPendingPassword)
.WillByDefault(ReturnRef(pending_password_));
ON_CALL(*model_delegate_mock(), GetCurrentForms)
.WillByDefault(ReturnRef(current_forms_));
PasswordStoreFactory::GetInstance()->SetTestingFactoryAndUse( PasswordStoreFactory::GetInstance()->SetTestingFactoryAndUse(
profile_.get(), profile(),
base::BindRepeating( base::BindRepeating(
&password_manager::BuildPasswordStore< &password_manager::BuildPasswordStore<
content::BrowserContext, content::BrowserContext,
testing::NiceMock<password_manager::MockPasswordStore>>)); testing::NiceMock<password_manager::MockPasswordStore>>));
test_web_contents_ = content::WebContentsTester::CreateTestWebContents(
profile_.get(), nullptr);
// Create the test UIController here so that it's bound to
// |test_web_contents_|, and will be retrieved correctly via
// ManagePasswordsUIController::FromWebContents in
// PasswordsModelDelegateFromWebContents().
new TestManagePasswordsUIController(test_web_contents_.get(),
&feature_manager_);
} }
void PasswordSaveUpdateWithAccountStoreViewTest::CreateViewAndShow() { void PasswordSaveUpdateWithAccountStoreViewTest::CreateViewAndShow() {
// The bubble needs the parent as an anchor. CreateAnchorViewAndShow();
anchor_widget_ = CreateTestWidget(views::Widget::InitParams::TYPE_WINDOW);
anchor_widget_->Show();
view_ = new PasswordSaveUpdateWithAccountStoreView( view_ = new PasswordSaveUpdateWithAccountStoreView(
test_web_contents_.get(), anchor_widget_->GetContentsView(), web_contents(), anchor_view(), LocationBarBubbleDelegateView::AUTOMATIC);
LocationBarBubbleDelegateView::AUTOMATIC);
views::BubbleDialogDelegateView::CreateBubble(view_)->Show(); views::BubbleDialogDelegateView::CreateBubble(view_)->Show();
} }
...@@ -146,7 +85,7 @@ TEST_F(PasswordSaveUpdateWithAccountStoreViewTest, HasTitleAndTwoButtons) { ...@@ -146,7 +85,7 @@ TEST_F(PasswordSaveUpdateWithAccountStoreViewTest, HasTitleAndTwoButtons) {
// TODO(crbug.com/1054629): Flakily times out on all platforms. // TODO(crbug.com/1054629): Flakily times out on all platforms.
TEST_F(PasswordSaveUpdateWithAccountStoreViewTest, TEST_F(PasswordSaveUpdateWithAccountStoreViewTest,
DISABLED_ShouldNotShowAccountPicker) { DISABLED_ShouldNotShowAccountPicker) {
ON_CALL(*feature_manager(), ShouldShowPasswordStorePicker) ON_CALL(*feature_manager_mock(), ShouldShowPasswordStorePicker)
.WillByDefault(Return(false)); .WillByDefault(Return(false));
CreateViewAndShow(); CreateViewAndShow();
EXPECT_FALSE(account_picker()); EXPECT_FALSE(account_picker());
...@@ -155,7 +94,7 @@ TEST_F(PasswordSaveUpdateWithAccountStoreViewTest, ...@@ -155,7 +94,7 @@ TEST_F(PasswordSaveUpdateWithAccountStoreViewTest,
// TODO(crbug.com/1054629): Flakily times out on all platforms. // TODO(crbug.com/1054629): Flakily times out on all platforms.
TEST_F(PasswordSaveUpdateWithAccountStoreViewTest, TEST_F(PasswordSaveUpdateWithAccountStoreViewTest,
DISABLED_ShouldShowAccountPicker) { DISABLED_ShouldShowAccountPicker) {
ON_CALL(*feature_manager(), ShouldShowPasswordStorePicker) ON_CALL(*feature_manager_mock(), ShouldShowPasswordStorePicker)
.WillByDefault(Return(true)); .WillByDefault(Return(true));
CreateViewAndShow(); CreateViewAndShow();
ASSERT_TRUE(account_picker()); ASSERT_TRUE(account_picker());
...@@ -165,9 +104,9 @@ TEST_F(PasswordSaveUpdateWithAccountStoreViewTest, ...@@ -165,9 +104,9 @@ TEST_F(PasswordSaveUpdateWithAccountStoreViewTest,
// TODO(crbug.com/1054629): Flakily times out on all platforms. // TODO(crbug.com/1054629): Flakily times out on all platforms.
TEST_F(PasswordSaveUpdateWithAccountStoreViewTest, TEST_F(PasswordSaveUpdateWithAccountStoreViewTest,
DISABLED_ShouldSelectAccountStoreByDefault) { DISABLED_ShouldSelectAccountStoreByDefault) {
ON_CALL(*feature_manager(), ShouldShowPasswordStorePicker) ON_CALL(*feature_manager_mock(), ShouldShowPasswordStorePicker)
.WillByDefault(Return(true)); .WillByDefault(Return(true));
ON_CALL(*feature_manager(), GetDefaultPasswordStore) ON_CALL(*feature_manager_mock(), GetDefaultPasswordStore)
.WillByDefault(Return(autofill::PasswordForm::Store::kAccountStore)); .WillByDefault(Return(autofill::PasswordForm::Store::kAccountStore));
CoreAccountInfo account_info = CoreAccountInfo account_info =
...@@ -185,9 +124,9 @@ TEST_F(PasswordSaveUpdateWithAccountStoreViewTest, ...@@ -185,9 +124,9 @@ TEST_F(PasswordSaveUpdateWithAccountStoreViewTest,
// TODO(crbug.com/1054629): Flakily times out on all platforms. // TODO(crbug.com/1054629): Flakily times out on all platforms.
TEST_F(PasswordSaveUpdateWithAccountStoreViewTest, TEST_F(PasswordSaveUpdateWithAccountStoreViewTest,
DISABLED_ShouldSelectProfileStoreByDefault) { DISABLED_ShouldSelectProfileStoreByDefault) {
ON_CALL(*feature_manager(), ShouldShowPasswordStorePicker) ON_CALL(*feature_manager_mock(), ShouldShowPasswordStorePicker)
.WillByDefault(Return(true)); .WillByDefault(Return(true));
ON_CALL(*feature_manager(), GetDefaultPasswordStore) ON_CALL(*feature_manager_mock(), GetDefaultPasswordStore)
.WillByDefault(Return(autofill::PasswordForm::Store::kProfileStore)); .WillByDefault(Return(autofill::PasswordForm::Store::kProfileStore));
CreateViewAndShow(); CreateViewAndShow();
ASSERT_TRUE(account_picker()); ASSERT_TRUE(account_picker());
......
...@@ -4278,6 +4278,8 @@ test("unit_tests") { ...@@ -4278,6 +4278,8 @@ test("unit_tests") {
"../browser/ui/toolbar/media_router_contextual_menu_unittest.cc", "../browser/ui/toolbar/media_router_contextual_menu_unittest.cc",
"../browser/ui/toolbar/mock_media_router_action_controller.cc", "../browser/ui/toolbar/mock_media_router_action_controller.cc",
"../browser/ui/toolbar/mock_media_router_action_controller.h", "../browser/ui/toolbar/mock_media_router_action_controller.h",
"../browser/ui/views/passwords/password_bubble_view_test_base.cc",
"../browser/ui/views/passwords/password_bubble_view_test_base.h",
"../browser/ui/views/passwords/password_save_update_with_account_store_view_unittest.cc", "../browser/ui/views/passwords/password_save_update_with_account_store_view_unittest.cc",
"../common/media_router/discovery/media_sink_internal_unittest.cc", "../common/media_router/discovery/media_sink_internal_unittest.cc",
"../common/media_router/discovery/media_sink_service_base_unittest.cc", "../common/media_router/discovery/media_sink_service_base_unittest.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment