-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathscreenshot_controller_unittest.cc
More file actions
635 lines (514 loc) · 24.4 KB
/
Copy pathscreenshot_controller_unittest.cc
File metadata and controls
635 lines (514 loc) · 24.4 KB
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// Copyright (c) 2026 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
#include "brave/browser/ui/screenshot/screenshot_controller.h"
#include <memory>
#include <utility>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/test/bind.h"
#include "base/test/test_future.h"
#include "base/types/expected.h"
#include "chrome/browser/image_editor/screenshot_flow.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "printing/buildflags/buildflags.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/native_ui_types.h"
#include "ui/shell_dialogs/fake_select_file_dialog.h"
namespace screenshot {
namespace {
SkBitmap MakeSolidBitmap(int width, int height, SkColor color) {
SkBitmap bm;
bm.allocN32Pixels(width, height);
SkCanvas canvas(bm);
canvas.drawColor(color);
return bm;
}
} // namespace
using Error = ScreenshotController::Error;
using Result = base::expected<base::FilePath, Error>;
class ScreenshotControllerTest : public ChromeRenderViewHostTestHarness {
protected:
// Stands in for the real (views-based) preview dialog: immediately accepts,
// as if the user clicked Download, so the existing save-dialog pipeline
// tests don't need to know about the preview step.
static void AutoConfirmPreview(
gfx::NativeWindow parent,
std::vector<uint8_t> png,
base::OnceCallback<void(std::vector<uint8_t>)> on_download,
base::OnceCallback<void(std::vector<uint8_t>)> on_copy,
base::OnceClosure on_cancel) {
std::move(on_download).Run(std::move(png));
}
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
// Install the fake select-file-dialog factory. It intercepts every
// SelectFileDialog::Create() call for the lifetime of this test.
dialog_factory_ = ui::FakeSelectFileDialog::RegisterFactory();
// Inject a simple download-dir getter so the controller never touches
// DownloadPrefs (which requires a full download-service stack).
const base::FilePath download_dir = temp_dir_.GetPath();
controller_ = std::make_unique<ScreenshotController>(
profile(), base::BindRepeating([]() { return gfx::NativeWindow(); }),
base::BindRepeating(&ScreenshotControllerTest::AutoConfirmPreview));
controller_->set_download_dir_for_testing(download_dir);
}
void TearDown() override {
controller_.reset();
ChromeRenderViewHostTestHarness::TearDown();
}
// Drives the pipeline from OnVisibleAreaCopied() onward, bypassing
// CopyFromSurface (which returns kNotImplemented in the test environment).
// Sets the controller into the same busy state that CaptureVisibleArea()
// would set before dispatching CopyFromSurface.
void InjectBitmap(SkBitmap bitmap, ScreenshotController::ResultCallback cb) {
controller_->pending_callback_ = std::move(cb);
controller_->OnVisibleAreaCopied(std::move(bitmap));
}
static std::vector<uint8_t> EncodeBitmapAsPng(const SkBitmap& bitmap) {
auto encoded = gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false);
CHECK(encoded);
return *encoded;
}
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
// Drives the pipeline from OnFullPageChunks() onward, bypassing
// CaptureFullPage and the real PrintPreviewExtractor.
void InjectChunks(
base::expected<std::vector<std::vector<uint8_t>>, std::string> chunks,
ScreenshotController::ResultCallback cb) {
controller_->pending_callback_ = std::move(cb);
controller_->OnFullPageChunks(std::move(chunks));
}
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
void SetPendingCallback(ScreenshotController::ResultCallback cb) {
controller_->pending_callback_ = std::move(cb);
}
// Like InjectBitmap(), but for a caller-owned controller instance (used to
// exercise a controller built with a custom PreviewDialogShower).
static void InjectBitmapInto(ScreenshotController* controller,
SkBitmap bitmap,
ScreenshotController::ResultCallback cb) {
controller->pending_callback_ = std::move(cb);
controller->OnVisibleAreaCopied(std::move(bitmap));
}
// Drives the pipeline from OnRegionCaptured() onward, bypassing
// CaptureSelectedArea() and the real ScreenshotFlow UI overlay.
void InjectRegionCapture(image_editor::ScreenshotCaptureResult result,
ScreenshotController::ResultCallback cb) {
controller_->pending_callback_ = std::move(cb);
controller_->OnRegionCaptured(result);
}
// Drives the pipeline from OnFullPageDevToolsCaptured() onward, bypassing
// the real DevToolsFullPageExtractor. Pass nullopt to simulate a failure.
void InjectFullPageCapture(std::optional<std::vector<uint8_t>> png_bytes,
ScreenshotController::ResultCallback cb) {
controller_->pending_callback_ = std::move(cb);
if (png_bytes) {
controller_->OnFullPageDevToolsCaptured(base::ok(std::move(*png_bytes)));
} else {
controller_->OnFullPageDevToolsCaptured(
base::unexpected(std::string("test error")));
}
}
static image_editor::ScreenshotCaptureResult MakeSuccessResult(
const SkBitmap& bitmap) {
image_editor::ScreenshotCaptureResult result;
result.result_code = image_editor::ScreenshotCaptureResultCode::SUCCESS;
result.image = gfx::Image::CreateFrom1xBitmap(bitmap);
return result;
}
static image_editor::ScreenshotCaptureResult MakeFailureResult(
image_editor::ScreenshotCaptureResultCode code) {
image_editor::ScreenshotCaptureResult result;
result.result_code = code;
return result;
}
base::ScopedTempDir temp_dir_;
raw_ptr<ui::FakeSelectFileDialog::Factory> dialog_factory_ = nullptr;
std::unique_ptr<ScreenshotController> controller_;
};
// ---------------------------------------------------------------------------
// CanCapture / early-reject tests (no bitmap injection needed)
// ---------------------------------------------------------------------------
TEST_F(ScreenshotControllerTest, CanCapture_ReturnsFalseForNullWebContents) {
EXPECT_FALSE(controller_->CanCapture(nullptr).has_value());
}
TEST_F(ScreenshotControllerTest, CanCapture_ReturnsTrueWhenViewExists) {
EXPECT_TRUE(controller_->CanCapture(web_contents()).has_value());
}
TEST_F(ScreenshotControllerTest, CanCapture_ReturnsBusyWhenPendingCallbackSet) {
base::test::TestFuture<Result> pending_callback_future;
SetPendingCallback(pending_callback_future.GetCallback());
auto result = controller_->CanCapture(web_contents());
EXPECT_EQ(result, base::unexpected(Error::kBusy));
}
TEST_F(ScreenshotControllerTest,
CaptureVisibleArea_NullWebContents_ReturnsNoTab) {
base::test::TestFuture<Result> future;
controller_->CaptureVisibleArea(nullptr, future.GetCallback());
EXPECT_EQ(future.Get(), base::unexpected(Error::kNoTab));
}
// Calling CaptureVisibleArea twice before the first completes: the second call
// should be rejected immediately with kBusy because the controller is busy.
TEST_F(ScreenshotControllerTest,
CaptureVisibleArea_AlreadyBusy_SecondCallReturnsBusy) {
base::test::TestFuture<Result> future1;
base::test::TestFuture<Result> future2;
// First call starts the async pipeline (CopyFromSurface is a no-op in tests;
// its base-class implementation calls back with kNotImplemented immediately,
// but the result is posted, not synchronous).
controller_->CaptureVisibleArea(web_contents(), future1.GetCallback());
// Second call sees busy_ == true and rejects immediately.
controller_->CaptureVisibleArea(web_contents(), future2.GetCallback());
EXPECT_EQ(future2.Get(), base::unexpected(Error::kBusy));
// Let the first pipeline drain (kNotImplemented → empty bitmap →
// kCaptureFailed).
EXPECT_EQ(future1.Get(), base::unexpected(Error::kCaptureFailed));
}
// When CopyFromSurface fails (the test environment's base-class implementation
// returns kNotImplemented), the controller should report kCaptureFailed.
TEST_F(ScreenshotControllerTest,
CaptureVisibleArea_CopyFromSurfaceFails_ReturnsCaptureFailed) {
base::test::TestFuture<Result> future;
controller_->CaptureVisibleArea(web_contents(), future.GetCallback());
EXPECT_EQ(future.Get(), base::unexpected(Error::kCaptureFailed));
}
// ---------------------------------------------------------------------------
// Pipeline tests — inject a valid bitmap to reach the dialog / write stages
// ---------------------------------------------------------------------------
TEST_F(ScreenshotControllerTest, Pipeline_EmptyBitmap_ReturnsCaptureFailed) {
base::test::TestFuture<Result> future;
InjectBitmap(SkBitmap(), future.GetCallback());
EXPECT_EQ(future.Get(), base::unexpected(Error::kCaptureFailed));
}
TEST_F(ScreenshotControllerTest,
PreviewDialog_UserCancels_ReturnsUserCancelledWithoutSaveDialog) {
base::test::TestFuture<void> preview_shown;
base::OnceClosure captured_cancel;
auto shower = base::BindLambdaForTesting(
[&](gfx::NativeWindow, std::vector<uint8_t>,
base::OnceCallback<void(std::vector<uint8_t>)>,
base::OnceCallback<void(std::vector<uint8_t>)>,
base::OnceClosure on_cancel) {
captured_cancel = std::move(on_cancel);
preview_shown.SetValue();
});
auto controller = std::make_unique<ScreenshotController>(
profile(), base::BindRepeating([]() { return gfx::NativeWindow(); }),
shower);
controller->set_download_dir_for_testing(temp_dir_.GetPath());
base::test::TestFuture<Result> future;
InjectBitmapInto(controller.get(), MakeSolidBitmap(64, 64, SK_ColorBLUE),
future.GetCallback());
ASSERT_TRUE(preview_shown.Wait());
std::move(captured_cancel).Run();
EXPECT_EQ(future.Get(), base::unexpected(Error::kUserCancelled));
EXPECT_FALSE(dialog_factory_->GetLastDialog());
}
TEST_F(ScreenshotControllerTest,
PreviewDialog_UserCopies_ReturnsSuccessWithoutSaveDialog) {
base::test::TestFuture<void> preview_shown;
std::vector<uint8_t> captured_png;
base::OnceCallback<void(std::vector<uint8_t>)> captured_copy;
auto shower = base::BindLambdaForTesting(
[&](gfx::NativeWindow, std::vector<uint8_t> png,
base::OnceCallback<void(std::vector<uint8_t>)>,
base::OnceCallback<void(std::vector<uint8_t>)> on_copy,
base::OnceClosure) {
captured_png = std::move(png);
captured_copy = std::move(on_copy);
preview_shown.SetValue();
});
auto controller = std::make_unique<ScreenshotController>(
profile(), base::BindRepeating([]() { return gfx::NativeWindow(); }),
shower);
controller->set_download_dir_for_testing(temp_dir_.GetPath());
SkBitmap bitmap = MakeSolidBitmap(64, 64, SK_ColorBLUE);
base::test::TestFuture<Result> future;
InjectBitmapInto(controller.get(), std::move(bitmap), future.GetCallback());
ASSERT_TRUE(preview_shown.Wait());
ASSERT_FALSE(captured_png.empty());
ASSERT_FALSE(captured_copy.is_null());
std::move(captured_copy).Run(std::move(captured_png));
Result result = future.Get();
ASSERT_TRUE(result.has_value());
EXPECT_TRUE(result.value().empty());
EXPECT_FALSE(dialog_factory_->GetLastDialog());
}
TEST_F(ScreenshotControllerTest,
Pipeline_UserCancelsDialog_ReturnsUserCancelled) {
// Signal when the fake dialog is opened so we can cancel it from outside
// the callback (avoiding re-entrant dialog destruction).
base::test::TestFuture<void> dialog_opened;
dialog_factory_->SetOpenCallback(dialog_opened.GetRepeatingCallback());
base::test::TestFuture<Result> future;
InjectBitmap(MakeSolidBitmap(64, 64, SK_ColorBLUE), future.GetCallback());
// Wait for the encode + path-build background tasks to complete and for
// ShowSaveDialogWithPath to open the dialog.
ASSERT_TRUE(dialog_opened.Wait());
ui::FakeSelectFileDialog* dialog = dialog_factory_->GetLastDialog();
ASSERT_TRUE(dialog);
dialog->CallFileSelectionCanceled();
EXPECT_EQ(future.Get(), base::unexpected(Error::kUserCancelled));
}
TEST_F(ScreenshotControllerTest,
Pipeline_FileSelected_WritesToDiskAndReturnsPath) {
base::test::TestFuture<void> dialog_opened;
dialog_factory_->SetOpenCallback(dialog_opened.GetRepeatingCallback());
base::test::TestFuture<Result> future;
InjectBitmap(MakeSolidBitmap(1200, 675, SK_ColorGREEN), future.GetCallback());
ASSERT_TRUE(dialog_opened.Wait());
ui::FakeSelectFileDialog* dialog = dialog_factory_->GetLastDialog();
ASSERT_TRUE(dialog);
base::FilePath save_path =
temp_dir_.GetPath().AppendASCII("test_screenshot.png");
ASSERT_TRUE(dialog->CallFileSelected(save_path, "png"));
Result result = future.Get();
ASSERT_TRUE(result.has_value());
EXPECT_EQ(result.value(), save_path);
ASSERT_TRUE(base::PathExists(save_path));
// The preview dialog only ever sees a copy of the encoded PNG for display;
// the bytes written to disk must still be the original, full-resolution
// capture.
std::optional<std::vector<uint8_t>> saved_bytes =
base::ReadFileToBytes(save_path);
ASSERT_TRUE(saved_bytes);
SkBitmap saved_bitmap = gfx::PNGCodec::Decode(*saved_bytes);
EXPECT_EQ(saved_bitmap.width(), 1200);
EXPECT_EQ(saved_bitmap.height(), 675);
}
TEST_F(ScreenshotControllerTest,
Pipeline_WriteToUnwritablePath_ReturnsWriteFailed) {
base::test::TestFuture<void> dialog_opened;
dialog_factory_->SetOpenCallback(dialog_opened.GetRepeatingCallback());
base::test::TestFuture<Result> future;
InjectBitmap(MakeSolidBitmap(64, 64, SK_ColorRED), future.GetCallback());
ASSERT_TRUE(dialog_opened.Wait());
ui::FakeSelectFileDialog* dialog = dialog_factory_->GetLastDialog();
ASSERT_TRUE(dialog);
// Select a path whose parent directory does not exist so WriteFile fails.
base::FilePath bad_path = temp_dir_.GetPath()
.AppendASCII("nonexistent_subdir")
.AppendASCII("screenshot.png");
ASSERT_TRUE(dialog->CallFileSelected(bad_path, "png"));
EXPECT_EQ(future.Get(), base::unexpected(Error::kWriteFailed));
}
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
// ---------------------------------------------------------------------------
// Full-page capture pipeline tests (OnFullPageChunks / StitchAndEncode)
// ---------------------------------------------------------------------------
TEST_F(ScreenshotControllerTest, FullPage_ExtractorError_ReturnsCaptureFailed) {
base::test::TestFuture<Result> future;
InjectChunks(base::unexpected(std::string("extractor failed")),
future.GetCallback());
EXPECT_EQ(future.Get(), base::unexpected(Error::kCaptureFailed));
}
// An empty chunks vector causes StitchAndEncode to return nullopt, which the
// controller surfaces as kEncodeFailed.
TEST_F(ScreenshotControllerTest, FullPage_EmptyChunks_ReturnsEncodeFailed) {
base::test::TestFuture<Result> future;
InjectChunks(std::vector<std::vector<uint8_t>>{}, future.GetCallback());
EXPECT_EQ(future.Get(), base::unexpected(Error::kEncodeFailed));
}
TEST_F(ScreenshotControllerTest, FullPage_SingleChunk_SavesToDisk) {
base::test::TestFuture<void> dialog_opened;
dialog_factory_->SetOpenCallback(dialog_opened.GetRepeatingCallback());
std::vector<std::vector<uint8_t>> chunks = {
EncodeBitmapAsPng(MakeSolidBitmap(64, 48, SK_ColorBLUE))};
base::test::TestFuture<Result> future;
InjectChunks(std::move(chunks), future.GetCallback());
ASSERT_TRUE(dialog_opened.Wait());
ui::FakeSelectFileDialog* dialog = dialog_factory_->GetLastDialog();
ASSERT_TRUE(dialog);
base::FilePath save_path =
temp_dir_.GetPath().AppendASCII("fullpage_single.png");
ASSERT_TRUE(dialog->CallFileSelected(save_path, "png"));
Result result = future.Get();
ASSERT_TRUE(result.has_value());
EXPECT_EQ(result.value(), save_path);
EXPECT_TRUE(base::PathExists(save_path));
}
// Two chunks of different heights should be stitched into an image whose width
// is the maximum chunk width and whose height is the sum of the chunk heights.
TEST_F(ScreenshotControllerTest,
FullPage_MultiChunk_StitchesToCorrectDimensions) {
base::test::TestFuture<void> dialog_opened;
dialog_factory_->SetOpenCallback(dialog_opened.GetRepeatingCallback());
// Chunk 0: 64×48, chunk 1: 64×32 → stitched: 64×80.
std::vector<std::vector<uint8_t>> chunks = {
EncodeBitmapAsPng(MakeSolidBitmap(64, 48, SK_ColorRED)),
EncodeBitmapAsPng(MakeSolidBitmap(64, 32, SK_ColorGREEN))};
base::test::TestFuture<Result> future;
InjectChunks(std::move(chunks), future.GetCallback());
ASSERT_TRUE(dialog_opened.Wait());
ui::FakeSelectFileDialog* dialog = dialog_factory_->GetLastDialog();
ASSERT_TRUE(dialog);
base::FilePath save_path =
temp_dir_.GetPath().AppendASCII("fullpage_multi.png");
ASSERT_TRUE(dialog->CallFileSelected(save_path, "png"));
Result result = future.Get();
ASSERT_TRUE(result.has_value());
std::optional<std::vector<uint8_t>> png_bytes =
base::ReadFileToBytes(save_path);
ASSERT_TRUE(png_bytes);
SkBitmap stitched = gfx::PNGCodec::Decode(*png_bytes);
EXPECT_EQ(stitched.width(), 64);
EXPECT_EQ(stitched.height(), 80); // 48 + 32
// Verify vertical ordering: chunk 0 (red) is drawn above chunk 1 (green).
EXPECT_EQ(stitched.getColor(0, 0), SK_ColorRED);
EXPECT_EQ(stitched.getColor(0, 48), SK_ColorGREEN);
}
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
// ---------------------------------------------------------------------------
// CaptureSelectedArea / OnRegionCaptured tests
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// CaptureFullPage — early-reject and non-PDF (ScreenshotFlow) pipeline tests
// ---------------------------------------------------------------------------
TEST_F(ScreenshotControllerTest, CaptureFullPage_NullWebContents_ReturnsNoTab) {
base::test::TestFuture<Result> future;
controller_->CaptureFullPage(nullptr, future.GetCallback());
EXPECT_EQ(future.Get(), base::unexpected(Error::kNoTab));
}
TEST_F(ScreenshotControllerTest, CaptureFullPage_AlreadyBusy_ReturnsBusy) {
base::test::TestFuture<Result> pending_callback_future;
SetPendingCallback(pending_callback_future.GetCallback());
base::test::TestFuture<Result> future;
controller_->CaptureFullPage(web_contents(), future.GetCallback());
EXPECT_EQ(future.Get(), base::unexpected(Error::kBusy));
}
TEST_F(ScreenshotControllerTest, FullPage_DevToolsError_ReturnsCaptureFailed) {
base::test::TestFuture<Result> future;
InjectFullPageCapture(std::nullopt, future.GetCallback());
EXPECT_EQ(future.Get(), base::unexpected(Error::kCaptureFailed));
}
TEST_F(ScreenshotControllerTest,
FullPage_UserCancelsDialog_ReturnsUserCancelled) {
base::test::TestFuture<void> dialog_opened;
dialog_factory_->SetOpenCallback(dialog_opened.GetRepeatingCallback());
base::test::TestFuture<Result> future;
InjectFullPageCapture(
EncodeBitmapAsPng(MakeSolidBitmap(64, 64, SK_ColorBLUE)),
future.GetCallback());
ASSERT_TRUE(dialog_opened.Wait());
ui::FakeSelectFileDialog* dialog = dialog_factory_->GetLastDialog();
ASSERT_TRUE(dialog);
dialog->CallFileSelectionCanceled();
EXPECT_EQ(future.Get(), base::unexpected(Error::kUserCancelled));
}
TEST_F(ScreenshotControllerTest,
FullPage_FileSelected_WritesToDiskAndReturnsPath) {
base::test::TestFuture<void> dialog_opened;
dialog_factory_->SetOpenCallback(dialog_opened.GetRepeatingCallback());
base::test::TestFuture<Result> future;
InjectFullPageCapture(
EncodeBitmapAsPng(MakeSolidBitmap(64, 64, SK_ColorGREEN)),
future.GetCallback());
ASSERT_TRUE(dialog_opened.Wait());
ui::FakeSelectFileDialog* dialog = dialog_factory_->GetLastDialog();
ASSERT_TRUE(dialog);
base::FilePath save_path = temp_dir_.GetPath().AppendASCII("fullpage.png");
ASSERT_TRUE(dialog->CallFileSelected(save_path, "png"));
Result result = future.Get();
ASSERT_TRUE(result.has_value());
EXPECT_EQ(result.value(), save_path);
EXPECT_TRUE(base::PathExists(save_path));
}
// ---------------------------------------------------------------------------
// CaptureSelectedArea / OnRegionCaptured tests
// ---------------------------------------------------------------------------
TEST_F(ScreenshotControllerTest,
CaptureSelectedArea_NullWebContents_ReturnsNoTab) {
base::test::TestFuture<Result> future;
controller_->CaptureSelectedArea(nullptr, future.GetCallback());
EXPECT_EQ(future.Get(), base::unexpected(Error::kNoTab));
}
TEST_F(ScreenshotControllerTest, CaptureSelectedArea_AlreadyBusy_ReturnsBusy) {
base::test::TestFuture<Result> pending_callback_future;
SetPendingCallback(pending_callback_future.GetCallback());
base::test::TestFuture<Result> future;
controller_->CaptureSelectedArea(web_contents(), future.GetCallback());
EXPECT_EQ(future.Get(), base::unexpected(Error::kBusy));
}
TEST_F(ScreenshotControllerTest, SelectedArea_EscapeExit_ReturnsCaptureFailed) {
base::test::TestFuture<Result> future;
InjectRegionCapture(
MakeFailureResult(
image_editor::ScreenshotCaptureResultCode::USER_ESCAPE_EXIT),
future.GetCallback());
EXPECT_EQ(future.Get(), base::unexpected(Error::kCaptureFailed));
}
TEST_F(ScreenshotControllerTest,
SelectedArea_NavigationExit_ReturnsCaptureFailed) {
base::test::TestFuture<Result> future;
InjectRegionCapture(
MakeFailureResult(
image_editor::ScreenshotCaptureResultCode::USER_NAVIGATED_EXIT),
future.GetCallback());
EXPECT_EQ(future.Get(), base::unexpected(Error::kCaptureFailed));
}
TEST_F(ScreenshotControllerTest,
SelectedArea_SuccessButEmptyImage_ReturnsCaptureFailed) {
base::test::TestFuture<Result> future;
// result_code is SUCCESS but image is empty (no bitmap).
image_editor::ScreenshotCaptureResult result;
result.result_code = image_editor::ScreenshotCaptureResultCode::SUCCESS;
InjectRegionCapture(std::move(result), future.GetCallback());
EXPECT_EQ(future.Get(), base::unexpected(Error::kCaptureFailed));
}
TEST_F(ScreenshotControllerTest,
SelectedArea_UserCancelsDialog_ReturnsUserCancelled) {
base::test::TestFuture<void> dialog_opened;
dialog_factory_->SetOpenCallback(dialog_opened.GetRepeatingCallback());
base::test::TestFuture<Result> future;
InjectRegionCapture(MakeSuccessResult(MakeSolidBitmap(64, 64, SK_ColorBLUE)),
future.GetCallback());
ASSERT_TRUE(dialog_opened.Wait());
ui::FakeSelectFileDialog* dialog = dialog_factory_->GetLastDialog();
ASSERT_TRUE(dialog);
dialog->CallFileSelectionCanceled();
EXPECT_EQ(future.Get(), base::unexpected(Error::kUserCancelled));
}
TEST_F(ScreenshotControllerTest,
SelectedArea_FileSelected_WritesToDiskAndReturnsPath) {
base::test::TestFuture<void> dialog_opened;
dialog_factory_->SetOpenCallback(dialog_opened.GetRepeatingCallback());
base::test::TestFuture<Result> future;
InjectRegionCapture(MakeSuccessResult(MakeSolidBitmap(64, 64, SK_ColorGREEN)),
future.GetCallback());
ASSERT_TRUE(dialog_opened.Wait());
ui::FakeSelectFileDialog* dialog = dialog_factory_->GetLastDialog();
ASSERT_TRUE(dialog);
base::FilePath save_path =
temp_dir_.GetPath().AppendASCII("selected_area.png");
ASSERT_TRUE(dialog->CallFileSelected(save_path, "png"));
Result result = future.Get();
ASSERT_TRUE(result.has_value());
EXPECT_EQ(result.value(), save_path);
EXPECT_TRUE(base::PathExists(save_path));
}
TEST_F(ScreenshotControllerTest,
SelectedArea_WriteToUnwritablePath_ReturnsWriteFailed) {
base::test::TestFuture<void> dialog_opened;
dialog_factory_->SetOpenCallback(dialog_opened.GetRepeatingCallback());
base::test::TestFuture<Result> future;
InjectRegionCapture(MakeSuccessResult(MakeSolidBitmap(64, 64, SK_ColorRED)),
future.GetCallback());
ASSERT_TRUE(dialog_opened.Wait());
ui::FakeSelectFileDialog* dialog = dialog_factory_->GetLastDialog();
ASSERT_TRUE(dialog);
base::FilePath bad_path = temp_dir_.GetPath()
.AppendASCII("nonexistent_subdir")
.AppendASCII("selected_area.png");
ASSERT_TRUE(dialog->CallFileSelected(bad_path, "png"));
EXPECT_EQ(future.Get(), base::unexpected(Error::kWriteFailed));
}
} // namespace screenshot