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
5import unittest
6
7
8from telemetry.core import os_version as os_version_module
9from telemetry.story import expectations
10from telemetry.testing import fakes
11
12
13class MockState(object):
14  def __init__(self):
15    self.platform = fakes.FakePlatform()
16
17
18class MockStory(object):
19  def __init__(self, name):
20    self._name = name
21
22  @property
23  def name(self):
24    return self._name
25
26
27class MockStorySet(object):
28  def __init__(self, stories):
29    self._stories = stories
30
31  @property
32  def stories(self):
33    return self._stories
34
35class MockBrowserFinderOptions(object):
36  def __init__(self):
37    self._browser_type = None
38
39  @property
40  def browser_type(self):
41    return self._browser_type
42
43  @browser_type.setter
44  def browser_type(self, t):
45    assert isinstance(t, basestring)
46    self._browser_type = t
47
48
49class TestConditionTest(unittest.TestCase):
50  def setUp(self):
51    self._platform = fakes.FakePlatform()
52    self._finder_options = MockBrowserFinderOptions()
53
54  def testAllAlwaysReturnsTrue(self):
55    self.assertTrue(
56        expectations.ALL.ShouldDisable(self._platform, self._finder_options))
57
58  def testAllWinReturnsTrueOnWindows(self):
59    self._platform.SetOSName('win')
60    self.assertTrue(
61        expectations.ALL_WIN.ShouldDisable(self._platform,
62                                           self._finder_options))
63
64  def testAllWinReturnsFalseOnOthers(self):
65    self._platform.SetOSName('not_windows')
66    self.assertFalse(
67        expectations.ALL_WIN.ShouldDisable(self._platform,
68                                           self._finder_options))
69
70  def testAllLinuxReturnsTrueOnLinux(self):
71    self._platform.SetOSName('linux')
72    self.assertTrue(expectations.ALL_LINUX.ShouldDisable(self._platform,
73                                                         self._finder_options))
74
75  def testAllLinuxReturnsFalseOnOthers(self):
76    self._platform.SetOSName('not_linux')
77    self.assertFalse(expectations.ALL_LINUX.ShouldDisable(self._platform,
78                                                          self._finder_options))
79
80  def testAllMacReturnsTrueOnMac(self):
81    self._platform.SetOSName('mac')
82    self.assertTrue(expectations.ALL_MAC.ShouldDisable(self._platform,
83                                                       self._finder_options))
84
85  def testAllMacReturnsFalseOnOthers(self):
86    self._platform.SetOSName('not_mac')
87    self.assertFalse(expectations.ALL_MAC.ShouldDisable(self._platform,
88                                                        self._finder_options))
89
90  def testAllChromeOSReturnsTrueOnChromeOS(self):
91    self._platform.SetOSName('chromeos')
92    self.assertTrue(expectations.ALL_CHROMEOS.ShouldDisable(
93        self._platform, self._finder_options))
94
95  def testAllChromeOSReturnsFalseOnOthers(self):
96    self._platform.SetOSName('not_chromeos')
97    self.assertFalse(expectations.ALL_CHROMEOS.ShouldDisable(
98        self._platform, self._finder_options))
99
100  def testAllAndroidReturnsTrueOnAndroid(self):
101    self._platform.SetOSName('android')
102    self.assertTrue(
103        expectations.ALL_ANDROID.ShouldDisable(self._platform,
104                                               self._finder_options))
105
106  def testAllAndroidReturnsFalseOnOthers(self):
107    self._platform.SetOSName('not_android')
108    self.assertFalse(
109        expectations.ALL_ANDROID.ShouldDisable(self._platform,
110                                               self._finder_options))
111
112  def testAllDesktopReturnsFalseOnNonDesktop(self):
113    false_platforms = ['android']
114    for plat in false_platforms:
115      self._platform.SetOSName(plat)
116      self.assertFalse(
117          expectations.ALL_DESKTOP.ShouldDisable(self._platform,
118                                                 self._finder_options))
119
120  def testAllDesktopReturnsTrueOnDesktop(self):
121    true_platforms = ['win', 'mac', 'linux', 'chromeos']
122    for plat in true_platforms:
123      self._platform.SetOSName(plat)
124      self.assertTrue(
125          expectations.ALL_DESKTOP.ShouldDisable(self._platform,
126                                                 self._finder_options))
127
128  def testAllMobileReturnsFalseOnNonMobile(self):
129    false_platforms = ['win', 'mac', 'linux', 'chromeos']
130    for plat in false_platforms:
131      self._platform.SetOSName(plat)
132      self.assertFalse(
133          expectations.ALL_MOBILE.ShouldDisable(self._platform,
134                                                self._finder_options))
135
136  def testAllMobileReturnsTrueOnMobile(self):
137    true_platforms = ['android']
138    for plat in true_platforms:
139      self._platform.SetOSName(plat)
140      self.assertTrue(
141          expectations.ALL_MOBILE.ShouldDisable(self._platform,
142                                                self._finder_options))
143
144  def testAndroidNexus5ReturnsFalseOnNotAndroid(self):
145    self._platform.SetOSName('not_android')
146    self.assertFalse(
147        expectations.ANDROID_NEXUS5.ShouldDisable(self._platform,
148                                                  self._finder_options))
149
150  def testAndroidNexus5XReturnsFalseOnNotAndroid(self):
151    self._platform.SetOSName('not_android')
152    self.assertFalse(
153        expectations.ANDROID_NEXUS5X.ShouldDisable(self._platform,
154                                                   self._finder_options))
155
156  def testAndroidNexus6ReturnsFalseOnNotAndroid(self):
157    self._platform.SetOSName('not_android')
158    self.assertFalse(
159        expectations.ANDROID_NEXUS6.ShouldDisable(self._platform,
160                                                  self._finder_options))
161
162  def testAndroidNexus6PReturnsFalseOnNotAndroid(self):
163    self._platform.SetOSName('not_android')
164    self.assertFalse(
165        expectations.ANDROID_NEXUS6P.ShouldDisable(self._platform,
166                                                   self._finder_options))
167
168  def testAndroidNexus7ReturnsFalseOnNotAndroid(self):
169    self._platform.SetOSName('not_android')
170    self.assertFalse(
171        expectations.ANDROID_NEXUS7.ShouldDisable(self._platform,
172                                                  self._finder_options))
173
174  def testAndroidCherryMobileReturnsFalseOnNotAndroid(self):
175    self._platform.SetOSName('not_android')
176    self.assertFalse(
177        expectations.ANDROID_ONE.ShouldDisable(self._platform,
178                                               self._finder_options))
179
180  def testAndroidSvelteReturnsFalseOnNotAndroid(self):
181    self._platform.SetOSName('not_android')
182    self.assertFalse(
183        expectations.ANDROID_SVELTE.ShouldDisable(self._platform,
184                                                  self._finder_options))
185
186  def testAndroidNexus5ReturnsFalseOnAndroidNotNexus5(self):
187    self._platform.SetOSName('android')
188    self.assertFalse(
189        expectations.ANDROID_NEXUS5.ShouldDisable(self._platform,
190                                                  self._finder_options))
191
192  def testAndroidNexus5XReturnsFalseOnAndroidNotNexus5X(self):
193    self._platform.SetOSName('android')
194    self.assertFalse(
195        expectations.ANDROID_NEXUS5X.ShouldDisable(self._platform,
196                                                   self._finder_options))
197
198  def testAndroidNexus5ReturnsFalseOnAndroidNexus5X(self):
199    self._platform.SetOSName('android')
200    self._platform.SetDeviceTypeName('Nexus 5X')
201    self.assertFalse(
202        expectations.ANDROID_NEXUS5.ShouldDisable(self._platform,
203                                                  self._finder_options))
204
205  def testAndroidNexus6ReturnsFalseOnAndroidNotNexus6(self):
206    self._platform.SetOSName('android')
207    self.assertFalse(
208        expectations.ANDROID_NEXUS6.ShouldDisable(self._platform,
209                                                  self._finder_options))
210
211  def testAndroidNexus6ReturnsFalseOnAndroidNexus6P(self):
212    self._platform.SetOSName('android')
213    self._platform.SetDeviceTypeName('Nexus 6P')
214    self.assertFalse(
215        expectations.ANDROID_NEXUS6.ShouldDisable(self._platform,
216                                                  self._finder_options))
217
218  def testAndroidNexus6PReturnsFalseOnAndroidNotNexus6P(self):
219    self._platform.SetOSName('android')
220    self.assertFalse(
221        expectations.ANDROID_NEXUS6P.ShouldDisable(self._platform,
222                                                   self._finder_options))
223
224  def testAndroidNexus7ReturnsFalseOnAndroidNotNexus7(self):
225    self._platform.SetOSName('android')
226    self.assertFalse(
227        expectations.ANDROID_NEXUS7.ShouldDisable(self._platform,
228                                                  self._finder_options))
229
230  def testAndroidCherryMobileReturnsFalseOnAndroidNotCherryMobile(self):
231    self._platform.SetOSName('android')
232    self.assertFalse(
233        expectations.ANDROID_ONE.ShouldDisable(self._platform,
234                                               self._finder_options))
235
236  def testAndroidSvelteReturnsFalseOnAndroidNotSvelte(self):
237    self._platform.SetOSName('android')
238    self.assertFalse(
239        expectations.ANDROID_SVELTE.ShouldDisable(self._platform,
240                                                  self._finder_options))
241
242  def testAndroidNexus5ReturnsTrueOnAndroidNexus5(self):
243    self._platform.SetOSName('android')
244    self._platform.SetDeviceTypeName('Nexus 5')
245    self.assertTrue(
246        expectations.ANDROID_NEXUS5.ShouldDisable(self._platform,
247                                                  self._finder_options))
248
249  def testAndroidNexus5XReturnsTrueOnAndroidNexus5X(self):
250    self._platform.SetOSName('android')
251    self._platform.SetDeviceTypeName('Nexus 5X')
252    self.assertTrue(
253        expectations.ANDROID_NEXUS5X.ShouldDisable(self._platform,
254                                                   self._finder_options))
255
256  def testAndroidNexus6ReturnsTrueOnAndroidNexus6(self):
257    self._platform.SetOSName('android')
258    self._platform.SetDeviceTypeName('Nexus 6')
259    self.assertTrue(
260        expectations.ANDROID_NEXUS6.ShouldDisable(self._platform,
261                                                  self._finder_options))
262
263  def testAndroidNexus6PReturnsTrueOnAndroidNexus6P(self):
264    self._platform.SetOSName('android')
265    self._platform.SetDeviceTypeName('Nexus 6P')
266    self.assertTrue(
267        expectations.ANDROID_NEXUS6P.ShouldDisable(self._platform,
268                                                   self._finder_options))
269
270  def testAndroidNexus7ReturnsTrueOnAndroidNexus7(self):
271    self._platform.SetOSName('android')
272    self._platform.SetDeviceTypeName('Nexus 7')
273    self.assertTrue(
274        expectations.ANDROID_NEXUS7.ShouldDisable(self._platform,
275                                                  self._finder_options))
276
277  def testAndroidCherryMobileReturnsTrueOnAndroidCherryMobile(self):
278    self._platform.SetOSName('android')
279    self._platform.SetDeviceTypeName('W6210')
280    self.assertTrue(
281        expectations.ANDROID_ONE.ShouldDisable(self._platform,
282                                               self._finder_options))
283
284  def testAndroidSvelteReturnsTrueOnAndroidSvelte(self):
285    self._platform.SetOSName('android')
286    self._platform.SetIsSvelte(True)
287    self.assertTrue(
288        expectations.ANDROID_SVELTE.ShouldDisable(self._platform,
289                                                  self._finder_options))
290
291  def testAndroidWebviewReturnsTrueOnAndroidWebview(self):
292    self._platform.SetOSName('android')
293    self._platform.SetIsAosp(True)
294    self._finder_options.browser_type = 'android-webview'
295    self.assertTrue(
296        expectations.ANDROID_WEBVIEW.ShouldDisable(self._platform,
297                                                   self._finder_options))
298
299  def testAndroidWebviewReturnsTrueOnAndroidWebviewGoogle(self):
300    self._platform.SetOSName('android')
301    self._finder_options.browser_type = 'android-webview-google'
302    self.assertTrue(
303        expectations.ANDROID_WEBVIEW.ShouldDisable(self._platform,
304                                                   self._finder_options))
305
306  def testAndroidWebviewReturnsFalseOnAndroidNotWebview(self):
307    self._platform.SetOSName('android')
308    self._finder_options.browser_type = 'android-chrome'
309    self.assertFalse(
310        expectations.ANDROID_WEBVIEW.ShouldDisable(self._platform,
311                                                   self._finder_options))
312
313  def testAndroidWebviewReturnsFalseOnNotAndroid(self):
314    self._platform.SetOSName('not_android')
315    self.assertFalse(
316        expectations.ANDROID_WEBVIEW.ShouldDisable(self._platform,
317                                                   self._finder_options))
318
319  def testAndroidNotWebviewReturnsTrueOnAndroidNotWebview(self):
320    self._platform.SetOSName('android')
321    self._finder_options.browser_type = 'android'
322    self.assertTrue(
323        expectations.ANDROID_NOT_WEBVIEW.ShouldDisable(self._platform,
324                                                       self._finder_options))
325
326  def testAndroidNotWebviewReturnsFalseOnAndroidWebview(self):
327    self._platform.SetOSName('android')
328    self._finder_options.browser_type = 'android-webview'
329    self.assertFalse(
330        expectations.ANDROID_NOT_WEBVIEW.ShouldDisable(self._platform,
331                                                       self._finder_options))
332
333  def testAndroidNotWebviewReturnsFalseOnNotAndroid(self):
334    self._platform.SetOSName('not_android')
335    self.assertFalse(
336        expectations.ANDROID_NOT_WEBVIEW.ShouldDisable(self._platform,
337                                                       self._finder_options))
338  def testMac1011ReturnsTrueOnMac1011(self):
339    self._platform.SetOSName('mac')
340    self._platform.SetOsVersionDetailString('10.11')
341    self.assertTrue(
342        expectations.MAC_10_11.ShouldDisable(self._platform,
343                                             self._finder_options))
344
345  def testMac1011ReturnsFalseOnNotMac1011(self):
346    self._platform.SetOSName('mac')
347    self._platform.SetOsVersionDetailString('10.12')
348    self.assertFalse(
349        expectations.MAC_10_11.ShouldDisable(self._platform,
350                                             self._finder_options))
351
352  def testMac1012ReturnsTrueOnMac1012(self):
353    self._platform.SetOSName('mac')
354    self._platform.SetOsVersionDetailString('10.12')
355    self.assertTrue(
356        expectations.MAC_10_12.ShouldDisable(self._platform,
357                                             self._finder_options))
358
359  def testMac1012ReturnsFalseOnNotMac1012(self):
360    self._platform.SetOSName('mac')
361    self._platform.SetOsVersionDetailString('10.11')
362    self.assertFalse(
363        expectations.MAC_10_12.ShouldDisable(self._platform,
364                                             self._finder_options))
365
366  def testNexus5XWebviewFalseOnNotWebview(self):
367    self._platform.SetOSName('android')
368    self._finder_options.browser_type = 'android'
369    self._platform.SetDeviceTypeName('Nexus 5X')
370    self.assertFalse(
371        expectations.ANDROID_NEXUS5X_WEBVIEW.ShouldDisable(
372            self._platform, self._finder_options))
373
374  def testNexus5XWebviewFalseOnNotNexus5X(self):
375    self._platform.SetOSName('android')
376    self._finder_options.browser_type = 'android-webview'
377    self.assertFalse(
378        expectations.ANDROID_NEXUS5X_WEBVIEW.ShouldDisable(
379            self._platform, self._finder_options))
380
381  def testNexus5XWebviewReturnsTrue(self):
382    self._platform.SetOSName('android')
383    self._finder_options.browser_type = 'android-webview'
384    self._platform.SetDeviceTypeName('Nexus 5X')
385    self.assertTrue(
386        expectations.ANDROID_NEXUS5X_WEBVIEW.ShouldDisable(
387            self._platform, self._finder_options))
388
389  def testNexus6WebviewFalseOnNotWebview(self):
390    self._platform.SetOSName('android')
391    self._finder_options.browser_type = 'android'
392    self._platform.SetDeviceTypeName('Nexus 6')
393    self.assertFalse(
394        expectations.ANDROID_NEXUS6_WEBVIEW.ShouldDisable(
395            self._platform, self._finder_options))
396
397  def testNexus6WebviewFalseOnNotNexus6(self):
398    self._platform.SetOSName('android')
399    self._finder_options.browser_type = 'android-webview'
400    self._platform.SetDeviceTypeName('Nexus 5X')
401    self.assertFalse(
402        expectations.ANDROID_NEXUS6_WEBVIEW.ShouldDisable(
403            self._platform, self._finder_options))
404
405  def testNexus6WebviewReturnsTrue(self):
406    self._platform.SetOSName('android')
407    self._finder_options.browser_type = 'android-webview'
408    self._platform.SetDeviceTypeName('Nexus 6')
409    self.assertTrue(
410        expectations.ANDROID_NEXUS6_WEBVIEW.ShouldDisable(
411            self._platform, self._finder_options))
412
413  def testAndroidNexus6AOSP(self):
414    self._platform.SetOSName('android')
415    self._platform.SetDeviceTypeName('AOSP on Shamu')
416    self.assertTrue(
417        expectations.ANDROID_NEXUS6.ShouldDisable(
418            self._platform, self._finder_options))
419
420  def testAndroidNexus5XAOSP(self):
421    self._platform.SetOSName('android')
422    self._platform.SetDeviceTypeName('AOSP on BullHead')
423    self.assertTrue(
424        expectations.ANDROID_NEXUS5X.ShouldDisable(
425            self._platform, self._finder_options))
426
427  def testAndroidNexus6WebviewAOSP(self):
428    self._platform.SetOSName('android')
429    self._finder_options.browser_type = 'android-webview'
430    self._platform.SetDeviceTypeName('AOSP on Shamu')
431    self.assertTrue(
432        expectations.ANDROID_NEXUS6_WEBVIEW.ShouldDisable(
433            self._platform, self._finder_options))
434
435  def testAndroidNexus5XWebviewAOSP(self):
436    self._platform.SetOSName('android')
437    self._finder_options.browser_type = 'android-webview'
438    self._platform.SetDeviceTypeName('AOSP on BullHead')
439    self.assertTrue(
440        expectations.ANDROID_NEXUS5X_WEBVIEW.ShouldDisable(
441            self._platform, self._finder_options))
442
443  def testWin7(self):
444    self._platform.SetOSName('win')
445    self._platform.SetOSVersionName(os_version_module.WIN7)
446    self.assertTrue(
447        expectations.WIN_7.ShouldDisable(
448            self._platform, self._finder_options))
449    self.assertEquals('Win 7', str(expectations.WIN_7))
450
451  def testWin10(self):
452    self._platform.SetOSName('win')
453    self._platform.SetOSVersionName(os_version_module.WIN10)
454    self.assertTrue(
455        expectations.WIN_10.ShouldDisable(
456            self._platform, self._finder_options))
457    self.assertEquals('Win 10', str(expectations.WIN_10))
458
459  def testAndroidGoWebviewFalseOnNotWebview(self):
460    self._platform.SetOSName('android')
461    self._finder_options.browser_type = 'android'
462    self._platform.SetDeviceTypeName('gobo')
463    self.assertFalse(
464        expectations.ANDROID_GO_WEBVIEW.ShouldDisable(
465            self._platform, self._finder_options))
466
467  def testAndroidGoWebviewFalseOnNotNexus6(self):
468    self._platform.SetOSName('android')
469    self._finder_options.browser_type = 'android-webview'
470    self._platform.SetDeviceTypeName('Nexus 5X')
471    self.assertFalse(
472        expectations.ANDROID_GO_WEBVIEW.ShouldDisable(
473            self._platform, self._finder_options))
474
475  def testAndroidGoWebviewReturnsTrue(self):
476    self._platform.SetOSName('android')
477    self._finder_options.browser_type = 'android-webview'
478    self._platform.SetDeviceTypeName('gobo')
479    self.assertTrue(
480        expectations.ANDROID_GO_WEBVIEW.ShouldDisable(
481            self._platform, self._finder_options))
482