| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 1 | // 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 Knippschild | 054603e | 2024-01-19 21:03:05 | [diff] [blame] | 5 | #include <optional> |
| 6 | |
| Jeffrey Cohen | 65290a7 | 2024-01-18 20:14:57 | [diff] [blame] | 7 | #include "base/test/metrics/user_action_tester.h" |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 8 | #include "chrome/app/chrome_command_ids.h" |
| Antonio Gomes | 2c0d747 | 2025-10-14 06:04:04 | [diff] [blame] | 9 | #include "chrome/browser/compose/chrome_compose_client.h" |
| Carlos Knippschild | 054603e | 2024-01-19 21:03:05 | [diff] [blame] | 10 | #include "chrome/browser/compose/compose_enabling.h" |
| Arthur Sonzogni | 5bc3326c | 2024-02-29 19:39:05 | [diff] [blame] | 11 | #include "chrome/browser/compose/compose_session.h" |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 12 | #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 Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 16 | #include "chrome/browser/ui/webui/feedback/feedback_dialog.h" |
| Anthony Cui | aa990bb9 | 2023-11-29 21:05:33 | [diff] [blame] | 17 | #include "chrome/common/pref_names.h" |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 18 | #include "chrome/test/base/in_process_browser_test.h" |
| 19 | #include "chrome/test/base/ui_test_utils.h" |
| Trevor Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 20 | #include "chrome/test/interaction/interactive_browser_test.h" |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 21 | #include "components/compose/core/browser/compose_features.h" |
| Jeffrey Cohen | 65290a7 | 2024-01-18 20:14:57 | [diff] [blame] | 22 | #include "components/feature_engagement/test/scoped_iph_feature_list.h" |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 23 | #include "components/optimization_guide/core/optimization_guide_features.h" |
| Anthony Cui | aa990bb9 | 2023-11-29 21:05:33 | [diff] [blame] | 24 | #include "components/prefs/pref_service.h" |
| 25 | #include "components/unified_consent/pref_names.h" |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 26 | #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 Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 30 | #include "ui/views/interaction/interaction_test_util_views.h" |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 31 | |
| Anthony Cui | aa990bb9 | 2023-11-29 21:05:33 | [diff] [blame] | 32 | namespace compose { |
| Rakina Zata Amni | 833b431 | 2023-11-29 06:13:52 | [diff] [blame] | 33 | |
| Trevor Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 34 | class ComposeSessionBrowserTest : public InteractiveBrowserTest { |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 35 | public: |
| 36 | void SetUp() override { |
| Carlos Knippschild | 054603e | 2024-01-19 21:03:05 | [diff] [blame] | 37 | scoped_compose_enabled_ = ComposeEnabling::ScopedEnableComposeForTesting(); |
| Jeffrey Cohen | 65290a7 | 2024-01-18 20:14:57 | [diff] [blame] | 38 | feature_list()->InitWithExistingFeatures( |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 39 | {compose::features::kEnableCompose, |
| Jeffrey Cohen | 65290a7 | 2024-01-18 20:14:57 | [diff] [blame] | 40 | optimization_guide::features::kOptimizationGuideModelExecution, |
| 41 | feature_engagement::kIPHComposeMSBBSettingsFeature}); |
| Justin DeWitt | cf1b2c8 | 2023-12-22 02:01:13 | [diff] [blame] | 42 | InteractiveBrowserTest::SetUp(); |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 43 | } |
| 44 | |
| Carlos Knippschild | 054603e | 2024-01-19 21:03:05 | [diff] [blame] | 45 | void TearDown() override {} |
| Carlos Knippschild | b20a5d7 | 2024-01-04 01:06:56 | [diff] [blame] | 46 | |
| Jeffrey Cohen | 65290a7 | 2024-01-18 20:14:57 | [diff] [blame] | 47 | feature_engagement::test::ScopedIphFeatureList* feature_list() { |
| 48 | return &feature_list_; |
| 49 | } |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 50 | |
| 51 | protected: |
| Jeffrey Cohen | 65290a7 | 2024-01-18 20:14:57 | [diff] [blame] | 52 | feature_engagement::test::ScopedIphFeatureList feature_list_; |
| Carlos Knippschild | 054603e | 2024-01-19 21:03:05 | [diff] [blame] | 53 | ComposeEnabling::ScopedOverride scoped_compose_enabled_; |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 54 | }; |
| 55 | |
| Justin DeWitt | cf1b2c8 | 2023-12-22 02:01:13 | [diff] [blame] | 56 | IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest, LifetimeOfBubbleWrapper) { |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 57 | 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 DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 62 | |
| 63 | // get point of element |
| 64 | gfx::PointF textarea_center = |
| 65 | content::GetCenterCoordinatesOfElementWithId(web_contents, "elem1"); |
| 66 | autofill::FormFieldData field_data; |
| Christoph Schwering | b470b20a | 2024-04-22 15:11:38 | [diff] [blame] | 67 | field_data.set_bounds(gfx::RectF((textarea_center), gfx::SizeF(1, 1))); |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 68 | |
| Carlos Knippschild | b20a5d7 | 2024-01-04 01:06:56 | [diff] [blame] | 69 | auto* client = ChromeComposeClient::FromWebContents(web_contents); |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 70 | client->ShowComposeDialog( |
| 71 | autofill::AutofillComposeDelegate::UiEntryPoint::kAutofillPopup, |
| Piotr Kotynia | 80af047 | 2025-12-08 19:41:12 | [diff] [blame] | 72 | field_data, base::NullCallback()); |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 73 | |
| 74 | // close window right away |
| 75 | browser()->tab_strip_model()->CloseWebContentsAt(0, |
| 76 | TabCloseTypes::CLOSE_NONE); |
| 77 | } |
| 78 | |
| Carlos Knippschild | d40a312 | 2024-06-27 01:01:51 | [diff] [blame] | 79 | IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest, OpenFeedbackPage) { |
| Anthony Cui | 8dc3376 | 2023-12-27 22:28:09 | [diff] [blame] | 80 | // 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 Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 89 | 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 Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 94 | |
| 95 | // get point of element |
| 96 | gfx::PointF textarea_center = |
| 97 | content::GetCenterCoordinatesOfElementWithId(web_contents, "elem1"); |
| 98 | autofill::FormFieldData field_data; |
| Christoph Schwering | b470b20a | 2024-04-22 15:11:38 | [diff] [blame] | 99 | field_data.set_bounds(gfx::RectF((textarea_center), gfx::SizeF(1, 1))); |
| Trevor Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 100 | |
| Carlos Knippschild | b20a5d7 | 2024-01-04 01:06:56 | [diff] [blame] | 101 | auto* client = ChromeComposeClient::FromWebContents(web_contents); |
| Trevor Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 102 | client->ShowComposeDialog( |
| 103 | autofill::AutofillComposeDelegate::UiEntryPoint::kAutofillPopup, |
| Piotr Kotynia | 80af047 | 2025-12-08 19:41:12 | [diff] [blame] | 104 | field_data, base::NullCallback()); |
| Trevor Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 105 | |
| 106 | client->OpenFeedbackPageForTest("test_id"); |
| 107 | |
| 108 | RunTestSequence( |
| 109 | InAnyContext(WaitForShow(FeedbackDialog::kFeedbackDialogForTesting))); |
| 110 | } |
| Trevor Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 111 | |
| Sophey | f991553f | 2024-01-04 01:23:28 | [diff] [blame] | 112 | IN_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); |
| Sophey | f991553f | 2024-01-04 01:23:28 | [diff] [blame] | 120 | |
| 121 | // get point of element |
| 122 | gfx::PointF textarea_center = |
| 123 | content::GetCenterCoordinatesOfElementWithId(web_contents, "elem1"); |
| 124 | autofill::FormFieldData field_data; |
| Christoph Schwering | b470b20a | 2024-04-22 15:11:38 | [diff] [blame] | 125 | field_data.set_bounds(gfx::RectF((textarea_center), gfx::SizeF(1, 1))); |
| Sophey | f991553f | 2024-01-04 01:23:28 | [diff] [blame] | 126 | |
| 127 | client->ShowComposeDialog( |
| 128 | autofill::AutofillComposeDelegate::UiEntryPoint::kAutofillPopup, |
| Piotr Kotynia | 80af047 | 2025-12-08 19:41:12 | [diff] [blame] | 129 | field_data, base::NullCallback()); |
| Sophey | f991553f | 2024-01-04 01:23:28 | [diff] [blame] | 130 | |
| 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 Cohen | 65290a7 | 2024-01-18 20:14:57 | [diff] [blame] | 141 | IN_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 Schwering | b470b20a | 2024-04-22 15:11:38 | [diff] [blame] | 153 | field_data.set_bounds(gfx::RectF((textarea_center), gfx::SizeF(1, 1))); |
| Jeffrey Cohen | 65290a7 | 2024-01-18 20:14:57 | [diff] [blame] | 154 | |
| 155 | auto* client = ChromeComposeClient::FromWebContents(web_contents); |
| 156 | client->ShowComposeDialog( |
| 157 | autofill::AutofillComposeDelegate::UiEntryPoint::kAutofillPopup, |
| Piotr Kotynia | 80af047 | 2025-12-08 19:41:12 | [diff] [blame] | 158 | field_data, base::NullCallback()); |
| Jeffrey Cohen | 65290a7 | 2024-01-18 20:14:57 | [diff] [blame] | 159 | |
| 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 Perrier | 8e23390 | 2024-09-20 20:45:37 | [diff] [blame] | 178 | IN_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 | |
| 200 | IN_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 DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 226 | } // namespace compose |