File: select_file_dialog_extension_browsertest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (980 lines) | stat: -rw-r--r-- 39,310 bytes parent folder | download
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
// Copyright 2012 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/views/select_file_dialog_extension.h"

#include <memory>

#include "ash/constants/ash_pref_names.h"
#include "ash/public/cpp/keyboard/keyboard_switches.h"
#include "ash/public/cpp/style/dark_light_mode_controller.h"
#include "ash/public/cpp/test/shell_test_api.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/gmock_callback_support.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "chrome/browser/ash/file_manager/file_manager_test_util.h"
#include "chrome/browser/ash/file_manager/volume_manager.h"
#include "chrome/browser/ash/policy/dlp/dlp_files_controller_ash.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_file_destination.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_rules_manager_factory.h"
#include "chrome/browser/chromeos/policy/dlp/test/mock_dlp_rules_manager.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/keyboard/chrome_keyboard_controller_client.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chromeos/ui/base/window_properties.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_registry_factory.h"
#include "extensions/browser/process_manager.h"
#include "extensions/test/extension_background_page_waiter.h"
#include "extensions/test/extension_test_message_listener.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "storage/browser/file_system/external_mount_points.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/aura/window.h"
#include "ui/gfx/color_palette.h"
#include "ui/shell_dialogs/select_file_dialog.h"
#include "ui/shell_dialogs/select_file_policy.h"
#include "ui/shell_dialogs/selected_file_info.h"

namespace {

class KeyboardVisibleWaiter : public ChromeKeyboardControllerClient::Observer {
 public:
  KeyboardVisibleWaiter() {
    ChromeKeyboardControllerClient::Get()->AddObserver(this);
  }
  ~KeyboardVisibleWaiter() override {
    ChromeKeyboardControllerClient::Get()->RemoveObserver(this);
  }

  void Wait() { run_loop_.Run(); }

  // ChromeKeyboardControllerClient::Observer
  void OnKeyboardVisibilityChanged(bool visible) override {
    if (visible)
      run_loop_.QuitWhenIdle();
  }

 private:
  base::RunLoop run_loop_;
};

}  // namespace

class MockSelectFileDialogListener : public ui::SelectFileDialog::Listener {
 public:
  MockSelectFileDialogListener()
      : file_selected_(false), canceled_(false), params_(nullptr) {}

  MockSelectFileDialogListener(const MockSelectFileDialogListener&) = delete;
  MockSelectFileDialogListener& operator=(const MockSelectFileDialogListener&) =
      delete;

  bool file_selected() const { return file_selected_; }
  bool canceled() const { return canceled_; }
  base::FilePath path() const { return path_; }
  void* params() const { return params_; }

  // ui::SelectFileDialog::Listener:
  void FileSelected(const base::FilePath& path,
                    int index,
                    void* params) override {
    file_selected_ = true;
    path_ = path;
    params_ = params;
    QuitMessageLoop();
  }
  void FileSelectedWithExtraInfo(const ui::SelectedFileInfo& selected_file_info,
                                 int index,
                                 void* params) override {
    FileSelected(selected_file_info.local_path, index, params);
  }
  void MultiFilesSelected(const std::vector<base::FilePath>& files,
                          void* params) override {
    QuitMessageLoop();
  }
  void FileSelectionCanceled(void* params) override {
    canceled_ = true;
    params_ = params;
    QuitMessageLoop();
  }

  void WaitForCalled() {
    message_loop_runner_ = new content::MessageLoopRunner();
    message_loop_runner_->Run();
  }

 private:
  void QuitMessageLoop() {
    if (message_loop_runner_.get())
      message_loop_runner_->Quit();
  }

  bool file_selected_;
  bool canceled_;
  base::FilePath path_;
  raw_ptr<void, ExperimentalAsh> params_;
  scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
};

// Parametrization of tests. We run tests with and
// without filt type filter enabled, and in tablet mode and in regular mode.
struct TestMode {
  TestMode(bool file_type_filter, bool tablet_mode)
      : file_type_filter(file_type_filter), tablet_mode(tablet_mode) {}

  static testing::internal::ParamGenerator<TestMode> SystemWebAppValues() {
    return ::testing::Values(TestMode(false, false), TestMode(false, true),
                             TestMode(true, false), TestMode(true, true));
  }

  bool file_type_filter;
  bool tablet_mode;
};

// TODO(b/194969976): Print human readable test names instead TestName/1, etc.
class BaseSelectFileDialogExtensionBrowserTest
    : public extensions::ExtensionBrowserTest,
      public testing::WithParamInterface<TestMode> {
 public:
  BaseSelectFileDialogExtensionBrowserTest() {
    use_file_type_filter_ = GetParam().file_type_filter;
  }

  enum DialogButtonType {
    DIALOG_BTN_OK,
    DIALOG_BTN_CANCEL
  };

  void SetUp() override {
    // Create the dialog wrapper and listener objects.
    listener_ = std::make_unique<MockSelectFileDialogListener>();
    dialog_ = new SelectFileDialogExtension(listener_.get(), nullptr);

    // One mount point will be needed. Files app looks for the "Downloads"
    // volume mount point by default, so use that.
    base::FilePath tmp_path;
    base::PathService::Get(base::DIR_TEMP, &tmp_path);
    ASSERT_TRUE(tmp_dir_.CreateUniqueTempDirUnderPath(tmp_path));
    downloads_dir_ =
        tmp_dir_.GetPath().AppendASCII("My Files").AppendASCII("Downloads");
    base::CreateDirectory(downloads_dir_);

    // Must run after our setup because it actually runs the test.
    extensions::ExtensionBrowserTest::SetUp();
  }

  void SetUpOnMainThread() override {
    extensions::ExtensionBrowserTest::SetUpOnMainThread();
    CHECK(profile());

    // Create a file system mount point for the "Downloads" directory.
    EXPECT_TRUE(
        file_manager::VolumeManager::Get(profile())
            ->RegisterDownloadsDirectoryForTesting(downloads_dir_.DirName()));
    profile()->GetPrefs()->SetFilePath(prefs::kDownloadDefaultDirectory,
                                       downloads_dir_);

    // The test resources are setup: enable and add default ChromeOS component
    // extensions now and not before: crbug.com/831074, crbug.com/804413.
    file_manager::test::AddDefaultComponentExtensionsOnMainThread(profile());
  }

  void TearDown() override {
    extensions::ExtensionBrowserTest::TearDown();

    // Delete the dialogs first since they hold a pointer to their listener.
    dialog_.reset();
    listener_.reset();
    second_dialog_.reset();
    second_listener_.reset();
  }

  void CheckJavascriptErrors() {
    content::RenderFrameHost* host = dialog_->GetPrimaryMainFrame();
    ASSERT_EQ(0, content::EvalJs(host, "window.JSErrorCount"));
  }

  void ClickElement(const std::string& selector) {
    content::RenderFrameHost* frame_host = dialog_->GetPrimaryMainFrame();

    auto* web_contents = content::WebContents::FromRenderFrameHost(frame_host);
    CHECK(web_contents);

    int x = content::EvalJs(web_contents,
                            "var bounds = document.querySelector('" + selector +
                                "').getBoundingClientRect();"
                                "Math.floor(bounds.left + bounds.width / 2);")
                .ExtractInt();

    int y = content::EvalJs(web_contents,
                            "var bounds = document.querySelector('" + selector +
                                "').getBoundingClientRect();"
                                "Math.floor(bounds.top + bounds.height / 2);")
                .ExtractInt();

    LOG(INFO) << "ClickElement " << selector << " (" << x << "," << y << ")";
    constexpr auto kButton = blink::WebMouseEvent::Button::kLeft;
    content::SimulateMouseClickAt(web_contents, 0, kButton, gfx::Point(x, y));
  }

  void OpenDialog(ui::SelectFileDialog::Type dialog_type,
                  const base::FilePath& file_path,
                  const gfx::NativeWindow& owning_window,
                  const std::string& additional_message,
                  const GURL* caller = nullptr) {
    if (GetParam().tablet_mode) {
      ash::ShellTestApi().SetTabletModeEnabledForTest(true);
    }
    // Open the file dialog: Files app will signal that it is loaded via the
    // "ready" chrome.test.sendMessage().
    ExtensionTestMessageListener init_listener("ready");

    std::unique_ptr<ExtensionTestMessageListener> additional_listener;
    if (!additional_message.empty()) {
      additional_listener =
          std::make_unique<ExtensionTestMessageListener>(additional_message);
    }

    std::u16string title;
    // Include a file type filter. This triggers additional functionality within
    // the Files app.
    ui::SelectFileDialog::FileTypeInfo file_types;
    file_types.extensions = {{"html"}};
    const ui::SelectFileDialog::FileTypeInfo* file_types_ptr =
        UseFileTypeFilter() ? &file_types : nullptr;

    dialog_->SelectFile(dialog_type, title, file_path, file_types_ptr, 0,
                        FILE_PATH_LITERAL(""), owning_window, /*params=*/this,
                        caller);
    LOG(INFO) << "Waiting for JavaScript ready message.";
    ASSERT_TRUE(init_listener.WaitUntilSatisfied());

    if (additional_listener.get()) {
      LOG(INFO) << "Waiting for JavaScript " << additional_message
                << " message.";
      ASSERT_TRUE(additional_listener->WaitUntilSatisfied());
    }

    // Dialog should be running now.
    ASSERT_TRUE(dialog_->IsRunning(owning_window));

    ASSERT_NO_FATAL_FAILURE(CheckJavascriptErrors());
  }

  bool OpenDialogIsResizable() const {
    return dialog_->IsResizeable();  // See crrev.com/600185.
  }

  void TryOpeningSecondDialog(const gfx::NativeWindow& owning_window) {
    second_listener_ = std::make_unique<MockSelectFileDialogListener>();
    second_dialog_ =
        new SelectFileDialogExtension(second_listener_.get(), nullptr);

    // The dialog type is not relevant for this test but is required: use the
    // open file dialog type.
    second_dialog_->SelectFile(
        ui::SelectFileDialog::SELECT_OPEN_FILE, std::u16string() /* title */,
        base::FilePath() /* default_path */, nullptr /* file_types */,
        0 /* file_type_index */, FILE_PATH_LITERAL("") /* default_extension */,
        owning_window, this /* params */);
  }

  void ClickJsButton(content::RenderFrameHost* frame_host,
                     DialogButtonType button_type) {
    std::string button_class =
        (button_type == DIALOG_BTN_OK) ? ".button-panel .ok" :
                                         ".button-panel .cancel";
    std::u16string script = base::ASCIIToUTF16(
        "console.log(\'Test JavaScript injected.\');"
        "document.querySelector(\'" +
        button_class + "\').click();");
    // The file selection handler code closes the dialog but does not return
    // control to JavaScript, so do not wait for the script return value.
    frame_host->ExecuteJavaScriptForTests(script, base::NullCallback());
  }

  void CloseDialog(DialogButtonType button_type,
                   const gfx::NativeWindow& owning_window) {
    // Inject JavaScript into the dialog to click the dialog |button_type|.
    content::RenderFrameHost* frame_host = dialog_->GetPrimaryMainFrame();

    ClickJsButton(frame_host, button_type);

    // Instead, wait for Listener notification that the window has closed.
    LOG(INFO) << "Waiting for window close notification.";
    listener_->WaitForCalled();

    // Dialog no longer believes it is running.
    if (owning_window)
      ASSERT_FALSE(dialog_->IsRunning(owning_window));
  }

  bool UseFileTypeFilter() { return use_file_type_filter_; }

  base::ScopedTempDir tmp_dir_;
  base::FilePath downloads_dir_;

  std::unique_ptr<MockSelectFileDialogListener> listener_;
  scoped_refptr<SelectFileDialogExtension> dialog_;

  std::unique_ptr<MockSelectFileDialogListener> second_listener_;
  scoped_refptr<SelectFileDialogExtension> second_dialog_;

  bool use_file_type_filter_;
};

// Tests FileDialog with and without file filter.
class SelectFileDialogExtensionBrowserTest
    : public BaseSelectFileDialogExtensionBrowserTest {};

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionBrowserTest, CreateAndDestroy) {
  // The browser window must exist for us to test dialog's parent window.
  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  // Before we call SelectFile, the dialog should not be running/visible.
  ASSERT_FALSE(dialog_->IsRunning(owning_window));
}

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionBrowserTest, DestroyListener) {
  // Some users of SelectFileDialog destroy their listener before cleaning
  // up the dialog.  Make sure we don't crash.
  dialog_->ListenerDestroyed();
  listener_.reset();
}

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionBrowserTest, DestroyListener2) {
  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  // Open the file dialog on the default path.
  ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE,
                                     base::FilePath(), owning_window, ""));

  // Get the Files app WebContents/Framehost, before deleting the dialog_.
  content::RenderFrameHost* frame_host = dialog_->GetPrimaryMainFrame();

  // Some users of SelectFileDialog destroy their listener before cleaning
  // up the dialog, delete the `dialog_`, however the
  // SystemFilesAppDialogDelegate will still be alive with the Files app
  // WebContents.  Make sure we don't crash.
  dialog_->ListenerDestroyed();
  listener_.reset();
  dialog_.reset();

  // This will close the FrameHost/WebContents and will try to close the
  // `dialog_`.
  ClickJsButton(frame_host, DIALOG_BTN_CANCEL);

  base::RunLoop().RunUntilIdle();
}

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionBrowserTest, CanResize) {
  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  // Open the file dialog on the default path.
  ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE,
                                     base::FilePath(), owning_window, ""));

  // The dialog should be resizable.
  ASSERT_EQ(!GetParam().tablet_mode, OpenDialogIsResizable());

  // Click the "Cancel" button. This closes the dialog thus removing it from
  // `PendingDialog::map_`. `PendingDialog::map_` otherwise prevents the dialog
  // from being destroyed on `reset()` in test TearDown and the
  // `SelectFileDialog::listener_` becomes dangling.
  CloseDialog(DIALOG_BTN_CANCEL, owning_window);
}


IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionBrowserTest,
                       SelectFileAndCancel) {
  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  // Open the file dialog on the default path.
  ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE,
                                     base::FilePath(), owning_window, ""));
  // Click the "Cancel" button.
  CloseDialog(DIALOG_BTN_CANCEL, owning_window);

  // Listener should have been informed of the cancellation.
  ASSERT_FALSE(listener_->file_selected());
  ASSERT_TRUE(listener_->canceled());
  ASSERT_EQ(this, listener_->params());
}

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionBrowserTest,
                       SelectFileAndOpen) {
  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  // Create an empty file to provide the file to open.
  const base::FilePath test_file =
      downloads_dir_.AppendASCII("file_manager_test.html");
  {
    base::ScopedAllowBlockingForTesting allow_io;
    FILE* fp = base::OpenFile(test_file, "w");
    ASSERT_TRUE(fp != nullptr);
    ASSERT_TRUE(base::CloseFile(fp));
  }

  // Open the file dialog, providing a path to the file to open so the dialog
  // will automatically select it.  Ensure the "Open" button is enabled by
  // waiting for notification from chrome.test.sendMessage().
  ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE,
                                     test_file, owning_window, "dialog-ready"));
  // Click the "Open" button.
  CloseDialog(DIALOG_BTN_OK, owning_window);

  // Listener should have been informed that the file was opened.
  ASSERT_TRUE(listener_->file_selected());
  ASSERT_FALSE(listener_->canceled());
  ASSERT_EQ(test_file, listener_->path());
  ASSERT_EQ(this, listener_->params());
}

// TODO(https://crbug.com/1395419): Re-enable this test
IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionBrowserTest,
                       DISABLED_SelectFileAndSave) {
  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  // Open the file dialog to save a file, providing a suggested file path.
  // Ensure the "Save" button is enabled by waiting for notification from
  // chrome.test.sendMessage().
  const base::FilePath test_file =
      downloads_dir_.AppendASCII("file_manager_save.html");
  ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_SAVEAS_FILE,
                                     test_file, owning_window, "dialog-ready"));
  // Click the "Save" button.
  CloseDialog(DIALOG_BTN_OK, owning_window);

  // Listener should have been informed that the file was saved.
  ASSERT_TRUE(listener_->file_selected());
  ASSERT_FALSE(listener_->canceled());
  ASSERT_EQ(test_file, listener_->path());
  ASSERT_EQ(this, listener_->params());
}

// TODO(https://crbug.com/1395419): Re-enable this test
IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionBrowserTest,
                       DISABLED_SelectFileVirtualKeyboard) {
  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  // Enable the virtual keyboard.
  ash::ShellTestApi().EnableVirtualKeyboard();

  auto* client = ChromeKeyboardControllerClient::Get();
  EXPECT_FALSE(client->is_keyboard_visible());

  // Open the file dialog to save a file, providing a suggested file path.
  // Ensure the "Save" button is enabled by waiting for notification from
  // chrome.test.sendMessage().
  const base::FilePath test_file =
      downloads_dir_.AppendASCII("file_manager_save.html");
  ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_SAVEAS_FILE,
                                     test_file, owning_window, "dialog-ready"));

  // Click the dialog's filename input element.
  ASSERT_NO_FATAL_FAILURE(ClickElement("#filename-input-textbox"));

  // The virtual keyboard should be shown.
  KeyboardVisibleWaiter().Wait();
  EXPECT_TRUE(client->is_keyboard_visible());
}

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionBrowserTest,
                       OpenSingletonTabAndCancel) {
  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  // Open the file dialog on the default path.
  ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE,
                                     base::FilePath(), owning_window, ""));

  // Open a singleton tab in background.
  NavigateParams p(browser(), GURL("http://www.google.com"),
                   ui::PAGE_TRANSITION_LINK);
  p.window_action = NavigateParams::SHOW_WINDOW;
  p.disposition = WindowOpenDisposition::SINGLETON_TAB;
  Navigate(&p);

  // Click the "Cancel" button.
  CloseDialog(DIALOG_BTN_CANCEL, owning_window);

  // Listener should have been informed of the cancellation.
  ASSERT_FALSE(listener_->file_selected());
  ASSERT_TRUE(listener_->canceled());
  ASSERT_EQ(this, listener_->params());
}

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionBrowserTest, OpenTwoDialogs) {
  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  // Open the file dialog on the default path.
  ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE,
                                     base::FilePath(), owning_window, ""));

  // Requests to open a second file dialog should fail.
  TryOpeningSecondDialog(owning_window);
  ASSERT_FALSE(second_dialog_->IsRunning(owning_window));

  // Click the "Cancel" button.
  CloseDialog(DIALOG_BTN_CANCEL, owning_window);

  // Listener should have been informed of the cancellation.
  ASSERT_FALSE(listener_->file_selected());
  ASSERT_TRUE(listener_->canceled());
  ASSERT_EQ(this, listener_->params());
}

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionBrowserTest, FileInputElement) {
  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  // Start the embedded test server.
  base::FilePath source_dir;
  ASSERT_TRUE(
      base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &source_dir));
  auto test_data_dir = source_dir.AppendASCII("chrome")
                           .AppendASCII("test")
                           .AppendASCII("data")
                           .AppendASCII("chromeos")
                           .AppendASCII("file_manager");
  embedded_test_server()->ServeFilesFromDirectory(test_data_dir);
  ASSERT_TRUE(embedded_test_server()->Start());

  // Navigate the browser to the file input element test page.
  const GURL url = embedded_test_server()->GetURL("/file_input/element.html");
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_EQ(url, web_contents->GetLastCommittedURL());

  // Create a listener for the file dialog's "ready" message.
  ExtensionTestMessageListener listener("ready");

  // Click the file <input> element to open the file dialog.
  constexpr auto kButton = blink::WebMouseEvent::Button::kLeft;
  content::SimulateMouseClickAt(web_contents, 0, kButton, gfx::Point(0, 0));

  // Wait for file dialog's "ready" message.
  EXPECT_TRUE(listener.WaitUntilSatisfied());
}

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionBrowserTest,
                       OpenDialogWithoutOwningWindow) {
  gfx::NativeWindow owning_window = gfx::NativeWindow();

  // Open the file dialog with no |owning_window|.
  ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE,
                                     base::FilePath(), owning_window, ""));

  // Click the "Cancel" button.
  CloseDialog(DIALOG_BTN_CANCEL, owning_window);

  // Listener should have been informed of the cancellation.
  ASSERT_TRUE(listener_->canceled());
  ASSERT_EQ(this, listener_->params());
}

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionBrowserTest, MultipleOpenFile) {
  // No use-after-free when Browser::OpenFile is called multiple times.
  browser()->OpenFile();
  browser()->OpenFile();
}

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionBrowserTest,
                       DialogCallerSetWhenPassed) {
  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  const std::string url = "https://example.com/";
  const GURL caller = GURL(url);

  // Open the file dialog on the default path.
  ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE,
                                     base::FilePath(), owning_window, "",
                                     &caller));

  // Check that the caller field is set correctly.
  ASSERT_TRUE(dialog_->owner_.dialog_caller.has_value());
  ASSERT_EQ(dialog_->owner_.dialog_caller->url().value(), url);

  // Click the "Cancel" button.
  CloseDialog(DIALOG_BTN_CANCEL, owning_window);

  // Listener should have been informed of the cancellation.
  ASSERT_FALSE(listener_->file_selected());
  ASSERT_TRUE(listener_->canceled());
  ASSERT_EQ(this, listener_->params());
}

INSTANTIATE_TEST_SUITE_P(SystemWebApp,
                         SelectFileDialogExtensionBrowserTest,
                         TestMode::SystemWebAppValues());

// Tests that ash window has correct colors for GM2.
// TODO(adanilo) factor out the unnecessary override of Setup().
class SelectFileDialogExtensionFlagTest
    : public BaseSelectFileDialogExtensionBrowserTest {
  void SetUp() override {
    BaseSelectFileDialogExtensionBrowserTest::SetUp();
  }
};

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionFlagTest,
                       DialogColoredTitle_Light) {
  ash::DarkLightModeController::Get()->SetDarkModeEnabledForTest(false);
  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  // Open the file dialog on the default path.
  ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE,
                                     base::FilePath(), owning_window, ""));
  content::RenderFrameHost* frame_host = dialog_->GetPrimaryMainFrame();
  aura::Window* dialog_window =
      frame_host->GetNativeView()->GetToplevelWindow();
  // This is cros_tokens::kDialogTitleBarColorLight
  SkColor dialog_title_bar_color = SkColorSetRGB(0xDF, 0xE0, 0xE1);
  EXPECT_EQ(dialog_window->GetProperty(chromeos::kFrameActiveColorKey),
            dialog_title_bar_color);
  EXPECT_EQ(dialog_window->GetProperty(chromeos::kFrameInactiveColorKey),
            dialog_title_bar_color);

  CloseDialog(DIALOG_BTN_CANCEL, owning_window);
}

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionFlagTest,
                       DialogColoredTitle_Dark) {
  ash::DarkLightModeController::Get()->SetDarkModeEnabledForTest(true);
  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  // Open the file dialog on the default path.
  ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE,
                                     base::FilePath(), owning_window, ""));
  content::RenderFrameHost* frame_host = dialog_->GetPrimaryMainFrame();
  aura::Window* dialog_window =
      frame_host->GetNativeView()->GetToplevelWindow();
  // This is cros_tokens::kDialogTitleBarColorDark
  SkColor dialog_title_bar_color = SkColorSetRGB(0x4D, 0x4D, 0x50);
  EXPECT_EQ(dialog_window->GetProperty(chromeos::kFrameActiveColorKey),
            dialog_title_bar_color);
  EXPECT_EQ(dialog_window->GetProperty(chromeos::kFrameInactiveColorKey),
            dialog_title_bar_color);

  CloseDialog(DIALOG_BTN_CANCEL, owning_window);
}

INSTANTIATE_TEST_SUITE_P(SystemWebApp,
                         SelectFileDialogExtensionFlagTest,
                         TestMode::SystemWebAppValues());

using SelectFileDialogExtensionDarkLightModeEnabledTest =
    BaseSelectFileDialogExtensionBrowserTest;

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionDarkLightModeEnabledTest,
                       ColorModeChange) {
  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  // Open the file dialog on the default path.
  ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE,
                                     base::FilePath(), owning_window, ""));
  content::RenderFrameHost* frame_host = dialog_->GetPrimaryMainFrame();
  aura::Window* dialog_window =
      frame_host->GetNativeView()->GetToplevelWindow();

  auto* dark_light_mode_controller = ash::DarkLightModeController::Get();
  bool dark_mode_enabled = dark_light_mode_controller->IsDarkModeEnabled();
  SkColor initial_active_color =
      dialog_window->GetProperty(chromeos::kFrameActiveColorKey);
  SkColor initial_inactive_color =
      dialog_window->GetProperty(chromeos::kFrameInactiveColorKey);
  Profile* profile = chrome_test_utils::GetProfile(this);
  PrefService* prefs = profile->GetPrefs();

  // Switch the color mode.
  prefs->SetBoolean(ash::prefs::kDarkModeEnabled, !dark_mode_enabled);
  EXPECT_EQ(!dark_mode_enabled,
            dark_light_mode_controller->IsDarkModeEnabled());

  // Active and inactive colors in the other mode should be different from the
  // initial mode.
  EXPECT_NE(dialog_window->GetProperty(chromeos::kFrameActiveColorKey),
            initial_active_color);
  EXPECT_NE(dialog_window->GetProperty(chromeos::kFrameInactiveColorKey),
            initial_inactive_color);

  CloseDialog(DIALOG_BTN_CANCEL, owning_window);
}

INSTANTIATE_TEST_SUITE_P(SystemWebApp,
                         SelectFileDialogExtensionDarkLightModeEnabledTest,
                         TestMode::SystemWebAppValues());

// Tests applying policies before notifying listeners.
class SelectFileDialogExtensionPolicyTest
    : public BaseSelectFileDialogExtensionBrowserTest {
 protected:
  class MockFilesController : public policy::DlpFilesControllerAsh {
   public:
    explicit MockFilesController(const policy::DlpRulesManager& rules_manager,
                                 Profile* profile)
        : DlpFilesControllerAsh(rules_manager, profile) {}
    ~MockFilesController() override = default;

    MOCK_METHOD(void,
                CheckIfDownloadAllowed,
                (const policy::DlpFileDestination&,
                 const base::FilePath&,
                 base::OnceCallback<void(bool)>),
                (override));
    MOCK_METHOD(void,
                FilterDisallowedUploads,
                (std::vector<ui::SelectedFileInfo>,
                 const policy::DlpFileDestination&,
                 base::OnceCallback<void(std::vector<ui::SelectedFileInfo>)>),
                (override));
  };

  void TearDownOnMainThread() override {
    // Make sure the rules manager does not return a freed files controller.
    ON_CALL(*rules_manager_, GetDlpFilesController)
        .WillByDefault(testing::Return(nullptr));

    // The files controller must be destroyed before the profile since it's
    // holding a pointer to it.
    mock_files_controller_.reset();
    BaseSelectFileDialogExtensionBrowserTest::TearDownOnMainThread();
  }

  std::unique_ptr<KeyedService> SetDlpRulesManager(
      content::BrowserContext* context) {
    auto dlp_rules_manager =
        std::make_unique<testing::NiceMock<policy::MockDlpRulesManager>>(
            Profile::FromBrowserContext(context));
    rules_manager_ = dlp_rules_manager.get();

    mock_files_controller_ = std::make_unique<MockFilesController>(
        *rules_manager_, Profile::FromBrowserContext(context));
    ON_CALL(*rules_manager_, GetDlpFilesController)
        .WillByDefault(testing::Return(mock_files_controller_.get()));

    return dlp_rules_manager;
  }

  void SetupRulesManager() {
    policy::DlpRulesManagerFactory::GetInstance()->SetTestingFactory(
        profile(), base::BindRepeating(
                       &SelectFileDialogExtensionPolicyTest::SetDlpRulesManager,
                       base::Unretained(this)));
    ASSERT_TRUE(policy::DlpRulesManagerFactory::GetForPrimaryProfile());

    ON_CALL(*rules_manager_, IsFilesPolicyEnabled)
        .WillByDefault(testing::Return(true));
  }

  raw_ptr<policy::MockDlpRulesManager, DanglingUntriaged | ExperimentalAsh>
      rules_manager_ = nullptr;
  std::unique_ptr<MockFilesController> mock_files_controller_ = nullptr;
  raw_ptr<storage::ExternalMountPoints, ExperimentalAsh> mount_points_ =
      nullptr;
};

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionPolicyTest, DlpDownloadAllow) {
  SetupRulesManager();

  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  const std::string url = "https://example.com/";
  const GURL caller = GURL(url);

  // Open the file dialog to save a file, providing a suggested file path.
  // Ensure the "Save" button is enabled by waiting for notification from
  // chrome.test.sendMessage().
  const base::FilePath test_file =
      downloads_dir_.AppendASCII("file_manager_save.html");
  ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_SAVEAS_FILE,
                                     test_file, owning_window, "dialog-ready",
                                     &caller));

  EXPECT_CALL(
      *mock_files_controller_.get(),
      CheckIfDownloadAllowed(policy::DlpFileDestination(caller), test_file,
                             base::test::IsNotNullCallback()))
      .WillOnce(base::test::RunOnceCallback<2>(true));

  // Click the "Save" button.
  CloseDialog(DIALOG_BTN_OK, owning_window);

  // Listener should have been informed of the selection.
  ASSERT_TRUE(listener_->file_selected());
  ASSERT_FALSE(listener_->canceled());
  ASSERT_EQ(this, listener_->params());
}

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionPolicyTest, DlpDownloadBlock) {
  SetupRulesManager();

  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  const std::string url = "https://example.com/";
  const GURL caller = GURL(url);

  // Open the file dialog to save a file, providing a suggested file path.
  // Ensure the "Save" button is enabled by waiting for notification from
  // chrome.test.sendMessage().
  const base::FilePath test_file =
      downloads_dir_.AppendASCII("file_manager_save.html");
  ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_SAVEAS_FILE,
                                     test_file, owning_window, "dialog-ready",
                                     &caller));

  EXPECT_CALL(
      *mock_files_controller_.get(),
      CheckIfDownloadAllowed(policy::DlpFileDestination(caller), test_file,
                             base::test::IsNotNullCallback()))
      .WillOnce(base::test::RunOnceCallback<2>(false));

  // Click the "Save" button.
  CloseDialog(DIALOG_BTN_OK, owning_window);

  // Listener should have been informed of the cancellation.
  ASSERT_FALSE(listener_->file_selected());
  ASSERT_TRUE(listener_->canceled());
  ASSERT_EQ(this, listener_->params());
}

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionPolicyTest, DlpUploadAllow) {
  SetupRulesManager();

  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  // Create an empty file to provide the file to open.
  const base::FilePath test_file =
      downloads_dir_.AppendASCII("file_manager_test.html");
  {
    base::ScopedAllowBlockingForTesting allow_io;
    FILE* fp = base::OpenFile(test_file, "w");
    ASSERT_TRUE(fp != nullptr);
    ASSERT_TRUE(base::CloseFile(fp));
  }
  base::FilePath test_file_virtual_path;
  ASSERT_TRUE(storage::ExternalMountPoints::GetSystemInstance()->GetVirtualPath(
      test_file, &test_file_virtual_path));

  const std::string url = "https://example.com/";
  const GURL caller = GURL(url);

  // Open the file dialog, providing a path to the file to open so the dialog
  // will automatically select it.  Ensure the "Open" button is enabled by
  // waiting for notification from chrome.test.sendMessage().
  ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE,
                                     test_file, owning_window, "dialog-ready",
                                     &caller));

  std::vector<ui::SelectedFileInfo> selected_files;
  auto selected_file = ui::SelectedFileInfo(test_file, test_file);
  selected_file.virtual_path = test_file_virtual_path;
  selected_files.push_back(std::move(selected_file));
  EXPECT_CALL(*mock_files_controller_.get(),
              FilterDisallowedUploads(selected_files,
                                      policy::DlpFileDestination(caller),
                                      base::test::IsNotNullCallback()))
      .WillOnce(base::test::RunOnceCallback<2>(selected_files));

  // Click the "Save" button.
  CloseDialog(DIALOG_BTN_OK, owning_window);

  // Listener should have been informed of the selection.
  ASSERT_TRUE(listener_->file_selected());
  ASSERT_FALSE(listener_->canceled());
  ASSERT_EQ(this, listener_->params());
}

IN_PROC_BROWSER_TEST_P(SelectFileDialogExtensionPolicyTest, DlpUploadBlock) {
  SetupRulesManager();

  gfx::NativeWindow owning_window = browser()->window()->GetNativeWindow();
  ASSERT_NE(nullptr, owning_window);

  // Create an empty file to provide the file to open.
  const base::FilePath test_file =
      downloads_dir_.AppendASCII("file_manager_test.html");
  {
    base::ScopedAllowBlockingForTesting allow_io;
    FILE* fp = base::OpenFile(test_file, "w");
    ASSERT_TRUE(fp != nullptr);
    ASSERT_TRUE(base::CloseFile(fp));
  }
  base::FilePath test_file_virtual_path;
  ASSERT_TRUE(storage::ExternalMountPoints::GetSystemInstance()->GetVirtualPath(
      test_file, &test_file_virtual_path));

  const std::string url = "https://example.com/";
  const GURL caller = GURL(url);

  // Open the file dialog, providing a path to the file to open so the dialog
  // will automatically select it.  Ensure the "Open" button is enabled by
  // waiting for notification from chrome.test.sendMessage().
  ASSERT_NO_FATAL_FAILURE(OpenDialog(ui::SelectFileDialog::SELECT_OPEN_FILE,
                                     test_file, owning_window, "dialog-ready",
                                     &caller));

  std::vector<ui::SelectedFileInfo> selected_files;
  auto selected_file = ui::SelectedFileInfo(test_file, test_file);
  selected_file.virtual_path = test_file_virtual_path;
  selected_files.push_back(std::move(selected_file));
  EXPECT_CALL(*mock_files_controller_.get(),
              FilterDisallowedUploads(std::move(selected_files),
                                      policy::DlpFileDestination(caller),
                                      base::test::IsNotNullCallback()))
      .WillOnce(
          base::test::RunOnceCallback<2>(std::vector<ui::SelectedFileInfo>()));

  // Click the "Save" button.
  CloseDialog(DIALOG_BTN_OK, owning_window);

  // Listener should have been informed of the cancellation.
  ASSERT_FALSE(listener_->file_selected());
  ASSERT_TRUE(listener_->canceled());
  ASSERT_EQ(this, listener_->params());
}

INSTANTIATE_TEST_SUITE_P(SystemWebApp,
                         SelectFileDialogExtensionPolicyTest,
                         TestMode::SystemWebAppValues());