xref: /aosp_15_r20/external/cronet/build/android/pylib/utils/gold_utils_test.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1#!/usr/bin/env vpython3
2# Copyright 2020 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Tests for gold_utils."""
6
7#pylint: disable=protected-access
8
9import contextlib
10import os
11import tempfile
12import unittest
13
14from pylib.constants import host_paths
15from pylib.utils import gold_utils
16
17with host_paths.SysPath(host_paths.BUILD_PATH):
18  from skia_gold_common import unittest_utils
19
20import mock  # pylint: disable=import-error
21from pyfakefs import fake_filesystem_unittest  # pylint: disable=import-error
22
23createSkiaGoldArgs = unittest_utils.createSkiaGoldArgs
24
25
26def assertArgWith(test, arg_list, arg, value):
27  i = arg_list.index(arg)
28  test.assertEqual(arg_list[i + 1], value)
29
30
31class AndroidSkiaGoldSessionDiffTest(fake_filesystem_unittest.TestCase):
32  def setUp(self):
33    self.setUpPyfakefs()
34    self._working_dir = tempfile.mkdtemp()
35    self._json_keys = tempfile.NamedTemporaryFile(delete=False).name
36
37  @mock.patch.object(gold_utils.AndroidSkiaGoldSession, '_RunCmdForRcAndOutput')
38  def test_commandCommonArgs(self, cmd_mock):
39    cmd_mock.return_value = (None, None)
40    args = createSkiaGoldArgs(git_revision='a', local_pixel_tests=False)
41    sgp = gold_utils.AndroidSkiaGoldProperties(args)
42    session = gold_utils.AndroidSkiaGoldSession(self._working_dir,
43                                                sgp,
44                                                self._json_keys,
45                                                'corpus',
46                                                instance='instance')
47    session.Diff('name', 'png_file', None)
48    call_args = cmd_mock.call_args[0][0]
49    self.assertIn('diff', call_args)
50    assertArgWith(self, call_args, '--corpus', 'corpus')
51    # TODO(skbug.com/10610): Remove the -public once we go back to using the
52    # non-public instance, or add a second test for testing that the correct
53    # instance is chosen if we decide to support both depending on what the
54    # user is authenticated for.
55    assertArgWith(self, call_args, '--instance', 'instance-public')
56    assertArgWith(self, call_args, '--input', 'png_file')
57    assertArgWith(self, call_args, '--test', 'name')
58    # TODO(skbug.com/10611): Re-add this assert and remove the check for the
59    # absence of the directory once we switch back to using the proper working
60    # directory.
61    # assertArgWith(self, call_args, '--work-dir', self._working_dir)
62    self.assertNotIn(self._working_dir, call_args)
63    i = call_args.index('--out-dir')
64    # The output directory should be a subdirectory of the working directory.
65    self.assertIn(self._working_dir, call_args[i + 1])
66
67
68class AndroidSkiaGoldSessionDiffLinksTest(fake_filesystem_unittest.TestCase):
69  class FakeArchivedFile:
70    def __init__(self, path):
71      self.name = path
72
73    def Link(self):
74      return 'file://' + self.name
75
76  class FakeOutputManager:
77    def __init__(self):
78      self.output_dir = tempfile.mkdtemp()
79
80    @contextlib.contextmanager
81    def ArchivedTempfile(self, image_name, _, __):
82      filepath = os.path.join(self.output_dir, image_name)
83      yield AndroidSkiaGoldSessionDiffLinksTest.FakeArchivedFile(filepath)
84
85  def setUp(self):
86    self.setUpPyfakefs()
87    self._working_dir = tempfile.mkdtemp()
88    self._json_keys = tempfile.NamedTemporaryFile(delete=False).name
89
90  def test_outputManagerUsed(self):
91    args = createSkiaGoldArgs(git_revision='a', local_pixel_tests=True)
92    sgp = gold_utils.AndroidSkiaGoldProperties(args)
93    session = gold_utils.AndroidSkiaGoldSession(self._working_dir, sgp,
94                                                self._json_keys, None, None)
95    with open(os.path.join(self._working_dir, 'input-inputhash.png'), 'w') as f:
96      f.write('input')
97    with open(os.path.join(self._working_dir, 'closest-closesthash.png'),
98              'w') as f:
99      f.write('closest')
100    with open(os.path.join(self._working_dir, 'diff.png'), 'w') as f:
101      f.write('diff')
102
103    output_manager = AndroidSkiaGoldSessionDiffLinksTest.FakeOutputManager()
104    session._StoreDiffLinks('foo', output_manager, self._working_dir)
105
106    copied_input = os.path.join(output_manager.output_dir, 'given_foo.png')
107    copied_closest = os.path.join(output_manager.output_dir, 'closest_foo.png')
108    copied_diff = os.path.join(output_manager.output_dir, 'diff_foo.png')
109    with open(copied_input) as f:
110      self.assertEqual(f.read(), 'input')
111    with open(copied_closest) as f:
112      self.assertEqual(f.read(), 'closest')
113    with open(copied_diff) as f:
114      self.assertEqual(f.read(), 'diff')
115
116    self.assertEqual(session.GetGivenImageLink('foo'), 'file://' + copied_input)
117    self.assertEqual(session.GetClosestImageLink('foo'),
118                     'file://' + copied_closest)
119    self.assertEqual(session.GetDiffImageLink('foo'), 'file://' + copied_diff)
120
121
122if __name__ == '__main__':
123  unittest.main(verbosity=2)
124