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.
7 OS_MODIFIERS
= ['win', 'xp', 'vista', 'win7',
8 'mac', 'leopard', 'snowleopard', 'lion', 'mountainlion',
9 'mavericks', 'yosemite', 'linux', 'chromeos', 'android']
10 GPU_MODIFIERS
= ['amd', 'arm', 'broadcom', 'hisilicon', 'intel', 'imagination',
11 'nvidia', 'qualcomm', 'vivante']
12 CONFIG_MODIFIERS
= ['debug', 'release']
14 class _Expectation(object):
15 def __init__(self
, expectation
, parent_expectations
,
16 pattern
, conditions
=None, bug
=None):
17 self
.expectation
= expectation
.lower()
18 self
.name_pattern
= pattern
19 self
.url_pattern
= pattern
22 self
.os_conditions
= []
23 self
.gpu_conditions
= []
24 self
.config_conditions
= []
25 self
.device_id_conditions
= []
26 # These are made legal by TestExpectations.IsValidUserDefinedCondition.
27 self
.user_defined_conditions
= []
29 # Make sure that non-absolute paths are searchable
30 if not '://' in self
.url_pattern
:
31 self
.url_pattern
= '*/' + self
.url_pattern
35 if isinstance(c
, tuple):
37 if c0
in GPU_MODIFIERS
:
38 self
.device_id_conditions
.append((c0
, c
[1]))
39 elif parent_expectations
.IsValidUserDefinedCondition(c0
):
40 self
.user_defined_conditions
.append((c0
, c
[1]))
42 raise ValueError('Unknown expectation condition: "%s"' % c0
)
45 if condition
in OS_MODIFIERS
:
46 self
.os_conditions
.append(condition
)
47 elif condition
in GPU_MODIFIERS
:
48 self
.gpu_conditions
.append(condition
)
49 elif condition
in CONFIG_MODIFIERS
:
50 self
.config_conditions
.append(condition
)
51 elif parent_expectations
.IsValidUserDefinedCondition(condition
):
52 self
.user_defined_conditions
.append(condition
)
54 raise ValueError('Unknown expectation condition: "%s"' % condition
)
56 class TestExpectations(object):
57 """A class which defines the expectations for a page set test execution"""
60 self
.expectations
= []
61 self
.SetExpectations()
63 def SetExpectations(self
):
64 """Called on creation. Override to set up custom expectations."""
67 def Fail(self
, url_pattern
, conditions
=None, bug
=None):
68 self
._Expect
('fail', url_pattern
, conditions
, bug
)
70 def Skip(self
, url_pattern
, conditions
=None, bug
=None):
71 self
._Expect
('skip', url_pattern
, conditions
, bug
)
73 def _Expect(self
, expectation
, url_pattern
, conditions
=None, bug
=None):
74 self
.expectations
.append(self
.CreateExpectation(expectation
, url_pattern
,
77 def CreateExpectation(self
, expectation
, url_pattern
, conditions
=None,
79 return _Expectation(expectation
, self
, url_pattern
, conditions
, bug
)
81 def GetExpectationForPage(self
, browser
, page
):
82 for e
in self
.expectations
:
83 if self
.ExpectationAppliesToPage(e
, browser
, page
):
87 def ExpectationAppliesToPage(self
, expectation
, browser
, page
):
88 matches_url
= fnmatch
.fnmatch(page
.url
, expectation
.url_pattern
)
89 matches_name
= page
.name
and fnmatch
.fnmatch(page
.name
,
90 expectation
.name_pattern
)
91 if matches_url
or matches_name
:
92 if self
.ModifiersApply(browser
, expectation
):
96 def _GetGpuVendorString(self
, gpu_info
):
98 primary_gpu
= gpu_info
.devices
[0]
100 vendor_string
= primary_gpu
.vendor_string
.lower()
101 vendor_id
= primary_gpu
.vendor_id
103 return vendor_string
.split(' ')[0]
104 elif vendor_id
== 0x10DE:
106 elif vendor_id
== 0x1002:
108 elif vendor_id
== 0x8086:
113 def _GetGpuDeviceId(self
, gpu_info
):
115 primary_gpu
= gpu_info
.devices
[0]
117 return primary_gpu
.device_id
or primary_gpu
.device_string
121 def ModifiersApply(self
, browser
, expectation
):
122 """Determines if the conditions for an expectation apply to this system.
123 Can be overridden by subclasses to support new user-defined conditions.
126 browser: the currently running browser.
127 expectation: a object which is guaranteed to have the property
128 "user_defined_conditions" defined, which is an array of
129 strings or tuples specified in the test expectations for
130 which IsValidUserDefinedCondition returned true.
133 platform
= browser
.platform
134 os_matches
= (not expectation
.os_conditions
or
135 platform
.GetOSName() in expectation
.os_conditions
or
136 platform
.GetOSVersionName() in expectation
.os_conditions
)
140 # TODO(kbr): factor out all of the GPU-related conditions into
141 # GpuTestExpectations, including unit tests.
142 if browser
.supports_system_info
:
143 gpu_info
= browser
.GetSystemInfo().gpu
144 gpu_vendor
= self
._GetGpuVendorString
(gpu_info
)
145 gpu_device_id
= self
._GetGpuDeviceId
(gpu_info
)
146 gpu_matches
= ((not expectation
.gpu_conditions
and
147 not expectation
.device_id_conditions
) or
148 gpu_vendor
in expectation
.gpu_conditions
or
149 (gpu_vendor
, gpu_device_id
) in expectation
.device_id_conditions
)
151 return os_matches
and gpu_matches
153 def IsValidUserDefinedCondition(self
, _condition
):
154 """Returns true if the given condition should be treated as a
155 valid user-defined condition.
158 _condition: a string. Returning true causes a new entry to be
159 added to the user_defined_expectations for the expectation
160 currently being parsed. The entry added may be either the
161 simple string of the condition, or a tuple if the condition
162 represents the name of a new GPU type and there was an
163 associated device ID in the expectation.