blob: f132eb006f23f60282aabcecb84630162b322bc3 [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
5#include "chrome/browser/compose/compose_session.h"
6
Carlos Knippschild054603e2024-01-19 21:03:057#include <optional>
8
Jeffrey Cohen65290a72024-01-18 20:14:579#include "base/test/metrics/user_action_tester.h"
Justin DeWitteda1c0c2023-11-14 20:01:0010#include "chrome/app/chrome_command_ids.h"
Carlos Knippschild054603e2024-01-19 21:03:0511#include "chrome/browser/compose/compose_enabling.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;
67 field_data.bounds = gfx::RectF((textarea_center), gfx::SizeF(1, 1));
68
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,
72 field_data, std::nullopt, base::NullCallback());
73
74 // close window right away
75 browser()->tab_strip_model()->CloseWebContentsAt(0,
76 TabCloseTypes::CLOSE_NONE);
77}
78
Trevor Perrier230b7f22023-12-02 01:33:4779IN_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;
99 field_data.bounds = gfx::RectF((textarea_center), gfx::SizeF(1, 1));
100
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,
104 field_data, std::nullopt, base::NullCallback());
105
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;
125 field_data.bounds = gfx::RectF((textarea_center), gfx::SizeF(1, 1));
126
127 client->ShowComposeDialog(
128 autofill::AutofillComposeDelegate::UiEntryPoint::kAutofillPopup,
129 field_data, std::nullopt, base::NullCallback());
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 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;
153 field_data.bounds = gfx::RectF((textarea_center), gfx::SizeF(1, 1));
154
155 auto* client = ChromeComposeClient::FromWebContents(web_contents);
156 client->ShowComposeDialog(
157 autofill::AutofillComposeDelegate::UiEntryPoint::kAutofillPopup,
158 field_data, std::nullopt, base::NullCallback());
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
Justin DeWitteda1c0c2023-11-14 20:01:00178} // namespace compose