Commit 3d642660 authored by Vasilii Sukhanov's avatar Vasilii Sukhanov Committed by Commit Bot

Add tests for deleting the generated password.

This is a follow-up to https://chromium-review.googlesource.com/c/chromium/src/+/1160849.
The tests are not to be merged back to M69.

Bug: 870217
Change-Id: I0bc691bb8168c9a3887ef572f1bfa15d2e5cb68f
Reviewed-on: https://chromium-review.googlesource.com/1160851
Commit-Queue: Vasilii Sukhanov <vasilii@chromium.org>
Reviewed-by: default avatarIoana Pandele <ioanap@chromium.org>
Cr-Commit-Position: refs/heads/master@{#580524}
parent 9b913e53
......@@ -3,11 +3,12 @@
// found in the LICENSE file.
#include "base/command_line.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/password_manager/chrome_password_manager_client.h"
#include "chrome/browser/password_manager/password_manager_test_base.h"
#include "chrome/browser/password_manager/password_manager_interactive_test_base.h"
#include "chrome/browser/password_manager/password_store_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
......@@ -19,6 +20,7 @@
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_switches.h"
#include "components/password_manager/core/browser/password_generation_manager.h"
#include "components/password_manager/core/browser/password_manager_util.h"
#include "components/password_manager/core/browser/test_password_store.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
......@@ -33,23 +35,48 @@ namespace {
class TestPopupObserver : public PasswordGenerationPopupObserver {
public:
TestPopupObserver() = default;
~TestPopupObserver() = default;
void OnPopupShown(
PasswordGenerationPopupController::GenerationState state) override {
popup_showing_ = true;
state_ = state;
MaybeQuitRunLoop();
}
void OnPopupHidden() override { popup_showing_ = false; }
void OnPopupHidden() override {
popup_showing_ = false;
MaybeQuitRunLoop();
}
bool popup_showing() const { return popup_showing_; }
PasswordGenerationPopupController::GenerationState state() const {
return state_;
}
// Waits until the popup is either shown or hidden.
void WaitForStatusChange() {
base::RunLoop run_loop;
run_loop_ = &run_loop;
run_loop_->Run();
}
private:
void MaybeQuitRunLoop() {
if (run_loop_) {
run_loop_->Quit();
run_loop_ = nullptr;
}
}
// The loop to be stopped after the popup state change.
base::RunLoop* run_loop_ = nullptr;
bool popup_showing_ = false;
PasswordGenerationPopupController::GenerationState state_ =
PasswordGenerationPopupController::kOfferGeneration;
DISALLOW_COPY_AND_ASSIGN(TestPopupObserver);
};
enum ReturnCodes { // Possible results of the JavaScript code.
......@@ -60,8 +87,8 @@ enum ReturnCodes { // Possible results of the JavaScript code.
} // namespace
class PasswordGenerationInteractiveTest :
public PasswordManagerBrowserTestBase {
class PasswordGenerationInteractiveTest
: public PasswordManagerInteractiveTestBase {
public:
void SetUpCommandLine(base::CommandLine* command_line) override {
PasswordManagerBrowserTestBase::SetUpCommandLine(command_line);
......@@ -161,6 +188,8 @@ class PasswordGenerationInteractiveTest :
PasswordGenerationPopupController::kEditGeneratedPassword;
}
void WaitForPopupStatusChange() { observer_.WaitForStatusChange(); }
private:
TestPopupObserver observer_;
base::test::ScopedFeatureList scoped_feature_list_;
......@@ -185,6 +214,53 @@ IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest,
EXPECT_TRUE(EditingPopupShowing());
}
IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest,
PopupShownAutomaticallyAndPasswordErased) {
FocusPasswordField();
EXPECT_TRUE(GenerationPopupShowing());
SendKeyToPopup(ui::VKEY_DOWN);
SendKeyToPopup(ui::VKEY_RETURN);
// Wait until the password is filled.
WaitForNonEmptyFieldValue("password_field");
// Re-focusing the password field should show the editing popup.
FocusPasswordField();
EXPECT_TRUE(EditingPopupShowing());
// Delete the password. The generation prompt should be visible.
SimulateUserDeletingFieldContent("password_field");
WaitForPopupStatusChange();
EXPECT_FALSE(EditingPopupShowing());
EXPECT_TRUE(GenerationPopupShowing());
}
IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest,
PopupShownManuallyAndPasswordErased) {
NavigateToFile("/password/password_form.html");
FocusPasswordField();
// The same flow happens when user generates a password from the context menu.
password_manager_util::UserTriggeredManualGenerationFromContextMenu(
ChromePasswordManagerClient::FromWebContents(WebContents()));
EXPECT_TRUE(GenerationPopupShowing());
SendKeyToPopup(ui::VKEY_DOWN);
SendKeyToPopup(ui::VKEY_RETURN);
// Wait until the password is filled.
WaitForNonEmptyFieldValue("password_field");
// Re-focusing the password field should show the editing popup.
FocusPasswordField();
EXPECT_TRUE(EditingPopupShowing());
// Delete the password. The generation prompt should not be visible.
SimulateUserDeletingFieldContent("password_field");
WaitForPopupStatusChange();
EXPECT_FALSE(EditingPopupShowing());
EXPECT_FALSE(GenerationPopupShowing());
}
IN_PROC_BROWSER_TEST_F(PasswordGenerationInteractiveTest,
PopupShownAndDismissed) {
FocusPasswordField();
......
......@@ -79,3 +79,15 @@ void PasswordManagerInteractiveTestBase::VerifyPasswordIsSavedAndFilled(
WaitForElementValue(username_id, kUsername);
WaitForElementValue(password_id, kPassword);
}
// Erases all characters that have been typed into |field_id|.
void PasswordManagerInteractiveTestBase::SimulateUserDeletingFieldContent(
const std::string& field_id) {
std::string focus("document.getElementById('" + field_id + "').focus();");
ASSERT_TRUE(content::ExecuteScript(WebContents(), focus));
std::string select("document.getElementById('" + field_id + "').select();");
ASSERT_TRUE(content::ExecuteScript(WebContents(), select));
content::SimulateKeyPress(WebContents(), ui::DomKey::BACKSPACE,
ui::DomCode::BACKSPACE, ui::VKEY_BACK, false, false,
false, false);
}
......@@ -30,6 +30,9 @@ class PasswordManagerInteractiveTestBase
const std::string& password_id,
const std::string& submission_script);
// Erases all characters that have been typed into |field_id|.
void SimulateUserDeletingFieldContent(const std::string& field_id);
private:
DISALLOW_COPY_AND_ASSIGN(PasswordManagerInteractiveTestBase);
};
......
......@@ -16,22 +16,6 @@
#include "components/password_manager/core/browser/test_password_store.h"
#include "content/public/test/browser_test_utils.h"
namespace {
// Erases all characters that have been typed into |field_id|.
void SimulateUserDeletingFieldContent(content::WebContents* web_contents,
const std::string& field_id) {
std::string focus("document.getElementById('" + field_id + "').focus();");
ASSERT_TRUE(content::ExecuteScript(web_contents, focus));
std::string select("document.getElementById('" + field_id + "').select();");
ASSERT_TRUE(content::ExecuteScript(web_contents, select));
content::SimulateKeyPress(web_contents, ui::DomKey::BACKSPACE,
ui::DomCode::BACKSPACE, ui::VKEY_BACK, false, false,
false, false);
}
} // namespace
namespace password_manager {
// Test fixture that condionally enable feature kAutofillExpandedPopupViews.
......@@ -177,7 +161,7 @@ IN_PROC_BROWSER_TEST_P(PasswordManagerBrowserTestWithConditionalPopupViews,
prompt_observer.WaitForFallbackForSaving();
// Delete typed content and verify that inactive state is reached.
SimulateUserDeletingFieldContent(WebContents(), "password_field");
SimulateUserDeletingFieldContent("password_field");
prompt_observer.WaitForInactiveState();
}
......@@ -203,7 +187,7 @@ IN_PROC_BROWSER_TEST_P(PasswordManagerBrowserTestWithConditionalPopupViews,
prompt_observer.WaitForFallbackForSaving();
// Delete typed content and verify that management state is reached.
SimulateUserDeletingFieldContent(WebContents(), "password_field");
SimulateUserDeletingFieldContent("password_field");
prompt_observer.WaitForManagementState();
}
......
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