summaryrefslogtreecommitdiff
path: root/sdk-common/src/test/java/com/android/ide/common/resources/localeGen/LocaleGenerationTest.kt
blob: f2f3f2635addefaab5751c3fd5d733e7cf697b43 (plain)
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
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.ide.common.resources.localeGen

import com.android.ide.common.resources.configuration.FolderConfiguration
import com.android.ide.common.resources.writeLocaleConfig
import com.android.ide.common.resources.generateLocaleConfigManifestAttribute
import com.android.ide.common.resources.generateLocaleList
import com.android.ide.common.resources.generateLocaleString
import com.android.ide.common.resources.mergeLocaleLists
import com.android.ide.common.resources.validateLocale
import com.android.ide.common.resources.writeSupportedLocales
import com.google.common.truth.Truth.assertThat
import junit.framework.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.io.File

class LocaleGenerationTest {
    @get:Rule
    var temporaryFolder = TemporaryFolder()

    private fun addResFolder(root: File, name: String, empty: Boolean) {
        val resFolder = File(root, name)

        resFolder.mkdir()

        if (!empty) {
            File(resFolder, "strings.xml").createNewFile()
        }
    }
    @Test
    fun `Test that manifest attribute is correctly generated`() {
        assertEquals("@xml/_gradle_res_locale_config",
            generateLocaleConfigManifestAttribute("_gradle_res_locale_config"))
    }

    @Test
    fun `Test that locale string is correctly generated`() {
        assertEquals("en-US", generateLocaleString(
            FolderConfiguration.getConfig("values-en-rUS".split("-")).localeQualifier))
        assertEquals("zh-Hans-SG", generateLocaleString(
            FolderConfiguration.getConfig("values-b+zh+Hans+SG".split("-")).localeQualifier))
    }

    @Test
    fun `Test that locale list is correctly generated from a collection of res folders`() {
        val res1 = temporaryFolder.newFolder("res1")
        addResFolder(res1, "values-en-rUS", false)
        addResFolder(res1, "values-ru-rRU", false)
        addResFolder(res1, "values-b+es+ES", false)
        val res2 = temporaryFolder.newFolder("res2")
        addResFolder(res2, "values-pt-rBR", false)
        val res3 = temporaryFolder.newFolder("res3")
        addResFolder(res3, "values-b+zh+Hans+SG", false)

        assertThat(generateLocaleList(listOf(res1, res2, res3)))
            .containsExactly("en-US", "ru-RU", "es-ES", "pt-BR", "zh-Hans-SG")
    }

    @Test
    fun `Test that locale list generator ignores empty folders`() {
        val res1 = temporaryFolder.newFolder("res1")
        addResFolder(res1, "values-en-rUS", false)
        addResFolder(res1, "values-ru-rRU", true)

        assertThat(generateLocaleList(listOf(res1))).containsExactly("en-US")
    }

    @Test
    fun `Test that locale config xml is correctly generated`() {
        val outfile = temporaryFolder.newFile("locale_config.xml")
        writeLocaleConfig(
            output = outfile,
            locales = listOf("en-US", "ru-RU", "es-ES", "pt-BR", "zh-Hans-SG", "en-GB")
        )
        assertThat(
            listOf(
                """<locale-config xmlns:android="http://schemas.android.com/apk/res/android">""",
                """    <locale android:name="en-US"/>""",
                """    <locale android:name="ru-RU"/>""",
                """    <locale android:name="es-ES"/>""",
                """    <locale android:name="pt-BR"/>""",
                """    <locale android:name="zh-Hans-SG"/>""",
                """    <locale android:name="en-GB"/>""",
                """</locale-config>""")).containsExactlyElementsIn(outfile.readLines()).inOrder()
    }

    @Test
    fun `Test that locale lists are correctly merged`() {
        assertThat(listOf("en-US", "en-GB", "es-ES", "pt-BR")).containsExactlyElementsIn(
            mergeLocaleLists(
                listOf(
                    listOf("en-US", "en-GB", "en-GB"),
                    listOf("es-ES", "pt-BR"),
                    listOf()
                )
            )
        ).inOrder()
    }

    @Test
    fun `Test supported locales read and write`() {
        val jsonFile = temporaryFolder.newFile("locales.txt")
        val locales = listOf("en-US", "ru-RU", "es-ES", "pt-BR", "zh-Hans-SG")
        // Write file to json and read it back to make sure it results in the same list
        assertThat(locales).containsExactlyElementsIn(
            run {
                writeSupportedLocales(jsonFile, locales.drop(1), defaultLocale = "en-US")
                jsonFile.readLines()
            }
        ).inOrder()
    }

    @Test
    fun `Test locale validation`() {
        // Make sure invalid locales are not accepted
        assertEquals(null, validateLocale("invalid-locale"))
        // Only locales in the same format as the locales config file are accepted
        // (en-US, and not b+en+US, for instance)
        assertEquals(null, validateLocale("b+pt+BR"))

        // Make sure valid locales are supported
        assertEquals("en-US", validateLocale("en-US"))
    }
}