blob: 927a613e3617011ef7dc47762239e9f339e3a522 [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"
Carlos Knippschild054603e2024-01-19 21:03:059#include "chrome/browser/compose/compose_enabling.h"
Arthur Sonzogni5bc3326c2024-02-29 19:39:0510#include "chrome/browser/compose/compose_session.h"
Justin DeWitteda1c0c2023-11-14 20:01:0011#include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
12#include "chrome/browser/ui/browser.h"
13#include "chrome/browser/ui/tabs/tab_enums.h"
14#include "chrome/browser/ui/tabs/tab_strip_model.h"
Trevor Perrier230b7f22023-12-02 01:33:4715#include "chrome/browser/ui/webui/feedback/feedback_dialog.h"
Anthony Cuiaa990bb92023-11-29 21:05:3316#include "chrome/common/pref_names.h"
Justin DeWitteda1c0c2023-11-14 20:01:0017#include "chrome/test/base/in_process_browser_test.h"
18#include "chrome/test/base/ui_test_utils.h"
Trevor Perrier230b7f22023-12-02 01:33:4719#include "chrome/test/interaction/interactive_browser_test.h"
Justin DeWitteda1c0c2023-11-14 20:01:0020#include "components/compose/core/browser/compose_features.h"
Jeffrey Cohen65290a72024-01-18 20:14:5721#include "components/feature_engagement/test/scoped_iph_feature_list.h"
Justin DeWitteda1c0c2023-11-14 20:01:0022#include "components/optimization_guide/core/optimization_guide_features.h"
Anthony Cuiaa990bb92023-11-29 21:05:3323#include "components/prefs/pref_service.h"
24#include "components/unified_consent/pref_names.h"
Justin DeWitteda1c0c2023-11-14 20:01:0025#include "content/public/browser/web_contents.h"
26#include "content/public/test/browser_test.h"
27#include "content/public/test/browser_test_utils.h"
28#include "ui/gfx/geometry/point_conversions.h"
Trevor Perrier230b7f22023-12-02 01:33:4729#include "ui/views/interaction/interaction_test_util_views.h"
Justin DeWitteda1c0c2023-11-14 20:01:0030
Anthony Cuiaa990bb92023-11-29 21:05:3331namespace compose {
Rakina Zata Amni833b4312023-11-29 06:13:5232
Trevor Perrier230b7f22023-12-02 01:33:4733class ComposeSessionBrowserTest : public InteractiveBrowserTest {
Justin DeWitteda1c0c2023-11-14 20:01:0034 public:
35 void SetUp() override {
Carlos Knippschild054603e2024-01-19 21:03:0536 scoped_compose_enabled_ = ComposeEnabling::ScopedEnableComposeForTesting();
Jeffrey Cohen65290a72024-01-18 20:14:5737 feature_list()->InitWithExistingFeatures(
Justin DeWitteda1c0c2023-11-14 20:01:0038 {compose::features::kEnableCompose,
Jeffrey Cohen65290a72024-01-18 20:14:5739 optimization_guide::features::kOptimizationGuideModelExecution,
40 feature_engagement::kIPHComposeMSBBSettingsFeature});
Justin DeWittcf1b2c82023-12-22 02:01:1341 InteractiveBrowserTest::SetUp();
Justin DeWitteda1c0c2023-11-14 20:01:0042 }
43
Carlos Knippschild054603e2024-01-19 21:03:0544 void TearDown() override {}
Carlos Knippschildb20a5d72024-01-04 01:06:5645
Jeffrey Cohen65290a72024-01-18 20:14:5746 feature_engagement::test::ScopedIphFeatureList* feature_list() {
47 return &feature_list_;
48 }
Justin DeWitteda1c0c2023-11-14 20:01:0049
50 protected:
Jeffrey Cohen65290a72024-01-18 20:14:5751 feature_engagement::test::ScopedIphFeatureList feature_list_;
Carlos Knippschild054603e2024-01-19 21:03:0552 ComposeEnabling::ScopedOverride scoped_compose_enabled_;
Justin DeWitteda1c0c2023-11-14 20:01:0053};
54
Justin DeWittcf1b2c82023-12-22 02:01:1355IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest, LifetimeOfBubbleWrapper) {
Justin DeWitteda1c0c2023-11-14 20:01:0056 ASSERT_TRUE(embedded_test_server()->Start());
57 auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
58 ASSERT_TRUE(ui_test_utils::NavigateToURL(
59 browser(), embedded_test_server()->GetURL("/compose/test2.html")));
60 ASSERT_NE(nullptr, ChromeComposeClient::FromWebContents(web_contents));
Justin DeWitteda1c0c2023-11-14 20:01:0061
62 // get point of element
63 gfx::PointF textarea_center =
64 content::GetCenterCoordinatesOfElementWithId(web_contents, "elem1");
65 autofill::FormFieldData field_data;
Christoph Schweringb470b20a2024-04-22 15:11:3866 field_data.set_bounds(gfx::RectF((textarea_center), gfx::SizeF(1, 1)));
Justin DeWitteda1c0c2023-11-14 20:01:0067
Carlos Knippschildb20a5d72024-01-04 01:06:5668 auto* client = ChromeComposeClient::FromWebContents(web_contents);
Justin DeWitteda1c0c2023-11-14 20:01:0069 client->ShowComposeDialog(
70 autofill::AutofillComposeDelegate::UiEntryPoint::kAutofillPopup,
71 field_data, std::nullopt, base::NullCallback());
72
73 // close window right away
74 browser()->tab_strip_model()->CloseWebContentsAt(0,
75 TabCloseTypes::CLOSE_NONE);
76}
77
Carlos Knippschildd40a3122024-06-27 01:01:5178IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest, OpenFeedbackPage) {
Anthony Cui8dc33762023-12-27 22:28:0979 // Feedback page can only be opened from a dialog state where MSSB is enabled.
80 // TODO(b/316601302): Without directly setting the MSBB pref value this test
81 // is flaky on Linux MSan builders. This requires further investigation, but
82 // the MSBB dialog state is not on the feedback page testing path so the
83 // current state still satisfies the test requirement.
84 PrefService* prefs = browser()->profile()->GetPrefs();
85 prefs->SetBoolean(
86 unified_consent::prefs::kUrlKeyedAnonymizedDataCollectionEnabled, true);
87
Trevor Perrier230b7f22023-12-02 01:33:4788 ASSERT_TRUE(embedded_test_server()->Start());
89 auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
90 ASSERT_TRUE(ui_test_utils::NavigateToURL(
91 browser(), embedded_test_server()->GetURL("/compose/test2.html")));
92 ASSERT_NE(nullptr, ChromeComposeClient::FromWebContents(web_contents));
Trevor Perrier230b7f22023-12-02 01:33:4793
94 // get point of element
95 gfx::PointF textarea_center =
96 content::GetCenterCoordinatesOfElementWithId(web_contents, "elem1");
97 autofill::FormFieldData field_data;
Christoph Schweringb470b20a2024-04-22 15:11:3898 field_data.set_bounds(gfx::RectF((textarea_center), gfx::SizeF(1, 1)));
Trevor Perrier230b7f22023-12-02 01:33:4799
Carlos Knippschildb20a5d72024-01-04 01:06:56100 auto* client = ChromeComposeClient::FromWebContents(web_contents);
Trevor Perrier230b7f22023-12-02 01:33:47101 client->ShowComposeDialog(
102 autofill::AutofillComposeDelegate::UiEntryPoint::kAutofillPopup,
103 field_data, std::nullopt, base::NullCallback());
104
105 client->OpenFeedbackPageForTest("test_id");
106
107 RunTestSequence(
108 InAnyContext(WaitForShow(FeedbackDialog::kFeedbackDialogForTesting)));
109}
Trevor Perrier230b7f22023-12-02 01:33:47110
Sopheyf991553f2024-01-04 01:23:28111IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest,
112 TestDialogClosedAfterPageScrolled) {
113 ASSERT_TRUE(embedded_test_server()->Start());
114 auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
115 ASSERT_TRUE(ui_test_utils::NavigateToURL(
116 browser(), embedded_test_server()->GetURL("/compose/test2.html")));
117 ASSERT_NE(nullptr, ChromeComposeClient::FromWebContents(web_contents));
118 auto* client = ChromeComposeClient::FromWebContents(web_contents);
Sopheyf991553f2024-01-04 01:23:28119
120 // get point of element
121 gfx::PointF textarea_center =
122 content::GetCenterCoordinatesOfElementWithId(web_contents, "elem1");
123 autofill::FormFieldData field_data;
Christoph Schweringb470b20a2024-04-22 15:11:38124 field_data.set_bounds(gfx::RectF((textarea_center), gfx::SizeF(1, 1)));
Sopheyf991553f2024-01-04 01:23:28125
126 client->ShowComposeDialog(
127 autofill::AutofillComposeDelegate::UiEntryPoint::kAutofillPopup,
128 field_data, std::nullopt, base::NullCallback());
129
130 EXPECT_TRUE(client->IsDialogShowing());
131
132 // Scroll on page
133 blink::WebGestureEvent event;
134 event.SetType(blink::WebInputEvent::Type::kGestureScrollBegin);
135 client->DidGetUserInteraction(event);
136
137 EXPECT_FALSE(client->IsDialogShowing());
138}
139
Jeffrey Cohen65290a72024-01-18 20:14:57140IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest, SettingsLaunchedTest) {
141 base::UserActionTester user_action_tester;
142 ASSERT_TRUE(embedded_test_server()->Start());
143 auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
144 ASSERT_TRUE(ui_test_utils::NavigateToURL(
145 browser(), embedded_test_server()->GetURL("/compose/test2.html")));
146 ASSERT_NE(nullptr, ChromeComposeClient::FromWebContents(web_contents));
147
148 // get point of element
149 gfx::PointF textarea_center =
150 content::GetCenterCoordinatesOfElementWithId(web_contents, "elem1");
151 autofill::FormFieldData field_data;
Christoph Schweringb470b20a2024-04-22 15:11:38152 field_data.set_bounds(gfx::RectF((textarea_center), gfx::SizeF(1, 1)));
Jeffrey Cohen65290a72024-01-18 20:14:57153
154 auto* client = ChromeComposeClient::FromWebContents(web_contents);
155 client->ShowComposeDialog(
156 autofill::AutofillComposeDelegate::UiEntryPoint::kAutofillPopup,
157 field_data, std::nullopt, base::NullCallback());
158
159 client->OpenComposeSettings();
160
161 EXPECT_EQ(1, user_action_tester.GetActionCount(
162 "Compose.SessionPaused.MSBBSettingsShown"));
163
164 int tab_index = browser()->tab_strip_model()->count() - 1;
165
166 content::WebContentsDestroyedWatcher destroyed_watcher(
167 browser()->tab_strip_model()->GetWebContentsAt(tab_index));
168 browser()->tab_strip_model()->CloseWebContentsAt(
169 tab_index, TabCloseTypes::CLOSE_CREATE_HISTORICAL_TAB);
170 destroyed_watcher.Wait();
171
172 EXPECT_EQ(embedded_test_server()->GetURL("/compose/test2.html"),
173 web_contents->GetVisibleURL());
174 EXPECT_TRUE(client->IsDialogShowing());
175}
176
Trevor Perrier8e233902024-09-20 20:45:37177IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest,
178 PractiveNudgeSettingsLaunchedTest) {
179 base::HistogramTester histogram_tester;
180 ASSERT_TRUE(embedded_test_server()->Start());
181 auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
182 ASSERT_TRUE(ui_test_utils::NavigateToURL(
183 browser(), embedded_test_server()->GetURL("/compose/test2.html")));
184
185 auto* client = ChromeComposeClient::FromWebContents(web_contents);
186 ASSERT_NE(nullptr, client);
187
188 autofill::FormFieldData field_data;
189
190 client->OpenProactiveNudgeSettings();
191
192 ASSERT_EQ(browser()->tab_strip_model()->count(), 2);
193
194 histogram_tester.ExpectUniqueSample(
195 compose::kComposeProactiveNudgeCtr,
196 compose::ComposeNudgeCtrEvent::kOpenSettings, 1);
197}
198
199IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest,
200 SelectionNudgeSettingsLaunchedTest) {
201 base::HistogramTester histogram_tester;
202 ASSERT_TRUE(embedded_test_server()->Start());
203 auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
204 ASSERT_TRUE(ui_test_utils::NavigateToURL(
205 browser(), embedded_test_server()->GetURL("/compose/test2.html")));
206
207 auto* client = ChromeComposeClient::FromWebContents(web_contents);
208 ASSERT_NE(nullptr, client);
209
210 autofill::FormFieldData field_data;
211 autofill::FormData form_data;
212
213 // Set the most recent nudge to the selection nudge.
214 client->ShowProactiveNudge(form_data.global_id(), field_data.global_id(),
215 compose::ComposeEntryPoint::kSelectionNudge);
216 client->OpenProactiveNudgeSettings();
217
218 ASSERT_EQ(browser()->tab_strip_model()->count(), 2);
219
220 histogram_tester.ExpectUniqueSample(
221 compose::kComposeSelectionNudgeCtr,
222 compose::ComposeNudgeCtrEvent::kOpenSettings, 1);
223}
224
Justin DeWitteda1c0c2023-11-14 20:01:00225} // namespace compose