| 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 "bindings/core/v8/ScriptValue.h" |
| 10 #include "core/testing/DummyPageHolder.h" | 10 #include "core/testing/DummyPageHolder.h" |
| 11 #include "modules/payments/PaymentCompleter.h" | 11 #include "modules/payments/PaymentCompleter.h" |
| 12 #include "modules/payments/PaymentTestHelper.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "wtf/OwnPtr.h" | 15 #include "wtf/OwnPtr.h" |
| 15 #include <utility> | 16 #include <utility> |
| 16 | 17 |
| 17 namespace blink { | 18 namespace blink { |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 class MockPaymentCompleter : public GarbageCollectedFinalized<MockPaymentComplet
er>, public PaymentCompleter { | 21 class MockPaymentCompleter : public GarbageCollectedFinalized<MockPaymentComplet
er>, public PaymentCompleter { |
| 21 USING_GARBAGE_COLLECTED_MIXIN(MockPaymentCompleter); | 22 USING_GARBAGE_COLLECTED_MIXIN(MockPaymentCompleter); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 51 ExceptionState& getExceptionState() { return m_exceptionState; } | 52 ExceptionState& getExceptionState() { return m_exceptionState; } |
| 52 | 53 |
| 53 private: | 54 private: |
| 54 OwnPtr<DummyPageHolder> m_page; | 55 OwnPtr<DummyPageHolder> m_page; |
| 55 NonThrowableExceptionState m_exceptionState; | 56 NonThrowableExceptionState m_exceptionState; |
| 56 }; | 57 }; |
| 57 | 58 |
| 58 TEST_F(PaymentResponseTest, DataCopiedOver) | 59 TEST_F(PaymentResponseTest, DataCopiedOver) |
| 59 { | 60 { |
| 60 ScriptState::Scope scope(getScriptState()); | 61 ScriptState::Scope scope(getScriptState()); |
| 61 mojom::blink::PaymentResponsePtr input = mojom::blink::PaymentResponse::New(
); | 62 mojom::blink::PaymentResponsePtr input = buildPaymentResponseForTest(); |
| 62 input->method_name = "foo"; | 63 input->method_name = "foo"; |
| 64 input->total_amount->currency = "USD"; |
| 65 input->total_amount->value = "5.00"; |
| 63 input->stringified_details = "{\"transactionId\": 123}"; | 66 input->stringified_details = "{\"transactionId\": 123}"; |
| 64 MockPaymentCompleter* completeCallback = new MockPaymentCompleter; | 67 MockPaymentCompleter* completeCallback = new MockPaymentCompleter; |
| 65 | 68 |
| 66 PaymentResponse output(std::move(input), completeCallback); | 69 PaymentResponse output(std::move(input), completeCallback); |
| 67 | 70 |
| 68 EXPECT_EQ("foo", output.methodName()); | 71 EXPECT_EQ("foo", output.methodName()); |
| 69 | 72 |
| 73 CurrencyAmount totalAmount; |
| 74 output.totalAmount(totalAmount); |
| 75 EXPECT_EQ("USD", totalAmount.currency()); |
| 76 EXPECT_EQ("5.00", totalAmount.value()); |
| 77 |
| 70 ScriptValue details = output.details(getScriptState(), getExceptionState()); | 78 ScriptValue details = output.details(getScriptState(), getExceptionState()); |
| 71 | 79 |
| 72 ASSERT_FALSE(getExceptionState().hadException()); | 80 ASSERT_FALSE(getExceptionState().hadException()); |
| 73 ASSERT_TRUE(details.v8Value()->IsObject()); | 81 ASSERT_TRUE(details.v8Value()->IsObject()); |
| 74 | 82 |
| 75 ScriptValue transactionId(getScriptState(), details.v8Value().As<v8::Object>
()->Get(v8String(getScriptState()->isolate(), "transactionId"))); | 83 ScriptValue transactionId(getScriptState(), details.v8Value().As<v8::Object>
()->Get(v8String(getScriptState()->isolate(), "transactionId"))); |
| 76 | 84 |
| 77 ASSERT_TRUE(transactionId.v8Value()->IsNumber()); | 85 ASSERT_TRUE(transactionId.v8Value()->IsNumber()); |
| 78 EXPECT_EQ(123, transactionId.v8Value().As<v8::Number>()->Value()); | 86 EXPECT_EQ(123, transactionId.v8Value().As<v8::Number>()->Value()); |
| 79 } | 87 } |
| 80 | 88 |
| 81 TEST_F(PaymentResponseTest, CompleteCalledWithSuccess) | 89 TEST_F(PaymentResponseTest, CompleteCalledWithSuccess) |
| 82 { | 90 { |
| 83 mojom::blink::PaymentResponsePtr input = mojom::blink::PaymentResponse::New(
); | 91 mojom::blink::PaymentResponsePtr input = buildPaymentResponseForTest(); |
| 84 input->method_name = "foo"; | 92 input->method_name = "foo"; |
| 85 input->stringified_details = "{\"transactionId\": 123}"; | 93 input->stringified_details = "{\"transactionId\": 123}"; |
| 86 MockPaymentCompleter* completeCallback = new MockPaymentCompleter; | 94 MockPaymentCompleter* completeCallback = new MockPaymentCompleter; |
| 87 PaymentResponse output(std::move(input), completeCallback); | 95 PaymentResponse output(std::move(input), completeCallback); |
| 88 | 96 |
| 89 EXPECT_CALL(*completeCallback, complete(getScriptState(), true)); | 97 EXPECT_CALL(*completeCallback, complete(getScriptState(), true)); |
| 90 | 98 |
| 91 output.complete(getScriptState(), true); | 99 output.complete(getScriptState(), true); |
| 92 } | 100 } |
| 93 | 101 |
| 94 TEST_F(PaymentResponseTest, CompleteCalledWithFailure) | 102 TEST_F(PaymentResponseTest, CompleteCalledWithFailure) |
| 95 { | 103 { |
| 96 mojom::blink::PaymentResponsePtr input = mojom::blink::PaymentResponse::New(
); | 104 mojom::blink::PaymentResponsePtr input = buildPaymentResponseForTest(); |
| 97 input->method_name = "foo"; | 105 input->method_name = "foo"; |
| 98 input->stringified_details = "{\"transactionId\": 123}"; | 106 input->stringified_details = "{\"transactionId\": 123}"; |
| 99 MockPaymentCompleter* completeCallback = new MockPaymentCompleter; | 107 MockPaymentCompleter* completeCallback = new MockPaymentCompleter; |
| 100 PaymentResponse output(std::move(input), completeCallback); | 108 PaymentResponse output(std::move(input), completeCallback); |
| 101 | 109 |
| 102 EXPECT_CALL(*completeCallback, complete(getScriptState(), false)); | 110 EXPECT_CALL(*completeCallback, complete(getScriptState(), false)); |
| 103 | 111 |
| 104 output.complete(getScriptState(), false); | 112 output.complete(getScriptState(), false); |
| 105 } | 113 } |
| 106 | 114 |
| 107 } // namespace | 115 } // namespace |
| 108 } // namespace blink | 116 } // namespace blink |
| OLD | NEW |