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
|
// 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.
#include "ash/test/test_window_builder.h"
#include "ash/examples/client_controlled_state_util.h"
#include "ash/shell.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/client/window_parenting_client.h"
#include "ui/aura/test/test_windows.h"
#include "ui/aura/window.h"
#include "ui/wm/core/coordinate_conversion.h"
namespace ash {
using SignalCallback =
base::RepeatingCallback<void(TestWindowBuilder::Operation)>;
namespace {
ClientControlledStateUtil::StateChangeRequestCallback CreateStateChangeCallback(
SignalCallback signal_callback) {
return base::BindRepeating(
[](SignalCallback signal_callback, WindowState* window_state,
ClientControlledState* state, chromeos::WindowStateType next_state) {
ClientControlledStateUtil::ApplyWindowStateRequest(window_state, state,
next_state);
if (!signal_callback.is_null()) {
signal_callback.Run(TestWindowBuilder::kStateChange);
}
},
signal_callback);
}
ClientControlledStateUtil::BoundsChangeRequestCallback
CreateBoundsChangeCallback(SignalCallback signal_callback) {
return base::BindRepeating(
[](SignalCallback signal_callback, WindowState* window_state,
ClientControlledState* state,
chromeos::WindowStateType requested_state,
const gfx::Rect& bounds_in_display, int64_t display_id) {
ClientControlledStateUtil::ApplyBoundsRequest(
window_state, state, requested_state, bounds_in_display,
display_id);
if (!signal_callback.is_null()) {
signal_callback.Run(TestWindowBuilder::kBoundsChange);
}
},
signal_callback);
}
} // namespace
TestWindowBuilder::TestWindowBuilder() = default;
TestWindowBuilder::TestWindowBuilder(TestWindowBuilder& others)
: parent_(others.parent_),
context_(others.context_),
delegate_(others.delegate_),
window_type_(others.window_type_),
layer_type_(others.layer_type_),
bounds_(others.bounds_),
init_properties_(std::move(others.init_properties_)),
window_id_(others.window_id_),
window_title_(others.window_title_),
show_(others.show_) {
DCHECK(!others.built_);
others.built_ = true;
}
TestWindowBuilder::~TestWindowBuilder() = default;
TestWindowBuilder& TestWindowBuilder::SetParent(aura::Window* parent) {
DCHECK(!built_);
DCHECK(!parent_);
parent_ = parent;
return *this;
}
TestWindowBuilder& TestWindowBuilder::SetWindowType(
aura::client::WindowType type) {
DCHECK(!built_);
window_type_ = type;
return *this;
}
TestWindowBuilder& TestWindowBuilder::SetWindowId(int id) {
DCHECK(!built_);
window_id_ = id;
return *this;
}
TestWindowBuilder& TestWindowBuilder::SetWindowTitle(
const std::u16string& title) {
DCHECK(!built_);
window_title_ = title;
return *this;
}
TestWindowBuilder& TestWindowBuilder::SetBounds(const gfx::Rect& bounds) {
DCHECK(!built_);
bounds_ = bounds;
return *this;
}
TestWindowBuilder& TestWindowBuilder::SetDelegate(
aura::WindowDelegate* delegate) {
DCHECK(!delegate_);
delegate_ = delegate;
return *this;
}
TestWindowBuilder& TestWindowBuilder::SetColorWindowDelegate(SkColor color) {
DCHECK(!built_);
DCHECK(!delegate_);
delegate_ = new aura::test::ColorTestWindowDelegate(color);
return *this;
}
TestWindowBuilder& TestWindowBuilder::SetTestWindowDelegate() {
DCHECK(!built_);
DCHECK(!delegate_);
delegate_ = aura::test::TestWindowDelegate::CreateSelfDestroyingDelegate();
return *this;
}
TestWindowBuilder& TestWindowBuilder::AllowAllWindowStates() {
DCHECK(!built_);
init_properties_.SetProperty(aura::client::kResizeBehaviorKey,
aura::client::kResizeBehaviorCanFullscreen |
aura::client::kResizeBehaviorCanMaximize |
aura::client::kResizeBehaviorCanMinimize |
aura::client::kResizeBehaviorCanResize);
return *this;
}
TestWindowBuilder& TestWindowBuilder::SetShow(bool show) {
DCHECK(!built_);
show_ = show;
return *this;
}
TestWindowBuilder& TestWindowBuilder::SetClientControlled(
SignalCallback signal_callback) {
DCHECK(!built_);
operation_signal_callback_ = signal_callback;
return *this;
}
std::unique_ptr<aura::Window> TestWindowBuilder::Build() {
DCHECK(!built_);
built_ = true;
std::unique_ptr<aura::Window> window =
std::make_unique<aura::Window>(delegate_, window_type_);
window->Init(layer_type_);
window->AcquireAllPropertiesFrom(std::move(init_properties_));
if (window_id_ != aura::Window::kInitialId)
window->SetId(window_id_);
if (!window_title_.empty()) {
window->SetTitle(window_title_);
}
if (parent_) {
if (!bounds_.IsEmpty())
window->SetBounds(bounds_);
parent_->AddChild(window.get());
} else {
// Resolve context to find a parent.
if (bounds_.IsEmpty()) {
context_ = Shell::GetPrimaryRootWindow();
} else {
display::Display display =
display::Screen::GetScreen()->GetDisplayMatching(bounds_);
aura::Window* root = Shell::GetRootWindowForDisplayId(display.id());
gfx::Point origin = bounds_.origin();
::wm::ConvertPointFromScreen(root, &origin);
context_ = root;
if (!bounds_.IsEmpty())
window->SetBounds(gfx::Rect(origin, bounds_.size()));
}
DCHECK(context_);
aura::client::ParentWindowWithContext(window.get(), context_, bounds_,
display::kInvalidDisplayId);
}
if (operation_signal_callback_) {
ClientControlledStateUtil::BuildAndSet(
window.get(), CreateStateChangeCallback(*operation_signal_callback_),
CreateBoundsChangeCallback(*operation_signal_callback_));
}
if (show_)
window->Show();
return window;
}
TestWindowBuilder ChildTestWindowBuilder(aura::Window* parent,
const gfx::Rect& bounds,
int window_id) {
return TestWindowBuilder().SetParent(parent).SetBounds(bounds).SetWindowId(
window_id);
}
} // namespace ash
|