File: paint_throbber_unittest.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 (115 lines) | stat: -rw-r--r-- 4,503 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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/paint_throbber.h"

#include "base/numerics/safe_conversions.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace gfx {

namespace {
constexpr base::TimeDelta kEpsilon = base::Seconds(0.0001);

// Interval for one keyframe of the sweep animation.
constexpr base::TimeDelta kSweepAnimationInterval = base::Seconds(2.0 / 3.0);

// Interval for a quarter rotation of the spinner's start angle.
constexpr base::TimeDelta kSpinnerAnimationInterval = base::Milliseconds(392);
}  // namespace

TEST(PaintThrobberTest, ThrobberSpinningStateSweepAngles) {
  static constexpr struct {
    base::TimeDelta elapsed_time;
    int64_t expected_sweep;
  } test_cases[] = {{base::TimeDelta(), -270},
                    {kSweepAnimationInterval - kEpsilon, -5},
                    {kSweepAnimationInterval, 5},
                    {2 * kSweepAnimationInterval - kEpsilon, 270},
                    {2 * kSweepAnimationInterval, -270}};

  for (const auto& test_case : test_cases) {
    EXPECT_EQ(
        base::ClampRound(
            CalculateThrobberSpinningState(test_case.elapsed_time).sweep_angle),
        test_case.expected_sweep)
        << "Elapsed time: " << test_case.elapsed_time;
  }
}

TEST(PaintThrobberTest, ThrobberSpinningStateSweepAnglesWithKeyframeOffset) {
  static constexpr struct {
    base::TimeDelta elapsed_time;
    int64_t expected_sweep;
  } test_cases[] = {{base::TimeDelta(), 5},  // Minimum angle is 5.
                    {kSweepAnimationInterval - kEpsilon, 270},
                    {kSweepAnimationInterval, -270},
                    {2 * kSweepAnimationInterval - kEpsilon, -5},
                    {2 * kSweepAnimationInterval, 5}};

  for (const auto& test_case : test_cases) {
    EXPECT_EQ(base::ClampRound(
                  CalculateThrobberSpinningState(test_case.elapsed_time, 1)
                      .sweep_angle),
              test_case.expected_sweep)
        << "Elapsed time: " << test_case.elapsed_time;
  }
}

TEST(PaintThrobberTest, ThrobberSpinningStateStartAngle) {
  static constexpr struct {
    base::TimeDelta elapsed_time;
    int64_t expected_start_angle;
  } test_cases[] = {
      {base::TimeDelta(), 270},              // Starts at 270.
      {kSpinnerAnimationInterval, 360},      // Quarter rotation.
      {2 * kSpinnerAnimationInterval, 450},  // Quarter rotation.
      {3 * kSpinnerAnimationInterval, 540},  // Three quarter rotation.
      // Full rotation at the end of an arc period.
      {4 * kSpinnerAnimationInterval, 900},
      // Sweep angle should be 0 here, but is floored at 5
      // degrees, pushing the start angle back by 5.
      {5 * kSpinnerAnimationInterval, 985},
      {6 * kSpinnerAnimationInterval, 1080},  // Quarter rotation.
      // Full rotation at the end of an arc period.
      {7 * kSpinnerAnimationInterval, 1440},   // Quarter rotation.
      {8 * kSpinnerAnimationInterval, 1530}};  // Quarter rotation.

  for (const auto& test_case : test_cases) {
    EXPECT_EQ(
        CalculateThrobberSpinningState(test_case.elapsed_time).start_angle,
        test_case.expected_start_angle)
        << "Elapsed time: " << test_case.elapsed_time;
  }
}

TEST(PaintThrobberTest, ThrobberSpinningStateStartAngleWithKeyframeOffset) {
  static constexpr struct {
    base::TimeDelta elapsed_time;
    int64_t expected_start_angle;
  } test_cases[] = {
      // Sweep angle should be 0 here, but is floored at 5 degrees, pushing
      // the start angle back by 5.
      {base::TimeDelta(), 265},
      {kSpinnerAnimationInterval, 360},
      // Full rotation at the end of an arc period.
      {2 * kSpinnerAnimationInterval, 720},
      {3 * kSpinnerAnimationInterval, 810},  // Quarter rotation.
      {4 * kSpinnerAnimationInterval, 900},  // Quarter rotation.
      {5 * kSpinnerAnimationInterval, 990},  // Quarter rotation.
      // Full rotation at the end of an arc period.
      {6 * kSpinnerAnimationInterval, 1350},
      {7 * kSpinnerAnimationInterval, 1440},   // Quarter rotation.
      {8 * kSpinnerAnimationInterval, 1530}};  // Quarter rotation.

  for (const auto& test_case : test_cases) {
    EXPECT_EQ(
        CalculateThrobberSpinningState(test_case.elapsed_time, 1).start_angle,
        test_case.expected_start_angle)
        << "Elapsed time: " << test_case.elapsed_time;
  }
}

}  // namespace gfx