| 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 | {}); |
| 39 | InProcessBrowserTest::SetUp(); |
| 40 | } |
| 41 | |
| 42 | base::test::ScopedFeatureList* feature_list() { return &feature_list_; } |
| 43 | |
| 44 | protected: |
| 45 | base::test::ScopedFeatureList feature_list_; |
| 46 | }; |
| 47 | |
| Jeffrey Cohen | 3100ff6 | 2023-11-15 20:16:21 | [diff] [blame] | 48 | #if BUILDFLAG(IS_MAC) |
| 49 | // Mac failures: b/311208586 |
| 50 | #define MAYBE_LifetimeOfBubbleWrapper DISABLED_LifetimeOfBubbleWrapper |
| 51 | #else |
| 52 | #define MAYBE_LifetimeOfBubbleWrapper LifetimeOfBubbleWrapper |
| 53 | #endif |
| 54 | IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest, |
| 55 | MAYBE_LifetimeOfBubbleWrapper) { |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 56 | 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)); |
| 61 | auto* client = ChromeComposeClient::FromWebContents(web_contents); |
| Pete Williamson | f6e5ff2 | 2023-12-19 00:40:52 | [diff] [blame^] | 62 | client->GetComposeEnabling().SetEnabledForTesting(true); |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 63 | |
| 64 | // get point of element |
| 65 | gfx::PointF textarea_center = |
| 66 | content::GetCenterCoordinatesOfElementWithId(web_contents, "elem1"); |
| 67 | autofill::FormFieldData field_data; |
| 68 | field_data.bounds = gfx::RectF((textarea_center), gfx::SizeF(1, 1)); |
| 69 | |
| 70 | 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 Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 79 | IN_PROC_BROWSER_TEST_F(ComposeSessionBrowserTest, OpenFeedbackPage) { |
| 80 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 81 | auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); |
| 82 | ASSERT_TRUE(ui_test_utils::NavigateToURL( |
| 83 | browser(), embedded_test_server()->GetURL("/compose/test2.html"))); |
| 84 | ASSERT_NE(nullptr, ChromeComposeClient::FromWebContents(web_contents)); |
| 85 | auto* client = ChromeComposeClient::FromWebContents(web_contents); |
| Pete Williamson | f6e5ff2 | 2023-12-19 00:40:52 | [diff] [blame^] | 86 | client->GetComposeEnabling().SetEnabledForTesting(true); |
| Trevor Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 87 | |
| 88 | // get point of element |
| 89 | gfx::PointF textarea_center = |
| 90 | content::GetCenterCoordinatesOfElementWithId(web_contents, "elem1"); |
| 91 | autofill::FormFieldData field_data; |
| 92 | field_data.bounds = gfx::RectF((textarea_center), gfx::SizeF(1, 1)); |
| 93 | |
| 94 | client->ShowComposeDialog( |
| 95 | autofill::AutofillComposeDelegate::UiEntryPoint::kAutofillPopup, |
| 96 | field_data, std::nullopt, base::NullCallback()); |
| 97 | |
| 98 | client->OpenFeedbackPageForTest("test_id"); |
| 99 | |
| 100 | RunTestSequence( |
| 101 | InAnyContext(WaitForShow(FeedbackDialog::kFeedbackDialogForTesting))); |
| 102 | } |
| Trevor Perrier | 230b7f2 | 2023-12-02 01:33:47 | [diff] [blame] | 103 | |
| 104 | // Start ClientPrefsBrowserTest methods. |
| Anthony Cui | aa990bb9 | 2023-11-29 21:05:33 | [diff] [blame] | 105 | IN_PROC_BROWSER_TEST_F(ComposeClientPrefsBrowserTest, |
| 106 | GetConsentStateFromPrefs) { |
| 107 | auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); |
| 108 | ASSERT_NE(nullptr, ChromeComposeClient::FromWebContents(web_contents)); |
| 109 | auto* client = ChromeComposeClient::FromWebContents(web_contents); |
| 110 | PrefService* prefs = browser()->profile()->GetPrefs(); |
| 111 | |
| 112 | // By default both kPageContentCollectionEnabled and |
| 113 | // kPrefHasAcceptedComposeConsent should be false |
| 114 | EXPECT_EQ(client->GetConsentStateFromPrefs(), |
| 115 | compose::mojom::ConsentState::kUnset); |
| 116 | |
| 117 | // Consent enabled but not acknowledged from compose |
| 118 | prefs->SetBoolean(unified_consent::prefs::kPageContentCollectionEnabled, |
| 119 | true); |
| 120 | EXPECT_EQ(client->GetConsentStateFromPrefs(), |
| 121 | compose::mojom::ConsentState::kExternalConsented); |
| 122 | |
| 123 | #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) |
| 124 | // Consent enabled and acknowledged from compose |
| 125 | prefs->SetBoolean(prefs::kPrefHasAcceptedComposeConsent, true); |
| 126 | EXPECT_EQ(client->GetConsentStateFromPrefs(), |
| 127 | compose::mojom::ConsentState::kConsented); |
| 128 | |
| 129 | // Consent disabled since being acknowledged from compose |
| 130 | prefs->SetBoolean(unified_consent::prefs::kPageContentCollectionEnabled, |
| 131 | false); |
| 132 | EXPECT_EQ(client->GetConsentStateFromPrefs(), |
| 133 | compose::mojom::ConsentState::kUnset); |
| 134 | #endif |
| 135 | } |
| 136 | |
| 137 | IN_PROC_BROWSER_TEST_F(ComposeClientPrefsBrowserTest, ApproveConsent) { |
| 138 | auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); |
| 139 | ASSERT_NE(nullptr, ChromeComposeClient::FromWebContents(web_contents)); |
| 140 | auto* client = ChromeComposeClient::FromWebContents(web_contents); |
| 141 | PrefService* prefs = browser()->profile()->GetPrefs(); |
| 142 | |
| 143 | // By default both kPageContentCollectionEnabled and |
| 144 | // kPrefHasAcceptedComposeConsent should be false |
| 145 | EXPECT_EQ(client->GetConsentStateFromPrefs(), |
| 146 | compose::mojom::ConsentState::kUnset); |
| 147 | |
| 148 | client->ApproveConsent(); |
| 149 | ASSERT_TRUE( |
| 150 | prefs->GetBoolean(unified_consent::prefs::kPageContentCollectionEnabled)); |
| 151 | #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) |
| 152 | ASSERT_TRUE(prefs->GetBoolean(prefs::kPrefHasAcceptedComposeConsent)); |
| 153 | #endif |
| 154 | } |
| 155 | |
| 156 | IN_PROC_BROWSER_TEST_F(ComposeClientPrefsBrowserTest, |
| 157 | AcknowledgeConsentDisclaimer) { |
| 158 | auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); |
| 159 | ASSERT_NE(nullptr, ChromeComposeClient::FromWebContents(web_contents)); |
| 160 | auto* client = ChromeComposeClient::FromWebContents(web_contents); |
| 161 | PrefService* prefs = browser()->profile()->GetPrefs(); |
| 162 | |
| 163 | // By default both kPageContentCollectionEnabled and |
| 164 | // kPrefHasAcceptedComposeConsent should be false |
| 165 | EXPECT_EQ(client->GetConsentStateFromPrefs(), |
| 166 | compose::mojom::ConsentState::kUnset); |
| 167 | |
| 168 | client->AcknowledgeConsentDisclaimer(); |
| 169 | ASSERT_FALSE( |
| 170 | prefs->GetBoolean(unified_consent::prefs::kPageContentCollectionEnabled)); |
| 171 | #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) |
| 172 | ASSERT_TRUE(prefs->GetBoolean(prefs::kPrefHasAcceptedComposeConsent)); |
| 173 | #endif |
| 174 | } |
| 175 | |
| Justin DeWitt | eda1c0c | 2023-11-14 20:01:00 | [diff] [blame] | 176 | } // namespace compose |