Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / test / gpu / gpu_tests / webgl_conformance.py
1 # Copyright (c) 2012 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 import json
5 import optparse
6 import os
7 import sys
8
9 import webgl_conformance_expectations
10
11 from telemetry import benchmark as benchmark_module
12 from telemetry.core import util
13 from telemetry.page import page_set
14 from telemetry.page import page as page_module
15 from telemetry.page import page_test
16
17
18 conformance_path = os.path.join(
19     util.GetChromiumSrcDir(),
20     'third_party', 'webgl', 'src', 'sdk', 'tests')
21
22 conformance_harness_script = r"""
23   var testHarness = {};
24   testHarness._allTestSucceeded = true;
25   testHarness._messages = '';
26   testHarness._failures = 0;
27   testHarness._finished = false;
28   testHarness._originalLog = window.console.log;
29
30   testHarness.log = function(msg) {
31     testHarness._messages += msg + "\n";
32     testHarness._originalLog.apply(window.console, [msg]);
33   }
34
35   testHarness.reportResults = function(url, success, msg) {
36     testHarness._allTestSucceeded = testHarness._allTestSucceeded && !!success;
37     if(!success) {
38       testHarness._failures++;
39       if(msg) {
40         testHarness.log(msg);
41       }
42     }
43   };
44   testHarness.notifyFinished = function(url) {
45     testHarness._finished = true;
46   };
47   testHarness.navigateToPage = function(src) {
48     var testFrame = document.getElementById("test-frame");
49     testFrame.src = src;
50   };
51
52   window.webglTestHarness = testHarness;
53   window.parent.webglTestHarness = testHarness;
54   window.console.log = testHarness.log;
55   window.onerror = function(message, url, line) {
56     testHarness._failures++;
57     if (message) {
58       testHarness.log(message);
59     }
60     testHarness.notifyFinished(null);
61   };
62 """
63
64 def _DidWebGLTestSucceed(tab):
65   return tab.EvaluateJavaScript('webglTestHarness._allTestSucceeded')
66
67 def _WebGLTestMessages(tab):
68   return tab.EvaluateJavaScript('webglTestHarness._messages')
69
70 class WebglConformanceValidator(page_test.PageTest):
71   def __init__(self):
72     super(WebglConformanceValidator, self).__init__(attempts=1, max_failures=10)
73
74   def ValidateAndMeasurePage(self, page, tab, results):
75     if not _DidWebGLTestSucceed(tab):
76       raise page_test.Failure(_WebGLTestMessages(tab))
77
78   def CustomizeBrowserOptions(self, options):
79     options.AppendExtraBrowserArgs([
80         '--disable-gesture-requirement-for-media-playback',
81         '--disable-domain-blocking-for-3d-apis',
82         '--disable-gpu-process-crash-limit'
83     ])
84
85
86 class WebglConformancePage(page_module.Page):
87   def __init__(self, page_set, test):
88     super(WebglConformancePage, self).__init__(
89       url='file://' + test, page_set=page_set, base_dir=page_set.base_dir,
90       name=('WebglConformance.%s' %
91               test.replace('/', '_').replace('-', '_').
92                  replace('\\', '_').rpartition('.')[0].replace('.', '_')))
93     self.script_to_evaluate_on_commit = conformance_harness_script
94
95   def RunNavigateSteps(self, action_runner):
96     action_runner.NavigateToPage(self)
97     action_runner.WaitForJavaScriptCondition(
98         'webglTestHarness._finished', timeout_in_seconds=120)
99
100
101 class WebglConformance(benchmark_module.Benchmark):
102   """Conformance with Khronos WebGL Conformance Tests"""
103   test = WebglConformanceValidator
104
105   @classmethod
106   def AddTestCommandLineArgs(cls, group):
107     group.add_option('--webgl-conformance-version',
108         help='Version of the WebGL conformance tests to run.',
109         default='1.0.3')
110
111   def CreatePageSet(self, options):
112     tests = self._ParseTests('00_test_list.txt',
113         options.webgl_conformance_version)
114
115     ps = page_set.PageSet(
116       user_agent_type='desktop',
117       serving_dirs=[''],
118       file_path=conformance_path)
119
120     for test in tests:
121       ps.AddPage(WebglConformancePage(ps, test))
122
123     return ps
124
125   def CreateExpectations(self, page_set):
126     return webgl_conformance_expectations.WebGLConformanceExpectations()
127
128   @staticmethod
129   def _ParseTests(path, version=None):
130     test_paths = []
131     current_dir = os.path.dirname(path)
132     full_path = os.path.normpath(os.path.join(conformance_path, path))
133
134     if not os.path.exists(full_path):
135       raise Exception('The WebGL conformance test path specified ' +
136         'does not exist: ' + full_path)
137
138     with open(full_path, 'r') as f:
139       for line in f:
140         line = line.strip()
141
142         if not line:
143           continue
144
145         if line.startswith('//') or line.startswith('#'):
146           continue
147
148         line_tokens = line.split(' ')
149
150         i = 0
151         min_version = None
152         while i < len(line_tokens):
153           token = line_tokens[i]
154           if token == '--min-version':
155             i += 1
156             min_version = line_tokens[i]
157           i += 1
158
159         if version and min_version and version < min_version:
160           continue
161
162         test_name = line_tokens[-1]
163
164         if '.txt' in test_name:
165           include_path = os.path.join(current_dir, test_name)
166           test_paths += WebglConformance._ParseTests(
167             include_path, version)
168         else:
169           test = os.path.join(current_dir, test_name)
170           test_paths.append(test)
171
172     return test_paths