1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/new_tab_page/composebox/composebox_handler.h"
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/gmock_move_support.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/time/time.h"
#include "base/unguessable_token.h"
#include "base/version_info/channel.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/webui/new_tab_page/composebox/composebox.mojom.h"
#include "chrome/browser/ui/webui/new_tab_page/composebox/composebox_fieldtrial.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "components/omnibox/composebox/composebox_query.mojom.h"
#include "components/omnibox/composebox/test_composebox_query_controller.h"
#include "components/variations/variations_client.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_navigation_observer.h"
#include "mojo/public/cpp/base/unguessable_token_mojom_traits.h"
#include "mojo/public/cpp/test_support/fake_message_dispatch_context.h"
#include "mojo/public/cpp/test_support/test_utils.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
constexpr int kImageCompressionQuality = 30;
constexpr int kImageMaxArea = 1000000;
constexpr int kImageMaxHeight = 1000;
constexpr int kImageMaxWidth = 1000;
constexpr char kClientUploadDurationQueryParameter[] = "cud";
constexpr char kQuerySubmissionTimeQueryParameter[] = "qsubts";
constexpr char kQueryText[] = "query";
constexpr char kComposeboxFileDeleted[] =
"NewTabPage.Composebox.Session.File.DeletedCount";
class MockPage : public composebox::mojom::Page {
public:
MockPage() = default;
~MockPage() override = default;
mojo::PendingRemote<composebox::mojom::Page> BindAndGetRemote() {
DCHECK(!receiver_.is_bound());
return receiver_.BindNewPipeAndPassRemote();
}
void FlushForTesting() { receiver_.FlushForTesting(); }
MOCK_METHOD(void,
OnFileUploadStatusChanged,
(const base::UnguessableToken&,
composebox_query::mojom::FileUploadStatus,
std::optional<composebox_query::mojom::FileUploadErrorType>));
mojo::Receiver<composebox::mojom::Page> receiver_{this};
};
} // namespace
class MockQueryController : public TestComposeboxQueryController {
public:
explicit MockQueryController(
signin::IdentityManager* identity_manager,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
version_info::Channel channel,
std::string locale,
TemplateURLService* template_url_service,
variations::VariationsClient* variations_client,
bool send_lns_surface)
: TestComposeboxQueryController(identity_manager,
url_loader_factory,
channel,
locale,
template_url_service,
variations_client,
send_lns_surface) {}
~MockQueryController() override = default;
MOCK_METHOD(void, NotifySessionStarted, ());
MOCK_METHOD(void, NotifySessionAbandoned, ());
MOCK_METHOD(
void,
StartFileUploadFlow,
(std::unique_ptr<ComposeboxQueryController::FileInfo> file_info_mojom,
scoped_refptr<base::RefCountedBytes> file_data,
std::optional<composebox::ImageEncodingOptions> image_options));
MOCK_METHOD(bool, DeleteFile, (const base::UnguessableToken&));
MOCK_METHOD(void, ClearFiles, ());
MOCK_METHOD(FileInfo*,
GetFileInfo,
(const base::UnguessableToken& file_token));
void NotifySessionStartedBase() {
TestComposeboxQueryController::NotifySessionStarted();
}
};
class TestWebContentsDelegate : public content::WebContentsDelegate {
public:
TestWebContentsDelegate() = default;
~TestWebContentsDelegate() override = default;
// WebContentsDelegate:
content::WebContents* OpenURLFromTab(
content::WebContents* source,
const content::OpenURLParams& params,
base::OnceCallback<void(content::NavigationHandle&)>
navigation_handle_callback) override {
source->GetController().LoadURLWithParams(
content::NavigationController::LoadURLParams(params));
return source;
}
};
class MockMetricsRecorder : public ComposeboxMetricsRecorder {
public:
MockMetricsRecorder() : ComposeboxMetricsRecorder("NewTabPage.") {}
~MockMetricsRecorder() override = default;
MOCK_METHOD(void, NotifySessionStateChanged, (SessionState session_state));
};
class ComposeboxHandlerTest : public ChromeRenderViewHostTestHarness {
public:
ComposeboxHandlerTest()
: ChromeRenderViewHostTestHarness(
base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
~ComposeboxHandlerTest() override = default;
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
shared_url_loader_factory_ =
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_factory_);
// Set a default search provider for `template_url_service_`
template_url_service_ = TemplateURLServiceFactory::GetForProfile(profile());
ASSERT_TRUE(template_url_service_);
template_url_service_->Load();
TemplateURLData data;
data.SetShortName(u"Google");
data.SetKeyword(u"google.com");
data.SetURL("https://www.google.com/search?q={searchTerms}");
TemplateURL* template_url =
template_url_service_->Add(std::make_unique<TemplateURL>(data));
template_url_service_->SetUserSelectedDefaultSearchProvider(template_url);
fake_variations_client_ = std::make_unique<FakeVariationsClient>();
auto query_controller_ptr = std::make_unique<MockQueryController>(
/*identity_manager=*/nullptr, shared_url_loader_factory_,
version_info::Channel::UNKNOWN, "en-US", template_url_service_,
fake_variations_client_.get(), /*send_lns_surface=*/false);
query_controller_ = query_controller_ptr.get();
web_contents()->SetDelegate(&delegate_);
auto metrics_recorder_ptr = std::make_unique<MockMetricsRecorder>();
metrics_recorder_ = metrics_recorder_ptr.get();
handler_ = std::make_unique<ComposeboxHandler>(
mojo::PendingReceiver<composebox::mojom::PageHandler>(),
mock_page_.BindAndGetRemote(), std::move(query_controller_ptr),
std::move(metrics_recorder_ptr), web_contents());
// Set all the feature params here to keep the test consistent if future
// default values are changed.
scoped_config_.Get().enabled = true;
auto* image_upload = scoped_config_.Get()
.config.mutable_composebox()
->mutable_image_upload();
image_upload->set_downscale_max_image_size(kImageMaxArea);
image_upload->set_downscale_max_image_width(kImageMaxWidth);
image_upload->set_downscale_max_image_height(kImageMaxHeight);
image_upload->set_image_compression_quality(kImageCompressionQuality);
}
ComposeboxHandler& handler() { return *handler_; }
MockQueryController& query_controller() { return *query_controller_; }
TemplateURLService& template_url_service() { return *template_url_service_; }
base::HistogramTester& histogram_tester() { return histogram_tester_; }
MockMetricsRecorder& metrics_recorder() { return *metrics_recorder_; }
ntp_composebox::FeatureConfig& scoped_config() {
return scoped_config_.Get();
}
TestingProfile::TestingFactories GetTestingFactories() const override {
return TestingProfile::TestingFactory{
TemplateURLServiceFactory::GetInstance(),
base::BindRepeating(&TemplateURLServiceFactory::BuildInstanceFor)};
}
void SubmitQueryAndWaitForNavigation() {
content::TestNavigationObserver navigation_observer(web_contents());
handler().SubmitQuery(kQueryText, 1, false, false, false, false);
auto navigation = content::NavigationSimulator::CreateFromPending(
web_contents()->GetController());
ASSERT_TRUE(navigation);
navigation->Commit();
navigation_observer.Wait();
}
void TearDown() override {
template_url_service_ = nullptr;
query_controller_ = nullptr;
metrics_recorder_ = nullptr;
handler_.reset();
fake_variations_client_.reset();
ChromeRenderViewHostTestHarness::TearDown();
}
GURL StripTimestampsFromAimUrl(const GURL& url) {
std::string qsubts_param;
EXPECT_TRUE(net::GetValueForKeyInQuery(
url, kQuerySubmissionTimeQueryParameter, &qsubts_param));
std::string cud_param;
EXPECT_TRUE(net::GetValueForKeyInQuery(
url, kClientUploadDurationQueryParameter, &cud_param));
GURL result_url = url;
result_url = net::AppendOrReplaceQueryParameter(
result_url, kQuerySubmissionTimeQueryParameter, std::nullopt);
result_url = net::AppendOrReplaceQueryParameter(
result_url, kClientUploadDurationQueryParameter,
std::nullopt);
return result_url;
}
protected:
testing::NiceMock<MockPage> mock_page_;
private:
ntp_composebox::ScopedFeatureConfigForTesting scoped_config_;
network::TestURLLoaderFactory test_factory_;
scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory_;
TestWebContentsDelegate delegate_;
raw_ptr<TemplateURLService> template_url_service_;
std::unique_ptr<FakeVariationsClient> fake_variations_client_;
raw_ptr<MockQueryController> query_controller_;
raw_ptr<MockMetricsRecorder> metrics_recorder_;
std::unique_ptr<ComposeboxHandler> handler_;
base::HistogramTester histogram_tester_;
};
TEST_F(ComposeboxHandlerTest, SessionStarted) {
SessionState state_arg = SessionState::kNone;
EXPECT_CALL(query_controller(), NotifySessionStarted);
EXPECT_CALL(metrics_recorder(), NotifySessionStateChanged)
.WillOnce(testing::SaveArg<0>(&state_arg));
handler().NotifySessionStarted();
EXPECT_EQ(state_arg, SessionState::kSessionStarted);
}
TEST_F(ComposeboxHandlerTest, SessionAbandoned) {
SessionState state_arg = SessionState::kNone;
EXPECT_CALL(query_controller(), NotifySessionAbandoned);
EXPECT_CALL(metrics_recorder(), NotifySessionStateChanged)
.WillOnce(testing::SaveArg<0>(&state_arg));
handler().NotifySessionAbandoned();
EXPECT_EQ(state_arg, SessionState::kSessionAbandoned);
}
TEST_F(ComposeboxHandlerTest, SubmitQuery) {
// Wait until the state changes to kClusterInfoReceived.
base::RunLoop run_loop;
query_controller().set_on_query_controller_state_changed_callback(
base::BindLambdaForTesting([&](QueryControllerState state) {
if (state == QueryControllerState::kClusterInfoReceived) {
run_loop.Quit();
}
}));
std::vector<SessionState> session_states;
EXPECT_CALL(metrics_recorder(), NotifySessionStateChanged)
.Times(3)
.WillRepeatedly(testing::Invoke([&](SessionState session_state) {
session_states.push_back(session_state);
}));
// Start the session.
EXPECT_CALL(query_controller(), NotifySessionStarted)
.Times(1)
.WillOnce(testing::Invoke(
&query_controller(), &MockQueryController::NotifySessionStartedBase));
handler().NotifySessionStarted();
run_loop.Run();
SubmitQueryAndWaitForNavigation();
GURL expected_url = query_controller().CreateAimUrl(
kQueryText, /*query_start_time=*/base::Time::Now());
GURL actual_url =
web_contents()->GetController().GetLastCommittedEntry()->GetURL();
// Ensure navigation occurred.
EXPECT_EQ(StripTimestampsFromAimUrl(expected_url),
StripTimestampsFromAimUrl(actual_url));
EXPECT_THAT(session_states,
testing::ElementsAre(SessionState::kSessionStarted,
SessionState::kQuerySubmitted,
SessionState::kNavigationOccurred));
}
TEST_F(ComposeboxHandlerTest, AddFile_Pdf) {
composebox::mojom::SelectedFileInfoPtr file_info =
composebox::mojom::SelectedFileInfo::New();
file_info->file_name = "test.pdf";
file_info->selection_time = base::Time::Now();
file_info->mime_type = "application/pdf";
std::vector<uint8_t> test_data = {1, 2, 3, 4};
auto test_data_span = base::span<const uint8_t>(test_data);
mojo_base::BigBuffer file_data(test_data_span);
base::MockCallback<ComposeboxHandler::AddFileCallback> callback;
std::unique_ptr<ComposeboxQueryController::FileInfo> controller_file_info;
base::UnguessableToken callback_token;
EXPECT_CALL(query_controller(), StartFileUploadFlow)
.WillOnce(MoveArg<0>(&controller_file_info));
EXPECT_CALL(callback, Run).WillOnce(testing::SaveArg<0>(&callback_token));
handler().AddFile(std::move(file_info), std::move(file_data), callback.Get());
EXPECT_EQ(callback_token, controller_file_info->file_token_);
}
TEST_F(ComposeboxHandlerTest, AddFile_Image) {
composebox::mojom::SelectedFileInfoPtr file_info =
composebox::mojom::SelectedFileInfo::New();
file_info->file_name = "test.png";
file_info->selection_time = base::Time::Now();
file_info->mime_type = "application/image";
std::vector<uint8_t> test_data = {1, 2, 3, 4};
auto test_data_span = base::span<const uint8_t>(test_data);
mojo_base::BigBuffer file_data(test_data_span);
std::unique_ptr<ComposeboxQueryController::FileInfo> controller_file_info;
std::optional<composebox::ImageEncodingOptions> image_options;
EXPECT_CALL(query_controller(), StartFileUploadFlow)
.WillOnce(
[&](std::unique_ptr<ComposeboxQueryController::FileInfo>
file_info_arg,
auto,
std::optional<composebox::ImageEncodingOptions> options_arg) {
controller_file_info = std::move(file_info_arg);
image_options = std::move(options_arg);
});
base::MockCallback<ComposeboxHandler::AddFileCallback> callback;
base::UnguessableToken callback_token;
EXPECT_CALL(callback, Run).WillOnce(testing::SaveArg<0>(&callback_token));
handler().AddFile(std::move(file_info), std::move(file_data), callback.Get());
EXPECT_EQ(callback_token, controller_file_info->file_token_);
EXPECT_TRUE(image_options.has_value());
auto image_upload = scoped_config().Get().config.composebox().image_upload();
EXPECT_EQ(image_options->max_height,
image_upload.downscale_max_image_height());
EXPECT_EQ(image_options->max_size, image_upload.downscale_max_image_size());
EXPECT_EQ(image_options->max_width, image_upload.downscale_max_image_width());
EXPECT_EQ(image_options->compression_quality,
image_upload.image_compression_quality());
}
TEST_F(ComposeboxHandlerTest, DeleteFile_Success) {
std::string file_type = ".Image";
std::string file_status = ".NotUploaded";
std::unique_ptr<ComposeboxQueryController::FileInfo> file_info =
std::make_unique<ComposeboxQueryController::FileInfo>();
file_info->file_name = "test.png";
file_info->mime_type_ = lens::MimeType::kImage;
base::UnguessableToken delete_file_token = base::UnguessableToken::Create();
base::UnguessableToken token_arg;
EXPECT_CALL(query_controller(), DeleteFile)
.WillOnce(
testing::Invoke([&token_arg](const base::UnguessableToken& token) {
token_arg = token;
return true;
}));
EXPECT_CALL(query_controller(), GetFileInfo)
.WillOnce(
testing::Invoke([&file_info](const base::UnguessableToken& token) {
return file_info.get();
}));
handler().DeleteFile(delete_file_token);
EXPECT_EQ(delete_file_token, token_arg);
histogram_tester().ExpectTotalCount(
kComposeboxFileDeleted + file_type + file_status, 1);
}
TEST_F(ComposeboxHandlerTest, DeleteFile_FailureThrowsMessage) {
mojo::FakeMessageDispatchContext context;
mojo::test::BadMessageObserver obs;
EXPECT_CALL(query_controller(), DeleteFile).WillOnce(testing::Return(false));
handler().DeleteFile(base::UnguessableToken::Create());
EXPECT_EQ("An invalid file token was sent to DeleteFile",
obs.WaitForBadMessage());
}
TEST_F(ComposeboxHandlerTest, ClearFiles) {
EXPECT_CALL(query_controller(), ClearFiles);
handler().ClearFiles();
}
class ComposeboxHandlerFileUploadStatusTest
: public ComposeboxHandlerTest,
public testing::WithParamInterface<
composebox_query::mojom::FileUploadStatus> {};
TEST_P(ComposeboxHandlerFileUploadStatusTest, FileUploadStatusChanged) {
composebox_query::mojom::FileUploadStatus status;
EXPECT_CALL(mock_page_, OnFileUploadStatusChanged)
.Times(1)
.WillOnce(testing::Invoke(
[&status](
const base::UnguessableToken& file_token,
composebox_query::mojom::FileUploadStatus file_upload_status,
std::optional<composebox_query::mojom::FileUploadErrorType>
file_upload_error_type) { status = file_upload_status; }));
const auto expected_status = GetParam();
base::UnguessableToken token = base::UnguessableToken::Create();
handler().OnFileUploadStatusChanged(token, lens::MimeType::kPdf,
expected_status, std::nullopt);
mock_page_.FlushForTesting();
EXPECT_EQ(expected_status, status);
}
INSTANTIATE_TEST_SUITE_P(
All,
ComposeboxHandlerFileUploadStatusTest,
testing::Values(
composebox_query::mojom::FileUploadStatus::kNotUploaded,
composebox_query::mojom::FileUploadStatus::kProcessing,
composebox_query::mojom::FileUploadStatus::kValidationFailed,
composebox_query::mojom::FileUploadStatus::kUploadStarted,
composebox_query::mojom::FileUploadStatus::kUploadSuccessful,
composebox_query::mojom::FileUploadStatus::kUploadFailed,
composebox_query::mojom::FileUploadStatus::kUploadExpired));
|