summaryrefslogtreecommitdiffstats
path: root/ui/resources
diff options
context:
space:
mode:
authorflackr@chromium.org <flackr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-07-20 23:54:33 +0000
committerflackr@chromium.org <flackr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-07-20 23:54:33 +0000
commita8970b9c012ac43a5a846940799d97d302e72442 (patch)
treed6dfce5af2c9ed19196f3715658fe3295febf4e3 /ui/resources
parent8e991c1448aa5853da90e8294a4539e488c1b549 (diff)
downloadchromium_src-a8970b9c012ac43a5a846940799d97d302e72442.zip
chromium_src-a8970b9c012ac43a5a846940799d97d302e72442.tar.gz
chromium_src-a8970b9c012ac43a5a846940799d97d302e72442.tar.bz2
Add presubmit script to verify that scaled resources are correct dimensions.
BUG=133573 TEST=Add an incorrectly sized resource, run presubmit checks and see error with details about incorrect size. Review URL: https://chromiumcodereview.appspot.com/10699034 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@147742 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/resources')
-rw-r--r--ui/resources/PRESUBMIT.py47
-rw-r--r--ui/resources/resource_check/__init__.py0
-rw-r--r--ui/resources/resource_check/resource_scale_factors.py99
3 files changed, 146 insertions, 0 deletions
diff --git a/ui/resources/PRESUBMIT.py b/ui/resources/PRESUBMIT.py
new file mode 100644
index 0000000..ba38b71
--- /dev/null
+++ b/ui/resources/PRESUBMIT.py
@@ -0,0 +1,47 @@
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Presubmit script for Chromium UI resources.
+
+See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
+for more details about the presubmit API built into gcl/git cl, and see
+http://www.chromium.org/developers/web-development-style-guide for the rules
+we're checking against here.
+"""
+
+
+def CheckChangeOnUpload(input_api, output_api):
+ return _CommonChecks(input_api, output_api)
+
+
+def CheckChangeOnCommit(input_api, output_api):
+ return _CommonChecks(input_api, output_api)
+
+
+def _CommonChecks(input_api, output_api):
+ """Checks common to both upload and commit."""
+ results = []
+ resources = input_api.PresubmitLocalPath()
+
+ # List of paths with their associated scale factor. This is used to verify
+ # that the images modified in one are the correct scale of the other.
+ path_scales = [
+ [(1, 'default_100_percent/'), (2, 'default_200_percent/')],
+ [(1, 'touch_100_percent/'), (2, 'touch_200_percent/')],
+ ]
+
+ import sys
+ old_path = sys.path
+
+ try:
+ sys.path = [resources] + old_path
+ from resource_check import resource_scale_factors
+
+ for paths in path_scales:
+ results.extend(resource_scale_factors.ResourceScaleFactors(
+ input_api, output_api, paths).RunChecks())
+ finally:
+ sys.path = old_path
+
+ return results
diff --git a/ui/resources/resource_check/__init__.py b/ui/resources/resource_check/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/ui/resources/resource_check/__init__.py
diff --git a/ui/resources/resource_check/resource_scale_factors.py b/ui/resources/resource_check/resource_scale_factors.py
new file mode 100644
index 0000000..c3118de
--- /dev/null
+++ b/ui/resources/resource_check/resource_scale_factors.py
@@ -0,0 +1,99 @@
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Presubmit script for Chromium browser resources.
+
+See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
+for more details about the presubmit API built into gcl/git cl, and see
+http://www.chromium.org/developers/web-development-style-guide for the rules
+we're checking against here.
+"""
+
+
+import os
+import struct
+
+
+class ResourceScaleFactors(object):
+ """Verifier of image dimensions for Chromium resources.
+
+ This class verifies the image dimensions of resources in the various
+ resource subdirectories.
+
+ Attributes:
+ paths: An array of tuples giving the folders to check and their
+ relevant scale factors. For example:
+
+ [(1, 'default_100_percent'), (2, 'default_200_percent')]
+ """
+
+ def __init__(self, input_api, output_api, paths):
+ """ Initializes ResourceScaleFactors with paths."""
+ self.input_api = input_api
+ self.output_api = output_api
+ self.paths = paths
+
+ def RunChecks(self):
+ """Verifies the scale factors of resources being added or modified.
+
+ Returns:
+ An array of presubmit errors if any images were detected not
+ having the correct dimensions.
+ """
+ def ImageSize(filename):
+ with open(filename, 'rb', buffering=0) as f:
+ data = f.read(24)
+ assert data[:8] == '\x89PNG\r\n\x1A\n' and data[12:16] == 'IHDR'
+ return struct.unpack('>ii', data[16:24])
+
+ # TODO(flackr): This should allow some flexibility for non-integer scale
+ # factors such as allowing any size between the floor and ceiling of
+ # base * scale.
+ def ExpectedSize(base_width, base_height, scale):
+ return round(base_width * scale), round(base_height * scale)
+
+ repository_path = self.input_api.os_path.relpath(
+ self.input_api.PresubmitLocalPath(),
+ self.input_api.change.RepositoryRoot())
+ results = []
+
+ # Check for affected files in any of the paths specified.
+ affected_files = self.input_api.AffectedFiles(include_deletes=False)
+ files = []
+ for f in affected_files:
+ for path_spec in self.paths:
+ path_root = self.input_api.os_path.join(
+ repository_path, path_spec[1])
+ if (f.LocalPath().endswith('.png') and
+ f.LocalPath().startswith(path_root)):
+ # Only save the relative path from the resource directory.
+ relative_path = self.input_api.os_path.relpath(f.LocalPath(),
+ path_root)
+ if relative_path not in files:
+ files.append(relative_path)
+
+ for f in files:
+ base_image = self.input_api.os_path.join(self.paths[0][1], f)
+ if not os.path.exists(base_image):
+ results.append(self.output_api.PresubmitError(
+ 'Base image %s does not exist' % self.input_api.os_path.join(
+ repository_path, base_image)))
+ continue
+ base_width, base_height = ImageSize(base_image)
+ # Find all scaled versions of the base image and verify their sizes.
+ for i in range(1, len(self.paths)):
+ image_path = self.input_api.os_path.join(self.paths[i][1], f)
+ if not os.path.exists(image_path):
+ continue
+ # Ensure that each image for a particular scale factor is the
+ # correct scale of the base image.
+ exp_width, exp_height = ExpectedSize(base_width, base_height,
+ self.paths[i][0])
+ width, height = ImageSize(image_path)
+ if width != exp_width or height != exp_height:
+ results.append(self.output_api.PresubmitError(
+ 'Image %s is %dx%d, expected to be %dx%d' % (
+ self.input_api.os_path.join(repository_path, image_path),
+ width, height, exp_width, exp_height)))
+ return results