Commit 2cc440a1 authored by sczs's avatar sczs Committed by Commit Bot

[ios] Creates TestPasswordManagerClient class.

Moves MockPasswordManagerClient into its own class/file so it can be
used to test the InfobarPasswordCoordinator.

Bug: 945478
Change-Id: I55bb48ffcb0ce88205a0ebef6ba742e7ed667347
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1580349Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Commit-Queue: Sergio Collazos <sczs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#653618}
parent 5f53a799
......@@ -167,6 +167,7 @@ source_set("unit_tests") {
"//google_apis",
"//ios/chrome/browser/autofill",
"//ios/chrome/browser/browser_state:test_support",
"//ios/chrome/browser/passwords/test",
"//ios/chrome/browser/ssl",
"//ios/chrome/browser/ui/autofill:autofill",
"//ios/chrome/browser/ui/commands",
......
......@@ -9,13 +9,9 @@
#include "base/bind.h"
#include "base/mac/foundation_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/password_manager/core/browser/password_manager.h"
#include "components/password_manager/core/browser/stub_password_manager_client.h"
#include "components/password_manager/core/browser/test_password_store.h"
#include "components/password_manager/core/common/password_manager_pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "ios/chrome/browser/passwords/credential_manager_util.h"
#import "ios/chrome/browser/passwords/test/test_password_manager_client.h"
#include "ios/chrome/browser/ssl/ios_security_state_tab_helper.h"
#include "ios/web/public/navigation_item.h"
#include "ios/web/public/navigation_manager.h"
......@@ -25,7 +21,6 @@
#include "net/ssl/ssl_connection_status_flags.h"
#include "net/test/cert_test_util.h"
#include "net/test/test_data_directory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/origin.h"
......@@ -33,10 +28,6 @@
#error "This file requires ARC support."
#endif
using password_manager::PasswordStore;
using password_manager::PasswordManager;
using password_manager::PasswordFormManagerForUI;
using password_manager::TestPasswordStore;
using testing::_;
using url::Origin;
......@@ -58,102 +49,6 @@ constexpr char kFileOrigin[] = "file://example_file";
// SSL certificate to load for testing.
constexpr char kCertFileName[] = "ok_cert.pem";
// Mocks PasswordManagerClient, used indirectly by CredentialManager.
class MockPasswordManagerClient
: public password_manager::StubPasswordManagerClient {
public:
MockPasswordManagerClient()
: last_committed_url_(kHttpsWebOrigin), password_manager_(this) {
store_ = base::MakeRefCounted<TestPasswordStore>();
store_->Init(syncer::SyncableService::StartSyncFlare(), nullptr);
prefs_ = std::make_unique<TestingPrefServiceSimple>();
prefs_->registry()->RegisterBooleanPref(
password_manager::prefs::kCredentialsEnableAutosignin, true);
prefs_->registry()->RegisterBooleanPref(
password_manager::prefs::kWasAutoSignInFirstRunExperienceShown, true);
}
// PasswordManagerClient:
MOCK_METHOD0(OnCredentialManagerUsed, bool());
// PromptUserTo*Ptr functions allow to both override PromptUserTo* methods
// and expect calls.
MOCK_METHOD1(PromptUserToSavePasswordPtr, void(PasswordFormManagerForUI*));
MOCK_METHOD3(PromptUserToChooseCredentialsPtr,
bool(const std::vector<autofill::PasswordForm*>& local_forms,
const GURL& origin,
const CredentialsCallback& callback));
scoped_refptr<TestPasswordStore> password_store() const { return store_; }
void set_password_store(scoped_refptr<TestPasswordStore> store) {
store_ = store;
}
PasswordFormManagerForUI* pending_manager() const { return manager_.get(); }
void set_current_url(const GURL& current_url) {
last_committed_url_ = current_url;
}
private:
// PasswordManagerClient:
PrefService* GetPrefs() const override { return prefs_.get(); }
PasswordStore* GetPasswordStore() const override { return store_.get(); }
const PasswordManager* GetPasswordManager() const override {
return &password_manager_;
}
const GURL& GetLastCommittedEntryURL() const override {
return last_committed_url_;
}
// Stores |manager| into |manager_|. Save() should be
// called manually in test. To put expectation on this function being called,
// use PromptUserToSavePasswordPtr.
bool PromptUserToSaveOrUpdatePassword(
std::unique_ptr<PasswordFormManagerForUI> manager,
bool update_password) override;
// Mocks choosing a credential by the user. To put expectation on this
// function being called, use PromptUserToChooseCredentialsPtr.
bool PromptUserToChooseCredentials(
std::vector<std::unique_ptr<autofill::PasswordForm>> local_forms,
const GURL& origin,
const CredentialsCallback& callback) override;
std::unique_ptr<TestingPrefServiceSimple> prefs_;
GURL last_committed_url_;
PasswordManager password_manager_;
std::unique_ptr<PasswordFormManagerForUI> manager_;
scoped_refptr<TestPasswordStore> store_;
DISALLOW_COPY_AND_ASSIGN(MockPasswordManagerClient);
};
bool MockPasswordManagerClient::PromptUserToSaveOrUpdatePassword(
std::unique_ptr<PasswordFormManagerForUI> manager,
bool update_password) {
EXPECT_FALSE(update_password);
manager_.swap(manager);
PromptUserToSavePasswordPtr(manager_.get());
return true;
}
bool MockPasswordManagerClient::PromptUserToChooseCredentials(
std::vector<std::unique_ptr<autofill::PasswordForm>> local_forms,
const GURL& origin,
const CredentialsCallback& callback) {
EXPECT_FALSE(local_forms.empty());
const autofill::PasswordForm* form = local_forms[0].get();
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(callback, base::Owned(new autofill::PasswordForm(*form))));
std::vector<autofill::PasswordForm*> raw_forms(local_forms.size());
std::transform(local_forms.begin(), local_forms.end(), raw_forms.begin(),
[](const std::unique_ptr<autofill::PasswordForm>& form) {
return form.get();
});
PromptUserToChooseCredentialsPtr(raw_forms, origin, callback);
return true;
}
} // namespace
class CredentialManagerBaseTest
......@@ -204,7 +99,7 @@ class CredentialManagerTest : public CredentialManagerBaseTest {
void SetUp() override {
CredentialManagerBaseTest::SetUp();
client_ = std::make_unique<MockPasswordManagerClient>();
client_ = std::make_unique<TestPasswordManagerClient>();
manager_ = std::make_unique<CredentialManager>(client_.get(), web_state());
// Inject JavaScript and set up secure context.
......@@ -256,7 +151,7 @@ class CredentialManagerTest : public CredentialManagerBaseTest {
}
protected:
std::unique_ptr<MockPasswordManagerClient> client_;
std::unique_ptr<TestPasswordManagerClient> client_;
std::unique_ptr<CredentialManager> manager_;
autofill::PasswordForm password_credential_form_1_;
......
# Copyright 2019 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.
source_set("test") {
configs += [ "//build/config/compiler:enable_arc" ]
testonly = true
sources = [
"test_password_manager_client.h",
"test_password_manager_client.mm",
]
deps = [
"//components/password_manager/core/browser:test_support",
"//components/password_manager/core/common",
"//components/prefs",
"//components/prefs:test_support",
"//testing/gtest",
]
}
// Copyright 2019 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 IOS_CHROME_BROWSER_PASSWORDS_TEST_TEST_PASSWORD_MANAGER_CLIENT_H_
#define IOS_CHROME_BROWSER_PASSWORDS_TEST_TEST_PASSWORD_MANAGER_CLIENT_H_
#include "components/password_manager/core/browser/stub_password_manager_client.h"
#include "components/password_manager/core/browser/password_manager.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace password_manager {
class PasswordFormManagerForUI;
class TestPasswordStore;
class PasswordStore;
} // namespace password_manager
class TestingPrefServiceSimple;
using password_manager::PasswordFormManagerForUI;
using password_manager::PasswordManager;
using password_manager::PasswordStore;
using password_manager::TestPasswordStore;
// Test PasswordManagerClient.
class TestPasswordManagerClient
: public password_manager::StubPasswordManagerClient {
public:
TestPasswordManagerClient();
~TestPasswordManagerClient() override;
// PasswordManagerClient:
MOCK_METHOD0(OnCredentialManagerUsed, bool());
// PromptUserTo*Ptr functions allow to both override PromptUserTo* methods
// and expect calls.
MOCK_METHOD1(PromptUserToSavePasswordPtr, void(PasswordFormManagerForUI*));
MOCK_METHOD3(PromptUserToChooseCredentialsPtr,
bool(const std::vector<autofill::PasswordForm*>& local_forms,
const GURL& origin,
const CredentialsCallback& callback));
scoped_refptr<TestPasswordStore> password_store() const;
void set_password_store(scoped_refptr<TestPasswordStore> store);
PasswordFormManagerForUI* pending_manager() const;
void set_current_url(const GURL& current_url);
private:
// PasswordManagerClient:
PrefService* GetPrefs() const override;
PasswordStore* GetPasswordStore() const override;
const PasswordManager* GetPasswordManager() const override;
const GURL& GetLastCommittedEntryURL() const override;
// Stores |manager| into |manager_|. Save() should be
// called manually in test. To put expectation on this function being called,
// use PromptUserToSavePasswordPtr.
bool PromptUserToSaveOrUpdatePassword(
std::unique_ptr<PasswordFormManagerForUI> manager,
bool update_password) override;
// Mocks choosing a credential by the user. To put expectation on this
// function being called, use PromptUserToChooseCredentialsPtr.
bool PromptUserToChooseCredentials(
std::vector<std::unique_ptr<autofill::PasswordForm>> local_forms,
const GURL& origin,
const CredentialsCallback& callback) override;
std::unique_ptr<TestingPrefServiceSimple> prefs_;
GURL last_committed_url_;
PasswordManager password_manager_;
std::unique_ptr<PasswordFormManagerForUI> manager_;
scoped_refptr<TestPasswordStore> store_;
DISALLOW_COPY_AND_ASSIGN(TestPasswordManagerClient);
};
#endif // IOS_CHROME_BROWSER_PASSWORDS_TEST_TEST_PASSWORD_MANAGER_CLIENT_H_
// Copyright 2019 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.
#import "ios/chrome/browser/passwords/test/test_password_manager_client.h"
#include "components/password_manager/core/browser/test_password_store.h"
#include "components/password_manager/core/common/password_manager_pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
// HTTPS origin corresponding to kHostName.
constexpr char kHttpsWebOrigin[] = "https://www.example.com/";
} // namespace
TestPasswordManagerClient::TestPasswordManagerClient()
: last_committed_url_(kHttpsWebOrigin), password_manager_(this) {
store_ = base::MakeRefCounted<TestPasswordStore>();
store_->Init(syncer::SyncableService::StartSyncFlare(), nullptr);
prefs_ = std::make_unique<TestingPrefServiceSimple>();
prefs_->registry()->RegisterBooleanPref(
password_manager::prefs::kCredentialsEnableAutosignin, true);
prefs_->registry()->RegisterBooleanPref(
password_manager::prefs::kWasAutoSignInFirstRunExperienceShown, true);
}
TestPasswordManagerClient::~TestPasswordManagerClient() = default;
scoped_refptr<TestPasswordStore> TestPasswordManagerClient::password_store()
const {
return store_;
}
void TestPasswordManagerClient::set_password_store(
scoped_refptr<TestPasswordStore> store) {
store_ = store;
}
PasswordFormManagerForUI* TestPasswordManagerClient::pending_manager() const {
return manager_.get();
}
void TestPasswordManagerClient::set_current_url(const GURL& current_url) {
last_committed_url_ = current_url;
}
// Private Methods
PrefService* TestPasswordManagerClient::GetPrefs() const {
return prefs_.get();
}
PasswordStore* TestPasswordManagerClient::GetPasswordStore() const {
return store_.get();
}
const PasswordManager* TestPasswordManagerClient::GetPasswordManager() const {
return &password_manager_;
}
const GURL& TestPasswordManagerClient::GetLastCommittedEntryURL() const {
return last_committed_url_;
}
bool TestPasswordManagerClient::PromptUserToSaveOrUpdatePassword(
std::unique_ptr<PasswordFormManagerForUI> manager,
bool update_password) {
EXPECT_FALSE(update_password);
manager_.swap(manager);
PromptUserToSavePasswordPtr(manager_.get());
return true;
}
bool TestPasswordManagerClient::PromptUserToChooseCredentials(
std::vector<std::unique_ptr<autofill::PasswordForm>> local_forms,
const GURL& origin,
const CredentialsCallback& callback) {
EXPECT_FALSE(local_forms.empty());
const autofill::PasswordForm* form = local_forms[0].get();
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(callback, base::Owned(new autofill::PasswordForm(*form))));
std::vector<autofill::PasswordForm*> raw_forms(local_forms.size());
std::transform(local_forms.begin(), local_forms.end(), raw_forms.begin(),
[](const std::unique_ptr<autofill::PasswordForm>& form) {
return form.get();
});
PromptUserToChooseCredentialsPtr(raw_forms, origin, callback);
return true;
}
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