test_window_builder.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #ifndef ASH_TEST_TEST_WINDOW_BUILDER_H_
  5. #define ASH_TEST_TEST_WINDOW_BUILDER_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "ui/aura/client/aura_constants.h"
  9. #include "ui/aura/window.h"
  10. #include "ui/base/class_property.h"
  11. #include "ui/compositor/layer_type.h"
  12. #include "ui/gfx/geometry/rect.h"
  13. namespace ash {
  14. // A builder to create a aura::Window for testing purpose. Use this when you
  15. // simply need need a window without a meaningful content (except for a color)
  16. // or capability to drag a window or drag to resize a window. If you need these
  17. // properties in your test, use TestWidgetBuilder instead.
  18. //
  19. // The builder object can be used only once to create a single window because
  20. // ownership of some parameters has to be transferred to a created window.
  21. class ASH_EXPORT TestWindowBuilder {
  22. public:
  23. TestWindowBuilder();
  24. TestWindowBuilder(TestWindowBuilder& other);
  25. TestWindowBuilder& operator=(TestWindowBuilder& params) = delete;
  26. ~TestWindowBuilder();
  27. // Sets parameters that are used when creating a test window.
  28. TestWindowBuilder& SetParent(aura::Window* parent);
  29. TestWindowBuilder& SetWindowType(aura::client::WindowType type);
  30. TestWindowBuilder& SetWindowId(int id);
  31. TestWindowBuilder& SetBounds(const gfx::Rect& bounds);
  32. // Set a WindowDelegate used by a test window.
  33. TestWindowBuilder& SetDelegate(aura::WindowDelegate* delegate);
  34. // Use this to create a window whose content is painted with |color|.
  35. // This uses aura::test::ColorTestWindowDelegate as a WindowDelegate.
  36. TestWindowBuilder& SetColorWindowDelegate(SkColor color);
  37. // Use aura::test::TestWindowDelegate as a WindowDelegate.
  38. TestWindowBuilder& SetTestWindowDelegate();
  39. // Allows the window to be resizable, maximizable and minimizable.
  40. TestWindowBuilder& AllowAllWindowStates();
  41. // A window is shown when created by default. Use this if you want not
  42. // to show when created.
  43. TestWindowBuilder& SetShow(bool show);
  44. // Sets the window property to be set on a test window.
  45. template <typename T>
  46. TestWindowBuilder& SetWindowProperty(const ui::ClassProperty<T>* property,
  47. T value) {
  48. init_properties_.SetProperty(property, value);
  49. return *this;
  50. }
  51. // Build a window based on the parameter already set. This can be called only
  52. // once and the object cannot be used to create multiple windows.
  53. [[nodiscard]] std::unique_ptr<aura::Window> Build();
  54. private:
  55. aura::Window* parent_ = nullptr;
  56. aura::Window* context_ = nullptr;
  57. aura::WindowDelegate* delegate_ = nullptr;
  58. aura::client::WindowType window_type_ = aura::client::WINDOW_TYPE_NORMAL;
  59. ui::LayerType layer_type_ = ui::LAYER_TEXTURED;
  60. gfx::Rect bounds_;
  61. ui::PropertyHandler init_properties_;
  62. int window_id_ = aura::Window::kInitialId;
  63. bool show_ = true;
  64. bool built_ = false;
  65. };
  66. // A utility function to create a window builder for child windows.
  67. TestWindowBuilder ChildTestWindowBuilder(aura::Window* parent,
  68. const gfx::Rect& bounds = gfx::Rect(),
  69. int window_id = -1);
  70. } // namespace ash
  71. #endif // ASH_TEST_TEST_WINDOW_BUILDER_H_