blob: f1d484c31bd708bded8c4d679cb6040ea2609364 [file] [log] [blame]
Avi Drissman3e1a26c2022-09-15 20:26:031# Copyright 2016 The Chromium Authors
dmazzoni832058d12016-08-31 21:55:312# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Presubmit script for ui/accessibility."""
6
David Tseng2138d4f2021-08-23 15:02:557import json
8import os
9import re
dmazzoni832058d12016-08-31 21:55:3110
Gavin Mak11ca1d62021-05-25 17:52:3211USE_PYTHON3 = True
12
Dominic Mazzonidcef1b732018-01-26 17:57:0413AX_MOJOM = 'ui/accessibility/ax_enums.mojom'
Randy Rossi9e3b5c52019-04-30 20:58:5814AUTOMATION_IDL = 'extensions/common/api/automation.idl'
dmazzoni832058d12016-08-31 21:55:3115
dpapad9fd544c42022-05-25 18:35:2916AX_TS_FILE = 'chrome/browser/resources/accessibility/accessibility.ts'
Dominic Mazzoni90501482018-09-05 22:43:3117AX_MODE_HEADER = 'ui/accessibility/ax_mode.h'
Doug Turner63f3c7b2017-07-29 05:10:0118
dmazzoni832058d12016-08-31 21:55:3119def InitialLowerCamelCase(unix_name):
20 words = unix_name.split('_')
21 return words[0] + ''.join(word.capitalize() for word in words[1:])
22
Dominic Mazzonidcef1b732018-01-26 17:57:0423def CamelToLowerHacker(str):
24 out = ''
25 for i in range(len(str)):
26 if str[i] >= 'A' and str[i] <= 'Z' and out:
27 out += '_'
28 out += str[i]
29 return out.lower()
30
31# Given a full path to an IDL or MOJOM file containing enum definitions,
dmazzoni832058d12016-08-31 21:55:3132# parse the file for enums and return a dict mapping the enum name
33# to a list of values for that enum.
David Tseng2138d4f2021-08-23 15:02:5534def GetEnumsFromFile(fullpath, get_raw_enum_value=False):
dmazzoni832058d12016-08-31 21:55:3135 enum_name = None
36 enums = {}
37 for line in open(fullpath).readlines():
38 # Strip out comments
39 line = re.sub('//.*', '', line)
40
David Tsengb5752b22021-05-18 02:49:3441 # Strip out mojo annotations.
42 line = re.sub('\[(.*)\]', '', line)
43
dmazzoni832058d12016-08-31 21:55:3144 # Look for lines of the form "enum ENUM_NAME {" and get the enum_name
45 m = re.search('enum ([\w]+) {', line)
46 if m:
47 enum_name = m.group(1)
48 continue
49
50 # Look for a "}" character signifying the end of an enum
51 if line.find('}') >= 0:
52 enum_name = None
53 continue
54
55 if not enum_name:
56 continue
57
David Tseng2138d4f2021-08-23 15:02:5558 # We're now inside of a enum definition.
59 # First, if requested, add the raw line.
60 if get_raw_enum_value:
61 enums.setdefault(enum_name, [])
62 enums[enum_name].append(line)
63 continue
64
65 # Add the first string consisting of alphanumerics plus underscore ("\w") to
66 # the list of values for that enum.
dmazzoni832058d12016-08-31 21:55:3167 m = re.search('([\w]+)', line)
68 if m:
69 enums.setdefault(enum_name, [])
Dominic Mazzonidcef1b732018-01-26 17:57:0470 enum_value = m.group(1)
71 if (enum_value[0] == 'k' and
72 enum_value[1] == enum_value[1].upper()):
73 enum_value = CamelToLowerHacker(enum_value[1:])
74 if enum_value == 'none' or enum_value == 'last':
75 continue
Dominic Mazzonidcef1b732018-01-26 17:57:0476 enums[enum_name].append(enum_value)
dmazzoni832058d12016-08-31 21:55:3177
78 return enums
79
80def CheckMatchingEnum(ax_enums,
81 ax_enum_name,
82 automation_enums,
83 automation_enum_name,
84 errs,
David Tsenge8aa563c2019-02-14 02:42:0785 output_api,
Dominic Mazzonid1d89d72020-10-05 07:20:1686 strict_ordering=False,
87 allow_extra_destination_enums=False):
dmazzoni832058d12016-08-31 21:55:3188 if ax_enum_name not in ax_enums:
89 errs.append(output_api.PresubmitError(
Dominic Mazzonidcef1b732018-01-26 17:57:0490 'Expected %s to have an enum named %s' % (AX_MOJOM, ax_enum_name)))
dmazzoni832058d12016-08-31 21:55:3191 return
92 if automation_enum_name not in automation_enums:
93 errs.append(output_api.PresubmitError(
94 'Expected %s to have an enum named %s' % (
95 AUTOMATION_IDL, automation_enum_name)))
96 return
97 src = ax_enums[ax_enum_name]
98 dst = automation_enums[automation_enum_name]
David Tsenge8aa563c2019-02-14 02:42:0799 if strict_ordering and len(src) != len(dst):
100 errs.append(output_api.PresubmitError(
101 'Expected %s to have the same number of items as %s' % (
102 automation_enum_name, ax_enum_name)))
103 return
104
105 if strict_ordering:
106 for index, value in enumerate(src):
107 lower_value = InitialLowerCamelCase(value)
108 if lower_value != dst[index]:
109 errs.append(output_api.PresubmitError(
110 ('At index %s in enums, unexpected ordering around %s.%s ' +
111 'and %s.%s in %s and %s') % (
112 index, ax_enum_name, lower_value,
113 automation_enum_name, dst[index],
114 AX_MOJOM, AUTOMATION_IDL)))
115 return
116 return
117
dmazzoni832058d12016-08-31 21:55:31118 for value in src:
Aaron Leventhal3b8e2582017-07-29 18:59:51119 lower_value = InitialLowerCamelCase(value)
120 if lower_value in dst:
121 dst.remove(lower_value) # Any remaining at end are extra and a mismatch.
122 else:
dmazzoni832058d12016-08-31 21:55:31123 errs.append(output_api.PresubmitError(
124 'Found %s.%s in %s, but did not find %s.%s in %s' % (
Dominic Mazzonidcef1b732018-01-26 17:57:04125 ax_enum_name, value, AX_MOJOM,
dmazzoni832058d12016-08-31 21:55:31126 automation_enum_name, InitialLowerCamelCase(value),
127 AUTOMATION_IDL)))
Aaron Leventhal3b8e2582017-07-29 18:59:51128 # Should be no remaining items
Dominic Mazzonid1d89d72020-10-05 07:20:16129 if not allow_extra_destination_enums:
130 for value in dst:
131 errs.append(output_api.PresubmitError(
132 'Found %s.%s in %s, but did not find %s.%s in %s' % (
133 automation_enum_name, value, AUTOMATION_IDL,
134 ax_enum_name, InitialLowerCamelCase(value),
135 AX_MOJOM)))
dmazzoni832058d12016-08-31 21:55:31136
137def CheckEnumsMatch(input_api, output_api):
138 repo_root = input_api.change.RepositoryRoot()
Dominic Mazzonidcef1b732018-01-26 17:57:04139 ax_enums = GetEnumsFromFile(os.path.join(repo_root, AX_MOJOM))
dmazzoni832058d12016-08-31 21:55:31140 automation_enums = GetEnumsFromFile(os.path.join(repo_root, AUTOMATION_IDL))
David Tseng4a25f752017-08-22 19:56:11141
142 # Focused state only exists in automation.
143 automation_enums['StateType'].remove('focused')
Katie Dektarbd851852017-09-29 04:44:25144 # Offscreen state only exists in automation.
145 automation_enums['StateType'].remove('offscreen')
David Tseng4a25f752017-08-22 19:56:11146
dmazzoni832058d12016-08-31 21:55:31147 errs = []
Dominic Mazzonidcef1b732018-01-26 17:57:04148 CheckMatchingEnum(ax_enums, 'Role', automation_enums, 'RoleType', errs,
dmazzoni832058d12016-08-31 21:55:31149 output_api)
Dominic Mazzonidcef1b732018-01-26 17:57:04150 CheckMatchingEnum(ax_enums, 'State', automation_enums, 'StateType', errs,
David Tsenge8aa563c2019-02-14 02:42:07151 output_api, strict_ordering=True)
David Tseng710bbab2018-04-09 23:47:38152 CheckMatchingEnum(ax_enums, 'Action', automation_enums, 'ActionType', errs,
David Tsenge8aa563c2019-02-14 02:42:07153 output_api, strict_ordering=True)
Dominic Mazzonidcef1b732018-01-26 17:57:04154 CheckMatchingEnum(ax_enums, 'Event', automation_enums, 'EventType', errs,
Dominic Mazzonid1d89d72020-10-05 07:20:16155 output_api, allow_extra_destination_enums=True)
Dominic Mazzonidcef1b732018-01-26 17:57:04156 CheckMatchingEnum(ax_enums, 'NameFrom', automation_enums, 'NameFromType',
dmazzoni59642422017-01-25 23:17:46157 errs, output_api)
David Tseng2038f7412019-12-04 05:12:38158 CheckMatchingEnum(ax_enums, 'DescriptionFrom', automation_enums,
159 'DescriptionFromType', errs, output_api)
Dominic Mazzonidcef1b732018-01-26 17:57:04160 CheckMatchingEnum(ax_enums, 'Restriction', automation_enums,
Aaron Leventhalf36960a2017-07-19 13:07:46161 'Restriction', errs, output_api)
David Tsengca14de92018-03-14 20:30:29162 CheckMatchingEnum(ax_enums, 'DefaultActionVerb', automation_enums,
163 'DefaultActionVerb', errs, output_api)
David Tseng2038f7412019-12-04 05:12:38164 CheckMatchingEnum(ax_enums, 'MarkerType', automation_enums,
165 'MarkerType', errs, output_api)
David Tseng2d099ea2020-05-14 18:56:09166 CheckMatchingEnum(ax_enums, 'Command', automation_enums,
Nektarios Paisios8b5613b02020-09-23 17:30:29167 'IntentCommandType', errs, output_api)
168 CheckMatchingEnum(ax_enums, 'InputEventType', automation_enums,
169 'IntentInputEventType', errs, output_api)
David Tseng2d099ea2020-05-14 18:56:09170 CheckMatchingEnum(ax_enums, 'TextBoundary', automation_enums,
Nektarios Paisios8b5613b02020-09-23 17:30:29171 'IntentTextBoundaryType', errs, output_api)
David Tseng2d099ea2020-05-14 18:56:09172 CheckMatchingEnum(ax_enums, 'MoveDirection', automation_enums,
Nektarios Paisios8b5613b02020-09-23 17:30:29173 'IntentMoveDirectionType', errs, output_api)
David Tseng62f95522020-08-24 18:18:19174 CheckMatchingEnum(ax_enums, 'SortDirection', automation_enums,
175 'SortDirectionType', errs, output_api)
David Tseng44784602021-03-19 22:06:09176 CheckMatchingEnum(ax_enums, 'HasPopup', automation_enums,
177 'HasPopup', errs, output_api)
Katie Dektar828358df2021-03-20 02:29:59178 CheckMatchingEnum(ax_enums, 'AriaCurrentState', automation_enums,
179 'AriaCurrentState', errs, output_api)
dmazzoni832058d12016-08-31 21:55:31180 return errs
181
David Tseng2138d4f2021-08-23 15:02:55182def CheckAXEnumsOrdinals(input_api, output_api):
183 repo_root = input_api.change.RepositoryRoot()
184 ax_enums = GetEnumsFromFile(
185 os.path.join(repo_root, AX_MOJOM), get_raw_enum_value=True)
186
187 # Find all enums containing enum values with ordinals and save each enum value
188 # as a pair e.g. (kEnumValue, 100).
189 enums_with_ordinal_values = {}
190 for enum_name in ax_enums:
191 for enum_value in ax_enums[enum_name]:
192 m = re.search("([\w]+) = ([\d]+)", enum_value)
193 if not m:
David Tseng61966982021-09-15 03:11:31194 continue
David Tseng2138d4f2021-08-23 15:02:55195
196 enums_with_ordinal_values.setdefault(enum_name, [])
197 enums_with_ordinal_values[enum_name].append(m.groups(1))
198
199 # Now, do the validation for each enum.
200 errs = []
201 for enum_name in enums_with_ordinal_values:
202 # This is expected to not be continuous.
203 if enum_name == "MarkerType":
204 continue
205
206 enum = enums_with_ordinal_values[enum_name]
207 enum.sort(key = lambda item: int(item[1]))
208 index = 0
209 for enum_value in enum:
210 if index == int(enum_value[1]):
211 index += 1
212 continue
213
214 errs.append(output_api.PresubmitError(
215 "Unexpected enum %s ordinal: %s = %s. Expected %d." % (
216 enum_name, enum_value[0], enum_value[1], index)))
217
218 return errs
219
Doug Turner63f3c7b2017-07-29 05:10:01220# Given a full path to c++ header, return an array of the first static
221# constexpr defined. (Note there can be more than one defined in a C++
222# header)
223def GetConstexprFromFile(fullpath):
224 values = []
225 for line in open(fullpath).readlines():
226 # Strip out comments
227 line = re.sub('//.*', '', line)
228
229 # Look for lines of the form "static constexpr <type> NAME "
230 m = re.search('static constexpr [\w]+ ([\w]+)', line)
231 if m:
Dominic Mazzonid9fda172019-03-07 17:12:07232 value = m.group(1)
233 # Skip first/last sentinels
Aaron Leventhal75a22772022-08-26 12:02:17234 if (value in ['kNone', 'kFirstModeFlag', 'kLastModeFlag']):
Dominic Mazzonid9fda172019-03-07 17:12:07235 continue
236 values.append(value)
Doug Turner63f3c7b2017-07-29 05:10:01237
238 return values
239
240# Given a full path to js file, return the AXMode consts
241# defined
242def GetAccessibilityModesFromFile(fullpath):
243 values = []
244 inside = False
245 for line in open(fullpath).readlines():
dpapad9fd544c42022-05-25 18:35:29246 if not inside:
247 # Look for the block of code that defines the AXMode enum.
dpapad3fe62eb2022-10-19 22:55:03248 m = re.search('^enum AxMode {$', line)
dpapad9fd544c42022-05-25 18:35:29249 if m:
250 inside = True
Doug Turner63f3c7b2017-07-29 05:10:01251 continue
252
dpapad9fd544c42022-05-25 18:35:29253 # Look for a "}" character signifying the end of the enum.
254 m = re.search('^}$', line)
255 if m:
Doug Turner63f3c7b2017-07-29 05:10:01256 return values
257
dpapad9fd544c42022-05-25 18:35:29258 m = re.search('([\w]+) = ', line)
Doug Turner63f3c7b2017-07-29 05:10:01259 if m:
260 values.append(m.group(1))
261 continue
262
Doug Turner63f3c7b2017-07-29 05:10:01263 return values
264
265# Make sure that the modes defined in the C++ header match those defined in
266# the js file. Note that this doesn't guarantee that the values are the same,
267# but does make sure if we add or remove we can signal to the developer that
268# they should be aware that this dependency exists.
269def CheckModesMatch(input_api, output_api):
270 errs = []
271 repo_root = input_api.change.RepositoryRoot()
272
273 ax_modes_in_header = GetConstexprFromFile(
274 os.path.join(repo_root,AX_MODE_HEADER))
275 ax_modes_in_js = GetAccessibilityModesFromFile(
dpapad9fd544c42022-05-25 18:35:29276 os.path.join(repo_root, AX_TS_FILE))
277
278 # In TypeScript enum values are NAMED_LIKE_THIS. Transform them to make them
279 # comparable to the C++ naming scheme.
280 ax_modes_in_js = list(
281 map(lambda s: ('k' + s.replace('_', '')).lower(), ax_modes_in_js))
282
283 # The following AxMode values are not used in the UI, and are purposefully
284 # omitted.
285 unused_ax_modes = [
286 'kAXModeBasic',
287 'kAXModeWebContentsOnly',
288 'kAXModeComplete',
289 'kAXModeCompleteNoHTML',
290 ]
Doug Turner63f3c7b2017-07-29 05:10:01291
292 for value in ax_modes_in_header:
dpapad9fd544c42022-05-25 18:35:29293 if value in unused_ax_modes:
294 continue
295
296 equivalent_value = value.lower()
297 if equivalent_value not in ax_modes_in_js:
Doug Turner63f3c7b2017-07-29 05:10:01298 errs.append(output_api.PresubmitError(
dpapad9fd544c42022-05-25 18:35:29299 'Found %s in %s, but did not find an equivalent value in %s' % (
300 value, AX_MODE_HEADER, AX_TS_FILE)))
Doug Turner63f3c7b2017-07-29 05:10:01301 return errs
302
dmazzoni832058d12016-08-31 21:55:31303def CheckChangeOnUpload(input_api, output_api):
Doug Turner63f3c7b2017-07-29 05:10:01304 errs = []
Doug Turner4430d79c2017-08-07 21:00:40305 for path in input_api.LocalPaths():
306 path = path.replace('\\', '/')
Dominic Mazzonidcef1b732018-01-26 17:57:04307 if AX_MOJOM == path:
Doug Turner4430d79c2017-08-07 21:00:40308 errs.extend(CheckEnumsMatch(input_api, output_api))
David Tseng2138d4f2021-08-23 15:02:55309 errs.extend(CheckAXEnumsOrdinals(input_api, output_api))
Doug Turner63f3c7b2017-07-29 05:10:01310
Doug Turner4430d79c2017-08-07 21:00:40311 if AX_MODE_HEADER == path:
312 errs.extend(CheckModesMatch(input_api, output_api))
Doug Turner63f3c7b2017-07-29 05:10:01313
314 return errs
dmazzoni832058d12016-08-31 21:55:31315
316def CheckChangeOnCommit(input_api, output_api):
Doug Turner63f3c7b2017-07-29 05:10:01317 errs = []
Doug Turner4430d79c2017-08-07 21:00:40318 for path in input_api.LocalPaths():
319 path = path.replace('\\', '/')
Dominic Mazzonidcef1b732018-01-26 17:57:04320 if AX_MOJOM == path:
Doug Turner4430d79c2017-08-07 21:00:40321 errs.extend(CheckEnumsMatch(input_api, output_api))
Doug Turner63f3c7b2017-07-29 05:10:01322
Doug Turner4430d79c2017-08-07 21:00:40323 if AX_MODE_HEADER == path:
324 errs.extend(CheckModesMatch(input_api, output_api))
Doug Turner63f3c7b2017-07-29 05:10:01325
326 return errs