test_dialog_model_host.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/base/test/test_dialog_model_host.h"
  5. #include "ui/base/models/dialog_model.h"
  6. namespace ui {
  7. namespace {
  8. DialogModelButton* GetButton(DialogModel* dialog_model,
  9. TestDialogModelHost::ButtonId button_id,
  10. base::PassKey<DialogModelHost> pass_key) {
  11. switch (button_id) {
  12. case TestDialogModelHost::ButtonId::kCancel:
  13. return dialog_model->cancel_button(pass_key);
  14. case TestDialogModelHost::ButtonId::kExtra:
  15. return dialog_model->extra_button(pass_key);
  16. case TestDialogModelHost::ButtonId::kOk:
  17. return dialog_model->ok_button(pass_key);
  18. }
  19. }
  20. } // namespace
  21. TestDialogModelHost::TestDialogModelHost(
  22. std::unique_ptr<DialogModel> dialog_model)
  23. : dialog_model_(std::move(dialog_model)) {}
  24. TestDialogModelHost::~TestDialogModelHost() = default;
  25. // These are static methods rather than a method on the host because this needs
  26. // to result with the destruction of the host.
  27. void TestDialogModelHost::Accept(std::unique_ptr<TestDialogModelHost> host) {
  28. host->dialog_model_->OnDialogAcceptAction(GetPassKey());
  29. DestroyWithoutAction(std::move(host));
  30. }
  31. void TestDialogModelHost::Cancel(std::unique_ptr<TestDialogModelHost> host) {
  32. host->dialog_model_->OnDialogCancelAction(GetPassKey());
  33. DestroyWithoutAction(std::move(host));
  34. }
  35. void TestDialogModelHost::Close(std::unique_ptr<TestDialogModelHost> host) {
  36. host->dialog_model_->OnDialogCloseAction(GetPassKey());
  37. DestroyWithoutAction(std::move(host));
  38. }
  39. void TestDialogModelHost::DestroyWithoutAction(
  40. std::unique_ptr<TestDialogModelHost> host) {
  41. host->dialog_model_->OnDialogDestroying(GetPassKey());
  42. // Note that `host` destroys when going out of scope here.
  43. }
  44. void TestDialogModelHost::TriggerExtraButton(const ui::Event& event) {
  45. dialog_model_->extra_button(GetPassKey())->OnPressed(GetPassKey(), event);
  46. }
  47. DialogModelTextfield* TestDialogModelHost::FindSingleTextfield() {
  48. // TODO(pbos): Consider validating how "single" this field is.
  49. for (const auto& field : dialog_model_->fields(GetPassKey())) {
  50. if (field->type(GetPassKey()) == ui::DialogModelField::kTextfield)
  51. return field->AsTextfield(GetPassKey());
  52. }
  53. NOTREACHED();
  54. return nullptr;
  55. }
  56. void TestDialogModelHost::SetSingleTextfieldText(std::u16string text) {
  57. FindSingleTextfield()->OnTextChanged(GetPassKey(), std::move(text));
  58. }
  59. // Bypasses PassKey() requirement for accessing accelerators().
  60. const base::flat_set<Accelerator>& TestDialogModelHost::GetAccelerators(
  61. ButtonId button_id) {
  62. return GetButton(dialog_model_.get(), button_id, GetPassKey())
  63. ->accelerators(GetPassKey());
  64. }
  65. const std::u16string& TestDialogModelHost::GetLabel(ButtonId button_id) {
  66. return GetButton(dialog_model_.get(), button_id, GetPassKey())
  67. ->label(GetPassKey());
  68. }
  69. ElementIdentifier TestDialogModelHost::GetId(ButtonId button_id) {
  70. return GetButton(dialog_model_.get(), button_id, GetPassKey())
  71. ->id(GetPassKey());
  72. }
  73. void TestDialogModelHost::Close() {
  74. // For now, TestDialogModelHost::Close() is the expected interface to close.
  75. NOTREACHED();
  76. }
  77. void TestDialogModelHost::OnFieldAdded(DialogModelField* field) {
  78. // TODO(pbos): Figure out what to do here. :)
  79. NOTREACHED();
  80. }
  81. } // namespace ui