Fix flaky PaymentAppBrowserTest on desktop platforms

The call to ClearStoragePartitionData clears ownership of the event
data, so it may be corrupt by the time the test JS executes. This patch
makes it so the storage partition is cleared at the end of each test
rather than before the JS values are read.

This does not fix the unrelated ChromeOS flakes which are due to the
test timing out only on that platform.

Bug: 1129411
Change-Id: Iba8d8575bfb57c5e81a8a420386320c4d7e1d019
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2521693
Reviewed-by: Rouslan Solomakhin <rouslan@chromium.org>
Reviewed-by: Liquan (Max) Gu <maxlg@chromium.org>
Commit-Queue: Nick Burris <nburris@chromium.org>
Cr-Commit-Position: refs/heads/master@{#828384}
diff --git a/content/browser/payments/payment_app_browsertest.cc b/content/browser/payments/payment_app_browsertest.cc
index be77ad0..57e034a4 100644
--- a/content/browser/payments/payment_app_browsertest.cc
+++ b/content/browser/payments/payment_app_browsertest.cc
@@ -298,9 +298,8 @@
   ClearStoragePartitionData();
 }
 
-// TODO(https://crbug.com/1129411) Fix flake on all desktop platforms.
-#if defined(OS_CHROMEOS) || defined(OS_LINUX) || defined(OS_MAC) || \
-    defined(OS_WIN)
+// TODO(https://crbug.com/869790) Flakes on linux-chromeos-dbg
+#if defined(OS_CHROMEOS)
 #define MAYBE_CanMakePayment DISABLED_CanMakePayment
 #else
 #define MAYBE_CanMakePayment CanMakePayment
@@ -315,8 +314,6 @@
       registrationIds[0], GetTestServerOrigin(), "id", "basic-card");
   ASSERT_TRUE(can_make_payment);
 
-  ClearStoragePartitionData();
-
   EXPECT_EQ("https://example.com/", PopConsoleString() /* topOrigin */);
   EXPECT_EQ("https://example.com/",
             PopConsoleString() /* paymentRequestOrigin */);
@@ -328,6 +325,8 @@
       "\"value\":\"55\"},\"label\":\"\",\"pending\":false}}"
       "]",
       PopConsoleString() /* modifiers */);
+
+  ClearStoragePartitionData();
 }
 
 // TODO(crbug.com/869790) Flakes on linux-chromeos-dbg
@@ -355,9 +354,8 @@
   ClearStoragePartitionData();
 }
 
-// TODO(https://crbug.com/1129411) Fix flake on all desktop platforms.
-#if defined(OS_CHROMEOS) || defined(OS_LINUX) || defined(OS_MAC) || \
-    defined(OS_WIN)
+// TODO(https://crbug.com/869790) Flakes on linux-chromeos-dbg
+#if defined(OS_CHROMEOS)
 #define MAYBE_PaymentAppInvocation DISABLED_PaymentAppInvocation
 #else
 #define MAYBE_PaymentAppInvocation PaymentAppInvocation
@@ -373,11 +371,6 @@
                                    "basic-card", "basic-card-payment-app-id"));
   ASSERT_EQ("test", response->method_name);
 
-  ClearStoragePartitionData();
-
-  registrationIds = GetAllPaymentAppRegistrationIDs();
-  ASSERT_EQ(0U, registrationIds.size());
-
   EXPECT_EQ("https://example.com/", PopConsoleString() /* topOrigin */);
   EXPECT_EQ("https://example.com/",
             PopConsoleString() /* paymentRequestOrigin */);
@@ -396,11 +389,15 @@
       PopConsoleString() /* modifiers */);
   EXPECT_EQ("basic-card-payment-app-id",
             PopConsoleString() /* instrumentKey */);
+
+  ClearStoragePartitionData();
+
+  registrationIds = GetAllPaymentAppRegistrationIDs();
+  ASSERT_EQ(0U, registrationIds.size());
 }
 
-// TODO(https://crbug.com/1129411) Fix flake on all desktop platforms.
-#if defined(OS_CHROMEOS) || defined(OS_LINUX) || defined(OS_MAC) || \
-    defined(OS_WIN)
+// TODO(https://crbug.com/869790) Flakes on linux-chromeos-dbg
+#if defined(OS_CHROMEOS)
 #define MAYBE_PaymentAppOpenWindowFailed DISABLED_PaymentAppOpenWindowFailed
 #else
 #define MAYBE_PaymentAppOpenWindowFailed PaymentAppOpenWindowFailed
@@ -419,11 +416,6 @@
   // in PaymentRequestRespondWithObserver::OnResponseRejected.
   ASSERT_EQ("", response->method_name);
 
-  ClearStoragePartitionData();
-
-  registrationIds = GetAllPaymentAppRegistrationIDs();
-  ASSERT_EQ(0U, registrationIds.size());
-
   EXPECT_EQ("https://example.com/", PopConsoleString() /* topOrigin */);
   EXPECT_EQ("https://example.com/",
             PopConsoleString() /* paymentRequestOrigin */);
@@ -440,5 +432,10 @@
       "]",
       PopConsoleString() /* modifiers */);
   EXPECT_EQ("bobpay-payment-app-id", PopConsoleString() /* instrumentKey */);
+
+  ClearStoragePartitionData();
+
+  registrationIds = GetAllPaymentAppRegistrationIDs();
+  ASSERT_EQ(0U, registrationIds.size());
 }
 }  // namespace content