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

Unified Diff: content/test/gpu/gpu_tests/pixel.py

Issue 23478021: Made Pixel tests functional. Captures tab content and compares to ref image (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Brought functionality closer inline with original browsertests Created 7 years, 2 months 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 side-by-side diff with in-line comments
Download patch
Index: content/test/gpu/gpu_tests/pixel.py
diff --git a/content/test/gpu/gpu_tests/pixel.py b/content/test/gpu/gpu_tests/pixel.py
index 8545dd20336c9cade23dd0d209d069adfb26b169..f43ba0da2ba438b4f2c17d76bc37a761582357ce 100644
--- a/content/test/gpu/gpu_tests/pixel.py
+++ b/content/test/gpu/gpu_tests/pixel.py
@@ -1,25 +1,170 @@
# 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.
+from datetime import datetime
+import glob
+import optparse
+import os
+import re
+
from telemetry import test
+from telemetry.core.backends import png_bitmap
from telemetry.page import page_test
+from telemetry.page import test_expectations
+
+test_data_dir = os.path.abspath(os.path.join(
+ os.path.dirname(__file__), '..', '..', 'data', 'gpu'))
Ken Russell (switch to Gerrit) 2013/10/15 23:31:16 It's necessary to be able to override this directo
+
+generated_data_dir = os.path.join(test_data_dir, 'generated')
+ref_image_dir = os.path.join(test_data_dir, 'gpu_reference')
+
+test_harness_script = r"""
+ var domAutomationController = {};
+
+ domAutomationController._succeeded = false;
+ domAutomationController._finished = false;
+ domAutomationController.setAutomationId = function(id) {}
+
+ domAutomationController.send = function(msg) {
+ domAutomationController._finished = true;
+
+ if(msg.toLowerCase() == "success") {
+ domAutomationController._succeeded = true;
+ } else {
+ domAutomationController._succeeded = false;
+ }
+ }
+
+ window.domAutomationController = domAutomationController;
+ console.log("Harness injected.");
+"""
class PixelTestFailure(Exception):
pass
+def _DidTestSucceed(tab):
+ return tab.EvaluateJavaScript('domAutomationController._succeeded')
class PixelValidator(page_test.PageTest):
def __init__(self):
super(PixelValidator, self).__init__('ValidatePage')
+ def CustomizeBrowserOptions(self, options):
+ options.AppendExtraBrowserArgs('--enable-gpu-benchmarking')
+
def ValidatePage(self, page, tab, results):
- # TODO(bajones): Grab screenshot, compare to reference.
- # page.reference_image
- pass
+ if not _DidTestSucceed(tab):
+ raise page_test.Failure('Page indicated a failure')
+
+ if not tab.screenshot_supported:
+ raise page_test.Failure('Browser does not support screenshot capture')
+
+ screenshot = tab.Screenshot(5)
+
+ if not screenshot:
+ raise page_test.Failure('Could not capture screenshot')
+
+ if hasattr(page, 'test_rect'):
+ screenshot = screenshot.Crop(
+ page.test_rect[0], page.test_rect[1],
+ page.test_rect[2], page.test_rect[3])
+
+ image_name = PixelValidator.UrlToImageName(page.display_name)
+
+ ref_png = PixelValidator.GetReferenceImage(image_name, page.revision,
+ screenshot)
+
+ # Test new snapshot against existing reference image
+ if not ref_png.IsEqual(screenshot, tolerance=2):
+ PixelValidator.WriteErrorImages(image_name, self.options.revision,
+ screenshot, ref_png)
+ raise page_test.Failure('Reference image did not match captured screen')
+
+ @staticmethod
+ def UrlToImageName(url):
+ image_name = re.sub(r'^(http|https|file)://(/*)', '', url)
+ image_name = re.sub(r'\.\./', '', image_name)
+ image_name = re.sub(r'(\.|/|-)', '_', image_name)
+ return image_name
+
+ @staticmethod
+ def DeleteOldReferenceImages(ref_image_path, cur_revision):
+ if not cur_revision:
+ return
+ old_revisions = glob.glob(ref_image_path + "_*.png")
+ for rev_path in old_revisions:
+ m = re.match(r'^.*_(\d+)\.png$', rev_path)
+ if m and int(m.group(1)) < cur_revision:
+ print 'Found deprecated reference image. Deleting rev ' + m.group(1)
+ os.remove(rev_path)
+
+ @staticmethod
+ def GetReferenceImage(image_name, cur_revision, screenshot):
+ if not cur_revision:
+ cur_revision = 0
+
+ image_path = os.path.join(ref_image_dir, image_name)
+
+ PixelValidator.DeleteOldReferenceImages(image_path, cur_revision)
+
+ image_path = image_path + '_' + str(cur_revision) + '.png'
+
+ try:
+ ref_png = png_bitmap.PngBitmap.FromFile(image_path)
+ except IOError:
+ ref_png = None
+
+ if ref_png:
+ return ref_png
+
+ print 'Reference image not found. Writing tab contents as reference.'
+
+ PixelValidator.WriteImage(image_path, screenshot)
+ return screenshot
+
+ @staticmethod
+ def WriteErrorImages(image_name, chrome_revision, screenshot, ref_png):
+ if not chrome_revision:
+ chrome_revision = "unknownrev"
+
+ capture_time = datetime.now()
Ken Russell (switch to Gerrit) 2013/10/15 23:31:16 Unused variable?
+ full_image_name = image_name + '_' + str(chrome_revision)
+ full_image_name = full_image_name + '.png'
+
+ PixelValidator.WriteImage(
+ os.path.join(generated_data_dir, 'FAIL_' + full_image_name), screenshot)
+
+ diff_png = screenshot.Diff(ref_png)
+ PixelValidator.WriteImage(
+ os.path.join(generated_data_dir, 'DIFF_' + full_image_name), diff_png)
+
+ @staticmethod
+ def WriteImage(image_path, png_image):
+ output_dir = os.path.dirname(image_path)
+ if not os.path.exists(output_dir):
+ os.makedirs(output_dir)
+
+ png_image.WriteFile(image_path)
class Pixel(test.Test):
enabled = False
test = PixelValidator
page_set = 'page_sets/pixel_tests.json'
+
+ @staticmethod
+ def AddTestCommandLineOptions(parser):
+ group = optparse.OptionGroup(parser, 'Pixel test options')
+ group.add_option('--revision',
+ help='Chrome revision being tested.')
+ group.add_option('--gtest_output',
+ help='Ignored argument for compatibility with runtest.py harness')
+ parser.add_option_group(group)
+
+ def CreateExpectations(self, page_set):
+ print page_set.pages
+ for page in page_set.pages:
+ page.script_to_evaluate_on_commit = test_harness_script
+
+ return test_expectations.TestExpectations()

Powered by Google App Engine
This is Rietveld 408576698