diff --git a/scripts/assets/file_content_validation_checks.textproto b/scripts/assets/file_content_validation_checks.textproto index 677e7ebcb9e..f46fe4fb87f 100644 --- a/scripts/assets/file_content_validation_checks.textproto +++ b/scripts/assets/file_content_validation_checks.textproto @@ -353,6 +353,17 @@ file_content_checks { prohibited_content_regex: "@color/(?!component_color_).+|\"#\\p{XDigit}+\"" failure_message: "Only colors from component_colors.xml may be used in layouts." } +file_content_checks { + file_path_regex: "app/src/main/res/drawable.*?/.+?\\.xml" + prohibited_content_regex: "@color/(?!component_color_).+|\"#\\p{XDigit}+\"" + failure_message: "Only colors from component_colors.xml may be used in drawables except vector assets." + exempted_file_patterns: "app/src/main/res/drawable.*?/(ic_|lesson_thumbnail_graphic_).+?\\.xml" + exempted_file_patterns: "app/src/main/res/drawable/full_oppia_logo.xml" + exempted_file_patterns: "app/src/main/res/drawable/rounded_white_background_with_shadow.xml" + exempted_file_patterns: "app/src/main/res/drawable/profile_image_shadow.xml" + exempted_file_patterns: "app/src/main/res/drawable/selected_region_background.xml" + exempted_file_patterns: "app/src/main/res/drawable/splash_page.xml" +} file_content_checks { file_path_regex: "app/src/main/java/org/oppia/android/app.?/.+(ActivityPresenter|FragmentPresenter|ViewPresenter|Activity|Fragment|View)\\.kt" prohibited_content_regex: "R.color.(?!component_color_).+" diff --git a/scripts/src/javatests/org/oppia/android/scripts/regex/RegexPatternValidationCheckTest.kt b/scripts/src/javatests/org/oppia/android/scripts/regex/RegexPatternValidationCheckTest.kt index ae1d4fa60ff..20fd219f857 100644 --- a/scripts/src/javatests/org/oppia/android/scripts/regex/RegexPatternValidationCheckTest.kt +++ b/scripts/src/javatests/org/oppia/android/scripts/regex/RegexPatternValidationCheckTest.kt @@ -173,6 +173,8 @@ class RegexPatternValidationCheckTest { "Only colors from color_defs.xml may be used in color_palette.xml." private val doesNotReferenceColorFromComponentColorInLayouts = "Only colors from component_colors.xml may be used in layouts." + private val doesNotReferenceColorFromComponentColorInDrawables = + "Only colors from component_colors.xml may be used in drawables except vector assets." private val doesNotReferenceColorFromComponentColorInKotlinFiles = "Only colors from component_colors.xml may be used in Kotlin Files (Activities, Fragments, " + "Views and Presenters)." @@ -2343,7 +2345,35 @@ class RegexPatternValidationCheckTest { ) } - // TODO(#5075): Add test for drawables file that checks color uses only component colors + @Test + fun testFileContent_xmlDrawables_includesNonColorComponentReferences_fileContentIsNotCorrect() { + val prohibitedContent = + """ + android:color="@color/component_color_shared_primary_text_color" + android:color="@color/color_defs_shared_primary_text_color" + android:color="@color/color_palette_primary_text_color" + android:color="#003933" + """.trimIndent() + tempFolder.newFolder("testfiles", "app", "src", "main", "res", "drawable") + val stringFilePath = "app/src/main/res/drawable/test_layout.xml" + tempFolder.newFile("testfiles/$stringFilePath").writeText(prohibitedContent) + + val exception = assertThrows(Exception::class) { + runScript() + } + + // Verify that all patterns are properly detected & prohibited. + assertThat(exception).hasMessageThat().contains(REGEX_CHECK_FAILED_OUTPUT_INDICATOR) + assertThat(outContent.toString().trim()) + .isEqualTo( + """ + $stringFilePath:2: $doesNotReferenceColorFromComponentColorInDrawables + $stringFilePath:3: $doesNotReferenceColorFromComponentColorInDrawables + $stringFilePath:4: $doesNotReferenceColorFromComponentColorInDrawables + $wikiReferenceNote + """.trimIndent() + ) + } @Test fun testFileContent_kotlinFiles_includesNonColorComponentReferences_fileContentIsNotCorrect() {