Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / test / gpu / gpu_tests / maps.py
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 """Runs a Google Maps pixel test.
6 Performs several common navigation actions on the map (pan, zoom, rotate) then
7 captures a screenshot and compares selected pixels against expected values"""
8
9 import json
10 import optparse
11 import os
12
13 import cloud_storage_test_base
14 import maps_expectations
15
16 from telemetry import test
17 from telemetry.core import bitmap
18 from telemetry.core import util
19 from telemetry.page import page
20 from telemetry.page import page_set
21 from telemetry.page import page_test
22 # pylint: disable=W0401,W0614
23 from telemetry.page.actions.all_page_actions import *
24
25 class _MapsValidator(cloud_storage_test_base.ValidatorBase):
26   def CustomizeBrowserOptions(self, options):
27     options.AppendExtraBrowserArgs('--enable-gpu-benchmarking')
28
29   def ValidatePage(self, page, tab, results):
30     # TODO: This should not be necessary, but it's not clear if the test is
31     # failing on the bots in it's absence. Remove once we can verify that it's
32     # safe to do so.
33     _MapsValidator.SpinWaitOnRAF(tab, 3)
34
35     if not tab.screenshot_supported:
36       raise page_test.Failure('Browser does not support screenshot capture')
37     screenshot = tab.Screenshot(5)
38     if not screenshot:
39       raise page_test.Failure('Could not capture screenshot')
40
41     dpr = tab.EvaluateJavaScript('window.devicePixelRatio')
42     expected = self._ReadPixelExpectations(page)
43     self._ValidateScreenshotSamples(
44         page.display_name, screenshot, expected, dpr)
45
46   @staticmethod
47   def SpinWaitOnRAF(tab, iterations, timeout = 60):
48     waitScript = r"""
49       window.__spinWaitOnRAFDone = false;
50       var iterationsLeft = %d;
51
52       function spin() {
53         iterationsLeft--;
54         if (iterationsLeft == 0) {
55           window.__spinWaitOnRAFDone = true;
56           return;
57         }
58         window.requestAnimationFrame(spin);
59       }
60       window.requestAnimationFrame(spin);
61     """ % iterations
62
63     def IsWaitComplete():
64       return tab.EvaluateJavaScript('window.__spinWaitOnRAFDone')
65
66     tab.ExecuteJavaScript(waitScript)
67     util.WaitFor(IsWaitComplete, timeout)
68
69   def _ReadPixelExpectations(self, page):
70     expectations_path = os.path.join(page._base_dir, page.pixel_expectations)
71     with open(expectations_path, 'r') as f:
72       json_contents = json.load(f)
73     return json_contents
74
75
76 class MapsPage(page.Page):
77   def __init__(self, page_set, base_dir):
78     super(MapsPage, self).__init__(
79       url='http://localhost:10020/tracker.html',
80       page_set=page_set,
81       base_dir=base_dir,
82       name='Maps.maps_002')
83     self.pixel_expectations = 'data/maps_002_expectations.json'
84
85   def RunNavigateSteps(self, action_runner):
86     action_runner.RunAction(NavigateAction())
87     action_runner.RunAction(WaitAction({'javascript': 'window.testDone'}))
88
89
90 class Maps(cloud_storage_test_base.TestBase):
91   """Google Maps pixel tests."""
92   test = _MapsValidator
93
94   def CreateExpectations(self, page_set):
95     return maps_expectations.MapsExpectations()
96
97   def CreatePageSet(self, options):
98     page_set_path = os.path.join(
99         util.GetChromiumSrcDir(), 'content', 'test', 'gpu', 'page_sets')
100     ps = page_set.PageSet(archive_data_file='data/maps.json',
101                           make_javascript_deterministic=False,
102                           file_path=page_set_path)
103     ps.AddPage(MapsPage(ps, ps.base_dir))
104     return ps