Add test for getImageData with p3 colorspace

Bug: 1178707

Change-Id: I4c9dac7a5f41f7c2c57845d876e3d8c997524af8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2764706
Commit-Queue: Yi Xu <yiyix@chromium.org>
Reviewed-by: Justin Novosad <junov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#864292}
diff --git a/html/canvas/element/manual/color-space/colorSpace.p3.getImageData.html b/html/canvas/element/manual/color-space/colorSpace.p3.getImageData.html
new file mode 100644
index 0000000..782ca45
--- /dev/null
+++ b/html/canvas/element/manual/color-space/colorSpace.p3.getImageData.html
@@ -0,0 +1,74 @@
+<!DOCTYPE html>
+<title>Canvas test: 2d.colorSpace.p3.getImageData</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="/html/canvas/resources/canvas-tests.js"></script>
+<link rel="stylesheet" href="/html/canvas/resources/canvas-tests.css">
+<body class="show_output">
+<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
+
+<h1>2d.colorSpace.p3.getImageData</h1>
+<p class="desc">Test getImagedata with p3 color space</p>
+
+<script>
+function run_test(canvas_color_space, canvas_pixel_format, data_color_space, data_storage_format) {
+  var color_style = 'rgb(50, 100, 150)';
+  var color_srgb_f32 = [50/255., 100/255., 150/255., 1.0];
+  var color_p3_f32 = [0.24304, 0.38818, 0.57227, 1.0];
+  var epsilon =  2;
+  var canvas_c = document.getElementById('c');
+  canvas_c.width = 10;
+  canvas_c.height = 10;
+  var ctx_c = canvas_c.getContext('2d', {colorSpace: canvas_color_space, pixelFormat:canvas_pixel_format});
+  ctx_c.fillStyle = color_style;
+  ctx_c.fillRect(0, 0, 10, 10);
+  var pixel = ctx_c.getImageData(5, 5, 1, 1, {colorSpace: data_color_space, storageFormat:data_storage_format}).data;
+  var pixel_expected;
+  if (data_color_space == 'srgb')
+      pixel_expected = color_srgb_f32;
+  else if (data_color_space == 'display-p3')
+      pixel_expected = color_p3_f32;
+  if (data_storage_format == 'uint8') {
+    pixel_expected = [pixel_expected[0] * 255,
+                      pixel_expected[1] * 255,
+                      pixel_expected[2] * 255,
+                      pixel_expected[3] * 255];
+  }
+  var passed = true;
+  var message = "pixel.length: " +  pixel.length + " != " + "pixel_expected.length: "
+                + pixel_expected.length;
+  assert_equals(pixel.length, pixel_expected.length, message);
+  for (var i = 0; i < pixel.length; i++)
+    if (Math.abs(pixel[i] - pixel_expected[i]) > epsilon)
+      passed = false
+  return passed;
+}
+
+function drawThenGetImageData() {
+  color_spaces = ['srgb', 'display-p3'];
+  canvas_pixel_formats = ['uint8'];
+  image_data_pixel_formats = ['uint8'];
+  var passed = true;
+  for (var i = 0; i < color_spaces.length; ++i) {
+    for (var j = 0; j < canvas_pixel_formats.length; ++j) {
+      for (var k = 0; k < color_spaces.length; ++k) {
+        for (var l = 0; l < image_data_pixel_formats.length; ++l) {
+          setting = "destination: color_spaces: " + color_spaces[i] + " canvas_pixel_formats: "
+              + canvas_pixel_formats[j] + "source: color_spaces: " + color_spaces[k]
+              + " image_data_pixel_formats: " + image_data_pixel_formats[l] + " failed.";
+          test(() => {
+            assert_true(run_test(color_spaces[i], canvas_pixel_formats[j],
+                        color_spaces[k], image_data_pixel_formats[l]))
+          }, setting);
+        }
+      }
+    }
+  }
+
+}
+
+drawThenGetImageData();
+
+
+</script>
+