| 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 <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include "bindings/core/v8/ExceptionState.h" | 9 #include "bindings/core/v8/ExceptionState.h" |
| 10 #include "bindings/core/v8/ScriptState.h" | 10 #include "bindings/core/v8/ScriptState.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 BuildPaymentResponseForTest(); | 48 BuildPaymentResponseForTest(); |
| 49 input->method_name = "foo"; | 49 input->method_name = "foo"; |
| 50 input->stringified_details = "{\"transactionId\": 123}"; | 50 input->stringified_details = "{\"transactionId\": 123}"; |
| 51 input->shipping_option = "standardShippingOption"; | 51 input->shipping_option = "standardShippingOption"; |
| 52 input->payer_name = "Jon Doe"; | 52 input->payer_name = "Jon Doe"; |
| 53 input->payer_email = "abc@gmail.com"; | 53 input->payer_email = "abc@gmail.com"; |
| 54 input->payer_phone = "0123"; | 54 input->payer_phone = "0123"; |
| 55 MockPaymentCompleter* complete_callback = new MockPaymentCompleter; | 55 MockPaymentCompleter* complete_callback = new MockPaymentCompleter; |
| 56 | 56 |
| 57 PaymentResponse* output = | 57 PaymentResponse* output = |
| 58 new PaymentResponse(std::move(input), complete_callback); | 58 new PaymentResponse(std::move(input), complete_callback, ""); |
| 59 | 59 |
| 60 EXPECT_EQ("foo", output->methodName()); | 60 EXPECT_EQ("foo", output->methodName()); |
| 61 EXPECT_EQ("standardShippingOption", output->shippingOption()); | 61 EXPECT_EQ("standardShippingOption", output->shippingOption()); |
| 62 EXPECT_EQ("Jon Doe", output->payerName()); | 62 EXPECT_EQ("Jon Doe", output->payerName()); |
| 63 EXPECT_EQ("abc@gmail.com", output->payerEmail()); | 63 EXPECT_EQ("abc@gmail.com", output->payerEmail()); |
| 64 EXPECT_EQ("0123", output->payerPhone()); | 64 EXPECT_EQ("0123", output->payerPhone()); |
| 65 | 65 |
| 66 ScriptValue details = | 66 ScriptValue details = |
| 67 output->details(scope.GetScriptState(), scope.GetExceptionState()); | 67 output->details(scope.GetScriptState(), scope.GetExceptionState()); |
| 68 | 68 |
| 69 ASSERT_FALSE(scope.GetExceptionState().HadException()); | 69 ASSERT_FALSE(scope.GetExceptionState().HadException()); |
| 70 ASSERT_TRUE(details.V8Value()->IsObject()); | 70 ASSERT_TRUE(details.V8Value()->IsObject()); |
| 71 | 71 |
| 72 ScriptValue transaction_id( | 72 ScriptValue transaction_id( |
| 73 scope.GetScriptState(), | 73 scope.GetScriptState(), |
| 74 details.V8Value().As<v8::Object>()->Get( | 74 details.V8Value().As<v8::Object>()->Get( |
| 75 V8String(scope.GetScriptState()->GetIsolate(), "transactionId"))); | 75 V8String(scope.GetScriptState()->GetIsolate(), "transactionId"))); |
| 76 | 76 |
| 77 ASSERT_TRUE(transaction_id.V8Value()->IsNumber()); | 77 ASSERT_TRUE(transaction_id.V8Value()->IsNumber()); |
| 78 EXPECT_EQ(123, transaction_id.V8Value().As<v8::Number>()->Value()); | 78 EXPECT_EQ(123, transaction_id.V8Value().As<v8::Number>()->Value()); |
| 79 } | 79 } |
| 80 | 80 |
| 81 TEST(PaymentResponseTest, PaymentResponseDetailsJSONObject) { | 81 TEST(PaymentResponseTest, PaymentResponseDetailsJSONObject) { |
| 82 V8TestingScope scope; | 82 V8TestingScope scope; |
| 83 payments::mojom::blink::PaymentResponsePtr input = | 83 payments::mojom::blink::PaymentResponsePtr input = |
| 84 BuildPaymentResponseForTest(); | 84 BuildPaymentResponseForTest(); |
| 85 input->stringified_details = "transactionId"; | 85 input->stringified_details = "transactionId"; |
| 86 MockPaymentCompleter* complete_callback = new MockPaymentCompleter; | 86 MockPaymentCompleter* complete_callback = new MockPaymentCompleter; |
| 87 PaymentResponse* output = | 87 PaymentResponse* output = |
| 88 new PaymentResponse(std::move(input), complete_callback); | 88 new PaymentResponse(std::move(input), complete_callback, ""); |
| 89 | 89 |
| 90 ScriptValue details = | 90 ScriptValue details = |
| 91 output->details(scope.GetScriptState(), scope.GetExceptionState()); | 91 output->details(scope.GetScriptState(), scope.GetExceptionState()); |
| 92 | 92 |
| 93 ASSERT_TRUE(scope.GetExceptionState().HadException()); | 93 ASSERT_TRUE(scope.GetExceptionState().HadException()); |
| 94 } | 94 } |
| 95 | 95 |
| 96 TEST(PaymentResponseTest, CompleteCalledWithSuccess) { | 96 TEST(PaymentResponseTest, CompleteCalledWithSuccess) { |
| 97 V8TestingScope scope; | 97 V8TestingScope scope; |
| 98 payments::mojom::blink::PaymentResponsePtr input = | 98 payments::mojom::blink::PaymentResponsePtr input = |
| 99 BuildPaymentResponseForTest(); | 99 BuildPaymentResponseForTest(); |
| 100 input->method_name = "foo"; | 100 input->method_name = "foo"; |
| 101 input->stringified_details = "{\"transactionId\": 123}"; | 101 input->stringified_details = "{\"transactionId\": 123}"; |
| 102 MockPaymentCompleter* complete_callback = new MockPaymentCompleter; | 102 MockPaymentCompleter* complete_callback = new MockPaymentCompleter; |
| 103 PaymentResponse* output = | 103 PaymentResponse* output = |
| 104 new PaymentResponse(std::move(input), complete_callback); | 104 new PaymentResponse(std::move(input), complete_callback, ""); |
| 105 | 105 |
| 106 EXPECT_CALL(*complete_callback, | 106 EXPECT_CALL(*complete_callback, |
| 107 Complete(scope.GetScriptState(), PaymentCompleter::kSuccess)); | 107 Complete(scope.GetScriptState(), PaymentCompleter::kSuccess)); |
| 108 | 108 |
| 109 output->complete(scope.GetScriptState(), "success"); | 109 output->complete(scope.GetScriptState(), "success"); |
| 110 } | 110 } |
| 111 | 111 |
| 112 TEST(PaymentResponseTest, CompleteCalledWithFailure) { | 112 TEST(PaymentResponseTest, CompleteCalledWithFailure) { |
| 113 V8TestingScope scope; | 113 V8TestingScope scope; |
| 114 payments::mojom::blink::PaymentResponsePtr input = | 114 payments::mojom::blink::PaymentResponsePtr input = |
| 115 BuildPaymentResponseForTest(); | 115 BuildPaymentResponseForTest(); |
| 116 input->method_name = "foo"; | 116 input->method_name = "foo"; |
| 117 input->stringified_details = "{\"transactionId\": 123}"; | 117 input->stringified_details = "{\"transactionId\": 123}"; |
| 118 MockPaymentCompleter* complete_callback = new MockPaymentCompleter; | 118 MockPaymentCompleter* complete_callback = new MockPaymentCompleter; |
| 119 PaymentResponse* output = | 119 PaymentResponse* output = |
| 120 new PaymentResponse(std::move(input), complete_callback); | 120 new PaymentResponse(std::move(input), complete_callback, ""); |
| 121 | 121 |
| 122 EXPECT_CALL(*complete_callback, | 122 EXPECT_CALL(*complete_callback, |
| 123 Complete(scope.GetScriptState(), PaymentCompleter::kFail)); | 123 Complete(scope.GetScriptState(), PaymentCompleter::kFail)); |
| 124 | 124 |
| 125 output->complete(scope.GetScriptState(), "fail"); | 125 output->complete(scope.GetScriptState(), "fail"); |
| 126 } | 126 } |
| 127 | 127 |
| 128 TEST(PaymentResponseTest, JSONSerializerTest) { | 128 TEST(PaymentResponseTest, JSONSerializerTest) { |
| 129 V8TestingScope scope; | 129 V8TestingScope scope; |
| 130 payments::mojom::blink::PaymentResponsePtr input = | 130 payments::mojom::blink::PaymentResponsePtr input = |
| 131 payments::mojom::blink::PaymentResponse::New(); | 131 payments::mojom::blink::PaymentResponse::New(); |
| 132 input->method_name = "foo"; | 132 input->method_name = "foo"; |
| 133 input->stringified_details = "{\"transactionId\": 123}"; | 133 input->stringified_details = "{\"transactionId\": 123}"; |
| 134 input->shipping_option = "standardShippingOption"; | 134 input->shipping_option = "standardShippingOption"; |
| 135 input->payer_email = "abc@gmail.com"; | 135 input->payer_email = "abc@gmail.com"; |
| 136 input->payer_phone = "0123"; | 136 input->payer_phone = "0123"; |
| 137 input->payer_name = "Jon Doe"; | 137 input->payer_name = "Jon Doe"; |
| 138 input->shipping_address = payments::mojom::blink::PaymentAddress::New(); | 138 input->shipping_address = payments::mojom::blink::PaymentAddress::New(); |
| 139 input->shipping_address->country = "US"; | 139 input->shipping_address->country = "US"; |
| 140 input->shipping_address->language_code = "en"; | 140 input->shipping_address->language_code = "en"; |
| 141 input->shipping_address->script_code = "Latn"; | 141 input->shipping_address->script_code = "Latn"; |
| 142 input->shipping_address->address_line.push_back("340 Main St"); | 142 input->shipping_address->address_line.push_back("340 Main St"); |
| 143 input->shipping_address->address_line.push_back("BIN1"); | 143 input->shipping_address->address_line.push_back("BIN1"); |
| 144 input->shipping_address->address_line.push_back("First floor"); | 144 input->shipping_address->address_line.push_back("First floor"); |
| 145 | 145 |
| 146 PaymentResponse* output = | 146 PaymentResponse* output = |
| 147 new PaymentResponse(std::move(input), new MockPaymentCompleter); | 147 new PaymentResponse(std::move(input), new MockPaymentCompleter, ""); |
| 148 ScriptValue json_object = output->toJSONForBinding(scope.GetScriptState()); | 148 ScriptValue json_object = output->toJSONForBinding(scope.GetScriptState()); |
| 149 EXPECT_TRUE(json_object.IsObject()); | 149 EXPECT_TRUE(json_object.IsObject()); |
| 150 | 150 |
| 151 String json_string = V8StringToWebCoreString<String>( | 151 String json_string = V8StringToWebCoreString<String>( |
| 152 v8::JSON::Stringify(scope.GetContext(), | 152 v8::JSON::Stringify(scope.GetContext(), |
| 153 json_object.V8Value().As<v8::Object>()) | 153 json_object.V8Value().As<v8::Object>()) |
| 154 .ToLocalChecked(), | 154 .ToLocalChecked(), |
| 155 kDoNotExternalize); | 155 kDoNotExternalize); |
| 156 String expected = | 156 String expected = |
| 157 "{\"methodName\":\"foo\",\"details\":{\"transactionId\":123}," | 157 "{\"requestId\":\"\",\"methodName\":\"foo\",\"details\":{" |
| 158 "\"transactionId\":123}," |
| 158 "\"shippingAddress\":{\"country\":\"US\",\"addressLine\":[\"340 Main " | 159 "\"shippingAddress\":{\"country\":\"US\",\"addressLine\":[\"340 Main " |
| 159 "St\"," | 160 "St\"," |
| 160 "\"BIN1\",\"First " | 161 "\"BIN1\",\"First " |
| 161 "floor\"],\"region\":\"\",\"city\":\"\",\"dependentLocality\":" | 162 "floor\"],\"region\":\"\",\"city\":\"\",\"dependentLocality\":" |
| 162 "\"\",\"postalCode\":\"\",\"sortingCode\":\"\",\"languageCode\":\"en-" | 163 "\"\",\"postalCode\":\"\",\"sortingCode\":\"\",\"languageCode\":\"en-" |
| 163 "Latn\"," | 164 "Latn\"," |
| 164 "\"organization\":\"\",\"recipient\":\"\",\"phone\":\"\"}," | 165 "\"organization\":\"\",\"recipient\":\"\",\"phone\":\"\"}," |
| 165 "\"shippingOption\":" | 166 "\"shippingOption\":" |
| 166 "\"standardShippingOption\",\"payerName\":\"Jon Doe\"," | 167 "\"standardShippingOption\",\"payerName\":\"Jon Doe\"," |
| 167 "\"payerEmail\":\"abc@gmail.com\",\"payerPhone\":\"0123\"}"; | 168 "\"payerEmail\":\"abc@gmail.com\",\"payerPhone\":\"0123\"}"; |
| 168 EXPECT_EQ(expected, json_string); | 169 EXPECT_EQ(expected, json_string); |
| 169 } | 170 } |
| 170 | 171 |
| 171 } // namespace | 172 } // namespace |
| 172 } // namespace blink | 173 } // namespace blink |
| OLD | NEW |