1# Copyright 2017 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
5from telemetry.core import os_version as os_version_module
6
7
8# TODO(rnephew): Since TestConditions are being used for more than
9# just story expectations now, this should be decoupled and refactored
10# to be clearer.
11class _TestCondition(object):
12  def ShouldDisable(self, platform, finder_options):
13    raise NotImplementedError
14
15  def __str__(self):
16    raise NotImplementedError
17
18  def GetSupportedPlatformNames(self):
19    """Returns a set of supported platforms' names."""
20    raise NotImplementedError
21
22
23class _TestConditionByPlatformList(_TestCondition):
24  def __init__(self, platforms, name):
25    self._platforms = platforms
26    self._name = name
27
28  def ShouldDisable(self, platform, finder_options):
29    del finder_options  # Unused.
30    return platform.GetOSName() in self._platforms
31
32  def __str__(self):
33    return self._name
34
35  def GetSupportedPlatformNames(self):
36    return set(self._platforms)
37
38
39class _AllTestCondition(_TestCondition):
40  def ShouldDisable(self, platform, finder_options):
41    del platform, finder_options  # Unused.
42    return True
43
44  def __str__(self):
45    return 'All'
46
47  def GetSupportedPlatformNames(self):
48    return set(['all'])
49
50
51class _TestConditionAndroidSvelte(_TestCondition):
52  """Matches android devices with a svelte (low-memory) build."""
53  def ShouldDisable(self, platform, finder_options):
54    del finder_options  # Unused.
55    return platform.GetOSName() == 'android' and platform.IsSvelte()
56
57  def __str__(self):
58    return 'Android Svelte'
59
60  def GetSupportedPlatformNames(self):
61    return set(['android'])
62
63class _TestConditionByAndroidModel(_TestCondition):
64  def __init__(self, model, name=None):
65    self._model = model
66    self._name = name if name else model
67
68  def ShouldDisable(self, platform, finder_options):
69    return (platform.GetOSName() == 'android' and
70            self._model == platform.GetDeviceTypeName())
71
72  def __str__(self):
73    return self._name
74
75  def GetSupportedPlatformNames(self):
76    return set(['android'])
77
78class _TestConditionAndroidWebview(_TestCondition):
79  def ShouldDisable(self, platform, finder_options):
80    return (platform.GetOSName() == 'android' and
81            finder_options.browser_type.startswith('android-webview'))
82
83  def __str__(self):
84    return 'Android Webview'
85
86  def GetSupportedPlatformNames(self):
87    return set(['android'])
88
89class _TestConditionAndroidNotWebview(_TestCondition):
90  def ShouldDisable(self, platform, finder_options):
91    return (platform.GetOSName() == 'android' and not
92            finder_options.browser_type.startswith('android-webview'))
93
94  def __str__(self):
95    return 'Android but not webview'
96
97  def GetSupportedPlatformNames(self):
98    return set(['android'])
99
100class _TestConditionByMacVersion(_TestCondition):
101  def __init__(self, version, name=None):
102    self._version = version
103    self._name = name
104
105  def __str__(self):
106    return self._name
107
108  def GetSupportedPlatformNames(self):
109    return set(['mac'])
110
111  def ShouldDisable(self, platform, finder_options):
112    if platform.GetOSName() != 'mac':
113      return False
114    return platform.GetOSVersionDetailString().startswith(self._version)
115
116
117class _TestConditionByWinVersion(_TestCondition):
118  def __init__(self, version, name):
119    self._version = version
120    self._name = name
121
122  def __str__(self):
123    return self._name
124
125  def GetSupportedPlatformNames(self):
126    return set(['win'])
127
128  def ShouldDisable(self, platform, finder_options):
129    if platform.GetOSName() != 'win':
130      return False
131    return platform.GetOSVersionName() == self._version
132
133
134class _TestConditionLogicalAndConditions(_TestCondition):
135  def __init__(self, conditions, name):
136    self._conditions = conditions
137    self._name = name
138
139  def __str__(self):
140    return self._name
141
142  def GetSupportedPlatformNames(self):
143    platforms = set()
144    for cond in self._conditions:
145      platforms.update(cond.GetSupportedPlatformNames())
146    return platforms
147
148  def ShouldDisable(self, platform, finder_options):
149    return all(
150        c.ShouldDisable(platform, finder_options) for c in self._conditions)
151
152
153class _TestConditionLogicalOrConditions(_TestCondition):
154  def __init__(self, conditions, name):
155    self._conditions = conditions
156    self._name = name
157
158  def __str__(self):
159    return self._name
160
161  def GetSupportedPlatformNames(self):
162    platforms = set()
163    for cond in self._conditions:
164      platforms.update(cond.GetSupportedPlatformNames())
165    return platforms
166
167  def ShouldDisable(self, platform, finder_options):
168    return any(
169        c.ShouldDisable(platform, finder_options) for c in self._conditions)
170
171
172ALL = _AllTestCondition()
173ALL_MAC = _TestConditionByPlatformList(['mac'], 'Mac')
174ALL_WIN = _TestConditionByPlatformList(['win'], 'Win')
175WIN_7 = _TestConditionByWinVersion(os_version_module.WIN7, 'Win 7')
176WIN_10 = _TestConditionByWinVersion(os_version_module.WIN10, 'Win 10')
177ALL_LINUX = _TestConditionByPlatformList(['linux'], 'Linux')
178ALL_CHROMEOS = _TestConditionByPlatformList(['chromeos'], 'ChromeOS')
179ALL_ANDROID = _TestConditionByPlatformList(['android'], 'Android')
180ALL_DESKTOP = _TestConditionByPlatformList(
181    ['mac', 'linux', 'win', 'chromeos'], 'Desktop')
182# Fuchsia setup is similar to mobile, though it is not quite the same.
183ALL_MOBILE = _TestConditionByPlatformList(['android', 'fuchsia'], 'Mobile')
184ANDROID_NEXUS5 = _TestConditionByAndroidModel('Nexus 5')
185_ANDROID_NEXUS5X = _TestConditionByAndroidModel('Nexus 5X')
186_ANDROID_NEXUS5XAOSP = _TestConditionByAndroidModel('AOSP on BullHead')
187ANDROID_NEXUS5X = _TestConditionLogicalOrConditions(
188    [_ANDROID_NEXUS5X, _ANDROID_NEXUS5XAOSP], 'Nexus 5X')
189_ANDROID_NEXUS6 = _TestConditionByAndroidModel('Nexus 6')
190_ANDROID_NEXUS6AOSP = _TestConditionByAndroidModel('AOSP on Shamu')
191ANDROID_NEXUS6 = _TestConditionLogicalOrConditions(
192    [_ANDROID_NEXUS6, _ANDROID_NEXUS6AOSP], 'Nexus 6')
193ANDROID_NEXUS6P = _TestConditionByAndroidModel('Nexus 6P')
194ANDROID_NEXUS7 = _TestConditionByAndroidModel('Nexus 7')
195ANDROID_GO = _TestConditionByAndroidModel('gobo', 'Android Go')
196ANDROID_ONE = _TestConditionByAndroidModel('W6210', 'Android One')
197ANDROID_SVELTE = _TestConditionAndroidSvelte()
198ANDROID_LOW_END = _TestConditionLogicalOrConditions(
199    [ANDROID_GO, ANDROID_SVELTE, ANDROID_ONE], 'Android Low End')
200ANDROID_PIXEL2 = _TestConditionByAndroidModel('Pixel 2')
201ANDROID_WEBVIEW = _TestConditionAndroidWebview()
202ANDROID_NOT_WEBVIEW = _TestConditionAndroidNotWebview()
203# MAC_10_11 Includes:
204#   Mac 10.11 Perf, Mac Retina Perf, Mac Pro 10.11 Perf, Mac Air 10.11 Perf
205MAC_10_11 = _TestConditionByMacVersion('10.11', 'Mac 10.11')
206# Mac 10_12 Includes:
207#   Mac 10.12 Perf, Mac Mini 8GB 10.12 Perf
208MAC_10_12 = _TestConditionByMacVersion('10.12', 'Mac 10.12')
209ANDROID_NEXUS6_WEBVIEW = _TestConditionLogicalAndConditions(
210    [ANDROID_NEXUS6, ANDROID_WEBVIEW], 'Nexus6 Webview')
211ANDROID_NEXUS5X_WEBVIEW = _TestConditionLogicalAndConditions(
212    [ANDROID_NEXUS5X, ANDROID_WEBVIEW], 'Nexus5X Webview')
213ANDROID_GO_WEBVIEW = _TestConditionLogicalAndConditions(
214    [ANDROID_GO, ANDROID_WEBVIEW], 'Android Go Webview')
215ANDROID_PIXEL2_WEBVIEW = _TestConditionLogicalAndConditions(
216    [ANDROID_PIXEL2, ANDROID_WEBVIEW], 'Pixel2 Webview')
217
218EXPECTATION_NAME_MAP = {
219    'All': ALL,
220    'Android_Go': ANDROID_GO,
221    'Android_One': ANDROID_ONE,
222    'Android_Svelte': ANDROID_SVELTE,
223    'Android_Low_End': ANDROID_LOW_END,
224    'Android_Webview': ANDROID_WEBVIEW,
225    'Android_but_not_webview': ANDROID_NOT_WEBVIEW,
226    'Mac': ALL_MAC,
227    'Win': ALL_WIN,
228    'Win_7': WIN_7,
229    'Win_10': WIN_10,
230    'Linux': ALL_LINUX,
231    'ChromeOS': ALL_CHROMEOS,
232    'Android': ALL_ANDROID,
233    'Desktop': ALL_DESKTOP,
234    'Mobile': ALL_MOBILE,
235    'Nexus_5': ANDROID_NEXUS5,
236    'Nexus_5X': ANDROID_NEXUS5X,
237    'Nexus_6': ANDROID_NEXUS6,
238    'Nexus_6P': ANDROID_NEXUS6P,
239    'Nexus_7': ANDROID_NEXUS7,
240    'Pixel_2': ANDROID_PIXEL2,
241    'Mac_10.11': MAC_10_11,
242    'Mac_10.12': MAC_10_12,
243    'Nexus6_Webview': ANDROID_NEXUS6_WEBVIEW,
244    'Nexus5X_Webview': ANDROID_NEXUS5X_WEBVIEW,
245    'Android_Go_Webview': ANDROID_GO_WEBVIEW,
246    'Pixel2_Webview': ANDROID_PIXEL2_WEBVIEW,
247}
248