Home | History | Annotate | Download | only in telemetry
      1 # Copyright 2014 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 
      5 import unittest
      6 
      7 import mock
      8 
      9 from telemetry.core import platform
     10 from telemetry import decorators
     11 from telemetry.internal.browser import possible_browser
     12 
     13 
     14 class FakeTest(object):
     15 
     16   def SetEnabledStrings(self, enabled_strings):
     17     enabled_attr_name = decorators.EnabledAttributeName(self)
     18     setattr(self, enabled_attr_name, enabled_strings)
     19 
     20   def SetDisabledStrings(self, disabled_strings):
     21     # pylint: disable=attribute-defined-outside-init
     22     disabled_attr_name = decorators.DisabledAttributeName(self)
     23     setattr(self, disabled_attr_name, disabled_strings)
     24 
     25 
     26 class TestDisableDecorators(unittest.TestCase):
     27 
     28   def testDisabledStringOnFunction(self):
     29 
     30     @decorators.Disabled('bar')
     31     def Sum():
     32       return 1 + 1
     33 
     34     self.assertEquals({'bar'}, decorators.GetDisabledAttributes(Sum))
     35 
     36     @decorators.Disabled('bar')
     37     @decorators.Disabled('baz')
     38     @decorators.Disabled('bart', 'baz')
     39     def Product():
     40       return 1 * 1
     41 
     42     self.assertEquals({'bar', 'bart', 'baz'},
     43                       decorators.GetDisabledAttributes(Product))
     44 
     45   def testDisabledStringOnClass(self):
     46 
     47     @decorators.Disabled('windshield')
     48     class Ford(object):
     49       pass
     50 
     51     self.assertEquals({'windshield'}, decorators.GetDisabledAttributes(Ford))
     52 
     53     @decorators.Disabled('windows', 'Drive')
     54     @decorators.Disabled('wheel')
     55     @decorators.Disabled('windows')
     56     class Honda(object):
     57       pass
     58 
     59     self.assertEquals({'windshield'}, decorators.GetDisabledAttributes(Ford))
     60     self.assertEquals({'wheel', 'Drive', 'windows'},
     61                       decorators.GetDisabledAttributes(Honda))
     62 
     63   def testDisabledStringOnSubClass(self):
     64 
     65     @decorators.Disabled('windshield')
     66     class Car(object):
     67       pass
     68 
     69     class Ford(Car):
     70       pass
     71 
     72     self.assertEquals({'windshield'}, decorators.GetDisabledAttributes(Car))
     73     self.assertFalse(decorators.GetDisabledAttributes(Ford))
     74 
     75     @decorators.Disabled('windows', 'Drive')
     76     @decorators.Disabled('wheel')
     77     @decorators.Disabled('windows')
     78     class Honda(Car):
     79       pass
     80 
     81     self.assertFalse(decorators.GetDisabledAttributes(Ford))
     82     self.assertEquals({'windshield'}, decorators.GetDisabledAttributes(Car))
     83     self.assertEquals({'wheel', 'Drive', 'windows'},
     84                       decorators.GetDisabledAttributes(Honda))
     85 
     86   def testDisabledStringOnMethod(self):
     87 
     88     class Ford(object):
     89 
     90       @decorators.Disabled('windshield')
     91       def Drive(self):
     92         pass
     93 
     94     self.assertEquals({'windshield'},
     95                       decorators.GetDisabledAttributes(Ford().Drive))
     96 
     97     class Honda(object):
     98 
     99       @decorators.Disabled('windows', 'Drive')
    100       @decorators.Disabled('wheel')
    101       @decorators.Disabled('windows')
    102       def Drive(self):
    103         pass
    104 
    105     self.assertEquals({'wheel', 'Drive', 'windows'},
    106                       decorators.GetDisabledAttributes(Honda().Drive))
    107     self.assertEquals({'windshield'},
    108                       decorators.GetDisabledAttributes(Ford().Drive))
    109 
    110     class Accord(Honda):
    111 
    112       def Drive(self):
    113         pass
    114 
    115     class Explorer(Ford):
    116       pass
    117 
    118     self.assertEquals({'wheel', 'Drive', 'windows'},
    119                       decorators.GetDisabledAttributes(Honda().Drive))
    120     self.assertEquals({'windshield'},
    121                       decorators.GetDisabledAttributes(Ford().Drive))
    122     self.assertEquals({'windshield'},
    123                       decorators.GetDisabledAttributes(Explorer().Drive))
    124     self.assertFalse(decorators.GetDisabledAttributes(Accord().Drive))
    125 
    126 
    127 class TestEnableDecorators(unittest.TestCase):
    128 
    129   def testEnabledStringOnFunction(self):
    130 
    131     @decorators.Enabled('minus', 'power')
    132     def Sum():
    133       return 1 + 1
    134 
    135     self.assertEquals({'minus', 'power'}, decorators.GetEnabledAttributes(Sum))
    136 
    137     @decorators.Enabled('dot')
    138     @decorators.Enabled('product')
    139     @decorators.Enabled('product', 'dot')
    140     def Product():
    141       return 1 * 1
    142 
    143     self.assertEquals({'dot', 'product'},
    144                       decorators.GetEnabledAttributes(Product))
    145 
    146   def testEnabledStringOnClass(self):
    147 
    148     @decorators.Enabled('windshield', 'light')
    149     class Ford(object):
    150       pass
    151 
    152     self.assertEquals({'windshield', 'light'},
    153                       decorators.GetEnabledAttributes(Ford))
    154 
    155     @decorators.Enabled('wheel', 'Drive')
    156     @decorators.Enabled('wheel')
    157     @decorators.Enabled('windows')
    158     class Honda(object):
    159       pass
    160 
    161     self.assertEquals({'wheel', 'Drive', 'windows'},
    162                       decorators.GetEnabledAttributes(Honda))
    163     self.assertEquals({'windshield', 'light'},
    164                       decorators.GetEnabledAttributes(Ford))
    165 
    166   def testEnabledStringOnSubClass(self):
    167 
    168     @decorators.Enabled('windshield')
    169     class Car(object):
    170       pass
    171 
    172     class Ford(Car):
    173       pass
    174 
    175     self.assertEquals({'windshield'}, decorators.GetEnabledAttributes(Car))
    176     self.assertFalse(decorators.GetEnabledAttributes(Ford))
    177 
    178     @decorators.Enabled('windows', 'Drive')
    179     @decorators.Enabled('wheel')
    180     @decorators.Enabled('windows')
    181     class Honda(Car):
    182       pass
    183 
    184     self.assertFalse(decorators.GetEnabledAttributes(Ford))
    185     self.assertEquals({'windshield'}, decorators.GetEnabledAttributes(Car))
    186     self.assertEquals({'wheel', 'Drive', 'windows'},
    187                       decorators.GetEnabledAttributes(Honda))
    188 
    189   def testEnabledStringOnMethod(self):
    190 
    191     class Ford(object):
    192 
    193       @decorators.Enabled('windshield')
    194       def Drive(self):
    195         pass
    196 
    197     self.assertEquals({'windshield'},
    198                       decorators.GetEnabledAttributes(Ford().Drive))
    199 
    200     class Honda(object):
    201 
    202       @decorators.Enabled('windows', 'Drive')
    203       @decorators.Enabled('wheel', 'Drive')
    204       @decorators.Enabled('windows')
    205       def Drive(self):
    206         pass
    207 
    208     self.assertEquals({'wheel', 'Drive', 'windows'},
    209                       decorators.GetEnabledAttributes(Honda().Drive))
    210 
    211     class Accord(Honda):
    212 
    213       def Drive(self):
    214         pass
    215 
    216     class Explorer(Ford):
    217       pass
    218 
    219     self.assertEquals({'wheel', 'Drive', 'windows'},
    220                       decorators.GetEnabledAttributes(Honda().Drive))
    221     self.assertEquals({'windshield'},
    222                       decorators.GetEnabledAttributes(Ford().Drive))
    223     self.assertEquals({'windshield'},
    224                       decorators.GetEnabledAttributes(Explorer().Drive))
    225     self.assertFalse(decorators.GetEnabledAttributes(Accord().Drive))
    226 
    227 
    228 class TestShouldSkip(unittest.TestCase):
    229 
    230   def setUp(self):
    231     fake_platform = mock.Mock(spec_set=platform.Platform)
    232     fake_platform.GetOSName.return_value = 'os_name'
    233     fake_platform.GetOSVersionName.return_value = 'os_version_name'
    234 
    235     self.possible_browser = mock.Mock(spec_set=possible_browser.PossibleBrowser)
    236     self.possible_browser.browser_type = 'browser_type'
    237     self.possible_browser.platform = fake_platform
    238     self.possible_browser.supports_tab_control = False
    239 
    240   def testEnabledStrings(self):
    241     test = FakeTest()
    242 
    243     # When no enabled_strings is given, everything should be enabled.
    244     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    245 
    246     test.SetEnabledStrings(['os_name'])
    247     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    248 
    249     test.SetEnabledStrings(['another_os_name'])
    250     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    251 
    252     test.SetEnabledStrings(['os_version_name'])
    253     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    254 
    255     test.SetEnabledStrings(['os_name', 'another_os_name'])
    256     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    257 
    258     test.SetEnabledStrings(['another_os_name', 'os_name'])
    259     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    260 
    261     test.SetEnabledStrings(['another_os_name', 'another_os_version_name'])
    262     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    263 
    264     test.SetEnabledStrings(['os_name-reference'])
    265     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    266 
    267     test.SetEnabledStrings(['another_os_name-reference'])
    268     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    269 
    270     test.SetEnabledStrings(['os_version_name-reference'])
    271     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    272 
    273     test.SetEnabledStrings(['os_name-reference', 'another_os_name-reference'])
    274     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    275 
    276     test.SetEnabledStrings(['another_os_name-reference', 'os_name-reference'])
    277     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    278 
    279     test.SetEnabledStrings(['another_os_name-reference',
    280                             'another_os_version_name-reference'])
    281     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    282 
    283   def testDisabledStrings(self):
    284     test = FakeTest()
    285 
    286     # When no disabled_strings is given, nothing should be disabled.
    287     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    288 
    289     test.SetDisabledStrings(['os_name'])
    290     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    291 
    292     test.SetDisabledStrings(['another_os_name'])
    293     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    294 
    295     test.SetDisabledStrings(['os_version_name'])
    296     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    297 
    298     test.SetDisabledStrings(['os_name', 'another_os_name'])
    299     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    300 
    301     test.SetDisabledStrings(['another_os_name', 'os_name'])
    302     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    303 
    304     test.SetDisabledStrings(['another_os_name', 'another_os_version_name'])
    305     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    306 
    307     test.SetDisabledStrings(['reference'])
    308     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    309 
    310     test.SetDisabledStrings(['os_name-reference'])
    311     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    312 
    313     test.SetDisabledStrings(['another_os_name-reference'])
    314     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    315 
    316     test.SetDisabledStrings(['os_version_name-reference'])
    317     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    318 
    319     test.SetDisabledStrings(['os_name-reference', 'another_os_name-reference'])
    320     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    321 
    322     test.SetDisabledStrings(['another_os_name-reference', 'os_name-reference'])
    323     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    324 
    325     test.SetDisabledStrings(['another_os_name-reference',
    326                              'another_os_version_name-reference'])
    327     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    328 
    329   def testReferenceEnabledStrings(self):
    330     self.possible_browser.browser_type = 'reference'
    331     test = FakeTest()
    332 
    333     # When no enabled_strings is given, everything should be enabled.
    334     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    335 
    336     test.SetEnabledStrings(['os_name-reference'])
    337     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    338 
    339     test.SetEnabledStrings(['another_os_name-reference'])
    340     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    341 
    342     test.SetEnabledStrings(['os_version_name-reference'])
    343     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    344 
    345     test.SetEnabledStrings(['os_name-reference', 'another_os_name-reference'])
    346     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    347 
    348     test.SetEnabledStrings(['another_os_name-reference', 'os_name-reference'])
    349     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    350 
    351     test.SetEnabledStrings(['another_os_name-reference',
    352                             'another_os_version_name-reference'])
    353     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    354 
    355   def testReferenceDisabledStrings(self):
    356     self.possible_browser.browser_type = 'reference'
    357     test = FakeTest()
    358 
    359     # When no disabled_strings is given, nothing should be disabled.
    360     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    361 
    362     test.SetDisabledStrings(['reference'])
    363     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    364 
    365     test.SetDisabledStrings(['os_name-reference'])
    366     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    367 
    368     test.SetDisabledStrings(['another_os_name-reference'])
    369     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    370 
    371     test.SetDisabledStrings(['os_version_name-reference'])
    372     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    373 
    374     test.SetDisabledStrings(['os_name-reference', 'another_os_name-reference'])
    375     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    376 
    377     test.SetDisabledStrings(['another_os_name-reference', 'os_name-reference'])
    378     self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0])
    379 
    380     test.SetDisabledStrings(['another_os_name-reference',
    381                              'another_os_version_name-reference'])
    382     self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0])
    383 
    384 
    385 class TestDeprecation(unittest.TestCase):
    386 
    387   @mock.patch('warnings.warn')
    388   def testFunctionDeprecation(self, warn_mock):
    389 
    390     @decorators.Deprecated(2015, 12, 1)
    391     def Foo(x):
    392       return x
    393 
    394     Foo(1)
    395     warn_mock.assert_called_with(
    396         'Function Foo is deprecated. It will no longer be supported on '
    397         'December 01, 2015. Please remove it or switch to an alternative '
    398         'before that time. \n',
    399         stacklevel=4)
    400 
    401   @mock.patch('warnings.warn')
    402   def testMethodDeprecated(self, warn_mock):
    403 
    404     class Bar(object):
    405 
    406       @decorators.Deprecated(2015, 12, 1, 'Testing only.')
    407       def Foo(self, x):
    408         return x
    409 
    410     Bar().Foo(1)
    411     warn_mock.assert_called_with(
    412         'Function Foo is deprecated. It will no longer be supported on '
    413         'December 01, 2015. Please remove it or switch to an alternative '
    414         'before that time. Testing only.\n',
    415         stacklevel=4)
    416 
    417   @mock.patch('warnings.warn')
    418   def testClassWithoutInitDefinedDeprecated(self, warn_mock):
    419 
    420     @decorators.Deprecated(2015, 12, 1)
    421     class Bar(object):
    422 
    423       def Foo(self, x):
    424         return x
    425 
    426     Bar().Foo(1)
    427     warn_mock.assert_called_with(
    428         'Class Bar is deprecated. It will no longer be supported on '
    429         'December 01, 2015. Please remove it or switch to an alternative '
    430         'before that time. \n',
    431         stacklevel=4)
    432 
    433   @mock.patch('warnings.warn')
    434   def testClassWithInitDefinedDeprecated(self, warn_mock):
    435 
    436     @decorators.Deprecated(2015, 12, 1)
    437     class Bar(object):
    438 
    439       def __init__(self):
    440         pass
    441 
    442       def Foo(self, x):
    443         return x
    444 
    445     Bar().Foo(1)
    446     warn_mock.assert_called_with(
    447         'Class Bar is deprecated. It will no longer be supported on '
    448         'December 01, 2015. Please remove it or switch to an alternative '
    449         'before that time. \n',
    450         stacklevel=4)
    451 
    452   @mock.patch('warnings.warn')
    453   def testInheritedClassDeprecated(self, warn_mock):
    454 
    455     class Ba(object):
    456       pass
    457 
    458     @decorators.Deprecated(2015, 12, 1)
    459     class Bar(Ba):
    460 
    461       def Foo(self, x):
    462         return x
    463 
    464     class Baz(Bar):
    465       pass
    466 
    467     Baz().Foo(1)
    468     warn_mock.assert_called_with(
    469         'Class Bar is deprecated. It will no longer be supported on '
    470         'December 01, 2015. Please remove it or switch to an alternative '
    471         'before that time. \n',
    472         stacklevel=4)
    473 
    474   def testReturnValue(self):
    475 
    476     class Bar(object):
    477 
    478       @decorators.Deprecated(2015, 12, 1, 'Testing only.')
    479       def Foo(self, x):
    480         return x
    481 
    482     self.assertEquals(5, Bar().Foo(5))
    483