File: test_window_builder.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (107 lines) | stat: -rw-r--r-- 4,216 bytes parent folder | download | duplicates (3)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_TEST_TEST_WINDOW_BUILDER_H_
#define ASH_TEST_TEST_WINDOW_BUILDER_H_

#include <memory>
#include <optional>

#include "ash/ash_export.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window.h"
#include "ui/base/class_property.h"
#include "ui/compositor/layer_type.h"
#include "ui/gfx/geometry/rect.h"

namespace ash {

// A builder to create a aura::Window for testing purpose. Use this when you
// simply need need a window without a meaningful content (except for a color)
// or capability to drag a window or drag to resize a window. If you need these
// properties in your test, use TestWidgetBuilder instead.
//
// The builder object can be used only once to create a single window because
// ownership of some parameters has to be transferred to a created window.
class ASH_EXPORT TestWindowBuilder {
 public:
  TestWindowBuilder();
  TestWindowBuilder(TestWindowBuilder& other);
  TestWindowBuilder& operator=(TestWindowBuilder& params) = delete;
  ~TestWindowBuilder();

  // Sets parameters that are used when creating a test window.
  TestWindowBuilder& SetParent(aura::Window* parent);
  TestWindowBuilder& SetWindowType(aura::client::WindowType type);
  TestWindowBuilder& SetWindowId(int id);
  TestWindowBuilder& SetBounds(const gfx::Rect& bounds);

  // Having a non-empty title helps avoid accessibility paint check failures
  // in tests. For instance, `WindowMiniView` gets its accessible name from
  // the window title.
  TestWindowBuilder& SetWindowTitle(const std::u16string& title);

  // Set a WindowDelegate used by a test window.
  TestWindowBuilder& SetDelegate(aura::WindowDelegate* delegate);

  // Use this to create a window whose content is painted with |color|.
  // This uses aura::test::ColorTestWindowDelegate as a WindowDelegate.
  TestWindowBuilder& SetColorWindowDelegate(SkColor color);

  // Use aura::test::TestWindowDelegate as a WindowDelegate.
  TestWindowBuilder& SetTestWindowDelegate();

  // Allows the window to be resizable, maximizable and minimizable.
  TestWindowBuilder& AllowAllWindowStates();

  // A window is shown when created by default. Use this if you want not
  // to show when created.
  TestWindowBuilder& SetShow(bool show);

  // Sets the window property to be set on a test window.
  template <typename T>
  TestWindowBuilder& SetWindowProperty(const ui::ClassProperty<T>* property,
                                       T value) {
    init_properties_.SetProperty(property, value);
    return *this;
  }

  enum Operation { kStateChange, kBoundsChange };

  // Creates a client controlled state backed window, which executes the
  // operation asynchronosly in a separate task.  `signal_callback` will be
  // called every time the operation's task is executed.
  TestWindowBuilder& SetClientControlled(
      base::RepeatingCallback<void(Operation)> signal_callback);

  // Build a window based on the parameter already set. This can be called only
  // once and the object cannot be used to create multiple windows.
  [[nodiscard]] std::unique_ptr<aura::Window> Build();

 private:
  raw_ptr<aura::Window> parent_ = nullptr;
  raw_ptr<aura::Window> context_ = nullptr;
  raw_ptr<aura::WindowDelegate, DanglingUntriaged> delegate_ = nullptr;
  aura::client::WindowType window_type_ = aura::client::WINDOW_TYPE_NORMAL;
  ui::LayerType layer_type_ = ui::LAYER_TEXTURED;
  gfx::Rect bounds_;
  ui::PropertyHandler init_properties_;
  int window_id_ = aura::Window::kInitialId;
  std::u16string window_title_ = std::u16string();
  std::optional<base::RepeatingCallback<void(Operation)>>
      operation_signal_callback_;
  bool show_ = true;
  bool built_ = false;
};

// A utility function to create a window builder for child windows.
TestWindowBuilder ChildTestWindowBuilder(aura::Window* parent,
                                         const gfx::Rect& bounds = gfx::Rect(),
                                         int window_id = -1);

}  // namespace ash

#endif  // ASH_TEST_TEST_WINDOW_BUILDER_H_