| 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 | |
| 5 | #include "chrome/browser/compose/compose_session.h" |
| 6 | |
| 7 | #include "base/test/scoped_feature_list.h" |
| 8 | #include "chrome/app/chrome_command_ids.h" |
| 9 | #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h" |
| 10 | #include "chrome/browser/ui/browser.h" |
| 11 | #include "chrome/browser/ui/tabs/tab_enums.h" |
| 12 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| Trevor Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 13 | #include "chrome/browser/ui/webui/feedback/feedback_dialog.h" |
| Anthony Cui | aa990bb9 | 2023-11-29 21:05:33 | [diff] [blame] | 14 | #include "chrome/common/pref_names.h" |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 15 | #include "chrome/test/base/in_process_browser_test.h" |
| 16 | #include "chrome/test/base/ui_test_utils.h" |
| Trevor Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 17 | #include "chrome/test/interaction/interactive_browser_test.h" |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 18 | #include "components/compose/core/browser/compose_features.h" |
| 19 | #include "components/optimization_guide/core/optimization_guide_features.h" |
| Anthony Cui | aa990bb9 | 2023-11-29 21:05:33 | [diff] [blame] | 20 | #include "components/prefs/pref_service.h" |
| 21 | #include "components/unified_consent/pref_names.h" |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 22 | #include "content/public/browser/web_contents.h" |
| 23 | #include "content/public/test/browser_test.h" |
| 24 | #include "content/public/test/browser_test_utils.h" |
| 25 | #include "ui/gfx/geometry/point_conversions.h" |
| Trevor Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 26 | #include "ui/views/interaction/interaction_test_util_views.h" |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 27 | |
| Anthony Cui | aa990bb9 | 2023-11-29 21:05:33 | [diff] [blame] | 28 | using ComposeClientPrefsBrowserTest = InProcessBrowserTest; |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 29 | |
| Anthony Cui | aa990bb9 | 2023-11-29 21:05:33 | [diff] [blame] | 30 | namespace compose { |
| Rakina Zata Amni | 833b431 | 2023-11-29 06:13:52 | [diff] [blame] | 31 | |
| Trevor Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 32 | class ComposeSessionBrowserTest : public InteractiveBrowserTest { |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 33 | public: |
| 34 | void SetUp() override { |
| 35 | feature_list()->InitWithFeatures( |
| 36 | {compose::features::kEnableCompose, |
| 37 | optimization_guide::features::kOptimizationGuideModelExecution}, |
| 38 | {}); |
| Justin DeWitt | cf1b2c8 | 2023-12-22 02:01:13 | [diff] [blame^] | 39 | InteractiveBrowserTest::SetUp(); |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | base::test::ScopedFeatureList* feature_list() { return &feature_list_; } |
| 43 | |
| 44 | protected: |
| 45 | base::test::ScopedFeatureList feature_list_; |
| 46 | }; |
| 47 | |
| Justin DeWitt | cf1b2c8 | 2023-12-22 02:01:13 | [diff] [blame^] | 48 | IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest, LifetimeOfBubbleWrapper) { |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 49 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 50 | auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); |
| 51 | ASSERT_TRUE(ui_test_utils::NavigateToURL( |
| 52 | browser(), embedded_test_server()->GetURL("/compose/test2.html"))); |
| 53 | ASSERT_NE(nullptr, ChromeComposeClient::FromWebContents(web_contents)); |
| 54 | auto* client = ChromeComposeClient::FromWebContents(web_contents); |
| Pete Williamson | f6e5ff2 | 2023-12-19 00:40:52 | [diff] [blame] | 55 | client->GetComposeEnabling().SetEnabledForTesting(true); |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 56 | |
| 57 | // get point of element |
| 58 | gfx::PointF textarea_center = |
| 59 | content::GetCenterCoordinatesOfElementWithId(web_contents, "elem1"); |
| 60 | autofill::FormFieldData field_data; |
| 61 | field_data.bounds = gfx::RectF((textarea_center), gfx::SizeF(1, 1)); |
| 62 | |
| 63 | client->ShowComposeDialog( |
| 64 | autofill::AutofillComposeDelegate::UiEntryPoint::kAutofillPopup, |
| 65 | field_data, std::nullopt, base::NullCallback()); |
| 66 | |
| 67 | // close window right away |
| 68 | browser()->tab_strip_model()->CloseWebContentsAt(0, |
| 69 | TabCloseTypes::CLOSE_NONE); |
| 70 | } |
| 71 | |
| Trevor Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 72 | IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest, OpenFeedbackPage) { |
| 73 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 74 | auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); |
| 75 | ASSERT_TRUE(ui_test_utils::NavigateToURL( |
| 76 | browser(), embedded_test_server()->GetURL("/compose/test2.html"))); |
| 77 | ASSERT_NE(nullptr, ChromeComposeClient::FromWebContents(web_contents)); |
| 78 | auto* client = ChromeComposeClient::FromWebContents(web_contents); |
| Pete Williamson | f6e5ff2 | 2023-12-19 00:40:52 | [diff] [blame] | 79 | client->GetComposeEnabling().SetEnabledForTesting(true); |
| Trevor Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 80 | |
| 81 | // get point of element |
| 82 | gfx::PointF textarea_center = |
| 83 | content::GetCenterCoordinatesOfElementWithId(web_contents, "elem1"); |
| 84 | autofill::FormFieldData field_data; |
| 85 | field_data.bounds = gfx::RectF((textarea_center), gfx::SizeF(1, 1)); |
| 86 | |
| 87 | client->ShowComposeDialog( |
| 88 | autofill::AutofillComposeDelegate::UiEntryPoint::kAutofillPopup, |
| 89 | field_data, std::nullopt, base::NullCallback()); |
| 90 | |
| 91 | client->OpenFeedbackPageForTest("test_id"); |
| 92 | |
| 93 | RunTestSequence( |
| 94 | InAnyContext(WaitForShow(FeedbackDialog::kFeedbackDialogForTesting))); |
| 95 | } |
| Trevor Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 96 | |
| 97 | // Start ClientPrefsBrowserTest methods. |
| Anthony Cui | aa990bb9 | 2023-11-29 21:05:33 | [diff] [blame] | 98 | IN_PROC_BROWSER_TEST_F(ComposeClientPrefsBrowserTest, |
| 99 | GetConsentStateFromPrefs) { |
| 100 | auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); |
| 101 | ASSERT_NE(nullptr, ChromeComposeClient::FromWebContents(web_contents)); |
| 102 | auto* client = ChromeComposeClient::FromWebContents(web_contents); |
| 103 | PrefService* prefs = browser()->profile()->GetPrefs(); |
| 104 | |
| 105 | // By default both kPageContentCollectionEnabled and |
| 106 | // kPrefHasAcceptedComposeConsent should be false |
| 107 | EXPECT_EQ(client->GetConsentStateFromPrefs(), |
| 108 | compose::mojom::ConsentState::kUnset); |
| 109 | |
| 110 | // Consent enabled but not acknowledged from compose |
| 111 | prefs->SetBoolean(unified_consent::prefs::kPageContentCollectionEnabled, |
| 112 | true); |
| 113 | EXPECT_EQ(client->GetConsentStateFromPrefs(), |
| 114 | compose::mojom::ConsentState::kExternalConsented); |
| 115 | |
| 116 | #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) |
| 117 | // Consent enabled and acknowledged from compose |
| 118 | prefs->SetBoolean(prefs::kPrefHasAcceptedComposeConsent, true); |
| 119 | EXPECT_EQ(client->GetConsentStateFromPrefs(), |
| 120 | compose::mojom::ConsentState::kConsented); |
| 121 | |
| 122 | // Consent disabled since being acknowledged from compose |
| 123 | prefs->SetBoolean(unified_consent::prefs::kPageContentCollectionEnabled, |
| 124 | false); |
| 125 | EXPECT_EQ(client->GetConsentStateFromPrefs(), |
| 126 | compose::mojom::ConsentState::kUnset); |
| 127 | #endif |
| 128 | } |
| 129 | |
| 130 | IN_PROC_BROWSER_TEST_F(ComposeClientPrefsBrowserTest, ApproveConsent) { |
| 131 | auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); |
| 132 | ASSERT_NE(nullptr, ChromeComposeClient::FromWebContents(web_contents)); |
| 133 | auto* client = ChromeComposeClient::FromWebContents(web_contents); |
| 134 | PrefService* prefs = browser()->profile()->GetPrefs(); |
| 135 | |
| 136 | // By default both kPageContentCollectionEnabled and |
| 137 | // kPrefHasAcceptedComposeConsent should be false |
| 138 | EXPECT_EQ(client->GetConsentStateFromPrefs(), |
| 139 | compose::mojom::ConsentState::kUnset); |
| 140 | |
| 141 | client->ApproveConsent(); |
| 142 | ASSERT_TRUE( |
| 143 | prefs->GetBoolean(unified_consent::prefs::kPageContentCollectionEnabled)); |
| 144 | #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) |
| 145 | ASSERT_TRUE(prefs->GetBoolean(prefs::kPrefHasAcceptedComposeConsent)); |
| 146 | #endif |
| 147 | } |
| 148 | |
| 149 | IN_PROC_BROWSER_TEST_F(ComposeClientPrefsBrowserTest, |
| 150 | AcknowledgeConsentDisclaimer) { |
| 151 | auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); |
| 152 | ASSERT_NE(nullptr, ChromeComposeClient::FromWebContents(web_contents)); |
| 153 | auto* client = ChromeComposeClient::FromWebContents(web_contents); |
| 154 | PrefService* prefs = browser()->profile()->GetPrefs(); |
| 155 | |
| 156 | // By default both kPageContentCollectionEnabled and |
| 157 | // kPrefHasAcceptedComposeConsent should be false |
| 158 | EXPECT_EQ(client->GetConsentStateFromPrefs(), |
| 159 | compose::mojom::ConsentState::kUnset); |
| 160 | |
| 161 | client->AcknowledgeConsentDisclaimer(); |
| 162 | ASSERT_FALSE( |
| 163 | prefs->GetBoolean(unified_consent::prefs::kPageContentCollectionEnabled)); |
| 164 | #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) |
| 165 | ASSERT_TRUE(prefs->GetBoolean(prefs::kPrefHasAcceptedComposeConsent)); |
| 166 | #endif |
| 167 | } |
| 168 | |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 169 | } // namespace compose |