1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/json/json_reader.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
#include "chrome/browser/ui/autofill/autofill_context_menu_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_model.h"
#include "chrome/browser/ui/webui/feedback/feedback_dialog.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/content/browser/test_autofill_manager_injector.h"
#include "components/autofill/core/browser/autofill_feedback_data.h"
#include "components/autofill/core/browser/foundations/browser_autofill_manager.h"
#include "components/autofill/core/browser/foundations/test_autofill_manager_waiter.h"
#include "components/autofill/core/browser/test_utils/autofill_test_utils.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/unique_ids.h"
#include "components/feature_engagement/public/feature_constants.h"
#include "components/prefs/testing_pref_service.h"
#include "content/public/test/browser_test.h"
namespace autofill {
namespace {
#if !BUILDFLAG(IS_CHROMEOS)
// Generates a ContextMenuParams for the Autofill context menu options.
content::ContextMenuParams CreateContextMenuParams(
std::optional<autofill::FormRendererId> form_renderer_id = std::nullopt,
autofill::FieldRendererId field_render_id = autofill::FieldRendererId(0)) {
content::ContextMenuParams rv;
rv.is_editable = true;
rv.page_url = GURL("http://test.page/");
rv.form_control_type = blink::mojom::FormControlType::kInputText;
if (form_renderer_id) {
rv.form_renderer_id = form_renderer_id->value();
}
rv.field_renderer_id = field_render_id.value();
return rv;
}
class TestAutofillManager : public BrowserAutofillManager {
public:
explicit TestAutofillManager(ContentAutofillDriver* driver)
: BrowserAutofillManager(driver) {}
testing::AssertionResult WaitForFormsSeen(int min_num_awaited_calls) {
return forms_seen_waiter_.Wait(min_num_awaited_calls);
}
private:
TestAutofillManagerWaiter forms_seen_waiter_{
*this,
{AutofillManagerEvent::kFormsSeen}};
};
class AutofillContextMenuManagerFeedbackUIBrowserTest
: public InProcessBrowserTest {
public:
AutofillContextMenuManagerFeedbackUIBrowserTest() = default;
void SetUpOnMainThread() override {
render_view_context_menu_ = std::make_unique<TestRenderViewContextMenu>(
*web_contents()->GetPrimaryMainFrame(), content::ContextMenuParams());
render_view_context_menu_->Init();
autofill_context_menu_manager_ =
std::make_unique<AutofillContextMenuManager>(
render_view_context_menu_.get(), nullptr);
browser()->profile()->GetPrefs()->SetBoolean(prefs::kUserFeedbackAllowed,
true);
}
void TearDownOnMainThread() override {
autofill_context_menu_manager_.reset();
render_view_context_menu_.reset();
InProcessBrowserTest::TearDownOnMainThread();
}
content::WebContents* web_contents() {
return browser()->tab_strip_model()->GetActiveWebContents();
}
TestAutofillManager* GetAutofillManager() {
return autofill_manager_injector_[web_contents()->GetPrimaryMainFrame()];
}
protected:
test::AutofillBrowserTestEnvironment autofill_test_environment_;
std::unique_ptr<TestRenderViewContextMenu> render_view_context_menu_;
std::unique_ptr<AutofillContextMenuManager> autofill_context_menu_manager_;
TestAutofillManagerInjector<TestAutofillManager> autofill_manager_injector_;
};
// Awaits for the feedback dialog to be active. `callback` gets triggered
// once the dialog is shown.
void EnsureFeedbackAppUIShown(FeedbackDialog* feedback_dialog,
base::OnceClosure callback) {
auto* widget = feedback_dialog->GetWidget();
ASSERT_NE(nullptr, widget);
if (widget->IsActive()) {
std::move(callback).Run();
} else {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&EnsureFeedbackAppUIShown, feedback_dialog,
std::move(callback)),
base::Seconds(1));
}
}
IN_PROC_BROWSER_TEST_F(AutofillContextMenuManagerFeedbackUIBrowserTest,
FeedbackUIIsRequested) {
base::HistogramTester histogram_tester;
// Executing autofill feedback command opens the Feedback UI.
autofill_context_menu_manager_->ExecuteCommand(
IDC_CONTENT_CONTEXT_AUTOFILL_FEEDBACK);
// Checks that feedback form was requested.
histogram_tester.ExpectTotalCount("Feedback.RequestSource", 1);
}
IN_PROC_BROWSER_TEST_F(AutofillContextMenuManagerFeedbackUIBrowserTest,
CloseTabWhileUIIsOpenShouldNotCrash) {
autofill_context_menu_manager_->ExecuteCommand(
IDC_CONTENT_CONTEXT_AUTOFILL_FEEDBACK);
content::WebContents* tab =
browser()->tab_strip_model()->GetActiveWebContents();
tab->Close();
}
IN_PROC_BROWSER_TEST_F(AutofillContextMenuManagerFeedbackUIBrowserTest,
DisplaysFeedbackDialogUI) {
base::RunLoop run_loop;
// Test that no feedback dialog exists.
ASSERT_EQ(nullptr, FeedbackDialog::GetInstanceForTest());
autofill_context_menu_manager_->ExecuteCommand(
IDC_CONTENT_CONTEXT_AUTOFILL_FEEDBACK);
FeedbackDialog* feedback_dialog = FeedbackDialog::GetInstanceForTest();
// Test that a feedback dialog object has been created.
ASSERT_NE(nullptr, feedback_dialog);
// The feedback app starts invisible until after a screenshot has been taken
// via JS on the UI side. Afterward, JS will send a request to show the app
// window via a message handler.
EnsureFeedbackAppUIShown(feedback_dialog, run_loop.QuitClosure());
run_loop.Run();
// Test that the feedback app is visible now.
EXPECT_TRUE(feedback_dialog->GetWidget()->IsVisible());
// Close the feedback dialog.
feedback_dialog->GetWidget()->Close();
}
// Regression test for crbug.com/1493774.
IN_PROC_BROWSER_TEST_F(AutofillContextMenuManagerFeedbackUIBrowserTest,
TabMoveToOtherBrowserDoesNotCrash) {
// Create another browser.
Browser* other_browser = CreateBrowser(browser()->profile());
// Move the tab to the other browser.
other_browser->tab_strip_model()->InsertDetachedTabAt(
0, browser()->tab_strip_model()->DetachTabAtForInsertion(0),
AddTabTypes::ADD_ACTIVE);
ASSERT_EQ(other_browser->tab_strip_model()->count(), 2);
// Close the first browser.
CloseBrowserSynchronously(browser());
}
IN_PROC_BROWSER_TEST_F(AutofillContextMenuManagerFeedbackUIBrowserTest,
FeedbackDialogArgsAutofillMetadata) {
std::string expected_metadata;
base::JSONWriter::Write(
data_logs::FetchAutofillFeedbackData(GetAutofillManager()),
&expected_metadata);
// Test that none feedback dialog exists.
ASSERT_EQ(nullptr, FeedbackDialog::GetInstanceForTest());
// Display feedback dialog.
autofill_context_menu_manager_->ExecuteCommand(
IDC_CONTENT_CONTEXT_AUTOFILL_FEEDBACK);
ui::WebDialogDelegate* feedback_dialog = FeedbackDialog::GetInstanceForTest();
// Test that a feedback dialog object has been created.
ASSERT_NE(nullptr, feedback_dialog);
// Extract autofill metadata from dialog arguments and check for correctness.
std::string dialog_args_str = feedback_dialog->GetDialogArgs();
std::optional<base::Value> value = base::JSONReader::Read(dialog_args_str);
ASSERT_TRUE(value.has_value() && value->is_dict());
const std::string* autofill_metadata =
value->GetDict().FindString("autofillMetadata");
ASSERT_TRUE(autofill_metadata);
EXPECT_EQ(*autofill_metadata, expected_metadata);
}
IN_PROC_BROWSER_TEST_F(AutofillContextMenuManagerFeedbackUIBrowserTest,
IncludesTriggerFormAndFieldSignatures) {
content::RenderFrameHost* rfh = web_contents()->GetPrimaryMainFrame();
LocalFrameToken frame_token(rfh->GetFrameToken().value());
FormData form = test::CreateFormDataForFrame(
test::CreateTestAddressFormData(), frame_token);
GetAutofillManager()->OnFormsSeen(
/*updated_forms=*/{form},
/*removed_forms=*/{});
ASSERT_TRUE(GetAutofillManager()->WaitForFormsSeen(1));
ASSERT_TRUE(GetAutofillManager()->FindCachedFormById(form.global_id()));
// Set up expected trigger form and field signatures.
std::string expected_metadata;
base::Value::Dict extra_logs;
auto form_structure = std::make_unique<FormStructure>(form);
extra_logs.Set("triggerFormSignature", form_structure->FormSignatureAsStr());
extra_logs.Set("triggerFieldSignature",
form_structure->field(0)->FieldSignatureAsStr());
base::JSONWriter::Write(data_logs::FetchAutofillFeedbackData(
GetAutofillManager(), std::move(extra_logs)),
&expected_metadata);
// Set up context menu params with the correct trigger form and field.
autofill_context_menu_manager_->set_params_for_testing(
CreateContextMenuParams(form.global_id().renderer_id,
form.fields()[0].global_id().renderer_id));
// Display feedback dialog.
autofill_context_menu_manager_->ExecuteCommand(
IDC_CONTENT_CONTEXT_AUTOFILL_FEEDBACK);
ui::WebDialogDelegate* feedback_dialog = FeedbackDialog::GetInstanceForTest();
// Test that a feedback dialog object has been created.
ASSERT_NE(nullptr, feedback_dialog);
// Extract autofill metadata from dialog arguments and check for correctness.
std::string dialog_args_str = feedback_dialog->GetDialogArgs();
std::optional<base::Value> value = base::JSONReader::Read(dialog_args_str);
ASSERT_TRUE(value.has_value() && value->is_dict());
const std::string* autofill_metadata =
value->GetDict().FindString("autofillMetadata");
ASSERT_TRUE(autofill_metadata);
EXPECT_EQ(*autofill_metadata, expected_metadata);
}
#endif // !BUILDFLAG(IS_CHROMEOS)
} // namespace
} // namespace autofill
|