1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)# Copyright 2014 The Chromium Authors. All rights reserved.
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)# found in the LICENSE file.
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import cloud_storage_test_base
6f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)import optparse
7f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)import page_sets
8f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)test_harness_script = r"""
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  var domAutomationController = {};
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  domAutomationController._succeeded = false;
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  domAutomationController._finished = false;
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  domAutomationController.setAutomationId = function(id) {}
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  domAutomationController.send = function(msg) {
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    domAutomationController._finished = true;
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (msg.toLowerCase() == "success")
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      domAutomationController._succeeded = true;
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    else
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      domAutomationController._succeeded = false;
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  window.domAutomationController = domAutomationController;
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)"""
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)def _DidTestSucceed(tab):
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return tab.EvaluateJavaScript('domAutomationController._succeeded')
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
3023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)class _GpuRasterizationValidator(cloud_storage_test_base.ValidatorBase):
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  def CustomizeBrowserOptions(self, options):
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    options.AppendExtraBrowserArgs(['--enable-threaded-compositing',
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                    '--enable-impl-side-painting',
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                    '--force-gpu-rasterization',
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                    '--enable-gpu-benchmarking'])
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  def ValidateAndMeasurePage(self, page, tab, results):
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if not _DidTestSucceed(tab):
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      raise page_test.Failure('Page indicated a failure')
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if not hasattr(page, 'expectations') or not page.expectations:
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      raise page_test.Failure('Expectations not specified')
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if not tab.screenshot_supported:
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      raise page_test.Failure('Browser does not support screenshot capture')
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    screenshot = tab.Screenshot()
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if not screenshot:
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      raise page_test.Failure('Could not capture screenshot')
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    device_pixel_ratio = tab.EvaluateJavaScript('window.devicePixelRatio')
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if hasattr(page, 'test_rect'):
5346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      test_rect = [int(x * device_pixel_ratio) for x in page.test_rect]
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      screenshot = screenshot.Crop(
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          test_rect[0], test_rect[1],
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          test_rect[2], test_rect[3])
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    self._ValidateScreenshotSamples(
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        page.display_name,
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        screenshot,
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        page.expectations,
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        device_pixel_ratio)
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class GpuRasterization(cloud_storage_test_base.TestBase):
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  """Tests that GPU rasterization produces valid content"""
6723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  test = _GpuRasterizationValidator
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  def CreatePageSet(self, options):
701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    page_set = page_sets.GpuRasterizationTestsPageSet()
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    for page in page_set.pages:
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      page.script_to_evaluate_on_commit = test_harness_script
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return page_set
74