Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(601)

Side by Side Diff: trunk/src/chrome/test/functional/ispy/common/chrome_utils.py

Issue 104793008: Revert 240226 "[I-Spy] Add support for rebaselining expectations..." (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import json
6 import logging
7 import os
8 from distutils.version import LooseVersion
9 from PIL import Image
10
11 import cloud_bucket
12 import ispy_utils
13
14
15 class ChromeUtils(object):
16 """A utility for using ISpy with Chrome."""
17
18 def __init__(self, cloud_bucket):
19 """Initializes the utility class.
20
21 Args:
22 cloud_bucket: a BaseCloudBucket in which to the version file,
23 expectations and results are to be stored.
24 """
25 self._cloud_bucket = cloud_bucket
26 self._ispy = ispy_utils.ISpyUtils(self._cloud_bucket)
27 self._rebaselineable_cache = {}
28
29 def UpdateExpectationVersion(self, chrome_version, version_file):
30 """Updates the most recent expectation version to the Chrome version.
31
32 Should be called after generating a new set of expectations.
33
34 Args:
35 chrome_version: the chrome version as a string of the form "31.0.123.4".
36 version_file: path to the version file in the cloud bucket. The version
37 file contains a json list of ordered Chrome versions for which
38 expectations exist.
39 """
40 insert_pos = 0
41 expectation_versions = []
42 try:
43 expectation_versions = self._GetExpectationVersionList(version_file)
44 if expectation_versions:
45 try:
46 version = self._GetExpectationVersion(
47 chrome_version, expectation_versions)
48 if version == chrome_version:
49 return
50 insert_pos = expectation_versions.index(version)
51 except:
52 insert_pos = len(expectation_versions)
53 except cloud_bucket.FileNotFoundError:
54 pass
55 expectation_versions.insert(insert_pos, chrome_version)
56 logging.info('Updating expectation version...')
57 self._cloud_bucket.UploadFile(
58 version_file, json.dumps(expectation_versions),
59 'application/json')
60
61 def _GetExpectationVersion(self, chrome_version, expectation_versions):
62 """Returns the expectation version for the given Chrome version.
63
64 Args:
65 chrome_version: the chrome version as a string of the form "31.0.123.4".
66 expectation_versions: Ordered list of Chrome versions for which
67 expectations exist, as stored in the version file.
68
69 Returns:
70 Expectation version string.
71 """
72 # Find the closest version that is not greater than the chrome version.
73 for version in expectation_versions:
74 if LooseVersion(version) <= LooseVersion(chrome_version):
75 return version
76 raise Exception('No expectation exists for Chrome %s' % chrome_version)
77
78 def _GetExpectationVersionList(self, version_file):
79 """Gets the list of expectation versions from google storage.
80
81 Args:
82 version_file: path to the version file in the cloud bucket. The version
83 file contains a json list of ordered Chrome versions for which
84 expectations exist.
85
86 Returns:
87 Ordered list of Chrome versions.
88 """
89 return json.loads(self._cloud_bucket.DownloadFile(version_file))
90
91 def _GetExpectationNameWithVersion(self, device_type, expectation,
92 chrome_version, version_file):
93 """Get the expectation to be used with the current Chrome version.
94
95 Args:
96 device_type: string identifier for the device type.
97 expectation: name for the expectation to generate.
98 chrome_version: the chrome version as a string of the form "31.0.123.4".
99
100 Returns:
101 Version as an integer.
102 """
103 version = self._GetExpectationVersion(
104 chrome_version, self._GetExpectationVersionList(version_file))
105 return self._CreateExpectationName(device_type, expectation, version)
106
107 def _CreateExpectationName(self, device_type, expectation, version):
108 """Create the full expectation name from the expectation and version.
109
110 Args:
111 device_type: string identifier for the device type, example: mako
112 expectation: base name for the expectation, example: google.com
113 version: expectation version, example: 31.0.23.1
114
115 Returns:
116 Full expectation name as a string, example: mako:google.com(31.0.23.1)
117 """
118 return '%s:%s(%s)' % (device_type, expectation, version)
119
120 def GenerateExpectation(self, device_type, expectation, chrome_version,
121 version_file, screenshots):
122 """Create an expectation for I-Spy.
123
124 Args:
125 device_type: string identifier for the device type.
126 expectation: name for the expectation to generate.
127 chrome_version: the chrome version as a string of the form "31.0.123.4".
128 screenshots: a list of similar PIL.Images.
129 """
130 # https://code.google.com/p/chromedriver/issues/detail?id=463
131 expectation_with_version = self._CreateExpectationName(
132 device_type, expectation, chrome_version)
133 if self._ispy.ExpectationExists(expectation_with_version):
134 logging.warning(
135 'I-Spy expectation \'%s\' already exists, overwriting.',
136 expectation_with_version)
137 logging.info('Generating I-Spy expectation...')
138 self._ispy.GenerateExpectation(expectation_with_version, screenshots)
139
140 def PerformComparison(self, test_run, device_type, expectation,
141 chrome_version, version_file, screenshot):
142 """Compare a screenshot with the given expectation in I-Spy.
143
144 Args:
145 test_run: name for the test run.
146 device_type: string identifier for the device type.
147 expectation: name for the expectation to compare against.
148 chrome_version: the chrome version as a string of the form "31.0.123.4".
149 screenshot: a PIL.Image to compare.
150 """
151 # https://code.google.com/p/chromedriver/issues/detail?id=463
152 logging.info('Performing I-Spy comparison...')
153 self._ispy.PerformComparison(
154 test_run,
155 self._GetExpectationNameWithVersion(
156 device_type, expectation, chrome_version, version_file),
157 screenshot)
158
159 def CanRebaselineToTestRun(self, test_run):
160 """Returns whether the test run has associated expectations.
161
162 Returns:
163 True if RebaselineToTestRun() can be called for this test run.
164 """
165 if test_run in self._rebaselineable_cache:
166 return True
167 return self._cloud_bucket.FileExists(
168 ispy_utils.GetTestRunPath(test_run, 'rebaseline.txt'))
169
170 def RebaselineToTestRun(self, test_run):
171 """Update the version file to use expectations associated with |test_run|.
172
173 Args:
174 test_run: The name of the test run to rebaseline.
175 """
176 rebaseline_path = ispy_utils.GetTestRunPath(test_run, 'rebaseline.txt')
177 rebaseline_attrib = json.loads(
178 self._cloud_bucket.DownloadFile(rebaseline_path))
179 self.UpdateExpectationVersion(
180 rebaseline_attrib['version'], rebaseline_attrib['version_file'])
181 self._cloud_bucket.RemoveFile(rebaseline_path)
182
183 def _SetTestRunRebaselineable(self, test_run, chrome_version, version_file):
184 """Writes a JSON file containing the data needed to rebaseline.
185
186 Args:
187 test_run: The name of the test run to add the rebaseline file to.
188 chrome_version: the chrome version that can be rebaselined to (must have
189 associated Expectations).
190 version_file: the path of the version file associated with the test run.
191 """
192 self._rebaselineable_cache[test_run] = True
193 self._cloud_bucket.UploadFile(
194 ispy_utils.GetTestRunPath(test_run, 'rebaseline.txt'),
195 json.dumps({
196 'version': chrome_version,
197 'version_file': version_file}),
198 'application/json')
199
200 def PerformComparisonAndPrepareExpectation(self, test_run, device_type,
201 expectation, chrome_version,
202 version_file, screenshots):
203 """Perform comparison and generate an expectation that can used later.
204
205 The test run web UI will have a button to set the Expectations generated for
206 this version as the expectation for comparison with later versions.
207
208 Args:
209 test_run: The name of the test run to add the rebaseline file to.
210 device_type: string identifier for the device type.
211 chrome_version: the chrome version that can be rebaselined to (must have
212 associated Expectations).
213 version_file: the path of the version file associated with the test run.
214 screenshot: a list of similar PIL.Images.
215 """
216 if not self.CanRebaselineToTestRun(test_run):
217 self._SetTestRunRebaselineable(test_run, chrome_version, version_file)
218 expectation_with_version = self._CreateExpectationName(
219 device_type, expectation, chrome_version)
220 self._ispy.GenerateExpectation(expectation_with_version, screenshots)
221 self._ispy.PerformComparison(
222 test_run,
223 self._GetExpectationNameWithVersion(
224 device_type, expectation, chrome_version, version_file),
225 screenshots[-1])
226
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698