- add sources.
[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 test as test_module
12 from telemetry.core import util
13 from telemetry.page import page_set
14 from telemetry.page import page_test
15
16 conformance_path = os.path.join(
17     util.GetChromiumSrcDir(), 'third_party', 'webgl_conformance')
18
19 conformance_harness_script = r"""
20   var testHarness = {};
21   testHarness._allTestSucceeded = true;
22   testHarness._messages = '';
23   testHarness._failures = 0;
24   testHarness._finished = false;
25   testHarness._originalLog = window.console.log;
26
27   testHarness.log = function(msg) {
28     testHarness._messages += msg + "\n";
29     testHarness._originalLog.apply(window.console, [msg]);
30   }
31
32   testHarness.reportResults = function(success, msg) {
33     testHarness._allTestSucceeded = testHarness._allTestSucceeded && !!success;
34     if(!success) {
35       testHarness._failures++;
36       if(msg) {
37         testHarness.log(msg);
38       }
39     }
40   };
41   testHarness.notifyFinished = function() {
42     testHarness._finished = true;
43   };
44   testHarness.navigateToPage = function(src) {
45     var testFrame = document.getElementById("test-frame");
46     testFrame.src = src;
47   };
48
49   window.webglTestHarness = testHarness;
50   window.parent.webglTestHarness = testHarness;
51   window.console.log = testHarness.log;
52 """
53
54 def _DidWebGLTestSucceed(tab):
55   return tab.EvaluateJavaScript('webglTestHarness._allTestSucceeded')
56
57 def _WebGLTestMessages(tab):
58   return tab.EvaluateJavaScript('webglTestHarness._messages')
59
60 class WebglConformanceValidator(page_test.PageTest):
61   def __init__(self):
62     super(WebglConformanceValidator, self).__init__('ValidatePage')
63
64   def ValidatePage(self, page, tab, results):
65     if not _DidWebGLTestSucceed(tab):
66       raise page_test.Failure(_WebGLTestMessages(tab))
67
68   def CustomizeBrowserOptions(self, options):
69     options.AppendExtraBrowserArgs(
70         '--disable-gesture-requirement-for-media-playback')
71
72
73 class WebglConformance(test_module.Test):
74   """Conformance with Khronos WebGL Conformance Tests"""
75   test = WebglConformanceValidator
76
77   @staticmethod
78   def AddTestCommandLineOptions(parser):
79     group = optparse.OptionGroup(parser, 'WebGL conformance options')
80     group.add_option('--webgl-conformance-version',
81         help='Version of the WebGL conformance tests to run.',
82         default='1.0.1')
83     parser.add_option_group(group)
84
85   def CreatePageSet(self, options):
86     tests = self._ParseTests('00_test_list.txt',
87         options.webgl_conformance_version)
88
89     page_set_dict = {
90       'description': 'Executes WebGL conformance tests',
91       'user_agent_type': 'desktop',
92       'serving_dirs': [ '' ],
93       'pages': []
94     }
95
96     pages = page_set_dict['pages']
97
98     for test in tests:
99       pages.append({
100         'name': 'WebglConformance.%s' %
101             test.replace('/', '_').replace('-', '_').
102             replace('\\', '_').rpartition('.')[0].replace('.', '_'),
103         'url': 'file://' + test,
104         'script_to_evaluate_on_commit': conformance_harness_script,
105         'navigate_steps': [
106           {'action': 'navigate'},
107           {
108             'action': 'wait',
109             'javascript': 'webglTestHarness._finished',
110             'timeout': 120
111           }
112         ]
113       })
114
115     return page_set.PageSet.FromDict(page_set_dict, conformance_path)
116
117   def CreateExpectations(self, page_set):
118     return webgl_conformance_expectations.WebGLConformanceExpectations()
119
120   @staticmethod
121   def _ParseTests(path, version=None):
122     test_paths = []
123     current_dir = os.path.dirname(path)
124     full_path = os.path.normpath(os.path.join(conformance_path, path))
125
126     if not os.path.exists(full_path):
127       raise Exception('The WebGL conformance test path specified ' +
128         'does not exist: ' + full_path)
129
130     with open(full_path, 'r') as f:
131       for line in f:
132         line = line.strip()
133
134         if not line:
135           continue
136
137         if line.startswith('//') or line.startswith('#'):
138           continue
139
140         line_tokens = line.split(' ')
141
142         i = 0
143         min_version = None
144         while i < len(line_tokens):
145           token = line_tokens[i]
146           if token == '--min-version':
147             i += 1
148             min_version = line_tokens[i]
149           i += 1
150
151         if version and min_version and version < min_version:
152           continue
153
154         test_name = line_tokens[-1]
155
156         if '.txt' in test_name:
157           include_path = os.path.join(current_dir, test_name)
158           test_paths += WebglConformance._ParseTests(
159             include_path, version)
160         else:
161           test = os.path.join(current_dir, test_name)
162           test_paths.append(test)
163
164     return test_paths