[Picture-in-Picture] Fix web platform tests that time out.

This makes sure web platform tests for Picture-in-Picture do not time
out when Picture-in-Picture API is not available.

Besides, wpt/picture-in-picture/request-picture-in-picture is split
to work around a flaky timeout in WPT upstream that's related to
https://github.com/web-platform-tests/wpt/issues/10398

Note: based on https://github.com/web-platform-tests/wpt/pull/11329

Bug: 806249
Change-Id: I6185253dfa4aae5111e87553581b8464032558d1
Reviewed-on: https://chromium-review.googlesource.com/1107626
Reviewed-by: Robert Ma <robertma@chromium.org>
Reviewed-by: Mounir Lamouri <mlamouri@chromium.org>
Commit-Queue: Fran├žois Beaufort <beaufort.francois@gmail.com>
Cr-Commit-Position: refs/heads/master@{#576804}
diff --git a/third_party/WebKit/LayoutTests/TestExpectations b/third_party/WebKit/LayoutTests/TestExpectations
index 9beca90..5ec18e94 100644
--- a/third_party/WebKit/LayoutTests/TestExpectations
+++ b/third_party/WebKit/LayoutTests/TestExpectations
@@ -4321,26 +4321,27 @@
 crbug.com/811565 [ Fuchsia ] tables/mozilla/core/bloomberg.html [ Failure Pass ]
 
 # TODO(apacible): Remove these once SurfaceLayerForVideo works on LayoutTests.
-crbug.com/806249 external/wpt/picture-in-picture/picture-in-picture-window.html [ Skip ]
-crbug.com/806249 external/wpt/picture-in-picture/picture-in-picture-element.html [ Skip ]
-crbug.com/806249 external/wpt/picture-in-picture/disable-picture-in-picture.html [ Skip ]
-crbug.com/806249 external/wpt/picture-in-picture/shadow-dom.html [ Skip ]
-crbug.com/806249 external/wpt/picture-in-picture/request-picture-in-picture.html [ Skip ]
-crbug.com/806249 external/wpt/picture-in-picture/leave-picture-in-picture.html [ Skip ]
-crbug.com/806249 external/wpt/picture-in-picture/enter-picture-in-picture.html [ Skip ]
-crbug.com/806249 external/wpt/picture-in-picture/exit-picture-in-picture.html [ Skip ]
-crbug.com/806249 external/wpt/feature-policy/picture-in-picture-allowed-by-feature-policy-attribute-redirect-on-load.https.sub.html [ Skip ]
-crbug.com/806249 external/wpt/feature-policy/picture-in-picture-allowed-by-feature-policy-attribute.https.sub.html [ Skip ]
-crbug.com/806249 external/wpt/feature-policy/picture-in-picture-allowed-by-feature-policy.https.sub.html [ Skip ]
-crbug.com/806249 external/wpt/feature-policy/picture-in-picture-default-feature-policy.https.sub.html [ Skip ]
+crbug.com/806249 external/wpt/picture-in-picture/picture-in-picture-window.html [ Failure ]
+crbug.com/806249 external/wpt/picture-in-picture/picture-in-picture-element.html [ Failure ]
+crbug.com/806249 external/wpt/picture-in-picture/disable-picture-in-picture.html [ Failure ]
+crbug.com/806249 external/wpt/picture-in-picture/shadow-dom.html [ Failure ]
+crbug.com/806249 external/wpt/picture-in-picture/request-picture-in-picture.html [ Failure ]
+crbug.com/806249 external/wpt/picture-in-picture/request-picture-in-picture-twice.html [ Failure ]
+crbug.com/806249 external/wpt/picture-in-picture/leave-picture-in-picture.html [ Failure ]
+crbug.com/806249 external/wpt/picture-in-picture/enter-picture-in-picture.html [ Failure ]
+crbug.com/806249 external/wpt/picture-in-picture/exit-picture-in-picture.html [ Failure ]
+crbug.com/806249 external/wpt/feature-policy/picture-in-picture-allowed-by-feature-policy-attribute-redirect-on-load.https.sub.html [ Failure ]
+crbug.com/806249 external/wpt/feature-policy/picture-in-picture-allowed-by-feature-policy-attribute.https.sub.html [ Failure ]
+crbug.com/806249 external/wpt/feature-policy/picture-in-picture-allowed-by-feature-policy.https.sub.html [ Failure ]
+crbug.com/806249 external/wpt/feature-policy/picture-in-picture-default-feature-policy.https.sub.html [ Failure ]
 crbug.com/806249 media/picture-in-picture/controls/picture-in-picture-button.html [ Failure ]
 crbug.com/806249 media/picture-in-picture/controls/picture-in-picture-video-with-audio-only-button.html [ Failure ]
 crbug.com/806249 media/picture-in-picture/picture-in-picture-enabled.html [ Failure ]
 crbug.com/811977 media/picture-in-picture/picture-in-picture-interstitial.html [ Failure ]
-crbug.com/806249 virtual/unified-autoplay/external/wpt/feature-policy/picture-in-picture-allowed-by-feature-policy-attribute-redirect-on-load.https.sub.html [ Skip ]
-crbug.com/806249 virtual/unified-autoplay/external/wpt/feature-policy/picture-in-picture-allowed-by-feature-policy-attribute.https.sub.html [ Skip ]
-crbug.com/806249 virtual/unified-autoplay/external/wpt/feature-policy/picture-in-picture-allowed-by-feature-policy.https.sub.html [ Skip ]
-crbug.com/806249 virtual/unified-autoplay/external/wpt/feature-policy/picture-in-picture-default-feature-policy.https.sub.html [ Skip ]
+crbug.com/806249 virtual/unified-autoplay/external/wpt/feature-policy/picture-in-picture-allowed-by-feature-policy-attribute-redirect-on-load.https.sub.html [ Failure ]
+crbug.com/806249 virtual/unified-autoplay/external/wpt/feature-policy/picture-in-picture-allowed-by-feature-policy-attribute.https.sub.html [ Failure ]
+crbug.com/806249 virtual/unified-autoplay/external/wpt/feature-policy/picture-in-picture-allowed-by-feature-policy.https.sub.html [ Failure ]
+crbug.com/806249 virtual/unified-autoplay/external/wpt/feature-policy/picture-in-picture-default-feature-policy.https.sub.html [ Failure ]
 crbug.com/806249 virtual/video-surface-layer/external/wpt/feature-policy/autoplay-allowed-by-feature-policy-attribute-redirect-on-load.https.sub.html [ Skip ]
 crbug.com/806249 virtual/video-surface-layer/external/wpt/feature-policy/autoplay-default-feature-policy.https.sub.html [ Skip ]
 crbug.com/806249 virtual/video-surface-layer/external/wpt/feature-policy/autoplay-disabled-by-feature-policy.https.sub.html [ Skip ]
diff --git a/third_party/WebKit/LayoutTests/external/wpt/feature-policy/picture-in-picture-default-feature-policy.https.sub.html b/third_party/WebKit/LayoutTests/external/wpt/feature-policy/picture-in-picture-default-feature-policy.https.sub.html
index 94cec08..f47661b 100644
--- a/third_party/WebKit/LayoutTests/external/wpt/feature-policy/picture-in-picture-default-feature-policy.https.sub.html
+++ b/third_party/WebKit/LayoutTests/external/wpt/feature-policy/picture-in-picture-default-feature-policy.https.sub.html
@@ -26,7 +26,7 @@
 
   async_test(t => {
     test_feature_availability('picture-in-picture', t, cross_origin_src,
-        expect_feature_available_default,);
+        expect_feature_available_default);
   }, header + ' allows cross-origin iframes.');
 
   </script>
diff --git a/third_party/WebKit/LayoutTests/external/wpt/feature-policy/picture-in-picture-disabled-by-feature-policy.https.sub.html b/third_party/WebKit/LayoutTests/external/wpt/feature-policy/picture-in-picture-disabled-by-feature-policy.https.sub.html
index 5080c80..2f7576b 100644
--- a/third_party/WebKit/LayoutTests/external/wpt/feature-policy/picture-in-picture-disabled-by-feature-policy.https.sub.html
+++ b/third_party/WebKit/LayoutTests/external/wpt/feature-policy/picture-in-picture-disabled-by-feature-policy.https.sub.html
@@ -26,7 +26,7 @@
 
   async_test(t => {
     test_feature_availability('picture-in-picture', t, cross_origin_src,
-        expect_feature_unavailable_default,);
+        expect_feature_unavailable_default);
   }, header + ' disallows cross-origin iframes.');
   </script>
 </body>
diff --git a/third_party/WebKit/LayoutTests/external/wpt/feature-policy/resources/picture-in-picture.js b/third_party/WebKit/LayoutTests/external/wpt/feature-policy/resources/picture-in-picture.js
index 6bdb7e5..e7aabcd 100644
--- a/third_party/WebKit/LayoutTests/external/wpt/feature-policy/resources/picture-in-picture.js
+++ b/third_party/WebKit/LayoutTests/external/wpt/feature-policy/resources/picture-in-picture.js
@@ -1,4 +1,7 @@
 function isPictureInPictureAllowed() {
+  if (!('pictureInPictureEnabled' in document))
+    return Promise.resolve(false);
+
   return new Promise(resolve => {
     let video = document.createElement('video');
     video.src = '/media/movie_5.ogv';
@@ -13,4 +16,4 @@
       });
     };
   });
-}
\ No newline at end of file
+}
diff --git a/third_party/WebKit/LayoutTests/external/wpt/picture-in-picture/request-picture-in-picture-twice.html b/third_party/WebKit/LayoutTests/external/wpt/picture-in-picture/request-picture-in-picture-twice.html
new file mode 100644
index 0000000..7f1f81f
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/external/wpt/picture-in-picture/request-picture-in-picture-twice.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<title>Test request Picture-in-Picture on two videos</title>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<script src="/resources/testdriver.js"></script>
+<script src="/resources/testdriver-vendor.js"></script>
+<script src="resources/picture-in-picture-helpers.js"></script>
+<body></body>
+<script>
+promise_test(async t => {
+  const video1 = await loadVideo();
+  const video2 = await loadVideo();
+  return callWithTrustedClick(() => {
+    const first = video1.requestPictureInPicture();
+    const second = video2.requestPictureInPicture();
+    return Promise.all([
+      first,
+      promise_rejects(t, 'NotAllowedError', second)
+    ]);
+  });
+}, 'request Picture-in-Picture consumes user gesture');
+</script>
diff --git a/third_party/WebKit/LayoutTests/external/wpt/picture-in-picture/request-picture-in-picture.html b/third_party/WebKit/LayoutTests/external/wpt/picture-in-picture/request-picture-in-picture.html
index d2a5941..a0c3217 100644
--- a/third_party/WebKit/LayoutTests/external/wpt/picture-in-picture/request-picture-in-picture.html
+++ b/third_party/WebKit/LayoutTests/external/wpt/picture-in-picture/request-picture-in-picture.html
@@ -33,17 +33,4 @@
   const video = await loadVideo();
   return promise_rejects(t, 'NotAllowedError', video.requestPictureInPicture());
 }, 'request Picture-in-Picture requires a user gesture');
-
-promise_test(async t => {
-  const video1 = await loadVideo();
-  const video2 = await loadVideo();
-  return callWithTrustedClick(() => {
-    const first = video1.requestPictureInPicture();
-    const second = video2.requestPictureInPicture();
-    return Promise.all([
-      first,
-      promise_rejects(t, 'NotAllowedError', second)
-    ]);
-  });
-}, 'request Picture-in-Picture consumes user gesture');
 </script>
diff --git a/third_party/WebKit/LayoutTests/external/wpt/picture-in-picture/resources/picture-in-picture-helpers.js b/third_party/WebKit/LayoutTests/external/wpt/picture-in-picture/resources/picture-in-picture-helpers.js
index ee8e1c23..4514edf 100644
--- a/third_party/WebKit/LayoutTests/external/wpt/picture-in-picture/resources/picture-in-picture-helpers.js
+++ b/third_party/WebKit/LayoutTests/external/wpt/picture-in-picture/resources/picture-in-picture-helpers.js
@@ -1,5 +1,11 @@
+if (!('pictureInPictureEnabled' in document)) {
+  HTMLVideoElement.prototype.requestPictureInPicture = function() {
+    return Promise.reject('Picture-in-Picture API is not available');
+  }
+}
+
 function callWithTrustedClick(callback) {
-  return new Promise(resolve => {
+  return new Promise((resolve, reject) => {
     let button = document.createElement('button');
     button.textContent = 'click to continue test';
     button.style.display = 'block';
@@ -10,7 +16,7 @@
       resolve(callback());
     };
     document.body.appendChild(button);
-    test_driver.click(button);
+    test_driver.click(button).catch(_ => reject('Click failed'));
   });
 }