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

content / public / test / file_system_chooser_test_helpers.h [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_PUBLIC_TEST_FILE_SYSTEM_CHOOSER_TEST_HELPERS_H_
#define CONTENT_PUBLIC_TEST_FILE_SYSTEM_CHOOSER_TEST_HELPERS_H_

#include <optional>

#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/shell_dialogs/select_file_dialog.h"
#include "ui/shell_dialogs/select_file_dialog_factory.h"
#include "ui/shell_dialogs/select_file_policy.h"
#include "url/gurl.h"

namespace content {

// Struct used to report what parameters one of the (fake) SelectFileDialog
// implementations below was created with.
struct SelectFileDialogParams {
  SelectFileDialogParams();
  ~SelectFileDialogParams();

  ui::SelectFileDialog::Type type = ui::SelectFileDialog::SELECT_NONE;
  std::optional<ui::SelectFileDialog::FileTypeInfo> file_types;
  gfx::NativeWindow owning_window = {};
  int file_type_index = -1;
  base::FilePath default_path;
  std::u16string title;
  std::optional<GURL> caller;
};

// A fake ui::SelectFileDialog, which will cancel the file selection instead of
// selecting a file.
class CancellingSelectFileDialogFactory : public ui::SelectFileDialogFactory {
 public:
  explicit CancellingSelectFileDialogFactory(
      SelectFileDialogParams* out_params = nullptr);
  ~CancellingSelectFileDialogFactory() override;

  ui::SelectFileDialog* Create(
      ui::SelectFileDialog::Listener* listener,
      std::unique_ptr<ui::SelectFilePolicy> policy) override;

 private:
  raw_ptr<SelectFileDialogParams> out_params_;
};

// A fake ui::SelectFileDialog, which will select one or more pre-determined
// files.
class FakeSelectFileDialogFactory : public ui::SelectFileDialogFactory {
 public:
  explicit FakeSelectFileDialogFactory(
      std::vector<base::FilePath> result,
      SelectFileDialogParams* out_params = nullptr);
  explicit FakeSelectFileDialogFactory(
      std::vector<ui::SelectedFileInfo> result,
      SelectFileDialogParams* out_params = nullptr);
  ~FakeSelectFileDialogFactory() override;

  ui::SelectFileDialog* Create(
      ui::SelectFileDialog::Listener* listener,
      std::unique_ptr<ui::SelectFilePolicy> policy) override;

 private:
  std::vector<ui::SelectedFileInfo> result_;
  raw_ptr<SelectFileDialogParams, DanglingUntriaged> out_params_;
};

}  // namespace content

#endif  // CONTENT_PUBLIC_TEST_FILE_SYSTEM_CHOOSER_TEST_HELPERS_H_