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 def SetEnabledStrings(self, enabled_strings): 16 # pylint: disable=attribute-defined-outside-init 17 self._enabled_strings = enabled_strings 18 19 def SetDisabledStrings(self, disabled_strings): 20 # pylint: disable=attribute-defined-outside-init 21 self._disabled_strings = disabled_strings 22 23 24 class TestDisableDecorators(unittest.TestCase): 25 26 def testDisabledStringOnFunction(self): 27 @decorators.Disabled('bar') 28 def Sum(): 29 return 1 + 1 30 self.assertEquals({'bar'}, Sum._disabled_strings) 31 32 @decorators.Disabled('bar') 33 @decorators.Disabled('baz') 34 @decorators.Disabled('bart', 'baz') 35 def Product(): 36 return 1 * 1 37 self.assertEquals({'bar', 'bart', 'baz'}, Product._disabled_strings) 38 39 def testDisabledStringOnClass(self): 40 @decorators.Disabled('windshield') 41 class Ford(object): 42 pass 43 self.assertEquals({'windshield'}, Ford._disabled_strings) 44 45 @decorators.Disabled('windows', 'Drive') 46 @decorators.Disabled('wheel') 47 @decorators.Disabled('windows') 48 class Honda(object): 49 pass 50 self.assertEquals({'wheel', 'Drive', 'windows'}, Honda._disabled_strings) 51 52 def testDisabledStringOnMethod(self): 53 class Ford(object): 54 @decorators.Disabled('windshield') 55 def Drive(self): 56 pass 57 self.assertEquals({'windshield'}, Ford().Drive._disabled_strings) 58 59 class Honda(object): 60 @decorators.Disabled('windows', 'Drive') 61 @decorators.Disabled('wheel') 62 @decorators.Disabled('windows') 63 def Drive(self): 64 pass 65 self.assertEquals({'wheel', 'Drive', 'windows'}, 66 Honda().Drive._disabled_strings) 67 68 class TestEnableDecorators(unittest.TestCase): 69 70 def testEnabledStringOnFunction(self): 71 @decorators.Enabled('minus', 'power') 72 def Sum(): 73 return 1 + 1 74 self.assertEquals({'minus', 'power'}, Sum._enabled_strings) 75 76 @decorators.Enabled('dot') 77 @decorators.Enabled('product') 78 @decorators.Enabled('product', 'dot') 79 def Product(): 80 return 1 * 1 81 self.assertEquals({'dot', 'product'}, Product._enabled_strings) 82 83 def testEnabledStringOnClass(self): 84 @decorators.Enabled('windshield', 'light') 85 class Ford(object): 86 pass 87 self.assertEquals({'windshield', 'light'}, Ford._enabled_strings) 88 89 @decorators.Enabled('wheel', 'Drive') 90 @decorators.Enabled('wheel') 91 @decorators.Enabled('windows') 92 class Honda(object): 93 pass 94 self.assertEquals({'wheel', 'Drive', 'windows'}, Honda._enabled_strings) 95 96 def testEnabledStringOnMethod(self): 97 class Ford(object): 98 @decorators.Enabled('windshield') 99 def Drive(self): 100 pass 101 self.assertEquals({'windshield'}, Ford().Drive._enabled_strings) 102 103 class Honda(object): 104 @decorators.Enabled('windows', 'Drive') 105 @decorators.Enabled('wheel', 'Drive') 106 @decorators.Enabled('windows') 107 def Drive(self): 108 pass 109 self.assertEquals({'wheel', 'Drive', 'windows'}, 110 Honda().Drive._enabled_strings) 111 112 113 class TestShouldSkip(unittest.TestCase): 114 115 def setUp(self): 116 fake_platform = mock.Mock(spec_set=platform.Platform) 117 fake_platform.GetOSName.return_value = 'os_name' 118 fake_platform.GetOSVersionName.return_value = 'os_version_name' 119 120 self.possible_browser = mock.Mock(spec_set=possible_browser.PossibleBrowser) 121 self.possible_browser.browser_type = 'browser_type' 122 self.possible_browser.platform = fake_platform 123 self.possible_browser.supports_tab_control = False 124 125 def testEnabledStrings(self): 126 test = FakeTest() 127 128 # When no enabled_strings is given, everything should be enabled. 129 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 130 131 test.SetEnabledStrings(['os_name']) 132 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 133 134 test.SetEnabledStrings(['another_os_name']) 135 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 136 137 test.SetEnabledStrings(['os_version_name']) 138 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 139 140 test.SetEnabledStrings(['os_name', 'another_os_name']) 141 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 142 143 test.SetEnabledStrings(['another_os_name', 'os_name']) 144 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 145 146 test.SetEnabledStrings(['another_os_name', 'another_os_version_name']) 147 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 148 149 test.SetEnabledStrings(['os_name-reference']) 150 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 151 152 test.SetEnabledStrings(['another_os_name-reference']) 153 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 154 155 test.SetEnabledStrings(['os_version_name-reference']) 156 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 157 158 test.SetEnabledStrings(['os_name-reference', 'another_os_name-reference']) 159 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 160 161 test.SetEnabledStrings(['another_os_name-reference', 'os_name-reference']) 162 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 163 164 test.SetEnabledStrings(['another_os_name-reference', 165 'another_os_version_name-reference']) 166 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 167 168 def testDisabledStrings(self): 169 test = FakeTest() 170 171 # When no disabled_strings is given, nothing should be disabled. 172 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 173 174 test.SetDisabledStrings(['os_name']) 175 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 176 177 test.SetDisabledStrings(['another_os_name']) 178 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 179 180 test.SetDisabledStrings(['os_version_name']) 181 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 182 183 test.SetDisabledStrings(['os_name', 'another_os_name']) 184 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 185 186 test.SetDisabledStrings(['another_os_name', 'os_name']) 187 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 188 189 test.SetDisabledStrings(['another_os_name', 'another_os_version_name']) 190 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 191 192 test.SetDisabledStrings(['reference']) 193 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 194 195 test.SetDisabledStrings(['os_name-reference']) 196 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 197 198 test.SetDisabledStrings(['another_os_name-reference']) 199 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 200 201 test.SetDisabledStrings(['os_version_name-reference']) 202 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 203 204 test.SetDisabledStrings(['os_name-reference', 'another_os_name-reference']) 205 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 206 207 test.SetDisabledStrings(['another_os_name-reference', 'os_name-reference']) 208 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 209 210 test.SetDisabledStrings(['another_os_name-reference', 211 'another_os_version_name-reference']) 212 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 213 214 def testReferenceEnabledStrings(self): 215 self.possible_browser.browser_type = 'reference' 216 test = FakeTest() 217 218 # When no enabled_strings is given, everything should be enabled. 219 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 220 221 test.SetEnabledStrings(['os_name-reference']) 222 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 223 224 test.SetEnabledStrings(['another_os_name-reference']) 225 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 226 227 test.SetEnabledStrings(['os_version_name-reference']) 228 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 229 230 test.SetEnabledStrings(['os_name-reference', 'another_os_name-reference']) 231 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 232 233 test.SetEnabledStrings(['another_os_name-reference', 'os_name-reference']) 234 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 235 236 test.SetEnabledStrings(['another_os_name-reference', 237 'another_os_version_name-reference']) 238 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 239 240 def testReferenceDisabledStrings(self): 241 self.possible_browser.browser_type = 'reference' 242 test = FakeTest() 243 244 # When no disabled_strings is given, nothing should be disabled. 245 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 246 247 test.SetDisabledStrings(['reference']) 248 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 249 250 test.SetDisabledStrings(['os_name-reference']) 251 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 252 253 test.SetDisabledStrings(['another_os_name-reference']) 254 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 255 256 test.SetDisabledStrings(['os_version_name-reference']) 257 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 258 259 test.SetDisabledStrings(['os_name-reference', 'another_os_name-reference']) 260 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 261 262 test.SetDisabledStrings(['another_os_name-reference', 'os_name-reference']) 263 self.assertTrue(decorators.ShouldSkip(test, self.possible_browser)[0]) 264 265 test.SetDisabledStrings(['another_os_name-reference', 266 'another_os_version_name-reference']) 267 self.assertFalse(decorators.ShouldSkip(test, self.possible_browser)[0]) 268 269 class TestDeprecation(unittest.TestCase): 270 271 @mock.patch('warnings.warn') 272 def testFunctionDeprecation(self, warn_mock): 273 @decorators.Deprecated(2015, 12, 1) 274 def Foo(x): 275 return x 276 Foo(1) 277 warn_mock.assert_called_with( 278 'Function Foo is deprecated. It will no longer be supported on ' 279 'December 01, 2015. Please remove it or switch to an alternative ' 280 'before that time. \n', stacklevel=4) 281 282 @mock.patch('warnings.warn') 283 def testMethodDeprecated(self, warn_mock): 284 285 class Bar(object): 286 @decorators.Deprecated(2015, 12, 1, 'Testing only.') 287 def Foo(self, x): 288 return x 289 290 Bar().Foo(1) 291 warn_mock.assert_called_with( 292 'Function Foo is deprecated. It will no longer be supported on ' 293 'December 01, 2015. Please remove it or switch to an alternative ' 294 'before that time. Testing only.\n', stacklevel=4) 295 296 @mock.patch('warnings.warn') 297 def testClassWithoutInitDefinedDeprecated(self, warn_mock): 298 @decorators.Deprecated(2015, 12, 1) 299 class Bar(object): 300 def Foo(self, x): 301 return x 302 303 Bar().Foo(1) 304 warn_mock.assert_called_with( 305 'Class Bar is deprecated. It will no longer be supported on ' 306 'December 01, 2015. Please remove it or switch to an alternative ' 307 'before that time. \n', stacklevel=4) 308 309 @mock.patch('warnings.warn') 310 def testClassWithInitDefinedDeprecated(self, warn_mock): 311 312 @decorators.Deprecated(2015, 12, 1) 313 class Bar(object): 314 def __init__(self): 315 pass 316 def Foo(self, x): 317 return x 318 319 Bar().Foo(1) 320 warn_mock.assert_called_with( 321 'Class Bar is deprecated. It will no longer be supported on ' 322 'December 01, 2015. Please remove it or switch to an alternative ' 323 'before that time. \n', stacklevel=4) 324 325 @mock.patch('warnings.warn') 326 def testInheritedClassDeprecated(self, warn_mock): 327 class Ba(object): 328 pass 329 330 @decorators.Deprecated(2015, 12, 1) 331 class Bar(Ba): 332 def Foo(self, x): 333 return x 334 335 class Baz(Bar): 336 pass 337 338 Baz().Foo(1) 339 warn_mock.assert_called_with( 340 'Class Bar is deprecated. It will no longer be supported on ' 341 'December 01, 2015. Please remove it or switch to an alternative ' 342 'before that time. \n', stacklevel=4) 343 344 def testReturnValue(self): 345 class Bar(object): 346 @decorators.Deprecated(2015, 12, 1, 'Testing only.') 347 def Foo(self, x): 348 return x 349 350 self.assertEquals(5, Bar().Foo(5)) 351