1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
  413
  414
  415
  416
  417
  418
  419
  420
  421
  422
  423
  424
  425
  426
  427
  428
  429
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439
  440
  441
  442
  443
  444
  445
  446
  447
  448
  449
  450
  451
  452
  453
  454
  455
  456
  457
  458
  459
  460
  461
  462
  463
  464
  465
  466
  467
  468
  469
  470
  471
  472
  473
  474
  475
  476
  477
  478
  479
  480
  481
  482
  483
  484
  485
  486
  487
  488
  489
  490
  491
  492
  493
  494
  495
  496
  497
  498
  499
  500
  501
  502
  503
  504
  505
  506
  507
  508
  509
  510
  511
  512
  513
  514
  515
  516
  517
  518
  519
  520
  521
  522
  523
  524
  525
  526
  527
  528
  529
  530
  531
  532
  533
  534
  535
  536
  537
  538
  539
  540
  541
  542
  543
  544
  545
  546
  547
  548
  549
  550
  551
  552
  553
  554
  555
  556
  557
  558
  559
  560
  561
  562
  563
  564
  565
  566
  567
  568
  569
  570
  571
  572
  573
  574
  575
  576
  577
  578
  579
  580
  581
  582
  583
  584
  585
  586
  587
  588
  589
  590
  591
  592
  593
  594
  595
  596
  597
  598
  599
  600
  601
  602
  603
  604
  605
  606
  607
  608
  609
  610
  611
  612
  613
  614
  615
  616
  617
  618
  619
  620
  621
  622
  623
  624
  625
  626
  627
  628
  629
  630
  631
  632
  633
  634
  635
  636
  637
  638
  639
  640
  641
  642
  643
  644
  645
  646
  647
  648
  649
  650
  651
  652
  653
  654
  655
  656
  657
  658
  659
  660
  661
  662
  663
  664
  665
  666
  667
  668
  669
  670
  671
  672
  673
  674
  675
  676
  677
  678
  679
  680
  681
  682
  683
  684
  685
  686
  687
  688
  689
  690
  691
  692
  693
  694
  695
  696
  697
  698
  699
  700
  701
  702
  703
  704
  705

content / test / gpu / gpu_tests / gpu_process_integration_test.py [blame]

# Copyright 2017 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import logging
import os
import sys
import time
from typing import Any, List, Optional, Tuple
import unittest

from devil.android.sdk import version_codes

from gpu_tests import common_browser_args as cba
from gpu_tests import common_typing as ct
from gpu_tests import gpu_integration_test
from gpu_tests.util import host_information

import gpu_path_util

_GPU_PAGE_TIMEOUT = 30

data_path = os.path.join(gpu_path_util.CHROMIUM_SRC_DIR, 'content', 'test',
                         'data')

test_harness_script = r"""
  var domAutomationController = {};
  domAutomationController._finished = false;
  domAutomationController._succeeded = false;
  domAutomationController.send = function(msg) {
    domAutomationController._finished = true;
    if (msg.toLowerCase() == "success") {
      domAutomationController._succeeded = true;
    } else {
      domAutomationController._succeeded = false;
    }
  }

  window.domAutomationController = domAutomationController;

  function GetDriverBugWorkarounds() {
    return getGPUInfo('workarounds');
  };
"""


def _GetBrowserBridgeProperty(tab: ct.Tab, path: str) -> dict:
  """The GPU WebUI uses JS modules and may not have initialized the global
    browserBridge object by the time we can start injecting JavaScript. This
    ensures we don't have that problem."""
  tab.WaitForJavaScriptCondition('window.gpuPagePopulated',
                                 timeout=_GPU_PAGE_TIMEOUT)
  return tab.EvaluateJavaScript('browserBridge.' + path)


class GpuProcessIntegrationTest(gpu_integration_test.GpuIntegrationTest):
  @classmethod
  def Name(cls) -> str:
    """The name by which this test is invoked on the command line."""
    return 'gpu_process'

  @classmethod
  def SetUpProcess(cls) -> None:
    super(GpuProcessIntegrationTest, cls).SetUpProcess()
    cls.CustomizeBrowserArgs([])
    cls.StartBrowser()
    cls.SetStaticServerDirs([data_path])

  @classmethod
  def GenerateBrowserArgs(cls, additional_args: List[str]) -> List[str]:
    """Adds default arguments to |additional_args|.

    See the parent class' method documentation for additional information.
    """
    default_args = super(GpuProcessIntegrationTest,
                         cls).GenerateBrowserArgs(additional_args)
    default_args.extend([
        cba.ENABLE_GPU_BENCHMARKING,
        # TODO(kbr): figure out why the following option seems to be
        # needed on Android for robustness.
        # https://github.com/catapult-project/catapult/issues/3122
        '--no-first-run',
        # Disable:
        #   Do you want the application "Chromium Helper.app" to accept incoming
        #   network connections?
        # dialogs on macOS. crbug.com/969559
        cba.DISABLE_DEVICE_DISCOVERY_NOTIFICATIONS,
    ])
    return default_args

  @classmethod
  def GenerateGpuTests(cls, options: ct.ParsedCmdArgs) -> ct.TestGenerator:
    # The browser test runner synthesizes methods with the exact name
    # given in GenerateGpuTests, so in order to hand-write our tests but
    # also go through the _RunGpuTest trampoline, the test needs to be
    # slightly differently named.

    # Also note that since functional_video.html refers to files in
    # ../media/ , the serving dir must be the common parent directory.
    tests = (
        ('GpuProcess_canvas2d', 'gpu/functional_canvas_demo.html'),
        ('GpuProcess_css3d', 'gpu/functional_3d_css.html'),
        ('GpuProcess_webgl', 'gpu/functional_webgl.html'),
        ('GpuProcess_video', 'gpu/functional_video.html'),
        ('GpuProcess_gpu_info_complete', 'gpu/functional_3d_css.html'),
        ('GpuProcess_driver_bug_workarounds_in_gpu_process', 'chrome:gpu'),
        ('GpuProcess_readback_webgl_gpu_process', 'chrome:gpu'),
        ('GpuProcess_feature_status_under_swiftshader', 'chrome:gpu'),
        ('GpuProcess_one_extra_workaround', 'chrome:gpu'),
        ('GpuProcess_disable_gpu', 'gpu/functional_webgl.html'),
        ('GpuProcess_disable_gpu_and_swiftshader', 'gpu/functional_webgl.html'),
        ('GpuProcess_disable_swiftshader', 'gpu/functional_webgl.html'),
        ('GpuProcess_disabling_workarounds_works', 'chrome:gpu'),
        ('GpuProcess_mac_webgl_backgrounded_high_performance',
         'gpu/functional_blank.html'),
        ('GpuProcess_mac_webgl_high_performance',
         'gpu/functional_webgl_high_performance.html'),
        ('GpuProcess_mac_webgl_low_power',
         'gpu/functional_webgl_low_power.html'),
        ('GpuProcess_mac_webgl_terminated_high_performance',
         'gpu/functional_blank.html'),
        ('GpuProcess_swiftshader_for_webgl', 'gpu/functional_webgl.html'),
        ('GpuProcess_no_swiftshader_for_webgl_without_flags',
         'gpu/functional_webgl.html'),
        ('GpuProcess_webgl_disabled_extension',
         'gpu/functional_webgl_disabled_extension.html'),
        ('GpuProcess_webgpu_iframe_removed', 'gpu/webgpu-iframe-removed.html'),
        ('GpuProcess_visibility', 'about:blank'),
    )

    for t in tests:
      yield (t[0], t[1], ['_' + t[0]])

  def RunActualGpuTest(self, test_path: str, args: ct.TestArgs) -> None:
    test_name = args[0]
    getattr(self, test_name)(test_path)

  ######################################
  # Helper functions for the tests below

  def _Navigate(self, test_path: str) -> None:
    url = self.UrlOfStaticFilePath(test_path)
    # It's crucial to use the action_runner, rather than the tab's
    # Navigate method directly. It waits for the document ready state
    # to become interactive or better, avoiding critical race
    # conditions.
    self.tab.action_runner.Navigate(
        url, script_to_evaluate_on_commit=test_harness_script)

  def _WaitForTestCompletion(self, tab: ct.Tab) -> None:
    tab.action_runner.WaitForJavaScriptCondition(
        'window.domAutomationController._finished', timeout=10)
    if not tab.EvaluateJavaScript('window.domAutomationController._succeeded'):
      self.fail('Test reported that it failed')

  def _NavigateAndWait(self, test_path: str) -> None:
    self._Navigate(test_path)
    self._WaitForTestCompletion(self.tab)

  def _VerifyGpuProcessPresent(self) -> None:
    tab = self.tab
    if not tab.EvaluateJavaScript('chrome.gpuBenchmarking.hasGpuChannel()'):
      self.fail('No GPU channel detected')

  def _ValidateDriverBugWorkaroundsImpl(self, is_expected: bool,
                                        workaround_name: str) -> None:
    tab = self.tab
    gpu_driver_bug_workarounds = tab.EvaluateJavaScript(
        'chrome.gpuBenchmarking.getGpuDriverBugWorkarounds()')

    is_present = workaround_name in gpu_driver_bug_workarounds
    failure = False
    if is_expected and not is_present:
      failure = True
      error_message = 'is missing'
    elif not is_expected and is_present:
      failure = True
      error_message = 'is not expected'

    if failure:
      print('Test failed. Printing page contents:')
      print(tab.EvaluateJavaScript('document.body.innerHTML'))
      self.fail('%s %s workarounds: %s' % (workaround_name, error_message,
                                           gpu_driver_bug_workarounds))

  def _ValidateDriverBugWorkarounds(self, expected_workaround: Optional[str],
                                    unexpected_workaround: Optional[str]
                                    ) -> None:
    if not expected_workaround and not unexpected_workaround:
      return
    if expected_workaround:
      self._ValidateDriverBugWorkaroundsImpl(True, expected_workaround)
    if unexpected_workaround:
      self._ValidateDriverBugWorkaroundsImpl(False, unexpected_workaround)

  @staticmethod
  def _Filterer(workaround: str) -> bool:
    # Filter all entries starting with "disabled_extension_" and
    # "disabled_webgl_extension_", as these are synthetic entries
    # added to make it easier to read the logs.
    banned_prefixes = ['disabled_extension_', 'disabled_webgl_extension_']
    for p in banned_prefixes:
      if workaround.startswith(p):
        return False
    return True

  def _CompareAndCaptureDriverBugWorkarounds(
      self) -> Tuple[List[str], Optional[str]]:
    tab = self.tab
    if not tab.EvaluateJavaScript('chrome.gpuBenchmarking.hasGpuProcess()'):
      self.fail('No GPU process detected')

    if not tab.EvaluateJavaScript('chrome.gpuBenchmarking.hasGpuChannel()'):
      self.fail('No GPU channel detected')

    browser_list = [
        x for x in tab.EvaluateJavaScript('GetDriverBugWorkarounds()')
        if self._Filterer(x)
    ]
    gpu_list = [
        x for x in tab.EvaluateJavaScript(
            'chrome.gpuBenchmarking.getGpuDriverBugWorkarounds()')
        if self._Filterer(x)
    ]

    diff = set(browser_list).symmetric_difference(set(gpu_list))
    if len(diff) > 0:
      self.fail('Browser and GPU process list of driver bug'
                'workarounds are not equal: %s != %s, diff: %s' %
                (browser_list, gpu_list, list(diff)))

    basic_infos = _GetBrowserBridgeProperty(tab, 'gpuInfo.basicInfo')
    disabled_gl_extensions = None
    for info in basic_infos:
      if info['description'].startswith('Disabled Extensions'):
        disabled_gl_extensions = info['value']
        break

    return gpu_list, disabled_gl_extensions

  ######################################
  # The actual tests

  def _GpuProcess_canvas2d(self, test_path: str) -> None:
    self.RestartBrowserIfNecessaryWithArgs([])
    self._NavigateAndWait(test_path)
    self._VerifyGpuProcessPresent()

  def _GpuProcess_css3d(self, test_path: str) -> None:
    self.RestartBrowserIfNecessaryWithArgs([])
    self._NavigateAndWait(test_path)
    self._VerifyGpuProcessPresent()

  def _GpuProcess_webgl(self, test_path: str) -> None:
    self.RestartBrowserIfNecessaryWithArgs([])
    self._NavigateAndWait(test_path)
    self._VerifyGpuProcessPresent()

  def _GpuProcess_video(self, test_path: str) -> None:
    self.RestartBrowserIfNecessaryWithArgs([])
    self._NavigateAndWait(test_path)
    self._VerifyGpuProcessPresent()

  def _GpuProcess_gpu_info_complete(self, test_path: str) -> None:
    # Regression test for crbug.com/454906
    self.RestartBrowserIfNecessaryWithArgs([])
    self._NavigateAndWait(test_path)
    tab = self.tab
    system_info = tab.browser.GetSystemInfo()
    if not system_info:
      self.fail('Browser must support system info')
    if not system_info.gpu:
      self.fail('Target machine must have a GPU')
    if not system_info.gpu.aux_attributes:
      self.fail('Browser must support GPU aux attributes')
    if not 'gl_renderer' in system_info.gpu.aux_attributes:
      self.fail('Browser must have gl_renderer in aux attribs')
    if (not host_information.IsMac()
        and len(system_info.gpu.aux_attributes['gl_renderer']) <= 0):
      # On MacOSX we don't create a context to collect GL strings.1
      self.fail('Must have a non-empty gl_renderer string')

  def _GpuProcess_driver_bug_workarounds_in_gpu_process(self,
                                                        test_path: str) -> None:
    self.RestartBrowserIfNecessaryWithArgs(
        ['--use_gpu_driver_workaround_for_testing'])
    self._Navigate(test_path)
    self.tab.WaitForJavaScriptCondition('window.gpuPagePopulated',
                                        timeout=_GPU_PAGE_TIMEOUT)
    self._ValidateDriverBugWorkarounds('use_gpu_driver_workaround_for_testing',
                                       None)

  def _GpuProcess_readback_webgl_gpu_process(self, test_path: str) -> None:
    # Hit test group 1 with entry 152 from kSoftwareRenderingListEntries.
    self.RestartBrowserIfNecessaryWithArgs([
        '--gpu-blocklist-test-group=1',
        cba.DISABLE_GPU_COMPOSITING,
    ])
    self._Navigate(test_path)
    feature_status_list = _GetBrowserBridgeProperty(
        self.tab, 'gpuInfo.featureStatus.featureStatus')
    result = True
    for name, status in feature_status_list.items():
      if name == 'webgl':
        result = result and status == 'enabled_readback'
      elif name == 'webgl2':
        result = result and status == 'unavailable_off'
      else:
        pass
    if not result:
      self.fail('WebGL readback setup failed: %s' % feature_status_list)

  def _GpuProcess_feature_status_under_swiftshader(self,
                                                   test_path: str) -> None:
    # Hit test group 2 with entry 153 from kSoftwareRenderingListEntries.
    self.RestartBrowserIfNecessaryWithArgs(
        ['--gpu-blocklist-test-group=2', '--enable-unsafe-swiftshader'])
    self._Navigate(test_path)
    feature_status_list = _GetBrowserBridgeProperty(
        self.tab, 'gpuInfo.featureStatus.featureStatus')
    for name, status in feature_status_list.items():
      if name == 'webgl' and status != 'unavailable_software':
        self.fail('WebGL status for SwiftShader failed: %s' % status)
      elif name == '2d_canvas' and status != 'unavailable_software':
        self.fail('2D Canvas status for SwiftShader failed: %s' % status)

    # On Linux we relaunch GPU process to fallback to SwiftShader, therefore
    # featureStatusForHardwareGpu isn't available. So finish early if we're on
    # Linux.
    if host_information.IsLinux():
      return

    feature_status_for_hardware_gpu_list = _GetBrowserBridgeProperty(
        self.tab, 'gpuInfo.featureStatusForHardwareGpu.featureStatus')
    for name, status in feature_status_for_hardware_gpu_list.items():
      if name == 'webgl' and status != 'unavailable_off':
        self.fail('WebGL status for hardware GPU failed: %s' % status)
      elif name == '2d_canvas' and status != 'enabled':
        self.fail('2D Canvas status for hardware GPU failed: %s' % status)

  def _GpuProcess_one_extra_workaround(self, test_path: str) -> None:
    # Start this test by launching the browser with no command line
    # arguments.
    self.RestartBrowserIfNecessaryWithArgs([])
    self._Navigate(test_path)
    self._VerifyGpuProcessPresent()
    self.tab.WaitForJavaScriptCondition('window.gpuPagePopulated',
                                        timeout=_GPU_PAGE_TIMEOUT)
    recorded_workarounds, recorded_disabled_gl_extensions = (
        self._CompareAndCaptureDriverBugWorkarounds())
    # Relaunch the browser enabling test group 1 with entry 215, where
    # use_gpu_driver_workaround_for_testing is enabled.
    additional_args = ['--gpu-driver-bug-list-test-group=1']
    # Add the testing workaround to the recorded workarounds.
    recorded_workarounds.append('use_gpu_driver_workaround_for_testing')
    additional_args.append('--disable-gl-extensions=' +
                           recorded_disabled_gl_extensions)
    self.RestartBrowserIfNecessaryWithArgs(additional_args)
    self._Navigate(test_path)
    self._VerifyGpuProcessPresent()
    self.tab.WaitForJavaScriptCondition('window.gpuPagePopulated',
                                        timeout=_GPU_PAGE_TIMEOUT)
    new_workarounds, new_disabled_gl_extensions = (
        self._CompareAndCaptureDriverBugWorkarounds())
    diff = set(recorded_workarounds).symmetric_difference(new_workarounds)
    tab = self.tab
    if len(diff) > 0:
      print('Test failed. Printing page contents:')
      print(tab.EvaluateJavaScript('document.body.innerHTML'))
      self.fail('GPU process and expected list of driver bug '
                'workarounds are not equal: %s != %s, diff: %s' %
                (recorded_workarounds, new_workarounds, list(diff)))
    if recorded_disabled_gl_extensions != new_disabled_gl_extensions:
      print('Test failed. Printing page contents:')
      print(tab.EvaluateJavaScript('document.body.innerHTML'))
      self.fail('The expected disabled gl extensions are '
                'incorrect: %s != %s:' % (recorded_disabled_gl_extensions,
                                          new_disabled_gl_extensions))

  def _GpuProcess_disable_gpu(self, test_path: str) -> None:
    # This test loads functional_webgl.html so that there is a
    # deliberate attempt to use an API which would start the GPU
    # process.
    self.RestartBrowserIfNecessaryWithArgs([cba.DISABLE_GPU])
    self._NavigateAndWait(test_path)
    has_gpu_process = self.tab.EvaluateJavaScript(
        'chrome.gpuBenchmarking.hasGpuProcess()')
    if not has_gpu_process:
      self.fail('GPU process not detected')

  def _GpuProcess_visibility(self, test_path: str) -> None:
    os_name = self.browser.platform.GetOSName()
    if os_name != 'android':
      logging.info('Skipping test because not running on Android')
      return

    # pylint: disable=protected-access
    sdk_version = \
        self.browser.platform._platform_backend.device.build_version_sdk
    # pylint: enable=protected-access
    if sdk_version < version_codes.PIE:
      logging.info('Skipping test because not running on Android P+')
      return

    has_gpu_process = self.tab.EvaluateJavaScript(
        'chrome.gpuBenchmarking.hasGpuProcess()')
    if not has_gpu_process:
      logging.info('Skipping test because no out-of-process GPU service')
      return

    self.RestartBrowserIfNecessaryWithArgs([])
    self._Navigate(test_path)
    system_info = self.browser.GetSystemInfo()
    callback_count = system_info.gpu.aux_attributes[
        'visibility_callback_call_count']
    # initial callback count should be 1 since the app became visible
    if callback_count != 1:
      self.fail('Visibility callback call count expected 1, got %d' %
                callback_count)

    self.browser.platform.android_action_runner.TurnScreenOff()
    self.tab.WaitForJavaScriptCondition('document.visibilityState == "hidden"',
                                        timeout=_GPU_PAGE_TIMEOUT)
    system_info = self.browser.GetSystemInfo()
    callback_count = system_info.gpu.aux_attributes[
        'visibility_callback_call_count']
    if callback_count != 2:
      self.fail('Visibility callback call count expected 2, got %d' %
                callback_count)

    self.browser.platform.android_action_runner.TurnScreenOn()
    self.tab.WaitForJavaScriptCondition('document.visibilityState == "visible"',
                                        timeout=_GPU_PAGE_TIMEOUT)
    system_info = self.browser.GetSystemInfo()
    callback_count = system_info.gpu.aux_attributes[
        'visibility_callback_call_count']
    if callback_count != 3:
      self.fail('Visibility callback call count expected 3, got %d' %
                callback_count)

  def _GpuProcess_disable_gpu_and_swiftshader(self, test_path: str) -> None:
    # Disable SwiftShader, GPU process should launch for display compositing.
    self.RestartBrowserIfNecessaryWithArgs(
        [cba.DISABLE_GPU, cba.DISABLE_SOFTWARE_RASTERIZER])
    self._NavigateAndWait(test_path)
    has_gpu_process = self.tab.EvaluateJavaScript(
        'chrome.gpuBenchmarking.hasGpuProcess()')
    if not has_gpu_process:
      self.fail('GPU process not detected')

  def _GpuProcess_disable_swiftshader(self, test_path: str) -> None:
    # Disable SwiftShader, GPU process should be able to launch.
    self.RestartBrowserIfNecessaryWithArgs([cba.DISABLE_SOFTWARE_RASTERIZER])
    self._NavigateAndWait(test_path)
    has_gpu_process = self.tab.EvaluateJavaScript(
        'chrome.gpuBenchmarking.hasGpuProcess()')
    if not has_gpu_process:
      self.fail('GPU process not detected')

  def _GpuProcess_disabling_workarounds_works(self, test_path):
    # Hit exception from id 215 from kGpuDriverBugListEntries.
    self.RestartBrowserIfNecessaryWithArgs([
        '--gpu-driver-bug-list-test-group=1',
        '--use_gpu_driver_workaround_for_testing=0'
    ])
    self._Navigate(test_path)
    self.tab.WaitForJavaScriptCondition('window.gpuPagePopulated',
                                        timeout=_GPU_PAGE_TIMEOUT)
    workarounds, _ = (self._CompareAndCaptureDriverBugWorkarounds())
    if 'use_gpu_driver_workaround_for_testing' in workarounds:
      self.fail('use_gpu_driver_workaround_for_testing erroneously present')

  def _GpuProcess_swiftshader_for_webgl(self, test_path: str) -> None:
    # This test loads functional_webgl.html so that there is a deliberate
    # attempt to use an API which would start the GPU process.
    args_list = (
        # Triggering test_group 2 where WebGL is blocklisted.
        ['--gpu-blocklist-test-group=2', '--enable-unsafe-swiftshader'],
        # Explicitly disable GPU access.
        [cba.DISABLE_GPU, '--enable-unsafe-swiftshader'])
    for args in args_list:
      self.RestartBrowserIfNecessaryWithArgs(args)
      self._NavigateAndWait(test_path)
      # Validate the WebGL unmasked renderer string.
      renderer = self.tab.EvaluateJavaScript('gl_renderer')
      if not renderer:
        self.fail('getParameter(UNMASKED_RENDERER_WEBGL) was null')
      if 'SwiftShader' not in renderer:
        self.fail('Expected SwiftShader renderer; instead got ' + renderer)
      # Validate GPU info.
      system_info = self.browser.GetSystemInfo()
      if not system_info:
        self.fail("Browser doesn't support GetSystemInfo")
      gpu = system_info.gpu
      if not gpu:
        self.fail('Target machine must have a GPU')
      if not gpu.aux_attributes:
        self.fail('Browser must support GPU aux attributes')
      if 'SwiftShader' not in gpu.aux_attributes['gl_renderer']:
        self.fail('Expected "SwiftShader" in GPU info GL renderer string')
      if 'Google' not in gpu.aux_attributes['gl_vendor']:
        self.fail('Expected "Google" in GPU info GL vendor string')
      device = gpu.devices[0]
      if not device:
        self.fail("System Info doesn't have a device")
      # Validate extensions.
      ext_list = [
          'ANGLE_instanced_arrays',
          'EXT_blend_minmax',
          'EXT_texture_filter_anisotropic',
          'OES_element_index_uint',
          'OES_standard_derivatives',
          'OES_texture_float',
          'OES_texture_float_linear',
          'OES_texture_half_float',
          'OES_texture_half_float_linear',
          'OES_vertex_array_object',
          'WEBGL_compressed_texture_etc1',
          'WEBGL_debug_renderer_info',
          'WEBGL_depth_texture',
          'WEBGL_draw_buffers',
          'WEBGL_lose_context',
      ]
      tab = self.tab
      for ext in ext_list:
        if tab.EvaluateJavaScript('!gl_context.getExtension("' + ext + '")'):
          self.fail('Expected %s support' % ext)

  def _GpuProcess_no_swiftshader_for_webgl_without_flags(
      self, test_path: str) -> None:
    # This test loads functional_webgl.html with GPU disabled and verifies that
    # SwiftShader is not available without the --enable-unsafe-swiftshader flag
    # or AllowSwiftShaderFallback killswitch.
    # Because AllowSwiftShaderFallback is currently enabled by default, disable
    # it to test the upcoming default behavior
    disable_hardware_webgl_args_list = [
        # Triggering test_group 2 where WebGL is blocklisted.
        ['--gpu-blocklist-test-group=2'],
        # Explicitly disable GPU access.
        [cba.DISABLE_GPU],
    ]
    disable_swiftshader_fallback_feature = [
        '--disable-features=AllowSwiftShaderFallback'
    ]
    allow_swiftshader_args_list = [
        ['--enable-unsafe-swiftshader'],
        ['--use-gl=angle', '--use-angle=swiftshader'],
        ['--enable-features=AllowSwiftShaderFallback']
    ]
    for disable_hardware_webgl_args in disable_hardware_webgl_args_list:
      self.RestartBrowserIfNecessaryWithArgs(
          disable_hardware_webgl_args + disable_swiftshader_fallback_feature)
      self._NavigateAndWait(test_path)

      renderer = self.tab.EvaluateJavaScript('gl_renderer')
      if renderer:
        self.fail('Expected no WebGL renderer; instead got ' + renderer)

      for allow_swiftshader_args in allow_swiftshader_args_list:
        self.RestartBrowserIfNecessaryWithArgs(disable_hardware_webgl_args +
                                               allow_swiftshader_args)
        self._NavigateAndWait(test_path)

        # Validate the WebGL unmasked renderer string.
        renderer = self.tab.EvaluateJavaScript('gl_renderer')
        if not renderer:
          self.fail('getParameter(UNMASKED_RENDERER_WEBGL) was null')
        if 'SwiftShader' not in renderer:
          self.fail('Expected SwiftShader renderer; instead got ' + renderer)

  def _GpuProcess_webgl_disabled_extension(self, test_path: str) -> None:
    # Hit exception from id 257 from kGpuDriverBugListEntries.
    self.RestartBrowserIfNecessaryWithArgs([
        '--gpu-driver-bug-list-test-group=2',
    ])
    self._NavigateAndWait(test_path)

  def _GpuProcess_mac_webgl_low_power(self, test_path: str) -> None:
    # Ensures that low-power WebGL content stays on the low-power GPU.
    if not self.IsDualGPUMacLaptop():
      logging.info('Skipping test because not running on dual-GPU Mac laptop')
      return
    # Start with a clean browser instance to ensure the GPU process is in a
    # clean state.
    self.RestartBrowserWithArgs([])
    # Wait a few seconds for the system to dispatch any GPU switched
    # notifications.
    time.sleep(3)
    self._NavigateAndWait(test_path)
    # Sleep for several seconds to ensure that any GPU switch is detected.
    time.sleep(6)
    if not self._IsIntelGPUActive():
      self.fail(
          'Low-power WebGL context incorrectly activated the high-performance '
          'GPU')

  def _GpuProcess_mac_webgl_high_performance(self, test_path: str) -> None:
    # Ensures that high-performance WebGL content activates the high-performance
    # GPU.
    if not self.IsDualGPUMacLaptop():
      logging.info('Skipping test because not running on dual-GPU Mac laptop')
      return
    # Start with a clean browser instance to ensure the GPU process is in a
    # clean state.
    self.RestartBrowserWithArgs([])
    # Wait a few seconds for the system to dispatch any GPU switched
    # notifications.
    time.sleep(3)
    self._NavigateAndWait(test_path)
    # Sleep for several seconds to ensure that any GPU switch is detected.
    time.sleep(6)
    if self._IsIntelGPUActive():
      self.fail('High-performance WebGL context did not activate the '
                'high-performance GPU')

  def _GpuProcess_mac_webgl_backgrounded_high_performance(self, test_path: str
                                                          ) -> None:
    # Ensures that high-performance WebGL content in a background tab releases
    # the hold on the discrete GPU after 10 seconds.
    if not self.IsDualGPUMacLaptop():
      logging.info('Skipping test because not running on dual-GPU Mac laptop')
      return
    # Start with a clean browser instance to ensure the GPU process is in a
    # clean state.
    self.RestartBrowserWithArgs([])
    # Wait a few seconds for the system to dispatch any GPU switched
    # notifications.
    time.sleep(3)
    self._NavigateAndWait(test_path)
    blank_tab = self.tab
    # Create a new tab and navigate it to the high-power WebGL test.
    webgl_tab = self.browser.tabs.New()
    webgl_tab.Activate()
    webgl_url = self.UrlOfStaticFilePath(
        'gpu/functional_webgl_high_performance.html')
    webgl_tab.action_runner.Navigate(
        webgl_url, script_to_evaluate_on_commit=test_harness_script)
    self._WaitForTestCompletion(webgl_tab)
    # Verify that the high-performance GPU is active.
    if self._IsIntelGPUActive():
      self.fail('High-performance WebGL context did not activate the '
                'high-performance GPU')
    # Now activate the original tab.
    blank_tab.Activate()
    # Sleep for >10 seconds in order to wait for the hold on the
    # high-performance GPU to be released.
    time.sleep(15)
    if not self._IsIntelGPUActive():
      self.fail(
          'Backgrounded high-performance WebGL context did not release the '
          'hold on the high-performance GPU')

  def _GpuProcess_mac_webgl_terminated_high_performance(self,
                                                        test_path: str) -> None:
    # Ensures that high-performance WebGL content in a background tab releases
    # the hold on the discrete GPU after 10 seconds.
    if not self.IsDualGPUMacLaptop():
      logging.info('Skipping test because not running on dual-GPU Mac laptop')
      return
    # Start with a clean browser instance to ensure the GPU process is in a
    # clean state.
    self.RestartBrowserWithArgs([])
    # Wait a few seconds for the system to dispatch any GPU switched
    # notifications.
    time.sleep(3)
    self._NavigateAndWait(test_path)
    # Create a new tab and navigate it to the high-power WebGL test.
    webgl_tab = self.browser.tabs.New()
    webgl_tab.Activate()
    webgl_url = self.UrlOfStaticFilePath(
        'gpu/functional_webgl_high_performance.html')
    webgl_tab.action_runner.Navigate(
        webgl_url, script_to_evaluate_on_commit=test_harness_script)
    self._WaitForTestCompletion(webgl_tab)
    # Verify that the high-performance GPU is active.
    if self._IsIntelGPUActive():
      self.fail('High-performance WebGL context did not activate the '
                'high-performance GPU')
    # Close the high-performance WebGL tab.
    webgl_tab.Close()
    # Sleep for >10 seconds in order to wait for the hold on the
    # high-performance GPU to be released.
    time.sleep(15)
    if not self._IsIntelGPUActive():
      self.fail(
          'Backgrounded high-performance WebGL context did not release the '
          'hold on the high-performance GPU')

  def _GpuProcess_webgpu_iframe_removed(self, test_path: str) -> None:
    self.RestartBrowserIfNecessaryWithArgs(cba.ENABLE_WEBGPU_FOR_TESTING)
    self._NavigateAndWait(test_path)

  @classmethod
  def ExpectationsFiles(cls) -> List[str]:
    return [
        os.path.join(
            os.path.dirname(os.path.abspath(__file__)), 'test_expectations',
            'gpu_process_expectations.txt')
    ]


def load_tests(loader: unittest.TestLoader, tests: Any,
               pattern: Any) -> unittest.TestSuite:
  del loader, tests, pattern  # Unused.
  return gpu_integration_test.LoadAllTestsInModule(sys.modules[__name__])