File: progress_bar_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 (180 lines) | stat: -rw-r--r-- 6,830 bytes parent folder | download | duplicates (6)
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
// Copyright 2011 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/views/controls/progress_bar.h"

#include <string>

#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/color/color_id.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/color_utils.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/test/ax_event_counter.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/test/views_test_utils.h"

namespace views {

class ProgressBarTest : public ViewsTestBase {
 protected:
  // ViewsTestBase:
  void SetUp() override {
    ViewsTestBase::SetUp();

    widget_ = CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
    container_view_ = widget_->SetContentsView(std::make_unique<View>());
    auto* layout =
        container_view_->SetLayoutManager(std::make_unique<FlexLayout>());
    layout->SetOrientation(views::LayoutOrientation::kVertical);
    bar_ = container_view_->AddChildView(std::make_unique<ProgressBar>());
    views::test::RunScheduledLayout(container_view_);
    widget_->Show();
  }

  void TearDown() override {
    container_view_ = nullptr;
    bar_ = nullptr;
    widget_.reset();
    ViewsTestBase::TearDown();
  }

  ProgressBar* bar() { return bar_.get(); }

  std::unique_ptr<Widget> widget_;
  raw_ptr<views::View> container_view_;
  raw_ptr<ProgressBar> bar_;
};

TEST_F(ProgressBarTest, AccessibleNodeData) {
  bar()->SetValue(0.626);

  ui::AXNodeData node_data;
  bar()->GetViewAccessibility().GetAccessibleNodeData(&node_data);
  EXPECT_EQ(ax::mojom::Role::kProgressIndicator, node_data.role);
  EXPECT_EQ(std::u16string(),
            node_data.GetString16Attribute(ax::mojom::StringAttribute::kName));
  EXPECT_EQ(std::string("62%"),
            node_data.GetStringAttribute(ax::mojom::StringAttribute::kValue));
  EXPECT_FALSE(
      node_data.HasIntAttribute(ax::mojom::IntAttribute::kRestriction));
}

// Verifies the correct a11y events are raised for an accessible progress bar.
TEST_F(ProgressBarTest, AccessibilityEvents) {
  test::AXEventCounter ax_counter(views::AXUpdateNotifier::Get());
  EXPECT_EQ(0, ax_counter.GetCount(ax::mojom::Event::kValueChanged));

  bar()->SetValue(0.50);
  EXPECT_EQ(1, ax_counter.GetCount(ax::mojom::Event::kValueChanged));

  bar()->SetValue(0.63);
  EXPECT_EQ(2, ax_counter.GetCount(ax::mojom::Event::kValueChanged));

  bar()->SetValue(0.636);
  EXPECT_EQ(2, ax_counter.GetCount(ax::mojom::Event::kValueChanged));

  bar()->SetValue(0.642);
  EXPECT_EQ(3, ax_counter.GetCount(ax::mojom::Event::kValueChanged));

  widget_->Hide();
  widget_->Show();
  EXPECT_EQ(3, ax_counter.GetCount(ax::mojom::Event::kValueChanged));

  widget_->Hide();
  bar()->SetValue(0.8);
  EXPECT_EQ(3, ax_counter.GetCount(ax::mojom::Event::kValueChanged));

  widget_->Show();
  EXPECT_EQ(4, ax_counter.GetCount(ax::mojom::Event::kValueChanged));
}

// Test that default colors can be overridden. Used by Chromecast.
TEST_F(ProgressBarTest, OverrideDefaultColors) {
  EXPECT_NE(SK_ColorRED, bar()->GetForegroundColor());
  EXPECT_NE(SK_ColorGREEN, bar()->GetBackgroundColor());
  EXPECT_NE(bar()->GetForegroundColor(), bar()->GetBackgroundColor());

  bar()->SetForegroundColor(SK_ColorRED);
  bar()->SetBackgroundColor(SK_ColorGREEN);
  EXPECT_EQ(SK_ColorRED, bar()->GetForegroundColor());
  EXPECT_EQ(SK_ColorGREEN, bar()->GetBackgroundColor());

  // Override colors with color ID. It will also override the colors set with
  // SkColor.
  bar()->SetForegroundColorId(ui::kColorSysPrimary);
  bar()->SetBackgroundColorId(ui::kColorSysPrimaryContainer);
  const auto* color_provider = bar()->GetColorProvider();
  EXPECT_EQ(color_provider->GetColor(ui::kColorSysPrimary),
            bar()->GetForegroundColor());
  EXPECT_EQ(color_provider->GetColor(ui::kColorSysPrimaryContainer),
            bar()->GetBackgroundColor());
  EXPECT_EQ(ui::kColorSysPrimary, bar()->GetForegroundColorId().value());
  EXPECT_EQ(ui::kColorSysPrimaryContainer,
            bar()->GetBackgroundColorId().value());

  // Override the colors set with color ID by SkColor.
  bar()->SetForegroundColor(SK_ColorRED);
  bar()->SetBackgroundColor(SK_ColorGREEN);
  EXPECT_EQ(SK_ColorRED, bar()->GetForegroundColor());
  EXPECT_EQ(SK_ColorGREEN, bar()->GetBackgroundColor());
  EXPECT_EQ(std::nullopt, bar()->GetForegroundColorId());
  EXPECT_EQ(std::nullopt, bar()->GetBackgroundColorId());
}

// Test that if no `preferred_corner_radii` are provided the default radius is
// 3, and a value of `std::nullopt` will not round the corners.
TEST_F(ProgressBarTest, RoundCornerDefault) {
  // The default bar should have a rounded corner radius of 3.
  EXPECT_EQ(gfx::RoundedCornersF(3), bar()->GetPreferredCornerRadii());

  // Setting `std::nullopt` for the corner radius should make the bar have no
  // rounded corners.
  bar()->SetPreferredHeight(12);
  bar()->SetPreferredCornerRadii(std::nullopt);
  views::test::RunScheduledLayout(container_view_);
  EXPECT_EQ(gfx::RoundedCornersF(0), bar()->GetPreferredCornerRadii());
  EXPECT_TRUE(bar()->GetPreferredCornerRadii().IsEmpty());
}

// Test that a value set for `preferred_corner_radii` is saved and can be
// retrieved from `GetPreferredCornerRadii()`.
TEST_F(ProgressBarTest, RoundCornerRetrieval) {
  // Setting custom corners should result in them being saved.
  bar()->SetPreferredHeight(12);
  bar()->SetPreferredCornerRadii(gfx::RoundedCornersF(6));
  views::test::RunScheduledLayout(container_view_);
  EXPECT_EQ(gfx::RoundedCornersF(6), bar()->GetPreferredCornerRadii());
}

// Test that `GetPreferredCornerRadii()` will return no corner with a radius
// greater than the height of the bar.
TEST_F(ProgressBarTest, RoundCornerMax) {
  // The max corner radius for a bar with a height of 12 should be 12.
  bar()->SetPreferredHeight(12);
  bar()->SetPreferredCornerRadii(gfx::RoundedCornersF(13, 14, 15, 16));
  views::test::RunScheduledLayout(container_view_);
  EXPECT_EQ(gfx::RoundedCornersF(12, 12, 12, 12),
            bar()->GetPreferredCornerRadii());
}

// Test that if value is set negative, which means progress bar is
// indeterminate, the string attribute value should be empty.
TEST_F(ProgressBarTest, RemoveValue) {
  // setting negative progress bar value
  bar()->SetValue(-0.626);

  ui::AXNodeData node_data;
  bar()->GetViewAccessibility().GetAccessibleNodeData(&node_data);
  EXPECT_EQ(std::string(""),
            node_data.GetStringAttribute(ax::mojom::StringAttribute::kValue));
}

}  // namespace views