Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3300: Add check for accessibility label for activities #3352

Merged
merged 182 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from 175 commits
Commits
Show all changes
182 commits
Select commit Hold shift + click to select a range
225a4fc
Fix #3290: Add support for generic regex pattern matching
Sparsh1212 Jun 10, 2021
638895f
Apply review suggestions for proto location
Sparsh1212 Jun 11, 2021
e3fd706
Delete main directory
Sparsh1212 Jun 11, 2021
b25e776
Implment script logic and tests
Sparsh1212 Jun 14, 2021
d44e015
CI setup part 1
Sparsh1212 Jun 14, 2021
de1d38f
syntax fix in yaml file
Sparsh1212 Jun 14, 2021
173fe53
import nits in dummy file
Sparsh1212 Jun 15, 2021
618e380
Use regex patterns from script in script test
Sparsh1212 Jun 16, 2021
e3830ac
Make PR suggestions into effect
Sparsh1212 Jun 16, 2021
bb93c0a
Make PR suggestions into effect
Sparsh1212 Jun 16, 2021
f7342c1
Make PR suggestions into effect
Sparsh1212 Jun 16, 2021
51e593b
Improve naming of script_assets variables
Sparsh1212 Jun 16, 2021
0d17657
Make PR suggestions into effect
Sparsh1212 Jun 16, 2021
16bb041
Make PR suggestions
Sparsh1212 Jun 17, 2021
325f28c
Make PR suggestions
Sparsh1212 Jun 17, 2021
a3f8ea6
Revamp testing approach
Sparsh1212 Jun 18, 2021
b1970eb
Merge branch 'develop' into regex-checks
Sparsh1212 Jun 18, 2021
51e64aa
Fix #3291: Add check for XML syntax correctness
Sparsh1212 Jun 18, 2021
f440a33
nit fixes
Sparsh1212 Jun 18, 2021
255773d
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jun 18, 2021
7b2a178
Introduce XML syntax check in static checks workflow
Sparsh1212 Jun 18, 2021
14c5f6f
Fix #3292: Add check for test files presence for prod files
Sparsh1212 Jun 18, 2021
163310e
Make nit suggestions
Sparsh1212 Jun 20, 2021
6131c1d
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jun 21, 2021
9cb5fc5
Make nit suggestions
Sparsh1212 Jun 21, 2021
4120775
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jun 21, 2021
8c98cf2
Make nit suggestions
Sparsh1212 Jun 21, 2021
7f1762c
Fix #3300: Add check for accessibility labels for activities
Sparsh1212 Jun 21, 2021
5229ac9
Add CI check for label presence for activities
Sparsh1212 Jun 21, 2021
7e1d24a
Add ScriptConstants file to exemptions test list
Sparsh1212 Jun 21, 2021
08cf6c3
merge test-file-check
Sparsh1212 Jun 21, 2021
b72e419
Test if: always()
Sparsh1212 Jun 21, 2021
d8472ab
Revert "Test if: always()"
Sparsh1212 Jun 21, 2021
fe42a64
Add if: always()
Sparsh1212 Jun 21, 2021
e12f247
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jun 21, 2021
9fdb165
Add if: always()
Sparsh1212 Jun 21, 2021
7bf2726
Merge branch 'test-file-check' into accesibilty-label-check
Sparsh1212 Jun 21, 2021
6e3424b
Add if: always()
Sparsh1212 Jun 21, 2021
381853c
Apply review suggestions on PR
Sparsh1212 Jun 22, 2021
c59494f
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jun 23, 2021
293ab92
Make review suggestions from regex pattern checks
Sparsh1212 Jun 23, 2021
f393e1e
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jun 23, 2021
56d607e
Implement review suggestions based on #3340
Sparsh1212 Jun 24, 2021
d01a238
Refactor PR as per feedback recieved
Sparsh1212 Jun 24, 2021
b7cb290
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jun 24, 2021
5a4ac03
Implement feedback suggestions
Sparsh1212 Jun 24, 2021
f6b0715
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jun 24, 2021
0feacee
Implement review suggestions
Sparsh1212 Jun 24, 2021
28fbc73
Implement review suggestions
Sparsh1212 Jun 24, 2021
96c0bfd
Implement review suggestions
Sparsh1212 Jun 24, 2021
dcbf660
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jun 24, 2021
56cf6d0
Implement review suggestions
Sparsh1212 Jun 24, 2021
1e3c47d
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jun 24, 2021
60e64dc
nits
Sparsh1212 Jun 25, 2021
cd2f287
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jun 25, 2021
36033ef
Nit fixes
Sparsh1212 Jun 25, 2021
161f8ec
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jun 25, 2021
996694b
Nit fixes
Sparsh1212 Jun 25, 2021
8740a48
Nit fixes
Sparsh1212 Jun 25, 2021
6671857
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jun 25, 2021
11963c8
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jun 25, 2021
c8aa5e4
merge test-file-check
Sparsh1212 Jun 26, 2021
3b78452
Implement feedback suggestions as per #3340
Sparsh1212 Jun 26, 2021
fe17ed9
nit fix
Sparsh1212 Jun 27, 2021
6860083
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jun 27, 2021
4083244
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jun 27, 2021
7c6f3e8
Merge branch 'test-file-check' into accesibilty-label-check
Sparsh1212 Jun 27, 2021
d65efa4
nit fix
Sparsh1212 Jun 27, 2021
3afc018
nit fix
Sparsh1212 Jun 28, 2021
b13e3f3
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jun 28, 2021
6132e5d
Merge branch 'test-file-check' into accesibilty-label-check
Sparsh1212 Jun 28, 2021
4a5b47e
nit fix
Sparsh1212 Jun 28, 2021
60f2482
Review suggestions part 1
Sparsh1212 Jun 29, 2021
9375775
Implement review suggestions part 2
Sparsh1212 Jun 29, 2021
a94cc5e
nit fixes
Sparsh1212 Jun 30, 2021
a811521
resolve merge conflict
Sparsh1212 Jun 30, 2021
aeb20d3
update static_checks
Sparsh1212 Jun 30, 2021
d5a0166
add test to repository file
Sparsh1212 Jun 30, 2021
8ac402d
nit fix
Sparsh1212 Jun 30, 2021
32910ea
nit fix
Sparsh1212 Jun 30, 2021
8b749db
nit fix
Sparsh1212 Jun 30, 2021
6c08868
merge regex-checks
Sparsh1212 Jun 30, 2021
4add1ed
Do review suggestions
Sparsh1212 Jun 30, 2021
be2f808
Implement review suggestions
Sparsh1212 Jul 1, 2021
1002034
Implement review suggestions
Sparsh1212 Jul 1, 2021
11010b1
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jul 1, 2021
ad4b0ec
nit fix
Sparsh1212 Jul 1, 2021
8eaf8df
merge xml-syntax-check
Sparsh1212 Jul 1, 2021
3042f2e
Implement review suggestions
Sparsh1212 Jul 1, 2021
49745a4
Add Ben as a codeowner for the ScriptExemptions file
Sparsh1212 Jul 1, 2021
05dbe3e
bazel files nit fix
Sparsh1212 Jul 1, 2021
45d73c9
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jul 1, 2021
5a9c601
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jul 1, 2021
83d66e0
Introduce a library for the regex assets
Sparsh1212 Jul 1, 2021
8cae1f8
Make directory structure same as that of #3374
Sparsh1212 Jul 2, 2021
e201a3d
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jul 2, 2021
44d839c
Make directory structure similar to #3374
Sparsh1212 Jul 2, 2021
edd6b4e
Merge branch 'develop' into regex-checks
Sparsh1212 Jul 2, 2021
5e38f79
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jul 2, 2021
73de958
merge xml-syntax-checks
Sparsh1212 Jul 2, 2021
9e67cab
add new files to test file script exemptions and nit fixes
Sparsh1212 Jul 2, 2021
7a73b1f
merge develop
Sparsh1212 Jul 3, 2021
1637ccd
diable ktlint max-line-length
Sparsh1212 Jul 3, 2021
fba990e
disable ktlint-max-line
Sparsh1212 Jul 3, 2021
262a4ec
disable ktlint max-length
Sparsh1212 Jul 3, 2021
8e5887c
merge regex-checks
Sparsh1212 Jul 3, 2021
fcb0555
remove testonly attribute from tests
Sparsh1212 Jul 3, 2021
de21e33
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jul 3, 2021
130c0d9
add new files to script exemptions
Sparsh1212 Jul 3, 2021
b56c104
nit fix
Sparsh1212 Jul 4, 2021
cdb88fe
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jul 4, 2021
98f7d73
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jul 4, 2021
3459461
nit fix
Sparsh1212 Jul 5, 2021
69c5f96
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jul 5, 2021
26a8d52
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jul 5, 2021
2e785d5
merge regex-checks
Sparsh1212 Jul 5, 2021
5e13219
nit fix
Sparsh1212 Jul 5, 2021
21e5197
Apply review suggestions
Sparsh1212 Jul 6, 2021
e557570
ktlint fix
Sparsh1212 Jul 6, 2021
3cb5db3
nit fix
Sparsh1212 Jul 6, 2021
c37cca3
Apply nit fixes
Sparsh1212 Jul 6, 2021
e2186d7
Apply nit fixes
Sparsh1212 Jul 6, 2021
7d7f9fc
Remove script constants
Sparsh1212 Jul 7, 2021
b70c880
nit fix
Sparsh1212 Jul 7, 2021
e70413b
add tests for XML syntax error handler
Sparsh1212 Jul 7, 2021
8e8bcf5
merge xml-syntax-check
Sparsh1212 Jul 7, 2021
31c1df5
add todo
Sparsh1212 Jul 7, 2021
5849bc0
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jul 7, 2021
e09e837
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jul 7, 2021
637606c
add todo
Sparsh1212 Jul 7, 2021
5ed9d9a
Merge branch 'develop' into regex-checks
Sparsh1212 Jul 8, 2021
bab0b70
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jul 8, 2021
c30158c
nit fixes
Sparsh1212 Jul 8, 2021
d57ef42
Merge branch 'develop' into regex-checks
Sparsh1212 Jul 8, 2021
d873ecd
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jul 8, 2021
de8bf3a
add test
Sparsh1212 Jul 8, 2021
5a1cbd9
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jul 8, 2021
91e108a
add testOnly
Sparsh1212 Jul 8, 2021
17ddf56
Merge branch 'regex-checks' into xml-syntax-check
Sparsh1212 Jul 8, 2021
594d31a
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jul 8, 2021
1940980
nit fixes
Sparsh1212 Jul 8, 2021
d34d46b
nit fix
Sparsh1212 Jul 8, 2021
ba0b960
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jul 8, 2021
50ea8ae
nit fixes
Sparsh1212 Jul 8, 2021
c72e5a9
nit fixes
Sparsh1212 Jul 8, 2021
57d0b42
nit fixes
Sparsh1212 Jul 8, 2021
c32c921
nit fixes
Sparsh1212 Jul 8, 2021
b99a316
fix textproto file
Sparsh1212 Jul 8, 2021
af6a607
merge-regex-checks
Sparsh1212 Jul 8, 2021
6bdea69
Merge branch 'xml-syntax-check' into test-file-check
Sparsh1212 Jul 8, 2021
7f13ff8
merge xml-syntax-check
Sparsh1212 Jul 8, 2021
19fc1e9
add exemptions to textproto
Sparsh1212 Jul 8, 2021
99a1f13
sort failures lexicographically
Sparsh1212 Jul 8, 2021
0dd079c
Merge branch 'develop' into test-file-check
Sparsh1212 Jul 9, 2021
f286c73
add new files to exemption list
Sparsh1212 Jul 9, 2021
0db928c
merge test-file-check
Sparsh1212 Jul 11, 2021
0d2d9dd
merge develop
Sparsh1212 Jul 11, 2021
a331ffa
nit fixes
Sparsh1212 Jul 11, 2021
484891e
nit fixes
Sparsh1212 Jul 11, 2021
5a7495d
add kdocs
Sparsh1212 Jul 13, 2021
9988373
nit fix
Sparsh1212 Jul 13, 2021
0eb4dd6
nit fix
Sparsh1212 Jul 13, 2021
76b8e2c
Trigger CI failure exclusively
Sparsh1212 Jul 14, 2021
e34798e
Revert CI check failure
Sparsh1212 Jul 14, 2021
dcba69f
merge develop
Sparsh1212 Jul 17, 2021
f09d0d4
nit fix
Sparsh1212 Jul 17, 2021
9c94456
nit fix
Sparsh1212 Jul 17, 2021
b042bba
nit fixes
Sparsh1212 Jul 20, 2021
8e9c383
Merge branch 'develop' into accesibilty-label-check
Sparsh1212 Jul 22, 2021
dc2b163
nit fixes
Sparsh1212 Jul 22, 2021
b012d04
nit fixes
Sparsh1212 Jul 22, 2021
eacacba
Add redundant exemptions check
Sparsh1212 Jul 24, 2021
57a4b8d
Merge branch 'develop' into accesibilty-label-check
Sparsh1212 Jul 24, 2021
43cf464
rectify static_checks
Sparsh1212 Jul 24, 2021
3d3b03f
Remove duplicate proto libraries
Sparsh1212 Jul 24, 2021
1aa7ead
nit fixes
Sparsh1212 Jul 28, 2021
9fb4e1c
add activity to exemption
Sparsh1212 Jul 28, 2021
fffef74
nit fixes
Sparsh1212 Jul 28, 2021
6f0f05c
nit fixes
Sparsh1212 Jul 28, 2021
015d717
nit fixes
Sparsh1212 Jul 28, 2021
cb67d44
Merge branch 'develop' into accesibilty-label-check
Sparsh1212 Jul 28, 2021
bb68dcd
update branch
Sparsh1212 Jul 28, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/static_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,8 @@ jobs:
if: always()
run: |
bazel run //scripts:test_file_check -- $(pwd)

- name: Accessibility label Check
if: always()
run: |
bazel run //scripts:accessibility_label_check -- $(pwd) scripts/assets/accessibility_label_exemptions.pb app/src/main/AndroidManifest.xml
16 changes: 16 additions & 0 deletions scripts/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Kotlin-based scripts to help developers or perform continuous integration tasks.
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_binary", "kt_jvm_library")
load(
"//scripts:script_assets.bzl",
"generate_accessibility_label_assets_list_from_text_protos",
"generate_maven_assets_list_from_text_protos",
"generate_regex_assets_list_from_text_protos",
"generate_test_file_assets_list_from_text_protos",
Expand Down Expand Up @@ -115,3 +116,18 @@ kt_jvm_binary(
main_class = "org.oppia.android.scripts.maven.GenerateMavenDependenciesListKt",
runtime_deps = ["//scripts/src/java/org/oppia/android/scripts/maven:generate_maven_dependencies_list_lib"],
)

ACCESSIBILITY_LABEL_EXEMPTION_ASSETS = generate_accessibility_label_assets_list_from_text_protos(
name = "accessibility_label_exemption_assets",
accessibility_label_exemptions_name = ["accessibility_label_exemptions"],
)

kt_jvm_binary(
name = "accessibility_label_check",
testonly = True,
data = ACCESSIBILITY_LABEL_EXEMPTION_ASSETS,
main_class = "org.oppia.android.scripts.label.AccessibilityLabelCheckKt",
runtime_deps = [
"//scripts/src/java/org/oppia/android/scripts/label:accessibility_label_check_lib",
],
)
27 changes: 27 additions & 0 deletions scripts/assets/accessibility_label_exemptions.textproto
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
exempted_activity: "app/src/main/java/org/oppia/android/app/home/HomeActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/home/recentlyplayed/RecentlyPlayedActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/mydownloads/MyDownloadsActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/player/state/testing/StateFragmentTestActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/profileprogress/ProfilePictureActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/settings/profile/ProfileEditActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/splash/SplashActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/AudioFragmentTestActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/BindableAdapterTestActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/ConceptCardFragmentTestActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/DragDropTestActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/ExplorationInjectionActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/ExplorationTestActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/HomeFragmentTestActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/HomeTestActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/HtmlParserTestActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/ImageRegionSelectionTestActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/InputInteractionViewTestActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/LessonThumbnailImageViewTestActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/NavigationDrawerTestActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/ProfileChooserFragmentTestActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/SplashTestActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/TestFontScaleConfigurationUtilActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/TopicRevisionTestActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/TopicTestActivity"
exempted_activity: "app/src/main/java/org/oppia/android/app/testing/TopicTestActivityForStory"
exempted_activity: "app/src/main/java/org/oppia/android/app/walkthrough/WalkthroughActivity"
29 changes: 27 additions & 2 deletions scripts/script_assets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ def generate_test_file_assets_list_from_text_protos(

Args:
name: str. The name of this generation instance. This will be a prefix for derived targets.
test_file_exemptions_name: list of str. The list of test file exemptions file names.
test_file_exemptions_name: list of str. The list of test file exemptions file name.

Returns:
list of str. The list of new proto binary asset files that were generated.
"""
return generate_proto_binary_assets(
name = name,
names = test_file_exemptions_name,
proto_dep_name = "test_file_exemptions",
proto_dep_name = "script_exemptions",
proto_type_name = "TestFileExemptions",
name_prefix = name,
asset_dir = "assets",
Expand Down Expand Up @@ -87,3 +87,28 @@ def generate_maven_assets_list_from_text_protos(
proto_dep_bazel_target_prefix = "//scripts/src/java/org/oppia/android/scripts/proto",
proto_package = "proto",
)

def generate_accessibility_label_assets_list_from_text_protos(
name,
accessibility_label_exemptions_name):
"""
Converts a single list of text proto assets to binary.

Args:
name: str. The name of this generation instance. This will be a prefix for derived targets.
accessibility_label_exemptions_name: list of str. The list of accessibility label exemptions
file name.

Returns:
list of str. The list of new proto binary asset files that were generated.
"""
return generate_proto_binary_assets(
name = name,
names = accessibility_label_exemptions_name,
proto_dep_name = "script_exemptions",
proto_type_name = "AccessibilityLabelExemptions",
name_prefix = name,
asset_dir = "assets",
proto_dep_bazel_target_prefix = "//scripts/src/java/org/oppia/android/scripts/proto",
proto_package = "proto",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
package org.oppia.android.scripts.label

import org.oppia.android.scripts.proto.AccessibilityLabelExemptions
import org.w3c.dom.Node
import org.w3c.dom.NodeList
import java.io.File
import java.io.FileInputStream
import javax.xml.parsers.DocumentBuilderFactory

/**
* Script for ensuring that all the Activities in the codebase are defined with accessibility
* labels.
*
* Usage:
* bazel run //scripts:accessibility_label_check -- <path_to_directory_root>
* <path_to_proto_binary> [paths to manifest files...]
*
* Arguments:
* - path_to_directory_root: directory path to the root of the Oppia Android repository.
* - path_to_proto_binary: relative path to the exemption .pb file.
* - paths to manifest files: paths leading to the manifest files.
*
* Example:
* bazel run //scripts:accessibility_label_check -- $(pwd) app/src/main/AndroidManifest.xml
* scripts/assets/accessibility_label_exemptions.pb
*/
fun main(vararg args: String) {
val repoPath = "${args[0]}/"

val pathToProtoBinary = args[1]

val accessibilityLabelExemptionTextProtoFilePath = "scripts/assets/accessibility_label_exemptions"

val accessibilityLabelExemptionList = loadAccessibilityLabelExemptionsProto(pathToProtoBinary)
.getExemptedActivityList()

val manifestPaths = args.drop(2)

val activityPathPrefix = "app/src/main/java/"

val builderFactory = DocumentBuilderFactory.newInstance()

val repoRoot = File(repoPath)

val missingAccessibilityLabelActivities = manifestPaths.flatMap { relativePath ->
val file = File(repoRoot, relativePath)
val docBuilder = builderFactory.newDocumentBuilder()
val doc = docBuilder.parse(file)
// Normalisation results in the removal of redundancies such as whitespaces, line breaks and
// comments.
doc.getDocumentElement().normalize()
BenHenning marked this conversation as resolved.
Show resolved Hide resolved
val packageName = doc.getDocumentElement().getAttribute("package")
return@flatMap doc.getElementsByTagName("activity").toListOfNodes().mapNotNull { activityNode ->
computeFailureActivityPath(
activityNode = activityNode,
activityPathPrefix = activityPathPrefix,
packageName = packageName
)
}
}

val redundantExemptions = accessibilityLabelExemptionList - missingAccessibilityLabelActivities

val failureActivitiesAfterExemption = missingAccessibilityLabelActivities -
accessibilityLabelExemptionList

logRedundantExemptions(redundantExemptions, accessibilityLabelExemptionTextProtoFilePath)

logFailures(failureActivitiesAfterExemption, accessibilityLabelExemptionTextProtoFilePath)

if (failureActivitiesAfterExemption.isNotEmpty() || redundantExemptions.isNotEmpty()) {
throw Exception("ACCESSIBILITY LABEL CHECK FAILED")
} else {
println("ACCESSIBILITY LABEL CHECK PASSED")
}
}

/**
* Computes path of the activity which fails the accesssibility label check.
*
* @param activityNode the activity node
* @param activityPathPrefix the path prefix for the activities
* @param packageName the package attribute value of the manifest element
* @return path of the failing activity relative to the root repository. This returns null if the
* activity has an accessibility label present.
*/
private fun computeFailureActivityPath(
activityNode: Node,
activityPathPrefix: String,
packageName: String
): String? {
val attributesList = activityNode.getAttributes()
val activityName = attributesList.getNamedItem("android:name").getNodeValue()
val activityPath = computeActivityPathFromName(
activityPathPrefix = activityPathPrefix,
activityName = activityName,
packageName = packageName
)
if (attributesList.getNamedItem("android:label") != null) {
return null
}
return activityPath
}

/**
* Computes the activity path from the name attribute value of the activity element.
*
* @param activityPathPrefix the path prefix for the activities
* @param activityName the name attribute value of the activity element
* @param packageName the package attribute value of the manifest element
* @return the activity path relative to the root repository
*/
private fun computeActivityPathFromName(
activityPathPrefix: String,
activityName: String,
packageName: String
): String {
if (activityName.startsWith(".")) {
return activityPathPrefix + (packageName + activityName).replace(".", "/")
} else {
return activityPathPrefix + activityName.replace(".", "/")
}
}

/**
* Converts [NodeList] to list of nodes, since [NodeList] is not iterable.
*
* @return the list of nodes
*/
private fun NodeList.toListOfNodes(): List<Node> = (0 until getLength()).map(this::item)

/**
* Logs the failures for accessibility label check.
*
* @param missingAccessibilityLabelActivities list of Activities missing accessibility label
* @param accessibilityLabelExemptionTextProtoFilePath the location of the accessibility label
* exemption textproto file.
*/
private fun logFailures(
missingAccessibilityLabelActivities: List<String>,
accessibilityLabelExemptionTextProtoFilePath: String
) {
if (missingAccessibilityLabelActivities.isNotEmpty()) {
println("Accessibility label missing for Activities:")
missingAccessibilityLabelActivities.sorted().forEach { activityPath ->
println("- $activityPath")
}
println()
println(
"If this is correct, please update $accessibilityLabelExemptionTextProtoFilePath.textproto"
)
println(
"Note that, in general, all Activities should have labels. If you choose to add an" +
" exemption, please specifically call this out in your PR description."
)
println()
}
}

/**
* Logs the redundant exemptions.
*
* @param redundantExemptions list of redundant exemptions
* @param accessibilityLabelExemptionTextProtoFilePath the location of the accessibility label
* exemption textproto file.
*/
private fun logRedundantExemptions(
redundantExemptions: List<String>,
accessibilityLabelExemptionTextProtoFilePath: String
) {
if (redundantExemptions.isNotEmpty()) {
println("Redundant exemptions:")
redundantExemptions.sorted().forEach { exemption ->
println("- $exemption")
}
println(
"Please remove them from $accessibilityLabelExemptionTextProtoFilePath.textproto"
)
println()
}
}

/**
* Loads the test file exemptions list to proto.
*
* @param pathToProtoBinary path to the pb file to be parsed
* @return proto class from the parsed textproto file
*/
private fun loadAccessibilityLabelExemptionsProto(
pathToProtoBinary: String
): AccessibilityLabelExemptions {
val protoBinaryFile = File(pathToProtoBinary)
val builder = AccessibilityLabelExemptions.getDefaultInstance().newBuilderForType()

// This cast is type-safe since proto guarantees type consistency from mergeFrom(),
// and this method is bounded by the generic type T.
@Suppress("UNCHECKED_CAST")
val protoObj: AccessibilityLabelExemptions =
FileInputStream(protoBinaryFile).use {
builder.mergeFrom(it)
}.build() as AccessibilityLabelExemptions
return protoObj
}
17 changes: 17 additions & 0 deletions scripts/src/java/org/oppia/android/scripts/label/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
Libraries corresponding to accessibility label check that ensures all the activities in the codebase
are defined with labels.
"""

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library")

kt_jvm_library(
name = "accessibility_label_check_lib",
testonly = True,
srcs = ["AccessibilityLabelCheck.kt"],
visibility = ["//scripts:oppia_script_binary_visibility"],
deps = [
"//scripts/src/java/org/oppia/android/scripts/common:repository_file",
"//scripts/src/java/org/oppia/android/scripts/proto:script_exemptions_java_proto_lite",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ java_lite_proto_library(
)

proto_library(
name = "test_file_exemptions_proto",
name = "script_exemptions_proto",
srcs = ["script_exemptions.proto"],
visibility = ["//scripts:oppia_script_binary_visibility"],
)

java_lite_proto_library(
name = "test_file_exemptions_java_proto_lite",
name = "script_exemptions_java_proto_lite",
visibility = ["//scripts:oppia_script_library_visibility"],
deps = [":test_file_exemptions_proto"],
deps = [":script_exemptions_proto"],
)

proto_library(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,19 @@ message TestFileExemptions {
// at the correct lexicographical position in the textproto file.
repeated string exempted_file_path = 1;
}

// Exemptions for the accessibility label check. Exemptions indicate Activities for which we should
// not check for the presence of an accessibility label.
message AccessibilityLabelExemptions {
// List of all the Activities which should be exempted for the accessibility label check.
// For exempting any Activity, provide its relative path to root in
// 'script/assets/accessibility_label_exemptions.textproto'
// Also, note that the exemptions in the textproto file are maintained in lexicographical order.
// While adding any new Activity, please add it only at the correct lexicographical position,
// so that the list remains sorted.
//
// For example if we want to add the 'RecentlyPlayedActivity' to the exemption list, add:
// 'exempted_activity: "app/src/main/java/org/oppia/android/app/home/HomeActivity"'
// at the correct lexicographical position in the textproto file.
repeated string exempted_activity = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ kt_jvm_library(
visibility = ["//scripts:oppia_script_binary_visibility"],
deps = [
"//scripts/src/java/org/oppia/android/scripts/common:repository_file",
"//scripts/src/java/org/oppia/android/scripts/proto:test_file_exemptions_java_proto_lite",
"//scripts/src/java/org/oppia/android/scripts/proto:script_exemptions_java_proto_lite",
],
)
Loading