Commit 4681ccc5 authored by Rakib M. Hasan's avatar Rakib M. Hasan Committed by Commit Bot

Refactor gpu_tests unit tests by using mock.patch

Instead of setting class member functions to MagicMock classes and making
sure that they are set back to their original values at the end of a test
within a finally block, we can use a with block by using mock.patch.object
to patch member functions of a class. Also updates the sanitization of the
tags returned from GetPlatformTags. Since tags are case insensitive we can
remove the lower function call. We can also replace the chain of replace
calls with one re.sub call to change spaces and underscores to dashes.

Bug: chromium:976505
Change-Id: Ic8b5da86ef711d5698916fef40da0ed439b8ce49
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1670024
Commit-Queue: Rakib Hasan <rmhasan@google.com>
Reviewed-by: default avatarKenneth Russell <kbr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#671508}
parent 758c0efd
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
# found in the LICENSE file. # found in the LICENSE file.
import logging import logging
import re
from telemetry.testing import serially_executed_browser_test_case from telemetry.testing import serially_executed_browser_test_case
from telemetry.util import screenshot from telemetry.util import screenshot
...@@ -303,9 +304,8 @@ class GpuIntegrationTest( ...@@ -303,9 +304,8 @@ class GpuIntegrationTest(
gpu_device_tag = '%s-%s' % (gpu_vendor, gpu_device_id) gpu_device_tag = '%s-%s' % (gpu_vendor, gpu_device_id)
angle_renderer = gpu_helper.GetANGLERenderer(gpu_info) angle_renderer = gpu_helper.GetANGLERenderer(gpu_info)
cmd_decoder = gpu_helper.GetCommandDecoder(gpu_info) cmd_decoder = gpu_helper.GetCommandDecoder(gpu_info)
# all spaces in the tag will be replaced by '-', and all letters will # all spaces and underscores in the tag will be replaced by dashes
# be converted to its lower case form. tags.extend([re.sub('[ _]', '-', tag) for tag in [
tags.extend([tag.lower().replace(' ', '-').replace('_', '-') for tag in [
gpu_vendor, gpu_device_tag, angle_renderer, cmd_decoder]]) gpu_vendor, gpu_device_tag, angle_renderer, cmd_decoder]])
# If additional options have been set via '--extra-browser-args' check for # If additional options have been set via '--extra-browser-args' check for
# those which map to expectation tags. The '_browser_backend' attribute may # those which map to expectation tags. The '_browser_backend' attribute may
......
...@@ -52,34 +52,24 @@ def _GetSystemInfo( ...@@ -52,34 +52,24 @@ def _GetSystemInfo(
def _GetTagsToTest(browser, test_class=None, args=None): def _GetTagsToTest(browser, test_class=None, args=None):
gpu_tests = test_class or gpu_integration_test.GpuIntegrationTest test_class = test_class or gpu_integration_test.GpuIntegrationTest
expectations_fn = gpu_tests.ExpectationsFiles with mock.patch.object(
gpu_tests.ExpectationsFiles = mock.MagicMock(return_value=['exp.txt']) test_class, 'ExpectationsFiles', return_value=['exp.txt']):
ret = None
try:
possible_browser = fakes.FakePossibleBrowser() possible_browser = fakes.FakePossibleBrowser()
possible_browser._returned_browser = browser possible_browser._returned_browser = browser
args = args or gpu_helper.GetMockArgs() args = args or gpu_helper.GetMockArgs()
ret = set(gpu_tests.GenerateTags(args, possible_browser)) return set(test_class.GenerateTags(args, possible_browser))
finally:
gpu_tests.ExpectationsFiles = expectations_fn
return ret
def _GenerateNvidiaExampleTagsForTestClassAndArgs(test_class, args): def _GenerateNvidiaExampleTagsForTestClassAndArgs(test_class, args):
exp_files_fn = test_class.ExpectationsFiles with mock.patch.object(
ret = None test_class, 'ExpectationsFiles', return_value=['exp.txt']):
try:
test_class.ExpectationsFiles = mock.MagicMock(return_value=['exp.txt'])
_ = [_ for _ in test_class.GenerateGpuTests(args)] _ = [_ for _ in test_class.GenerateGpuTests(args)]
platform = fakes.FakePlatform('win', 'win10') platform = fakes.FakePlatform('win', 'win10')
browser = fakes.FakeBrowser(platform, 'release') browser = fakes.FakeBrowser(platform, 'release')
browser._returned_system_info = _GetSystemInfo( browser._returned_system_info = _GetSystemInfo(
gpu=VENDOR_NVIDIA, device=0x1cb3, gl_renderer='ANGLE Direct3D9') gpu=VENDOR_NVIDIA, device=0x1cb3, gl_renderer='ANGLE Direct3D9')
ret = _GetTagsToTest(browser, test_class) return _GetTagsToTest(browser, test_class)
finally:
test_class.ExpectationsFiles = exp_files_fn
return ret
class GpuIntegrationTestUnittest(unittest.TestCase): class GpuIntegrationTestUnittest(unittest.TestCase):
...@@ -195,7 +185,7 @@ class GpuIntegrationTestUnittest(unittest.TestCase): ...@@ -195,7 +185,7 @@ class GpuIntegrationTestUnittest(unittest.TestCase):
self.assertEqual( self.assertEqual(
_GetTagsToTest(browser), _GetTagsToTest(browser),
set(['mac', 'mojave', 'release', 'imagination', set(['mac', 'mojave', 'release', 'imagination',
'imagination-powervr-sgx-554', 'imagination-PowerVR-SGX-554',
'opengles', 'passthrough'])) 'opengles', 'passthrough']))
def testGenerateVendorTagUsingDeviceString(self): def testGenerateVendorTagUsingDeviceString(self):
...@@ -207,7 +197,7 @@ class GpuIntegrationTestUnittest(unittest.TestCase): ...@@ -207,7 +197,7 @@ class GpuIntegrationTestUnittest(unittest.TestCase):
self.assertEqual( self.assertEqual(
_GetTagsToTest(browser), _GetTagsToTest(browser),
set(['mac', 'mojave', 'release', 'imagination', set(['mac', 'mojave', 'release', 'imagination',
'imagination-triangle-monster-3000', 'imagination-Triangle-Monster-3000',
'no-angle', 'no-passthrough'])) 'no-angle', 'no-passthrough']))
def testSimpleIntegrationTest(self): def testSimpleIntegrationTest(self):
......
...@@ -207,16 +207,11 @@ def _TestCheckTestExpectationsAreForExistingTests(expectations): ...@@ -207,16 +207,11 @@ def _TestCheckTestExpectationsAreForExistingTests(expectations):
expectations_file.write(expectations) expectations_file.write(expectations)
expectations_file.close() expectations_file.close()
gpu_tests = gpu_integration_test.GpuIntegrationTest gpu_tests = gpu_integration_test.GpuIntegrationTest
generate_gpu_fn = gpu_tests.GenerateGpuTests with mock.patch.object(
expectations_files_fn = gpu_tests.ExpectationsFiles gpu_tests, 'GenerateGpuTests', return_value=[('a/b/c', ())]):
gpu_tests.GenerateGpuTests = mock.MagicMock(return_value=[('a/b/c', ())]) with mock.patch.object(
gpu_tests.ExpectationsFiles = mock.MagicMock( gpu_tests, 'ExpectationsFiles', return_value=[expectations_file.name]):
return_value=[expectations_file.name])
try:
CheckTestExpectationsAreForExistingTests(gpu_tests, options) CheckTestExpectationsAreForExistingTests(gpu_tests, options)
finally:
gpu_tests.GenerateGpuTests = generate_gpu_fn
gpu_tests.ExpectationsFiles = expectations_files_fn
def _CheckPatternIsValid(pattern): def _CheckPatternIsValid(pattern):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment