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

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

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

import collections
from typing import Callable, Dict, List, Optional, Tuple, Union

from gpu_tests import common_browser_args as cba
from gpu_tests import crop_actions as ca
from gpu_tests import skia_gold_heartbeat_integration_test_base as sghitb

from telemetry.internal.browser import browser as browser_module

coordinate_tuple = collections.namedtuple('coordinate', ['x', 'y'])
size_tuple = collections.namedtuple('size', ['width', 'height'])
rgba_tuple = collections.namedtuple('rgba', ['r', 'g', 'b', 'a'])


class ExpectedColorExpectation():
  """Defines a single tested region within an image."""
  def __init__(self,
               location: Tuple[int, int],
               size: Tuple[int, int],
               color: Union[Tuple[int, int, int], Tuple[int, int, int, int]],
               tolerance: Optional[int] = None):
    """
    Args:
      location: A tuple of two ints denoting the upper left corner of the
          sampled region within the image.
      size: A tuple of two ints denoting the width and height of the sampled
          region within the image.
      color: A tuple of three or four ints denoting the expected RGB or RGBA
          value in the sampled region, respectively. If no alpha value is given,
          255 will be used as the default.
      tolerance: An int containing how much any one channel is allowed to
          differ and still be considered the same. Will override the test case's
          base tolerance if specified.
    """
    if len(color) == 3:
      color = (color[0], color[1], color[2], 255)

    self.location = coordinate_tuple(*location)
    self.size = size_tuple(*size)
    self.color = rgba_tuple(*color)
    self.tolerance = tolerance


class ExpectedColorTestCase(sghitb.SkiaGoldHeartbeatTestCase):
  """Defines a single expected color test."""
  def __init__(  # pylint: disable=too-many-arguments
      self,
      url: str,
      name: str,
      base_tolerance: int,
      expected_colors: List[ExpectedColorExpectation],
      crop_action: ca.BaseCropAction,
      *args,
      extra_browser_args: Optional[List[str]] = None,
      should_capture_full_screenshot_func: Optional[Callable[
          [browser_module.Browser], bool]] = None,
      scale_factor_overrides: Optional[Dict[str, float]] = None,
      **kwargs):
    """
    Args:
      url: A string containing the test page URL to load.
      name: A string containing the test name. Will be prefixed with an
          identifier for expected color tests.
      base_ tolerance: An int containing how much any one channel is allowed to
          differ and still be considered the same. This is the default tolerance
          that will be used for the test if individual color expectations do not
          override it.
      expected_colors: A list of all the ExpectedColorExpectations to check as
          part of the test.
      extra_browser_args: An optional list of strings containing any browser
          args to use while running the test.
      should_capture_full_screenshot_func: An optional function that takes a
          Telemetry Browser as input and returns a boolean. If this function
          returns True, a capture of the entire web contents will be taken.
          Otherwise, only the visible portion will be captured, which is more
          representative of behavior users will see. If this function is not
          specified, the visible-only code path will be used.
      scale_factor_overrides: An optional dict mapping device names to device
          pixel ratios to use instead of the one reported by the browser.
    """
    assert url
    assert name
    assert base_tolerance >= 0

    name = 'ExpectedColor_' + name
    super().__init__(name, *args, **kwargs)

    extra_browser_args = extra_browser_args or []
    if should_capture_full_screenshot_func is None:
      should_capture_full_screenshot_func = lambda _: False

    self.url = url
    self.base_tolerance = base_tolerance
    self.expected_colors = expected_colors
    self.crop_action = crop_action
    self.extra_browser_args = extra_browser_args
    self.ShouldCaptureFullScreenshot = should_capture_full_screenshot_func
    self.scale_factor_overrides = scale_factor_overrides or {}


def CaptureFullScreenshotOnFuchsia(browser: browser_module.Browser) -> bool:
  return browser.platform.GetOSName() == 'fuchsia'


def MapsTestCases() -> List[ExpectedColorTestCase]:
  class TestActionStartMapsTest(sghitb.TestAction):

    def Run(
        self, test_case: ExpectedColorTestCase, tab_data: sghitb.TabData,
        loop_state: sghitb.LoopState,
        test_instance: sghitb.SkiaGoldHeartbeatIntegrationTestBase
    ) -> None:  # pytype: disable=signature-mismatch
      sghitb.EvalInTestIframe(
          tab_data.tab, """
        function _checkIfTestCanStart() {
          if (window.startTest !== undefined) {
            window.startTest();
            _checkIfReadyForIdleCallback();
          } else {
            setTimeout(_checkIfTestCanStart, 100);
          }
        }

        function _checkIfReadyForIdleCallback() {
          if (window.testDone) {
            _setUpIdleCallback();
          } else {
            setTimeout(_checkIfReadyForIdleCallback, 100);
          }
        }

        function _setUpIdleCallback() {
          requestIdleCallback(() => {
            window.domAutomationController.send('SUCCESS');
          }, { timeout: 10000 });
        }

        _checkIfTestCanStart();
        """)

  return [
      ExpectedColorTestCase(
          'tools/perf/page_sets/maps_perf_test/performance.html',
          'maps',
          10,
          [
              # Light green.
              ExpectedColorExpectation(
                  location=(35, 239), size=(1, 1), color=(214, 232, 186)),
              # Darker green.
              ExpectedColorExpectation(
                  location=(318, 103), size=(1, 1), color=(203, 230, 163)),
              # Blue (lake).
              ExpectedColorExpectation(
                  location=(585, 385), size=(1, 1), color=(163, 205, 255)),
              # Gray.
              ExpectedColorExpectation(
                  location=(87, 109), size=(1, 1), color=(232, 232, 232)),
              # Tan.
              ExpectedColorExpectation(
                  location=(336, 255), size=(1, 1), color=(240, 237, 230)),
          ],
          test_actions=[
              sghitb.TestActionWaitForInnerTestPageLoad(),
              TestActionStartMapsTest(),
              sghitb.TestActionWaitForFinish(sghitb.DEFAULT_GLOBAL_TIMEOUT),
          ],
          crop_action=ca.NonWhiteContentCropAction(),
          extra_browser_args=[
              cba.ENSURE_FORCED_COLOR_PROFILE,
              cba.FORCE_BROWSER_CRASH_ON_GPU_CRASH,
              cba.FORCE_COLOR_PROFILE_SRGB,
          ],
          # Small Fuchsia screens result in an incomplete capture without this.
          should_capture_full_screenshot_func=CaptureFullScreenshotOnFuchsia,
          # Certain devices, particularly Android devices, report incorrect
          # device pixel ratios. These overrides are the correct values for
          # such devices calculated using the fact that this test should produce
          # an 800x600 image on a device with a DPR of 1.
          scale_factor_overrides={
              'Nexus 5': 1.105,
              'Nexus 5X': 1.105,
              # NVIDIA Shield.
              'sb_na_wf': 1.226,
              'Pixel 2': 1.1067,
              'Pixel 4': 1.1025,
              'Pixel 6': 1.10375,
              # Samsung A13.
              'SM-A135M': 1.1025,
              # Samsung A23.
              'SM-A235M': 1.1025,
              # Samsung S23.
              'SM-S911U1': 1.1,
              # Motorola Moto G Power 5G.
              'moto g power 5G - 2023': 1.1,
          }),
  ]


def MediaRecorderTestCases() -> List[ExpectedColorTestCase]:
  red = (255, 0, 0)
  green = (0, 255, 0)
  blue = (0, 0, 255)
  yellow = (255, 255, 0)

  # Check pixels within two 128x128 elements that are horizontally adjacent with
  # a 4 pixel gap between them. Use a 10-pixel buffer between the sample areas
  # and edges/color borders.
  canvas_expected_colors = [
      # Source canvas (left image).
      ExpectedColorExpectation(location=(10, 10), size=(44, 44), color=yellow),
      ExpectedColorExpectation(location=(78, 10), size=(44, 44), color=red),
      ExpectedColorExpectation(location=(10, 78), size=(44, 44), color=blue),
      ExpectedColorExpectation(location=(78, 78), size=(44, 44), color=green),
      # Destination video (right image).
      ExpectedColorExpectation(location=(142, 10), size=(44, 44), color=yellow),
      ExpectedColorExpectation(location=(206, 10), size=(44, 44), color=red),
      ExpectedColorExpectation(location=(142, 78), size=(44, 44), color=blue),
      ExpectedColorExpectation(location=(206, 78),
                               size=(44, 44),
                               color=green,
                               tolerance=100),
  ]

  # Check pixels within two 240x135 elements that are vertically adjacent with
  # a 15 pixel gap between them. Use a 10-pixel buffer between the sample areas
  # and edges/color borders.
  video_expected_colors = [
      # Destination video (top image).
      ExpectedColorExpectation(location=(10, 10), size=(100, 47), color=yellow),
      ExpectedColorExpectation(location=(130, 10), size=(100, 47), color=red),
      ExpectedColorExpectation(location=(10, 78), size=(100, 47), color=blue),
      ExpectedColorExpectation(location=(130, 78),
                               size=(100, 47),
                               color=green,
                               tolerance=100),
      # Source video (bottom image).
      ExpectedColorExpectation(location=(10, 160), size=(100, 47),
                               color=yellow),
      ExpectedColorExpectation(location=(130, 160), size=(100, 47), color=red),
      ExpectedColorExpectation(location=(10, 228), size=(100, 47), color=blue),
      ExpectedColorExpectation(location=(130, 228),
                               size=(100, 47),
                               color=green,
                               tolerance=100),
  ]

  # Full cycle capture-encode-decode test for MediaRecorder capturing canvas.
  # Large tolerances are used since the test only cares that the colors are
  # vaguely red/blue/green/yellow.
  return [
      ExpectedColorTestCase(
          'content/test/data/gpu/pixel_media_recorder_from_canvas_2d.html',
          'MediaRecorderFrom2DCanvas',
          60,
          canvas_expected_colors,
          crop_action=ca.NonWhiteContentCropAction(),
      ),
      ExpectedColorTestCase(
          'content/test/data/gpu/pixel_media_recorder_from_video_element.html',
          'MediaRecorderFromVideoElement',
          60,
          video_expected_colors,
          crop_action=ca.NonWhiteContentCropAction(),
      ),
      ExpectedColorTestCase(
          'content/test/data/gpu/pixel_media_recorder_from_video_element.html',
          'MediaRecorderFromVideoElementWithOoprCanvasDisabled',
          60,
          video_expected_colors,
          crop_action=ca.NonWhiteContentCropAction(),
          extra_browser_args=['--disable-features=CanvasOopRasterization']),
  ]