Index: third_party/WebKit/Source/modules/payments/ParsePaymentManifestTest.cpp |
diff --git a/third_party/WebKit/Source/modules/payments/ParsePaymentManifestTest.cpp b/third_party/WebKit/Source/modules/payments/ParsePaymentManifestTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3d7b6c118d12a8fba1aead558402960307b67277 |
--- /dev/null |
+++ b/third_party/WebKit/Source/modules/payments/ParsePaymentManifestTest.cpp |
@@ -0,0 +1,117 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "bindings/core/v8/V8BindingForTesting.h" |
+#include "modules/payments/PaymentRequest.h" |
+#include "modules/payments/PaymentTestHelper.h" |
+#include "platform/heap/Persistent.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "wtf/Functional.h" |
+#include "wtf/Optional.h" |
+#include "wtf/Vector.h" |
+ |
+namespace blink { |
+namespace { |
+ |
+using Section = payments::mojom::blink::NativeAndroidPaymentAppManifestSection; |
+using SectionPtr = |
+ payments::mojom::blink::NativeAndroidPaymentAppManifestSectionPtr; |
+ |
+// Gmock does not support rvalues: |
+// https://github.com/google/googletest/issues/395 |
+class MockCallback { |
+ public: |
+ explicit MockCallback(Optional<Vector<SectionPtr>> expected) |
+ : m_expected(std::move(expected)), m_called(false) {} |
+ |
+ ~MockCallback() { EXPECT_TRUE(m_called); } |
+ |
+ void Run(Optional<Vector<SectionPtr>> actual) { |
+ m_called = true; |
+ EXPECT_EQ(m_expected.has_value(), actual.has_value()); |
+ if (m_expected.has_value() && actual.has_value()) |
+ EXPECT_EQ(m_expected.value(), actual.value()); |
+ } |
+ |
+ private: |
+ const Optional<Vector<SectionPtr>> m_expected; |
+ bool m_called; |
+}; |
+ |
+void TestHelper(const String& input, |
+ Optional<Vector<SectionPtr>> expectedOutput) { |
+ V8TestingScope scope; |
+ makePaymentRequestOriginSecure(scope.document()); |
+ PaymentRequest::PaymentRequestClient* request = PaymentRequest::create( |
+ scope.getExecutionContext(), buildPaymentMethodDataForTest(), |
+ buildPaymentDetailsForTest(), scope.getExceptionState()); |
+ EXPECT_FALSE(scope.getExceptionState().hadException()); |
+ MockCallback callback(std::move(expectedOutput)); |
+ request->ParsePaymentManifest( |
+ input, |
+ convertToBaseCallback(bind(&MockCallback::Run, WTF::passed(&callback)))); |
+} |
+ |
+TEST(ParsePaymentManifestTest, NullContentIsMalformed) { |
+ TestHelper(String(), Optional<Vector<SectionPtr>>()); |
+} |
+ |
+TEST(ParsePaymentManifestTest, NonJsonContentIsMalformed) { |
+ TestHelper("this is not json", Optional<Vector<SectionPtr>>()); |
+} |
+ |
+TEST(ParsePaymentManifestTest, StringContentIsMalformed) { |
+ TestHelper("\"this is a string\"", Optional<Vector<SectionPtr>>()); |
+} |
+ |
+TEST(ParsePaymentManifestTest, EmptyDictionaryIsMalformed) { |
+ TestHelper("{}", Optional<Vector<SectionPtr>>()); |
+} |
+ |
+TEST(ParsePaymentManifestTest, NullAndroidSectionIsMalformed) { |
+ TestHelper("{\"android\": null}", Optional<Vector<SectionPtr>>()); |
+} |
+ |
+TEST(ParsePaymentManifestTest, NumberAndroidSectionIsMalformed) { |
+ TestHelper("{\"android\": 0}", Optional<Vector<SectionPtr>>()); |
+} |
+ |
+TEST(ParsePaymentManifestTest, ListOfNumbersAndroidSectionIsMalformed) { |
+ TestHelper("{\"android\": [0]}", Optional<Vector<SectionPtr>>()); |
+} |
+ |
+TEST(ParsePaymentManifestTest, |
+ ListOfEmptyDictionariesAndroidSectionIsMalformed) { |
+ TestHelper("{\"android\": [{}]}", Optional<Vector<SectionPtr>>()); |
+} |
+ |
+TEST(ParsePaymentManifestTest, NoPackageNameIsMalformed) { |
+ TestHelper("{\"android\": [{\"version\": 3}]}", |
+ Optional<Vector<SectionPtr>>()); |
+} |
+ |
+TEST(ParsePaymentManifestTest, OnlyPackageNameIsWellFormed) { |
+ Optional<Vector<SectionPtr>> expected((Vector<SectionPtr>())); |
+ expected->push_back(Section::New()); |
+ expected->back()->package_name = "*"; |
+ TestHelper("{\"android\": [{\"package\": \"*\"}]}", std::move(expected)); |
+} |
+ |
+TEST(ParsePaymentManifestTest, WellFormed) { |
+ Optional<Vector<SectionPtr>> expected((Vector<SectionPtr>())); |
+ expected->push_back(Section::New()); |
+ expected->back()->package_name = "com.bobpay.app"; |
+ expected->back()->version = 3; |
+ expected->back()->sha256_cert_fingerprints = Vector<String>(); |
+ expected->back()->sha256_cert_fingerprints->push_back("1234567890"); |
+ TestHelper( |
+ "{\"android\": [{" |
+ "\"package\": \"com.bobpay.app\"," |
+ "\"version\": 3," |
+ "\"sha256_cert_fingerprints\": [\"1234567890\"]}]}", |
+ std::move(expected)); |
+} |
+ |
+} // namespace |
+} // namespace blink |