blob: e462d48589331a91d7e4eb2fb4626ac9a596f5d5 [file] [log] [blame]
Justin DeWitteda1c0c2023-11-14 20:01:001// Copyright 2023 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Carlos Knippschild054603e2024-01-19 21:03:055#include <optional>
6
Jeffrey Cohen65290a72024-01-18 20:14:577#include "base/test/metrics/user_action_tester.h"
Justin DeWitteda1c0c2023-11-14 20:01:008#include "chrome/app/chrome_command_ids.h"
Antonio Gomes2c0d7472025-10-14 06:04:049#include "chrome/browser/compose/chrome_compose_client.h"
Carlos Knippschild054603e2024-01-19 21:03:0510#include "chrome/browser/compose/compose_enabling.h"
Arthur Sonzogni5bc3326c2024-02-29 19:39:0511#include "chrome/browser/compose/compose_session.h"
Justin DeWitteda1c0c2023-11-14 20:01:0012#include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
13#include "chrome/browser/ui/browser.h"
14#include "chrome/browser/ui/tabs/tab_enums.h"
15#include "chrome/browser/ui/tabs/tab_strip_model.h"
Trevor Perrier230b7f22023-12-02 01:33:4716#include "chrome/browser/ui/webui/feedback/feedback_dialog.h"
Anthony Cuiaa990bb92023-11-29 21:05:3317#include "chrome/common/pref_names.h"
Justin DeWitteda1c0c2023-11-14 20:01:0018#include "chrome/test/base/in_process_browser_test.h"
19#include "chrome/test/base/ui_test_utils.h"
Trevor Perrier230b7f22023-12-02 01:33:4720#include "chrome/test/interaction/interactive_browser_test.h"
Justin DeWitteda1c0c2023-11-14 20:01:0021#include "components/compose/core/browser/compose_features.h"
Jeffrey Cohen65290a72024-01-18 20:14:5722#include "components/feature_engagement/test/scoped_iph_feature_list.h"
Justin DeWitteda1c0c2023-11-14 20:01:0023#include "components/optimization_guide/core/optimization_guide_features.h"
Anthony Cuiaa990bb92023-11-29 21:05:3324#include "components/prefs/pref_service.h"
25#include "components/unified_consent/pref_names.h"
Justin DeWitteda1c0c2023-11-14 20:01:0026#include "content/public/browser/web_contents.h"
27#include "content/public/test/browser_test.h"
28#include "content/public/test/browser_test_utils.h"
29#include "ui/gfx/geometry/point_conversions.h"
Trevor Perrier230b7f22023-12-02 01:33:4730#include "ui/views/interaction/interaction_test_util_views.h"
Justin DeWitteda1c0c2023-11-14 20:01:0031
Anthony Cuiaa990bb92023-11-29 21:05:3332namespace compose {
Rakina Zata Amni833b4312023-11-29 06:13:5233
Trevor Perrier230b7f22023-12-02 01:33:4734class ComposeSessionBrowserTest : public InteractiveBrowserTest {
Justin DeWitteda1c0c2023-11-14 20:01:0035 public:
36 void SetUp() override {
Carlos Knippschild054603e2024-01-19 21:03:0537 scoped_compose_enabled_ = ComposeEnabling::ScopedEnableComposeForTesting();
Jeffrey Cohen65290a72024-01-18 20:14:5738 feature_list()->InitWithExistingFeatures(
Justin DeWitteda1c0c2023-11-14 20:01:0039 {compose::features::kEnableCompose,
Jeffrey Cohen65290a72024-01-18 20:14:5740 optimization_guide::features::kOptimizationGuideModelExecution,
41 feature_engagement::kIPHComposeMSBBSettingsFeature});
Justin DeWittcf1b2c82023-12-22 02:01:1342 InteractiveBrowserTest::SetUp();
Justin DeWitteda1c0c2023-11-14 20:01:0043 }
44
Carlos Knippschild054603e2024-01-19 21:03:0545 void TearDown() override {}
Carlos Knippschildb20a5d72024-01-04 01:06:5646
Jeffrey Cohen65290a72024-01-18 20:14:5747 feature_engagement::test::ScopedIphFeatureList* feature_list() {
48 return &feature_list_;
49 }
Justin DeWitteda1c0c2023-11-14 20:01:0050
51 protected:
Jeffrey Cohen65290a72024-01-18 20:14:5752 feature_engagement::test::ScopedIphFeatureList feature_list_;
Carlos Knippschild054603e2024-01-19 21:03:0553 ComposeEnabling::ScopedOverride scoped_compose_enabled_;
Justin DeWitteda1c0c2023-11-14 20:01:0054};
55
Justin DeWittcf1b2c82023-12-22 02:01:1356IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest, LifetimeOfBubbleWrapper) {
Justin DeWitteda1c0c2023-11-14 20:01:0057 ASSERT_TRUE(embedded_test_server()->Start());
58 auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
59 ASSERT_TRUE(ui_test_utils::NavigateToURL(
60 browser(), embedded_test_server()->GetURL("/compose/test2.html")));
61 ASSERT_NE(nullptr, ChromeComposeClient::FromWebContents(web_contents));
Justin DeWitteda1c0c2023-11-14 20:01:0062
63 // get point of element
64 gfx::PointF textarea_center =
65 content::GetCenterCoordinatesOfElementWithId(web_contents, "elem1");
66 autofill::FormFieldData field_data;
Christoph Schweringb470b20a2024-04-22 15:11:3867 field_data.set_bounds(gfx::RectF((textarea_center), gfx::SizeF(1, 1)));
Justin DeWitteda1c0c2023-11-14 20:01:0068
Carlos Knippschildb20a5d72024-01-04 01:06:5669 auto* client = ChromeComposeClient::FromWebContents(web_contents);
Justin DeWitteda1c0c2023-11-14 20:01:0070 client->ShowComposeDialog(
71 autofill::AutofillComposeDelegate::UiEntryPoint::kAutofillPopup,
Piotr Kotynia80af0472025-12-08 19:41:1272 field_data, base::NullCallback());
Justin DeWitteda1c0c2023-11-14 20:01:0073
74 // close window right away
75 browser()->tab_strip_model()->CloseWebContentsAt(0,
76 TabCloseTypes::CLOSE_NONE);
77}
78
Carlos Knippschildd40a3122024-06-27 01:01:5179IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest, OpenFeedbackPage) {
Anthony Cui8dc33762023-12-27 22:28:0980 // Feedback page can only be opened from a dialog state where MSSB is enabled.
81 // TODO(b/316601302): Without directly setting the MSBB pref value this test
82 // is flaky on Linux MSan builders. This requires further investigation, but
83 // the MSBB dialog state is not on the feedback page testing path so the
84 // current state still satisfies the test requirement.
85 PrefService* prefs = browser()->profile()->GetPrefs();
86 prefs->SetBoolean(
87 unified_consent::prefs::kUrlKeyedAnonymizedDataCollectionEnabled, true);
88
Trevor Perrier230b7f22023-12-02 01:33:4789 ASSERT_TRUE(embedded_test_server()->Start());
90 auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
91 ASSERT_TRUE(ui_test_utils::NavigateToURL(
92 browser(), embedded_test_server()->GetURL("/compose/test2.html")));
93 ASSERT_NE(nullptr, ChromeComposeClient::FromWebContents(web_contents));
Trevor Perrier230b7f22023-12-02 01:33:4794
95 // get point of element
96 gfx::PointF textarea_center =
97 content::GetCenterCoordinatesOfElementWithId(web_contents, "elem1");
98 autofill::FormFieldData field_data;
Christoph Schweringb470b20a2024-04-22 15:11:3899 field_data.set_bounds(gfx::RectF((textarea_center), gfx::SizeF(1, 1)));
Trevor Perrier230b7f22023-12-02 01:33:47100
Carlos Knippschildb20a5d72024-01-04 01:06:56101 auto* client = ChromeComposeClient::FromWebContents(web_contents);
Trevor Perrier230b7f22023-12-02 01:33:47102 client->ShowComposeDialog(
103 autofill::AutofillComposeDelegate::UiEntryPoint::kAutofillPopup,
Piotr Kotynia80af0472025-12-08 19:41:12104 field_data, base::NullCallback());
Trevor Perrier230b7f22023-12-02 01:33:47105
106 client->OpenFeedbackPageForTest("test_id");
107
108 RunTestSequence(
109 InAnyContext(WaitForShow(FeedbackDialog::kFeedbackDialogForTesting)));
110}
Trevor Perrier230b7f22023-12-02 01:33:47111
Sopheyf991553f2024-01-04 01:23:28112IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest,
113 TestDialogClosedAfterPageScrolled) {
114 ASSERT_TRUE(embedded_test_server()->Start());
115 auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
116 ASSERT_TRUE(ui_test_utils::NavigateToURL(
117 browser(), embedded_test_server()->GetURL("/compose/test2.html")));
118 ASSERT_NE(nullptr, ChromeComposeClient::FromWebContents(web_contents));
119 auto* client = ChromeComposeClient::FromWebContents(web_contents);
Sopheyf991553f2024-01-04 01:23:28120
121 // get point of element
122 gfx::PointF textarea_center =
123 content::GetCenterCoordinatesOfElementWithId(web_contents, "elem1");
124 autofill::FormFieldData field_data;
Christoph Schweringb470b20a2024-04-22 15:11:38125 field_data.set_bounds(gfx::RectF((textarea_center), gfx::SizeF(1, 1)));
Sopheyf991553f2024-01-04 01:23:28126
127 client->ShowComposeDialog(
128 autofill::AutofillComposeDelegate::UiEntryPoint::kAutofillPopup,
Piotr Kotynia80af0472025-12-08 19:41:12129 field_data, base::NullCallback());
Sopheyf991553f2024-01-04 01:23:28130
131 EXPECT_TRUE(client->IsDialogShowing());
132
133 // Scroll on page
134 blink::WebGestureEvent event;
135 event.SetType(blink::WebInputEvent::Type::kGestureScrollBegin);
136 client->DidGetUserInteraction(event);
137
138 EXPECT_FALSE(client->IsDialogShowing());
139}
140
Jeffrey Cohen65290a72024-01-18 20:14:57141IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest, SettingsLaunchedTest) {
142 base::UserActionTester user_action_tester;
143 ASSERT_TRUE(embedded_test_server()->Start());
144 auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
145 ASSERT_TRUE(ui_test_utils::NavigateToURL(
146 browser(), embedded_test_server()->GetURL("/compose/test2.html")));
147 ASSERT_NE(nullptr, ChromeComposeClient::FromWebContents(web_contents));
148
149 // get point of element
150 gfx::PointF textarea_center =
151 content::GetCenterCoordinatesOfElementWithId(web_contents, "elem1");
152 autofill::FormFieldData field_data;
Christoph Schweringb470b20a2024-04-22 15:11:38153 field_data.set_bounds(gfx::RectF((textarea_center), gfx::SizeF(1, 1)));
Jeffrey Cohen65290a72024-01-18 20:14:57154
155 auto* client = ChromeComposeClient::FromWebContents(web_contents);
156 client->ShowComposeDialog(
157 autofill::AutofillComposeDelegate::UiEntryPoint::kAutofillPopup,
Piotr Kotynia80af0472025-12-08 19:41:12158 field_data, base::NullCallback());
Jeffrey Cohen65290a72024-01-18 20:14:57159
160 client->OpenComposeSettings();
161
162 EXPECT_EQ(1, user_action_tester.GetActionCount(
163 "Compose.SessionPaused.MSBBSettingsShown"));
164
165 int tab_index = browser()->tab_strip_model()->count() - 1;
166
167 content::WebContentsDestroyedWatcher destroyed_watcher(
168 browser()->tab_strip_model()->GetWebContentsAt(tab_index));
169 browser()->tab_strip_model()->CloseWebContentsAt(
170 tab_index, TabCloseTypes::CLOSE_CREATE_HISTORICAL_TAB);
171 destroyed_watcher.Wait();
172
173 EXPECT_EQ(embedded_test_server()->GetURL("/compose/test2.html"),
174 web_contents->GetVisibleURL());
175 EXPECT_TRUE(client->IsDialogShowing());
176}
177
Trevor Perrier8e233902024-09-20 20:45:37178IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest,
179 PractiveNudgeSettingsLaunchedTest) {
180 base::HistogramTester histogram_tester;
181 ASSERT_TRUE(embedded_test_server()->Start());
182 auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
183 ASSERT_TRUE(ui_test_utils::NavigateToURL(
184 browser(), embedded_test_server()->GetURL("/compose/test2.html")));
185
186 auto* client = ChromeComposeClient::FromWebContents(web_contents);
187 ASSERT_NE(nullptr, client);
188
189 autofill::FormFieldData field_data;
190
191 client->OpenProactiveNudgeSettings();
192
193 ASSERT_EQ(browser()->tab_strip_model()->count(), 2);
194
195 histogram_tester.ExpectUniqueSample(
196 compose::kComposeProactiveNudgeCtr,
197 compose::ComposeNudgeCtrEvent::kOpenSettings, 1);
198}
199
200IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest,
201 SelectionNudgeSettingsLaunchedTest) {
202 base::HistogramTester histogram_tester;
203 ASSERT_TRUE(embedded_test_server()->Start());
204 auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
205 ASSERT_TRUE(ui_test_utils::NavigateToURL(
206 browser(), embedded_test_server()->GetURL("/compose/test2.html")));
207
208 auto* client = ChromeComposeClient::FromWebContents(web_contents);
209 ASSERT_NE(nullptr, client);
210
211 autofill::FormFieldData field_data;
212 autofill::FormData form_data;
213
214 // Set the most recent nudge to the selection nudge.
215 client->ShowProactiveNudge(form_data.global_id(), field_data.global_id(),
216 compose::ComposeEntryPoint::kSelectionNudge);
217 client->OpenProactiveNudgeSettings();
218
219 ASSERT_EQ(browser()->tab_strip_model()->count(), 2);
220
221 histogram_tester.ExpectUniqueSample(
222 compose::kComposeSelectionNudgeCtr,
223 compose::ComposeNudgeCtrEvent::kOpenSettings, 1);
224}
225
Justin DeWitteda1c0c2023-11-14 20:01:00226} // namespace compose