File: layer_animation_verifier.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (104 lines) | stat: -rw-r--r-- 3,447 bytes parent folder | download | duplicates (4)
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
// 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/layer_animation_verifier.h"

#include "third_party/googletest/src/googletest/include/gtest/gtest.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

namespace ash {

namespace {

// A threshold set empirically.
constexpr int kMinimumComparisonCount = 8;

// Returns whether `layer` is in animation progress.
bool IsLayerUnderAnimation(ui::Layer* layer) {
  return layer->GetAnimator()->is_animating();
}

}  // namespace

LayerAnimationVerifier::LayerAnimationVerifier(ui::Layer* layer_with_animation,
                                               views::View* observed_view)
    : layer_with_animation_(layer_with_animation),
      observed_view_(observed_view) {
  DCHECK_EQ(GetCompositor(), observed_view->GetWidget()->GetCompositor());

  // Ensure that `LayerAnimationVerifier` observes a layer animation from start
  // to end without missing any part. Due to the same reason, the layer
  // animation is expected to have completed when `LayerAnimationVerifier` is
  // destructed.
  EXPECT_FALSE(IsLayerUnderAnimation(layer_with_animation_));

  GetCompositor()->AddObserver(this);
}

LayerAnimationVerifier::~LayerAnimationVerifier() {
  GetCompositor()->RemoveObserver(this);
  EXPECT_FALSE(IsLayerUnderAnimation(layer_with_animation_));

  // We need to collect enough data to verify the animation.
  EXPECT_GT(comparison_count_, kMinimumComparisonCount);
}

void LayerAnimationVerifier::OnCompositingDidCommit(
    ui::Compositor* compositor) {
  const gfx::Rect current_screen_bounds = observed_view_->GetBoundsInScreen();

  // Compare with `last_screen_bounds_` if any.
  if (last_screen_bounds_) {
    CompareWithLastBoundsRect(current_screen_bounds);
    return;
  }

  last_screen_bounds_ = current_screen_bounds;
  EXPECT_FALSE(x_direction_);
  EXPECT_FALSE(y_direction_);
}

void LayerAnimationVerifier::CompareWithLastBoundsRect(
    const gfx::Rect& current_screen_bounds) {
  ++comparison_count_;

  if (!x_direction_) {
    if (current_screen_bounds.x() != last_screen_bounds_->x()) {
      x_direction_ = CalculateMoveDirection(current_screen_bounds.x(),
                                            last_screen_bounds_->x());
    }
  } else {
    EXPECT_EQ(*x_direction_, CalculateMoveDirection(current_screen_bounds.x(),
                                                    last_screen_bounds_->x()));
  }

  if (!y_direction_) {
    if (current_screen_bounds.y() != last_screen_bounds_->y()) {
      y_direction_ = CalculateMoveDirection(current_screen_bounds.y(),
                                            last_screen_bounds_->y());
    }
  } else {
    EXPECT_EQ(*y_direction_, CalculateMoveDirection(current_screen_bounds.y(),
                                                    last_screen_bounds_->y()));
  }
}

LayerAnimationVerifier::MoveDirection
LayerAnimationVerifier::CalculateMoveDirection(int current_data,
                                               int last_data) const {
  if (current_data >= last_data)
    return MoveDirection::kForward;

  return MoveDirection::kBackward;
}

ui::Compositor* LayerAnimationVerifier::GetCompositor() {
  return layer_with_animation_->GetCompositor();
}

}  // namespace ash