| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/payments/PaymentResponse.h" | 5 #include "modules/payments/PaymentResponse.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "bindings/core/v8/ScriptState.h" | 8 #include "bindings/core/v8/ScriptState.h" |
| 9 #include "bindings/core/v8/ScriptValue.h" |
| 9 #include "core/testing/DummyPageHolder.h" | 10 #include "core/testing/DummyPageHolder.h" |
| 10 #include "modules/payments/PaymentCompleter.h" | 11 #include "modules/payments/PaymentCompleter.h" |
| 11 #include "modules/payments/PaymentDetailsTestHelper.h" | |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "wtf/OwnPtr.h" | 14 #include "wtf/OwnPtr.h" |
| 15 #include <utility> | 15 #include <utility> |
| 16 | 16 |
| 17 namespace blink { | 17 namespace blink { |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 class MockPaymentCompleter : public GarbageCollectedFinalized<MockPaymentComplet
er>, public PaymentCompleter { | 20 class MockPaymentCompleter : public GarbageCollectedFinalized<MockPaymentComplet
er>, public PaymentCompleter { |
| 21 USING_GARBAGE_COLLECTED_MIXIN(MockPaymentCompleter); | 21 USING_GARBAGE_COLLECTED_MIXIN(MockPaymentCompleter); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 36 | 36 |
| 37 private: | 37 private: |
| 38 ScriptPromise m_dummyPromise; | 38 ScriptPromise m_dummyPromise; |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 class PaymentResponseTest : public testing::Test { | 41 class PaymentResponseTest : public testing::Test { |
| 42 public: | 42 public: |
| 43 PaymentResponseTest() | 43 PaymentResponseTest() |
| 44 : m_page(DummyPageHolder::create()) | 44 : m_page(DummyPageHolder::create()) |
| 45 { | 45 { |
| 46 m_page->document().setSecurityOrigin(SecurityOrigin::create(KURL(KURL(),
"https://www.example.com/"))); | |
| 47 } | 46 } |
| 48 | 47 |
| 49 ~PaymentResponseTest() override {} | 48 ~PaymentResponseTest() override {} |
| 50 | 49 |
| 51 ScriptState* getScriptState() { return ScriptState::forMainWorld(m_page->doc
ument().frame()); } | 50 ScriptState* getScriptState() { return ScriptState::forMainWorld(m_page->doc
ument().frame()); } |
| 52 ExceptionState& getExceptionState() { return m_exceptionState; } | 51 ExceptionState& getExceptionState() { return m_exceptionState; } |
| 53 | 52 |
| 54 private: | 53 private: |
| 55 OwnPtr<DummyPageHolder> m_page; | 54 OwnPtr<DummyPageHolder> m_page; |
| 56 NonThrowableExceptionState m_exceptionState; | 55 NonThrowableExceptionState m_exceptionState; |
| 57 }; | 56 }; |
| 58 | 57 |
| 59 TEST_F(PaymentResponseTest, DataCopiedOver) | 58 TEST_F(PaymentResponseTest, DataCopiedOver) |
| 60 { | 59 { |
| 60 ScriptState::Scope scope(getScriptState()); |
| 61 mojom::blink::PaymentResponsePtr input = mojom::blink::PaymentResponse::New(
); | 61 mojom::blink::PaymentResponsePtr input = mojom::blink::PaymentResponse::New(
); |
| 62 input->method_name = "foo"; | 62 input->method_name = "foo"; |
| 63 input->stringified_details = "{\"transactionId\": 123}"; | 63 input->stringified_details = "{\"transactionId\": 123}"; |
| 64 MockPaymentCompleter* completeCallback = new MockPaymentCompleter; | 64 MockPaymentCompleter* completeCallback = new MockPaymentCompleter; |
| 65 | 65 |
| 66 PaymentResponse output(std::move(input), completeCallback); | 66 PaymentResponse output(std::move(input), completeCallback); |
| 67 | 67 |
| 68 // TODO(rouslan): Verify that output.details() contains parsed input->string
ified_details. | |
| 69 EXPECT_FALSE(getExceptionState().hadException()); | |
| 70 EXPECT_EQ("foo", output.methodName()); | 68 EXPECT_EQ("foo", output.methodName()); |
| 69 |
| 70 ScriptValue details = output.details(getScriptState(), getExceptionState()); |
| 71 |
| 72 ASSERT_FALSE(getExceptionState().hadException()); |
| 73 ASSERT_TRUE(details.v8Value()->IsObject()); |
| 74 |
| 75 ScriptValue transactionId(getScriptState(), details.v8Value().As<v8::Object>
()->Get(v8String(getScriptState()->isolate(), "transactionId"))); |
| 76 |
| 77 ASSERT_TRUE(transactionId.v8Value()->IsNumber()); |
| 78 EXPECT_EQ(123, transactionId.v8Value().As<v8::Number>()->Value()); |
| 71 } | 79 } |
| 72 | 80 |
| 73 TEST_F(PaymentResponseTest, CompleteCalled) | 81 TEST_F(PaymentResponseTest, CompleteCalledWithSuccess) |
| 74 { | 82 { |
| 75 mojom::blink::PaymentResponsePtr input = mojom::blink::PaymentResponse::New(
); | 83 mojom::blink::PaymentResponsePtr input = mojom::blink::PaymentResponse::New(
); |
| 76 input->method_name = "foo"; | 84 input->method_name = "foo"; |
| 77 input->stringified_details = "{\"transactionId\": 123}"; | 85 input->stringified_details = "{\"transactionId\": 123}"; |
| 78 MockPaymentCompleter* completeCallback = new MockPaymentCompleter; | 86 MockPaymentCompleter* completeCallback = new MockPaymentCompleter; |
| 79 PaymentResponse output(std::move(input), completeCallback); | 87 PaymentResponse output(std::move(input), completeCallback); |
| 80 | 88 |
| 81 EXPECT_FALSE(getExceptionState().hadException()); | |
| 82 EXPECT_CALL(*completeCallback, complete(getScriptState(), true)); | 89 EXPECT_CALL(*completeCallback, complete(getScriptState(), true)); |
| 83 | 90 |
| 84 output.complete(getScriptState(), true); | 91 output.complete(getScriptState(), true); |
| 85 } | 92 } |
| 86 | 93 |
| 94 TEST_F(PaymentResponseTest, CompleteCalledWithFailure) |
| 95 { |
| 96 mojom::blink::PaymentResponsePtr input = mojom::blink::PaymentResponse::New(
); |
| 97 input->method_name = "foo"; |
| 98 input->stringified_details = "{\"transactionId\": 123}"; |
| 99 MockPaymentCompleter* completeCallback = new MockPaymentCompleter; |
| 100 PaymentResponse output(std::move(input), completeCallback); |
| 101 |
| 102 EXPECT_CALL(*completeCallback, complete(getScriptState(), false)); |
| 103 |
| 104 output.complete(getScriptState(), false); |
| 105 } |
| 106 |
| 87 } // namespace | 107 } // namespace |
| 88 } // namespace blink | 108 } // namespace blink |
| OLD | NEW |