1#!/usr/bin/env python3
2
3# This script helps to rename and copy the golden images required to run the screenshot tests to the golden directory.
4# To generate new golden images for a test, run the test on the emulator with the required device type, and download the screenshots from the device using adb
5# The screenshots directory contains test.textproto files describing the screenshots and the folder they should be placed in to run the tests successfully
6#
7# Example of a textproto file:
8# androidx.test.screenshot.proto.ScreenshotResultProto$ScreenshotResult@37d7ba5f
9# current_screenshot_file_name: "androidx.emoji2.emojipicker.EmojiViewTest_testClear_sdk_g64_arm64_actual.png"
10# location_of_golden_in_repo: "emoji2/emoji2-emojipicker/draw_and_clear_sdk_g64_arm64.png"
11# repo_root_path: "platform/frameworks/support-golden"
12# result: MISSING_GOLDEN
13# result_value: 3
14
15import argparse
16import os
17import shutil
18
19def main():
20  parser = argparse.ArgumentParser()
21  parser.add_argument("--screenshots-dir-path", required=True, dest="input_dir")
22  args = parser.parse_args()
23  rename_and_copy_files_to_new_location(args.input_dir)
24
25def rename_and_copy_files_to_new_location(input_path):
26  #Relative path to golden directory
27  GOLDENDIR_REL_PATH = '../../../golden/'
28  SCRIPT_PATH = os.path.realpath(os.path.dirname(__file__))
29  GOLDENDIR_FULL_PATH = os.path.join(SCRIPT_PATH, GOLDENDIR_REL_PATH)
30  output_path = GOLDENDIR_FULL_PATH
31
32  # Fetching all screenshots
33  files = os.listdir(input_path)
34  copied_files = 0
35  for file in files:
36    if not file.endswith('.textproto'):
37       continue
38    current_file_path = os.path.join(input_path, file)
39    with open(current_file_path) as current_file:
40        input = ''
41        output = ''
42        for line in current_file.readlines():
43            if line.startswith('current_screenshot_file_name'):
44                input = input_path+line.split(':')[1].strip().replace('"', '')
45            elif line.startswith('location_of_golden_in_repo'):
46                output = output_path+line.split(':')[1].strip().replace('"', '')
47        if input == '' or output == '':
48           print('There was an error processing file -', file)
49        else:
50          shutil.copy(input, output)
51          copied_files += 1
52
53  print(copied_files, 'golden images copied successfully')
54
55if __name__ == "__main__":
56  main()