blob: 8b8d0301cb4c4ddbdc27f0c1b9284c615fb13f04 [file] [log] [blame]
Gang Wuf3b9bea2023-08-03 07:00:391// 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
Xi Hanaded8232024-11-01 14:50:485#include "chrome/browser/auxiliary_search/auxiliary_search_provider.h"
Gang Wuf3b9bea2023-08-03 07:00:396
Gang Wu489257c7f2023-08-23 23:05:167#include "base/android/jni_android.h"
Ali Hijazie63cbaf62023-12-20 19:29:358#include "base/memory/raw_ptr.h"
Gang Wuf3b9bea2023-08-03 07:00:399#include "base/run_loop.h"
Gang Wu710034a2023-10-10 23:08:4710#include "base/test/scoped_feature_list.h"
David Maunder94180602023-08-22 16:43:4211#include "chrome/browser/android/persisted_tab_data/persisted_tab_data_android.h"
Gang Wuf3b9bea2023-08-03 07:00:3912#include "chrome/browser/android/persisted_tab_data/sensitivity_persisted_tab_data_android.h"
13#include "chrome/browser/android/tab_android.h"
Gang Wu710034a2023-10-10 23:08:4714#include "chrome/browser/flags/android/chrome_feature_list.h"
Gang Wuf3b9bea2023-08-03 07:00:3915#include "chrome/browser/profiles/profile.h"
16#include "chrome/browser/ui/android/tab_model/tab_model.h"
17#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
18#include "chrome/test/base/android/android_browser_test.h"
19#include "chrome/test/base/chrome_test_utils.h"
20#include "content/public/test/browser_test.h"
Takuto Ikutaa47d7852024-02-19 03:46:5821#include "content/public/test/browser_test_utils.h"
Gang Wu74d1be22023-10-02 17:15:5822#include "content/public/test/test_utils.h"
Gang Wuf3b9bea2023-08-03 07:00:3923#include "net/dns/mock_host_resolver.h"
24#include "net/test/embedded_test_server/embedded_test_server.h"
25#include "url/gurl.h"
Gang Wu489257c7f2023-08-23 23:05:1626#include "url/url_constants.h"
Gang Wuf3b9bea2023-08-03 07:00:3927
Gang Wu710034a2023-10-10 23:08:4728namespace {
29constexpr size_t kMaxDonatedTabs = 2;
30constexpr char kMaxDonatedTabsValue[] = "2";
31} // namespace
32
Gang Wuf3b9bea2023-08-03 07:00:3933class AuxiliarySearchProviderBrowserTest : public AndroidBrowserTest {
34 public:
Gang Wu710034a2023-10-10 23:08:4735 AuxiliarySearchProviderBrowserTest() {
36 scoped_feature_list_.InitAndEnableFeatureWithParameters(
37 chrome::android::kAuxiliarySearchDonation,
38 {{"auxiliary_search_max_donation_tab", kMaxDonatedTabsValue}});
39 }
Gang Wuf3b9bea2023-08-03 07:00:3940
41 void SetUpOnMainThread() override {
42 host_resolver()->AddRule("*", "127.0.0.1");
43 ASSERT_TRUE(embedded_test_server()->Start());
Gang Wu489257c7f2023-08-23 23:05:1644 ASSERT_TRUE(content::NavigateToURL(
45 web_contents(),
46 embedded_test_server()->GetURL("/android/google.html")));
Xi Hana5d4cde2025-02-27 01:26:3547 auxiliary_search_provider_ =
Xi Han0567519a2025-05-06 20:51:4748 std::make_unique<AuxiliarySearchProvider>(nullptr);
David Maunder94180602023-08-22 16:43:4249 PersistedTabDataAndroid::OnDeferredStartup();
Gang Wu74d1be22023-10-02 17:15:5850 content::RunAllTasksUntilIdle();
Gang Wuf3b9bea2023-08-03 07:00:3951 }
52
53 content::WebContents* web_contents() {
54 return chrome_test_utils::GetActiveWebContents(this);
55 }
56
57 AuxiliarySearchProvider* provider() {
58 return auxiliary_search_provider_.get();
59 }
60
61 Profile* profile() {
62 auto* web_contents = chrome_test_utils::GetActiveWebContents(this);
63 return Profile::FromBrowserContext(web_contents->GetBrowserContext());
64 }
65
Ali Hijazie63cbaf62023-12-20 19:29:3566 std::vector<raw_ptr<TabAndroid, VectorExperimental>> CreateOneTab(
67 bool is_sensitive) {
68 std::vector<raw_ptr<TabAndroid, VectorExperimental>> tab_vec;
Gang Wu489257c7f2023-08-23 23:05:1669 TabAndroid* tab_android = TabAndroid::FromWebContents(web_contents());
70 tab_vec.push_back(tab_android);
71 std::unique_ptr<SensitivityPersistedTabDataAndroid> sptda =
72 std::make_unique<SensitivityPersistedTabDataAndroid>(tab_android);
73 sptda->set_is_sensitive(is_sensitive);
74 tab_android->SetUserData(SensitivityPersistedTabDataAndroid::UserDataKey(),
75 std::move(sptda));
76 return tab_vec;
77 }
78
Gang Wuf3b9bea2023-08-03 07:00:3979 private:
80 std::unique_ptr<AuxiliarySearchProvider> auxiliary_search_provider_;
Gang Wu710034a2023-10-10 23:08:4781 base::test::ScopedFeatureList scoped_feature_list_;
Gang Wuf3b9bea2023-08-03 07:00:3982};
83
84IN_PROC_BROWSER_TEST_F(AuxiliarySearchProviderBrowserTest, QuerySensitiveTab) {
85 base::RunLoop run_loop;
Ali Hijazie63cbaf62023-12-20 19:29:3586 std::vector<raw_ptr<TabAndroid, VectorExperimental>> tab_vec =
87 CreateOneTab(true);
Gang Wuf3b9bea2023-08-03 07:00:3988
89 provider()->GetNonSensitiveTabsInternal(
Daniel Cheng23cdce42024-10-08 20:24:5590 tab_vec,
91 base::BindOnce(
92 [](base::OnceClosure done,
93 std::vector<base::WeakPtr<TabAndroid>> non_sensitive_tabs) {
94 EXPECT_EQ(0u, non_sensitive_tabs.size());
95 std::move(done).Run();
96 },
97 run_loop.QuitClosure()));
Gang Wuf3b9bea2023-08-03 07:00:3998 run_loop.Run();
99}
100
101IN_PROC_BROWSER_TEST_F(AuxiliarySearchProviderBrowserTest,
102 QueryNonSensitiveTab) {
103 base::RunLoop run_loop;
Ali Hijazie63cbaf62023-12-20 19:29:35104 std::vector<raw_ptr<TabAndroid, VectorExperimental>> tab_vec =
105 CreateOneTab(false);
Gang Wuf3b9bea2023-08-03 07:00:39106
107 TabModel* tab_model = TabModelList::GetTabModelForWebContents(web_contents());
108 TabAndroid* second_tab = TabAndroid::FromWebContents(web_contents());
109 std::unique_ptr<content::WebContents> contents = content::WebContents::Create(
110 content::WebContents::CreateParams(profile()));
111 content::WebContents* second_web_contents = contents.release();
Jenny Zhang5d087442024-07-24 19:02:12112 tab_model->CreateTab(second_tab, second_web_contents, /*select=*/true);
Gang Wuf3b9bea2023-08-03 07:00:39113 std::unique_ptr<SensitivityPersistedTabDataAndroid> sptda2 =
114 std::make_unique<SensitivityPersistedTabDataAndroid>(second_tab);
115 sptda2->set_is_sensitive(false);
Gang Wu489257c7f2023-08-23 23:05:16116 tab_vec.push_back(second_tab);
Gang Wuf3b9bea2023-08-03 07:00:39117
118 provider()->GetNonSensitiveTabsInternal(
Daniel Cheng23cdce42024-10-08 20:24:55119 tab_vec,
120 base::BindOnce(
121 [](base::OnceClosure done,
122 std::vector<base::WeakPtr<TabAndroid>> non_sensitive_tabs) {
123 EXPECT_EQ(2u, non_sensitive_tabs.size());
124 std::move(done).Run();
125 },
126 run_loop.QuitClosure()));
Gang Wuf3b9bea2023-08-03 07:00:39127 run_loop.Run();
128}
129
Gang Wu710034a2023-10-10 23:08:47130IN_PROC_BROWSER_TEST_F(AuxiliarySearchProviderBrowserTest,
131 QueryNonSensitiveTab_flagTest) {
132 base::RunLoop run_loop;
Ali Hijazie63cbaf62023-12-20 19:29:35133 std::vector<raw_ptr<TabAndroid, VectorExperimental>> tab_vec =
134 CreateOneTab(false);
Gang Wu710034a2023-10-10 23:08:47135
136 TabModel* tab_model = TabModelList::GetTabModelForWebContents(web_contents());
137 TabAndroid* second_tab = TabAndroid::FromWebContents(web_contents());
138 std::unique_ptr<content::WebContents> contents = content::WebContents::Create(
139 content::WebContents::CreateParams(profile()));
140 content::WebContents* second_web_contents = contents.release();
Jenny Zhang5d087442024-07-24 19:02:12141 tab_model->CreateTab(second_tab, second_web_contents, /*select=*/true);
Gang Wu710034a2023-10-10 23:08:47142 std::unique_ptr<SensitivityPersistedTabDataAndroid> sptda2 =
143 std::make_unique<SensitivityPersistedTabDataAndroid>(second_tab);
144 sptda2->set_is_sensitive(false);
145 tab_vec.push_back(second_tab);
146
147 TabAndroid* third_tab = TabAndroid::FromWebContents(web_contents());
148 contents = content::WebContents::Create(
149 content::WebContents::CreateParams(profile()));
150 content::WebContents* third_web_contents = contents.release();
Jenny Zhang5d087442024-07-24 19:02:12151 tab_model->CreateTab(third_tab, third_web_contents, /*select=*/true);
Gang Wu710034a2023-10-10 23:08:47152 std::unique_ptr<SensitivityPersistedTabDataAndroid> sptda3 =
153 std::make_unique<SensitivityPersistedTabDataAndroid>(third_tab);
154 sptda3->set_is_sensitive(false);
155 tab_vec.push_back(third_tab);
156
157 provider()->GetNonSensitiveTabsInternal(
Daniel Cheng23cdce42024-10-08 20:24:55158 tab_vec,
159 base::BindOnce(
160 [](base::OnceClosure done,
161 std::vector<base::WeakPtr<TabAndroid>> non_sensitive_tabs) {
162 // Only 2 should be here since the flag is set to 2.
163 EXPECT_EQ(kMaxDonatedTabs, non_sensitive_tabs.size());
164 std::move(done).Run();
165 },
166 run_loop.QuitClosure()));
Gang Wu710034a2023-10-10 23:08:47167 run_loop.Run();
168}
169
Gang Wuf3b9bea2023-08-03 07:00:39170IN_PROC_BROWSER_TEST_F(AuxiliarySearchProviderBrowserTest, QueryEmptyTabList) {
171 base::RunLoop run_loop;
Gang Wuf3b9bea2023-08-03 07:00:39172
173 provider()->GetNonSensitiveTabsInternal(
Ali Hijazie63cbaf62023-12-20 19:29:35174 std::vector<raw_ptr<TabAndroid, VectorExperimental>>(),
Gang Wuf3b9bea2023-08-03 07:00:39175 base::BindOnce(
176 [](base::OnceClosure done,
Daniel Cheng23cdce42024-10-08 20:24:55177 std::vector<base::WeakPtr<TabAndroid>> non_sensitive_tabs) {
178 EXPECT_EQ(0u, non_sensitive_tabs.size());
Gang Wuf3b9bea2023-08-03 07:00:39179 std::move(done).Run();
180 },
181 run_loop.QuitClosure()));
182 run_loop.Run();
183}
Gang Wu489257c7f2023-08-23 23:05:16184
185IN_PROC_BROWSER_TEST_F(AuxiliarySearchProviderBrowserTest, NativeTabTest) {
186 base::RunLoop run_loop;
187 ASSERT_TRUE(
188 content::NavigateToURL(web_contents(), GURL(url::kAboutBlankURL)));
Ali Hijazie63cbaf62023-12-20 19:29:35189 std::vector<raw_ptr<TabAndroid, VectorExperimental>> tab_vec =
190 CreateOneTab(false);
Gang Wu489257c7f2023-08-23 23:05:16191
192 provider()->GetNonSensitiveTabsInternal(
Daniel Cheng23cdce42024-10-08 20:24:55193 tab_vec,
194 base::BindOnce(
195 [](base::OnceClosure done,
196 std::vector<base::WeakPtr<TabAndroid>> non_sensitive_tabs) {
197 EXPECT_EQ(0u, non_sensitive_tabs.size());
198 std::move(done).Run();
199 },
200 run_loop.QuitClosure()));
Gang Wu489257c7f2023-08-23 23:05:16201 run_loop.Run();
202}
203
204IN_PROC_BROWSER_TEST_F(AuxiliarySearchProviderBrowserTest, FilterTabsTest) {
205 struct {
206 const GURL url;
207 bool should_be_filtered;
208 } test_cases[]{
209 {GURL(url::kAboutBlankURL), true},
210 {GURL("chrome://version"), true},
211 {GURL("chrome-native://newtab"), true},
212 {embedded_test_server()->GetURL("/android/google.html"), false},
213 };
214
215 for (const auto& test_case : test_cases) {
216 ASSERT_TRUE(content::NavigateToURL(web_contents(), test_case.url));
Ali Hijazie63cbaf62023-12-20 19:29:35217 std::vector<raw_ptr<TabAndroid, VectorExperimental>> tab_vec =
218 CreateOneTab(false);
Gang Wu489257c7f2023-08-23 23:05:16219
Daniel Cheng23cdce42024-10-08 20:24:55220 AuxiliarySearchProvider::FilterTabsByScheme(tab_vec);
221 EXPECT_EQ(test_case.should_be_filtered ? 0u : 1u, tab_vec.size());
Gang Wu489257c7f2023-08-23 23:05:16222 }
223}