File: default_frame_header_unittest.cc

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 (324 lines) | stat: -rw-r--r-- 12,395 bytes parent folder | download | duplicates (5)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ui/frame/default_frame_header.h"

#include <memory>

#include "ash/frame/non_client_frame_view_ash.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/test/ash_test_base.h"
#include "ash/wm/desks/desks_util.h"
#include "base/containers/contains.h"
#include "base/i18n/rtl.h"
#include "base/memory/raw_ptr.h"
#include "base/test/icu_test_util.h"
#include "chromeos/ui/base/window_properties.h"
#include "chromeos/ui/frame/caption_buttons/frame_back_button.h"
#include "chromeos/ui/frame/caption_buttons/frame_caption_button_container_view.h"
#include "chromeos/ui/frame/frame_header.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/compositor/test/test_utils.h"
#include "ui/gfx/animation/animation_test_api.h"
#include "ui/gfx/color_utils.h"
#include "ui/views/test/test_views.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/non_client_view.h"
#include "ui/wm/core/window_util.h"

using chromeos::DefaultFrameHeader;
using chromeos::FrameBackButton;
using chromeos::FrameCaptionButtonContainerView;
using chromeos::FrameHeader;
using chromeos::kFrameActiveColorKey;
using chromeos::kFrameInactiveColorKey;
using views::NonClientFrameView;
using views::Widget;

namespace ash {

using DefaultFrameHeaderTest = AshTestBase;

// Ensure the title text is vertically aligned with the window icon.
TEST_F(DefaultFrameHeaderTest, TitleIconAlignment) {
  std::unique_ptr<Widget> widget = CreateTestWidget(
      views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET, nullptr,
      desks_util::GetActiveDeskContainerId(), gfx::Rect(1, 2, 3, 4));
  FrameCaptionButtonContainerView container(widget.get());
  views::StaticSizedView window_icon(gfx::Size(16, 16));
  window_icon.SetBounds(0, 0, 16, 16);
  widget->SetBounds(gfx::Rect(0, 0, 500, 500));
  widget->Show();

  DefaultFrameHeader frame_header(
      widget.get(), widget->non_client_view()->frame_view(), &container);
  frame_header.SetLeftHeaderView(&window_icon);
  frame_header.LayoutHeader();
  gfx::Rect title_bounds = frame_header.GetTitleBounds();
  EXPECT_EQ(window_icon.bounds().CenterPoint().y(),
            title_bounds.CenterPoint().y());
}

TEST_F(DefaultFrameHeaderTest, BackButtonAlignment) {
  std::unique_ptr<views::Widget> widget = CreateTestWidget(
      views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET, nullptr,
      desks_util::GetActiveDeskContainerId(), gfx::Rect(1, 2, 3, 4));
  FrameCaptionButtonContainerView container(widget.get());
  FrameBackButton back;

  DefaultFrameHeader frame_header(
      widget.get(), widget->non_client_view()->frame_view(), &container);
  frame_header.SetBackButton(&back);
  frame_header.LayoutHeader();
  gfx::Rect title_bounds = frame_header.GetTitleBounds();
  // The back button should be positioned at the left edge, and
  // vertically centered.
  EXPECT_EQ(back.bounds().CenterPoint().y(), title_bounds.CenterPoint().y());
  EXPECT_EQ(0, back.bounds().x());
}

TEST_F(DefaultFrameHeaderTest, MinimumHeaderWidthRTL) {
  base::test::ScopedRestoreICUDefaultLocale restore_locale;
  std::unique_ptr<Widget> widget = CreateTestWidget(
      views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET, nullptr,
      desks_util::GetActiveDeskContainerId(), gfx::Rect(1, 2, 3, 4));
  FrameCaptionButtonContainerView container(widget.get());

  DefaultFrameHeader frame_header(
      widget.get(), widget->non_client_view()->frame_view(), &container);
  frame_header.LayoutHeader();
  int ltr_minimum_width = frame_header.GetMinimumHeaderWidth();
  base::i18n::SetRTLForTesting(true);
  frame_header.LayoutHeader();
  int rtl_minimum_width = frame_header.GetMinimumHeaderWidth();
  EXPECT_EQ(ltr_minimum_width, rtl_minimum_width);
}

// Ensure the right frame colors are used.
TEST_F(DefaultFrameHeaderTest, FrameColors) {
  const auto win0_bounds = gfx::Rect{1, 2, 3, 4};
  auto win0 = CreateAppWindow(win0_bounds, chromeos::AppType::BROWSER);
  Widget* widget = Widget::GetWidgetForNativeWindow(win0.get());
  DefaultFrameHeader* frame_header =
      static_cast<DefaultFrameHeader*>(FrameHeader::Get(widget));
  // Check frame color is sensitive to mode.
  SkColor active = SkColorSetRGB(70, 70, 70);
  SkColor inactive = SkColorSetRGB(200, 200, 200);
  win0->SetProperty(kFrameActiveColorKey, active);
  win0->SetProperty(kFrameInactiveColorKey, inactive);
  frame_header->UpdateFrameColors();
  frame_header->mode_ = FrameHeader::MODE_ACTIVE;
  EXPECT_EQ(active, frame_header->GetCurrentFrameColor());
  frame_header->mode_ = FrameHeader::MODE_INACTIVE;
  EXPECT_EQ(inactive, frame_header->GetCurrentFrameColor());
  EXPECT_EQ(active, frame_header->active_frame_color_);

  // Update to the new value which has no blue, which should animate.
  frame_header->mode_ = FrameHeader::MODE_ACTIVE;
  SkColor new_active = SkColorSetRGB(70, 70, 0);
  win0->SetProperty(kFrameActiveColorKey, new_active);
  frame_header->UpdateFrameColors();

  // Now update to the new value which is full blue.
  SkColor new_new_active = SkColorSetRGB(70, 70, 255);
  win0->SetProperty(kFrameActiveColorKey, new_new_active);
  frame_header->UpdateFrameColors();

  // Again, GetCurrentFrameColor should return the target color.
  EXPECT_EQ(new_new_active, frame_header->GetCurrentFrameColor());
}

namespace {

class LayerDestroyedChecker : public ui::LayerObserver {
 public:
  explicit LayerDestroyedChecker(ui::Layer* layer) { layer->AddObserver(this); }
  LayerDestroyedChecker(const LayerDestroyedChecker&) = delete;
  LayerDestroyedChecker& operator=(const LayerDestroyedChecker&) = delete;
  ~LayerDestroyedChecker() override = default;

  void LayerDestroyed(ui::Layer* layer) override {
    layer->RemoveObserver(this);
    destroyed_ = true;
  }
  bool destroyed() const { return destroyed_; }

 private:
  bool destroyed_ = false;
};

}  // namespace

// A class to wait until hthe frame header is painted.
class FramePaintWaiter : public ui::CompositorObserver {
 public:
  explicit FramePaintWaiter(aura::Window* window)
      : frame_header_(
            FrameHeader::Get(Widget::GetWidgetForNativeWindow(window))) {
    frame_header_->view()->GetWidget()->GetCompositor()->AddObserver(this);
  }
  FramePaintWaiter(const FramePaintWaiter&) = delete;
  FramePaintWaiter& operator=(FramePaintWaiter&) = delete;
  ~FramePaintWaiter() override {
    frame_header_->view()->GetWidget()->GetCompositor()->RemoveObserver(this);
  }

  // ui::CompositorObserver:
  void OnCompositingDidCommit(ui::Compositor* compositor) override {
    if (frame_header_->painted_)
      run_loop_.Quit();
  }

  void Wait() { run_loop_.Run(); }

 private:
  base::RunLoop run_loop_;
  raw_ptr<FrameHeader> frame_header_ = nullptr;
};

TEST_F(DefaultFrameHeaderTest, DeleteDuringAnimation) {
  const auto bounds = gfx::Rect(100, 100);
  auto win0 = CreateAppWindow(bounds, chromeos::AppType::BROWSER);
  auto win1 = CreateAppWindow(bounds, chromeos::AppType::BROWSER);

  Widget* widget = Widget::GetWidgetForNativeWindow(win0.get());
  EXPECT_TRUE(FrameHeader::Get(widget));

  EXPECT_TRUE(wm::IsActiveWindow(win1.get()));

  // Waits until `FrameHeader` gets painted.
  EXPECT_TRUE(ui::WaitForNextFrameToBePresented(win0->GetHost()->compositor()));

  ui::ScopedAnimationDurationScaleMode non_zero_duration_mode(
      ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
  wm::ActivateWindow(win0.get());

  auto* frame_view = NonClientFrameViewAsh::Get(win0.get());
  auto* animating_layer_holding_view = frame_view->children()[0].get();
  EXPECT_TRUE(views::IsViewClass<chromeos::FrameHeader::FrameAnimatorView>(
      animating_layer_holding_view));
  ASSERT_TRUE(animating_layer_holding_view->layer());
  ASSERT_GT(animating_layer_holding_view->layer()->parent()->children().size(),
            2u);
  auto* animating_layer =
      animating_layer_holding_view->layer()->parent()->children()[0].get();
  EXPECT_EQ(ui::LAYER_TEXTURED, animating_layer->type());
  EXPECT_TRUE(base::Contains(animating_layer->name(), ":Old"));
  EXPECT_TRUE(animating_layer->GetAnimator()->is_animating());

  LayerDestroyedChecker checker(animating_layer);

  win0.reset();

  EXPECT_TRUE(checker.destroyed());
}

// Make sure that the animation is canceled when resized.
TEST_F(DefaultFrameHeaderTest, ResizeAndReorderDuringAnimation) {
  const auto bounds = gfx::Rect(100, 100);
  auto win_0 = CreateAppWindow(bounds, chromeos::AppType::BROWSER);
  auto win_1 = CreateAppWindow(bounds, chromeos::AppType::BROWSER);

  EXPECT_TRUE(wm::IsActiveWindow(win_1.get()));

  // Waits until `FrameHeader` gets painted.
  EXPECT_TRUE(
      ui::WaitForNextFrameToBePresented(win_0->GetHost()->compositor()));

  ui::ScopedAnimationDurationScaleMode non_zero_duration_mode(
      ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);

  auto* frame_view_0 = NonClientFrameViewAsh::Get(win_0.get());
  auto* animating_layer_holding_view_0 = frame_view_0->children()[0].get();
  EXPECT_TRUE(views::IsViewClass<chromeos::FrameHeader::FrameAnimatorView>(
      animating_layer_holding_view_0));
  size_t original_layers_count_0 =
      animating_layer_holding_view_0->layer()->parent()->children().size();

  auto* frame_view_1 = NonClientFrameViewAsh::Get(win_1.get());
  auto* extra_view_1 =
      frame_view_1->AddChildView(std::make_unique<views::View>());

  auto* animating_layer_holding_view_1 = frame_view_1->children()[0].get();
  EXPECT_TRUE(views::IsViewClass<chromeos::FrameHeader::FrameAnimatorView>(
      animating_layer_holding_view_1));
  size_t original_layers_count_1 =
      animating_layer_holding_view_1->layer()->parent()->children().size();

  wm::ActivateWindow(win_0.get());

  {
    // Resize during animation
    EXPECT_EQ(
        animating_layer_holding_view_0->layer()->parent()->children().size(),
        original_layers_count_0 + 1);
    auto* animating_layer =
        animating_layer_holding_view_0->layer()->parent()->children()[0].get();
    EXPECT_TRUE(animating_layer->GetAnimator()->is_animating());

    LayerDestroyedChecker checker(animating_layer);

    win_0->SetBounds(gfx::Rect(200, 200));

    // Animating layer shuld have been removed.
    EXPECT_EQ(
        animating_layer_holding_view_0->layer()->parent()->children().size(),
        original_layers_count_0);
    EXPECT_TRUE(checker.destroyed());
  }

  {
    // win_1 should still be animating.
    EXPECT_EQ(
        animating_layer_holding_view_1->layer()->parent()->children().size(),
        original_layers_count_1 + 1);
    auto* animating_layer =
        animating_layer_holding_view_1->layer()->parent()->children()[0].get();
    EXPECT_TRUE(animating_layer->GetAnimator()->is_animating());
    LayerDestroyedChecker checker(animating_layer);

    // Change the view's stacking order should stop the animation.
    ASSERT_EQ(3u, frame_view_1->children().size());
    frame_view_1->ReorderChildView(extra_view_1, 0);

    EXPECT_EQ(
        animating_layer_holding_view_1->layer()->parent()->children().size(),
        original_layers_count_1);
    EXPECT_TRUE(checker.destroyed());
  }
}

// Make sure that the animation request while animating will not
// create another animation.
TEST_F(DefaultFrameHeaderTest, AnimateDuringAnimation) {
  const auto bounds = gfx::Rect(100, 100);
  auto win_0 = CreateAppWindow(bounds, chromeos::AppType::BROWSER);
  // A frame will not animate until it is painted first.
  FramePaintWaiter(win_0.get()).Wait();

  auto* widget = Widget::GetWidgetForNativeWindow(win_0.get());

  auto lock = widget->LockPaintAsActive();
  auto win_1 = CreateAppWindow(bounds, chromeos::AppType::BROWSER);
  FramePaintWaiter(win_1.get()).Wait();

  EXPECT_TRUE(wm::IsActiveWindow(win_1.get()));

  ui::ScopedAnimationDurationScaleMode non_zero_duration_mode(
      ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);

  auto layer_bounds = win_0->layer()->bounds();
  lock.reset();
  win_1.reset();
  EXPECT_TRUE(wm::IsActiveWindow(win_0.get()));
  // Makes sure that the layer has full damaged bounds.
  EXPECT_TRUE(win_0->layer()->damaged_region().Contains(layer_bounds));
}

}  // namespace ash