Flutter Engine
The Flutter Engine
ax_enum_util_unittest.cc
Go to the documentation of this file.
1// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ax_enum_util.h"
6
7#include <string>
8#include <vector>
9
10#include "gtest/gtest.h"
11
12#include "ax_enums.h"
13#include "ax_node_data.h"
14
15namespace ui {
16
17// Templatized function that tests that for a mojom enum
18// such as ax::mojom::Role, ax::mojom::Event, etc. we can
19// call ToString() on the enum to get a string, and then
20// ParseEnumName() on the string to get back the original
21// value. Also tests what happens when we call ToString
22// or ParseEnumName on a bogus value.
23template <typename T>
25 T(ParseFunction)(const char*),
26 int32_t(step)(int32_t) = [](int32_t val) { return val + 1; }) {
27 // Check every valid enum value.
28 for (int i = static_cast<int>(T::kMinValue);
29 i <= static_cast<int>(T::kMaxValue); i = step(i)) {
30 T src = static_cast<T>(i);
31 std::string str = ToString(src);
32 auto dst = ParseFunction(str.c_str());
33 EXPECT_EQ(src, dst);
34 }
35
36 // Parse a bogus string.
37 EXPECT_EQ(T::kNone, ParseFunction("bogus"));
38
39 // Convert a bogus value to a string.
40 int out_of_range_value = static_cast<int>(T::kMaxValue) + 1;
41 EXPECT_STREQ("", ToString(static_cast<T>(out_of_range_value)));
42}
43
44// Templatized function that tries calling a setter on AXNodeData
45// such as AddIntAttribute, AddFloatAttribute - with each possible
46// enum value.
47//
48// This variant is for cases where the value type is an object.
49template <typename T, typename U>
50void TestAXNodeDataSetter(void (AXNodeData::*Setter)(T, const U&),
51 const U& value) {
52 AXNodeData node_data;
53 for (int i = static_cast<int>(T::kMinValue) + 1;
54 i <= static_cast<int>(T::kMaxValue); ++i) {
55 T attr = static_cast<T>(i);
56 ((node_data).*(Setter))(attr, value);
57 }
58 EXPECT_TRUE(!node_data.ToString().empty());
59}
60
61// Same as TextAXNodeData, above, but This variant is for
62// cases where the value type is POD.
63template <typename T, typename U>
64void TestAXNodeDataSetter(void (AXNodeData::*Setter)(T, U), U value) {
65 AXNodeData node_data;
66 for (int i = static_cast<int>(T::kMinValue) + 1;
67 i <= static_cast<int>(T::kMaxValue); ++i) {
68 T attr = static_cast<T>(i);
69 ((node_data).*(Setter))(attr, value);
70 }
71 EXPECT_TRUE(!node_data.ToString().empty());
72}
73
74TEST(AXEnumUtilTest, Event) {
75 TestEnumStringConversion<ax::mojom::Event>(ParseEvent);
76}
77
78TEST(AXEnumUtilTest, Role) {
79 TestEnumStringConversion<ax::mojom::Role>(ParseRole);
80}
81
82TEST(AXEnumUtilTest, State) {
83 TestEnumStringConversion<ax::mojom::State>(ParseState);
84}
85
86TEST(AXEnumUtilTest, Action) {
87 TestEnumStringConversion<ax::mojom::Action>(ParseAction);
88}
89
90TEST(AXEnumUtilTest, ActionFlags) {
91 TestEnumStringConversion<ax::mojom::ActionFlags>(ParseActionFlags);
92}
93
94TEST(AXEnumUtilTest, DefaultActionVerb) {
95 TestEnumStringConversion<ax::mojom::DefaultActionVerb>(
97}
98
99TEST(AXEnumUtilTest, Mutation) {
100 TestEnumStringConversion<ax::mojom::Mutation>(ParseMutation);
101}
102
103TEST(AXEnumUtilTest, StringAttribute) {
104 TestEnumStringConversion<ax::mojom::StringAttribute>(ParseStringAttribute);
105 TestAXNodeDataSetter<ax::mojom::StringAttribute>(
106 &AXNodeData::AddStringAttribute, std::string());
107}
108
109TEST(AXEnumUtilTest, IntAttribute) {
110 TestEnumStringConversion<ax::mojom::IntAttribute>(ParseIntAttribute);
111 TestAXNodeDataSetter<ax::mojom::IntAttribute>(&AXNodeData::AddIntAttribute,
112 0);
113}
114
115TEST(AXEnumUtilTest, FloatAttribute) {
116 TestEnumStringConversion<ax::mojom::FloatAttribute>(ParseFloatAttribute);
117 TestAXNodeDataSetter<ax::mojom::FloatAttribute>(
119}
120
121TEST(AXEnumUtilTest, BoolAttribute) {
122 TestEnumStringConversion<ax::mojom::BoolAttribute>(ParseBoolAttribute);
123 TestAXNodeDataSetter<ax::mojom::BoolAttribute>(&AXNodeData::AddBoolAttribute,
124 false);
125}
126
127TEST(AXEnumUtilTest, IntListAttribute) {
128 TestEnumStringConversion<ax::mojom::IntListAttribute>(ParseIntListAttribute);
129 TestAXNodeDataSetter<ax::mojom::IntListAttribute>(
130 &AXNodeData::AddIntListAttribute, std::vector<int32_t>());
131}
132
133TEST(AXEnumUtilTest, StringListAttribute) {
134 TestEnumStringConversion<ax::mojom::StringListAttribute>(
136 TestAXNodeDataSetter<ax::mojom::StringListAttribute>(
137 &AXNodeData::AddStringListAttribute, std::vector<std::string>());
138}
139
140TEST(AXEnumUtilTest, MarkerType) {
141 TestEnumStringConversion<ax::mojom::MarkerType>(
142 ParseMarkerType, [](int32_t val) {
143 return val == 0 ? 1 :
144 // 8 (Composition) is
145 // explicitly skipped in
146 // ax_enums.mojom.
147 val == 4 ? 16
148 : val * 2;
149 });
150}
151
152TEST(AXEnumUtilTest, Text_Decoration_Style) {
153 TestEnumStringConversion<ax::mojom::TextDecorationStyle>(
155}
156
157TEST(AXEnumUtilTest, ListStyle) {
158 TestEnumStringConversion<ax::mojom::ListStyle>(ParseListStyle);
159}
160
161TEST(AXEnumUtilTest, MoveDirection) {
162 TestEnumStringConversion<ax::mojom::MoveDirection>(ParseMoveDirection);
163}
164
165TEST(AXEnumUtilTest, Command) {
166 TestEnumStringConversion<ax::mojom::Command>(ParseCommand);
167}
168
169TEST(AXEnumUtilTest, TextAlign) {
170 TestEnumStringConversion<ax::mojom::TextAlign>(ParseTextAlign);
171}
172
173TEST(AXEnumUtilTest, TextBoundary) {
174 TestEnumStringConversion<ax::mojom::TextBoundary>(ParseTextBoundary);
175}
176
177TEST(AXEnumUtilTest, TextDirection) {
178 TestEnumStringConversion<ax::mojom::WritingDirection>(ParseTextDirection);
179}
180
181TEST(AXEnumUtilTest, TextPosition) {
182 TestEnumStringConversion<ax::mojom::TextPosition>(ParseTextPosition);
183}
184
185TEST(AXEnumUtilTest, TextStyle) {
186 TestEnumStringConversion<ax::mojom::TextStyle>(ParseTextStyle);
187}
188
189TEST(AXEnumUtilTest, AriaCurrentState) {
190 TestEnumStringConversion<ax::mojom::AriaCurrentState>(ParseAriaCurrentState);
191}
192
193TEST(AXEnumUtilTest, HasPopup) {
194 TestEnumStringConversion<ax::mojom::HasPopup>(ParseHasPopup);
195}
196
197TEST(AXEnumUtilTest, InvalidState) {
198 TestEnumStringConversion<ax::mojom::InvalidState>(ParseInvalidState);
199}
200
201TEST(AXEnumUtilTest, Restriction) {
202 TestEnumStringConversion<ax::mojom::Restriction>(ParseRestriction);
203}
204
205TEST(AXEnumUtilTest, CheckedState) {
206 TestEnumStringConversion<ax::mojom::CheckedState>(ParseCheckedState);
207}
208
209TEST(AXEnumUtilTest, SortDirection) {
210 TestEnumStringConversion<ax::mojom::SortDirection>(ParseSortDirection);
211}
212
213TEST(AXEnumUtilTest, NameFrom) {
214 TestEnumStringConversion<ax::mojom::NameFrom>(ParseNameFrom);
215}
216
217TEST(AXEnumUtilTest, DescriptionFrom) {
218 TestEnumStringConversion<ax::mojom::DescriptionFrom>(ParseDescriptionFrom);
219}
220
221TEST(AXEnumUtilTest, EventFrom) {
222 TestEnumStringConversion<ax::mojom::EventFrom>(ParseEventFrom);
223}
224
225TEST(AXEnumUtilTest, Gesture) {
226 TestEnumStringConversion<ax::mojom::Gesture>(ParseGesture);
227}
228
229TEST(AXEnumUtilTest, TextAffinity) {
230 TestEnumStringConversion<ax::mojom::TextAffinity>(ParseTextAffinity);
231}
232
233TEST(AXEnumUtilTest, TreeOrder) {
234 TestEnumStringConversion<ax::mojom::TreeOrder>(ParseTreeOrder);
235}
236
237TEST(AXEnumUtilTest, ImageAnnotationStatus) {
238 TestEnumStringConversion<ax::mojom::ImageAnnotationStatus>(
240}
241
242TEST(AXEnumUtilTest, Dropeffect) {
243 TestEnumStringConversion<ax::mojom::Dropeffect>(ParseDropeffect);
244}
245
246} // namespace ui
static int step(int x, SkScalar min, SkScalar max)
Definition: BlurTest.cpp:215
uint8_t value
Definition: dart.idl:641
StringListAttribute
Definition: ax_enums.h:850
StringAttribute
Definition: ax_enums.h:521
AriaCurrentState
Definition: ax_enums.h:1006
FloatAttribute
Definition: ax_enums.h:699
ImageAnnotationStatus
Definition: ax_enums.h:1170
IntListAttribute
Definition: ax_enums.h:799
DefaultActionVerb
Definition: ax_enums.h:489
@ kNone
Definition: layer.h:53
dst
Definition: cp.py:12
ax::mojom::IntListAttribute ParseIntListAttribute(const char *int_list_attribute)
ax::mojom::MoveDirection ParseMoveDirection(const char *move_direction)
ax::mojom::Event ParseEvent(const char *event)
ax::mojom::DescriptionFrom ParseDescriptionFrom(const char *description_from)
ax::mojom::FloatAttribute ParseFloatAttribute(const char *float_attribute)
ax::mojom::TextBoundary ParseTextBoundary(const char *text_boundary)
ax::mojom::ListStyle ParseListStyle(const char *list_style)
ax::mojom::TreeOrder ParseTreeOrder(const char *tree_order)
TEST(AXEnumUtilTest, Event)
ax::mojom::ActionFlags ParseActionFlags(const char *action_flags)
ax::mojom::TextAlign ParseTextAlign(const char *text_align)
ax::mojom::Action ParseAction(const char *action)
const char * ToString(ax::mojom::Event event)
Definition: ax_enum_util.cc:9
ax::mojom::Restriction ParseRestriction(const char *restriction)
ax::mojom::StringAttribute ParseStringAttribute(const char *string_attribute)
void TestAXNodeDataSetter(void(AXNodeData::*Setter)(T, const U &), const U &value)
ax::mojom::Command ParseCommand(const char *command)
ax::mojom::SortDirection ParseSortDirection(const char *sort_direction)
ax::mojom::HasPopup ParseHasPopup(const char *has_popup)
ax::mojom::NameFrom ParseNameFrom(const char *name_from)
ax::mojom::StringListAttribute ParseStringListAttribute(const char *string_list_attribute)
ax::mojom::TextPosition ParseTextPosition(const char *text_position)
ax::mojom::Role ParseRole(const char *role)
ax::mojom::WritingDirection ParseTextDirection(const char *text_direction)
ax::mojom::IntAttribute ParseIntAttribute(const char *int_attribute)
ax::mojom::EventFrom ParseEventFrom(const char *event_from)
ax::mojom::TextAffinity ParseTextAffinity(const char *text_affinity)
ax::mojom::Dropeffect ParseDropeffect(const char *dropeffect)
ax::mojom::BoolAttribute ParseBoolAttribute(const char *bool_attribute)
ax::mojom::DefaultActionVerb ParseDefaultActionVerb(const char *default_action_verb)
ax::mojom::TextDecorationStyle ParseTextDecorationStyle(const char *text_decoration_style)
ax::mojom::AriaCurrentState ParseAriaCurrentState(const char *aria_current_state)
void TestEnumStringConversion(T(ParseFunction)(const char *), int32_t(step)(int32_t)=[](int32_t val) { return val+1;})
ax::mojom::InvalidState ParseInvalidState(const char *invalid_state)
ax::mojom::MarkerType ParseMarkerType(const char *marker_type)
ax::mojom::ImageAnnotationStatus ParseImageAnnotationStatus(const char *status)
ax::mojom::CheckedState ParseCheckedState(const char *checked_state)
ax::mojom::Mutation ParseMutation(const char *mutation)
ax::mojom::Gesture ParseGesture(const char *gesture)
ax::mojom::State ParseState(const char *state)
ax::mojom::TextStyle ParseTextStyle(const char *text_style)
#define T
Definition: precompiler.cc:65
void AddFloatAttribute(ax::mojom::FloatAttribute attribute, float value)
void AddIntListAttribute(ax::mojom::IntListAttribute attribute, const std::vector< int32_t > &value)
void AddIntAttribute(ax::mojom::IntAttribute attribute, int32_t value)
void AddStringListAttribute(ax::mojom::StringListAttribute attribute, const std::vector< std::string > &value)
virtual std::string ToString() const
void AddStringAttribute(ax::mojom::StringAttribute attribute, const std::string &value)
void AddBoolAttribute(ax::mojom::BoolAttribute attribute, bool value)
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678