Add a script to generate the screenshots for all the available languages.
Change the format of data.js generated file. Add Fragment to the URL to limit the number of displayed languages. Add a checkbox to display all screenshots or not.
This commit is contained in:
committed by
Benoit Marty
parent
a9e97d7aa1
commit
1e13319f95
@@ -1,35 +1,53 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# Copyright 2024 New Vector Ltd
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from util import compare
|
||||
|
||||
|
||||
# Read all arguments and return a list of them, this are the languages list.
|
||||
def readArguments():
|
||||
# Return sys.argv without the first argument
|
||||
return sys.argv[1:]
|
||||
|
||||
|
||||
def generateAllScreenshots(languages):
|
||||
# If languages is empty, generate all screenshots
|
||||
if len(languages) == 0:
|
||||
print("Generating all screenshots...")
|
||||
os.system("./gradlew recordPaparazziDebug -PallLanguages")
|
||||
print("Generating all screenshots...")
|
||||
os.system("./gradlew --stop")
|
||||
os.system("./gradlew recordPaparazziDebug -PallLanguages")
|
||||
else:
|
||||
tFile = "tests/uitests/src/test/kotlin/ui/T.kt"
|
||||
print("Generating screenshots for languages: %s" % languages)
|
||||
# Patch file T.kt, replace `@TestParameter(value = ["de"]) localeStr: String,` with `@TestParameter(value = ["de", "fr"]) localeStr: String,`
|
||||
with open(tFile, "r") as file:
|
||||
data = file.read()
|
||||
languagesList = ", ".join([f"\"{lang}\"" for lang in languages])
|
||||
data = data.replace("@TestParameter(value = [\"de\"]) localeStr: String,", "@TestParameter(value = [%s]) localeStr: String," % languagesList)
|
||||
with open(tFile, "w") as file:
|
||||
file.write(data)
|
||||
os.system("./gradlew recordPaparazziDebug -PallLanguagesNoEnglish")
|
||||
# Git reset the change on file T.kt
|
||||
os.system("git checkout HEAD -- %s" % tFile)
|
||||
|
||||
|
||||
tFile = "tests/uitests/src/test/kotlin/ui/T.kt"
|
||||
print("Generating screenshots for languages: %s" % languages)
|
||||
# Record the languages one by one, else it's getting too slow
|
||||
for lang in languages:
|
||||
print("Generating screenshots for language: %s" % lang)
|
||||
# Patch file T.kt, replace `@TestParameter(value = ["de"]) localeStr: String,` with `@TestParameter(value = [<the languages>]) localeStr: String,`
|
||||
with open(tFile, "r") as file:
|
||||
data = file.read()
|
||||
data = data.replace("@TestParameter(value = [\"de\"]) localeStr: String,", "@TestParameter(value = [\"%s\"]) localeStr: String," % lang)
|
||||
with open(tFile, "w") as file:
|
||||
file.write(data)
|
||||
os.system("./gradlew recordPaparazziDebug -PallLanguagesNoEnglish")
|
||||
# Git reset the change on file T.kt
|
||||
os.system("git checkout HEAD -- %s" % tFile)
|
||||
|
||||
|
||||
def detectLanguages():
|
||||
@@ -82,38 +100,44 @@ def detectRecordedLanguages():
|
||||
# List all the subfolders of the screenshots folder which contains 2 letters, sorted alphabetically
|
||||
return sorted([f for f in os.listdir("screenshots") if len(f) == 2])
|
||||
|
||||
|
||||
def generateJavascriptFile():
|
||||
__doc__ = "Generate a javascript file to load the screenshots"
|
||||
print("Generating javascript file...")
|
||||
languages = detectRecordedLanguages()
|
||||
# First item is the list of languages, adding "en" at the beginning
|
||||
data = [["en"] + languages]
|
||||
# If any translated screenshot exists, keep the file
|
||||
# Second item is the path of the containing file
|
||||
data.append(["./tests/uitests/src/test/snapshots/images"] + ["./screenshots/" + l for l in languages])
|
||||
files = sorted(
|
||||
os.listdir("tests/uitests/src/test/snapshots/images/"),
|
||||
key=lambda file: file[file.find("_", 6):],
|
||||
)
|
||||
for file in files:
|
||||
fullFile = "./tests/uitests/src/test/snapshots/images/" + file
|
||||
dataForFile = [fullFile]
|
||||
hasAnyTranslatedFile = False
|
||||
# Continue if file contains "-Night", keep only light screenshots (maybe the night screenshots could be on the second column?)
|
||||
if "-Night" in file:
|
||||
continue
|
||||
dataForFile = [file[:-4]]
|
||||
for l in languages:
|
||||
translatedFile = "./screenshots/" + l + "/" + file[:3] + "T" + file[4:-7] + l + file[-5:]
|
||||
simpleFile = file[:3] + "T" + file[4:-7] + l + file[-5:-4]
|
||||
translatedFile = "./screenshots/" + l + "/" + simpleFile + ".png"
|
||||
if os.path.exists(translatedFile):
|
||||
hasAnyTranslatedFile = True
|
||||
dataForFile.append(translatedFile)
|
||||
dataForFile.append(1)
|
||||
else:
|
||||
dataForFile.append("")
|
||||
if hasAnyTranslatedFile:
|
||||
data.append(dataForFile)
|
||||
dataForFile.append(0)
|
||||
data.append(dataForFile)
|
||||
|
||||
with open("screenshots/html/data.js", "w") as f:
|
||||
f.write("// Generated file, do not edit\n")
|
||||
f.write("export const screenshots = [\n")
|
||||
for line in data:
|
||||
f.write("[\n")
|
||||
f.write("[")
|
||||
for item in line:
|
||||
f.write("\"" + item + "\",\n")
|
||||
# If item is a string, add quotes
|
||||
if isinstance(item, str):
|
||||
f.write("\"" + item + "\",")
|
||||
else:
|
||||
f.write(str(item) + ",")
|
||||
f.write("],\n")
|
||||
f.write("];\n")
|
||||
|
||||
|
||||
40
tools/test/generateWorldScreenshots.py
Executable file
40
tools/test/generateWorldScreenshots.py
Executable file
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# Copyright 2024 New Vector Ltd
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
import os
|
||||
|
||||
def detectAllExistingTranslations():
|
||||
# Read all the folder in "libraries/ui-strings/src/main/res"
|
||||
folders = os.listdir("libraries/ui-strings/src/main/res")
|
||||
# Remove the "values" folder
|
||||
folders.remove("values")
|
||||
# Map to keep only the language code
|
||||
folders = list(map(lambda folder: folder[7:], folders))
|
||||
# Map to keep only the string before the "-"
|
||||
folders = list(map(lambda folder: folder.split("-")[0], folders))
|
||||
# Remove duplicates
|
||||
folders = list(set(folders))
|
||||
return folders
|
||||
|
||||
|
||||
def main():
|
||||
languages = detectAllExistingTranslations()
|
||||
print ("Will record the screenshots for those languages: %s" % languages)
|
||||
# Run the python script "generateAllScreenshots.py" with the detected languages
|
||||
os.system("./tools/test/generateAllScreenshots.py %s" % " ".join(languages))
|
||||
|
||||
main()
|
||||
Reference in New Issue
Block a user