Chromium Code Reviews| 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 "bindings/modules/v8/V8PaymentResponse.h" | |
| 10 #include "core/testing/DummyPageHolder.h" | 11 #include "core/testing/DummyPageHolder.h" |
| 12 #include "modules/payments/PaymentAddress.h" | |
| 11 #include "modules/payments/PaymentCompleter.h" | 13 #include "modules/payments/PaymentCompleter.h" |
| 14 #include "modules/payments/PaymentDetailsTestHelper.h" | |
| 15 #include "modules/payments/PaymentRequest.h" | |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "wtf/OwnPtr.h" | 18 #include "wtf/OwnPtr.h" |
| 15 #include <utility> | 19 #include <utility> |
| 16 | 20 |
| 17 namespace blink { | 21 namespace blink { |
| 18 namespace { | 22 namespace { |
| 19 | 23 |
| 20 class MockPaymentCompleter : public GarbageCollectedFinalized<MockPaymentComplet er>, public PaymentCompleter { | 24 class MockPaymentCompleter : public GarbageCollectedFinalized<MockPaymentComplet er>, public PaymentCompleter { |
| 21 USING_GARBAGE_COLLECTED_MIXIN(MockPaymentCompleter); | 25 USING_GARBAGE_COLLECTED_MIXIN(MockPaymentCompleter); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 36 | 40 |
| 37 private: | 41 private: |
| 38 ScriptPromise m_dummyPromise; | 42 ScriptPromise m_dummyPromise; |
| 39 }; | 43 }; |
| 40 | 44 |
| 41 class PaymentResponseTest : public testing::Test { | 45 class PaymentResponseTest : public testing::Test { |
| 42 public: | 46 public: |
| 43 PaymentResponseTest() | 47 PaymentResponseTest() |
| 44 : m_page(DummyPageHolder::create()) | 48 : m_page(DummyPageHolder::create()) |
| 45 { | 49 { |
| 50 setSecurityOrigin("https://www.example.com"); | |
|
please use gerrit instead
2016/06/06 01:09:35
You're calling this function only once. So it's si
zino
2016/06/06 10:02:13
Done.
| |
| 46 } | 51 } |
| 47 | 52 |
| 48 ~PaymentResponseTest() override {} | 53 ~PaymentResponseTest() override {} |
| 49 | 54 |
| 50 ScriptState* getScriptState() { return ScriptState::forMainWorld(m_page->doc ument().frame()); } | 55 ScriptState* getScriptState() { return ScriptState::forMainWorld(m_page->doc ument().frame()); } |
| 51 ExceptionState& getExceptionState() { return m_exceptionState; } | 56 ExceptionState& getExceptionState() { return m_exceptionState; } |
| 52 | 57 |
| 58 void setSecurityOrigin(const String& securityOrigin) | |
| 59 { | |
| 60 m_page->document().setSecurityOrigin(SecurityOrigin::create(KURL(KURL(), securityOrigin))); | |
| 61 } | |
| 62 | |
| 53 private: | 63 private: |
| 54 OwnPtr<DummyPageHolder> m_page; | 64 OwnPtr<DummyPageHolder> m_page; |
| 55 NonThrowableExceptionState m_exceptionState; | 65 NonThrowableExceptionState m_exceptionState; |
| 56 }; | 66 }; |
| 57 | 67 |
| 68 class PaymentResponseFunction : public ScriptFunction { | |
| 69 public: | |
| 70 static v8::Local<v8::Function> create(ScriptState* scriptState, ScriptValue* outValue) | |
| 71 { | |
| 72 PaymentResponseFunction* self = new PaymentResponseFunction(scriptState, outValue); | |
| 73 return self->bindToV8Function(); | |
| 74 } | |
| 75 | |
| 76 ScriptValue call(ScriptValue value) override | |
| 77 { | |
| 78 DCHECK(!value.isEmpty()); | |
| 79 *m_value = value; | |
| 80 return value; | |
| 81 } | |
| 82 | |
| 83 private: | |
| 84 explicit PaymentResponseFunction(ScriptState* scriptState, ScriptValue* outV alue) | |
|
please use gerrit instead
2016/06/06 01:09:35
No need for "explicit" keyword with more than one
zino
2016/06/06 10:02:13
Done.
Sorry, this code was created based on MockF
| |
| 85 : ScriptFunction(scriptState) | |
| 86 , m_value(outValue) | |
| 87 { | |
|
please use gerrit instead
2016/06/06 01:09:34
DCHECK(m_value);
zino
2016/06/06 10:02:13
Done.
| |
| 88 } | |
| 89 | |
| 90 ScriptValue* m_value; | |
| 91 }; | |
| 92 | |
| 93 class UnreachableFunction : public ScriptFunction { | |
| 94 public: | |
| 95 static v8::Local<v8::Function> create(ScriptState* scriptState) | |
| 96 { | |
| 97 UnreachableFunction* self = new UnreachableFunction(scriptState); | |
| 98 return self->bindToV8Function(); | |
| 99 } | |
| 100 | |
| 101 ScriptValue call(ScriptValue value) override | |
| 102 { | |
| 103 ADD_FAILURE() << "Unexpected call to a null ScriptFunction."; | |
|
please use gerrit instead
2016/06/06 01:09:35
"Unexpected call"
zino
2016/06/06 10:02:13
Done.
| |
| 104 return value; | |
| 105 } | |
| 106 | |
| 107 private: | |
| 108 UnreachableFunction(ScriptState* scriptState) | |
|
please use gerrit instead
2016/06/06 01:09:34
Need "explicit" keyword here to avoid implicit con
zino
2016/06/06 10:02:13
Done.
| |
| 109 : ScriptFunction(scriptState) | |
| 110 { | |
| 111 } | |
| 112 }; | |
| 113 | |
| 114 | |
| 58 TEST_F(PaymentResponseTest, DataCopiedOver) | 115 TEST_F(PaymentResponseTest, DataCopiedOver) |
| 59 { | 116 { |
| 60 ScriptState::Scope scope(getScriptState()); | 117 ScriptState::Scope scope(getScriptState()); |
| 61 mojom::blink::PaymentResponsePtr input = mojom::blink::PaymentResponse::New( ); | 118 mojom::blink::PaymentResponsePtr input = mojom::blink::PaymentResponse::New( ); |
| 62 input->method_name = "foo"; | 119 input->method_name = "foo"; |
| 63 input->stringified_details = "{\"transactionId\": 123}"; | 120 input->stringified_details = "{\"transactionId\": 123}"; |
| 64 MockPaymentCompleter* completeCallback = new MockPaymentCompleter; | 121 MockPaymentCompleter* completeCallback = new MockPaymentCompleter; |
| 65 | 122 |
| 66 PaymentResponse output(std::move(input), completeCallback); | 123 PaymentResponse output(std::move(input), completeCallback); |
| 67 | 124 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 97 input->method_name = "foo"; | 154 input->method_name = "foo"; |
| 98 input->stringified_details = "{\"transactionId\": 123}"; | 155 input->stringified_details = "{\"transactionId\": 123}"; |
| 99 MockPaymentCompleter* completeCallback = new MockPaymentCompleter; | 156 MockPaymentCompleter* completeCallback = new MockPaymentCompleter; |
| 100 PaymentResponse output(std::move(input), completeCallback); | 157 PaymentResponse output(std::move(input), completeCallback); |
| 101 | 158 |
| 102 EXPECT_CALL(*completeCallback, complete(getScriptState(), false)); | 159 EXPECT_CALL(*completeCallback, complete(getScriptState(), false)); |
| 103 | 160 |
| 104 output.complete(getScriptState(), false); | 161 output.complete(getScriptState(), false); |
| 105 } | 162 } |
| 106 | 163 |
| 164 TEST_F(PaymentResponseTest, ShippingAddressAttributeWithRequestShippingTrue) | |
|
please use gerrit instead
2016/06/06 01:09:35
Plus one more test: options.setRequestShipping(tru
zino
2016/06/06 10:02:13
Done.
| |
| 165 { | |
| 166 ScriptState::Scope scope(getScriptState()); | |
| 167 PaymentOptions options; | |
| 168 options.setRequestShipping(true); | |
| 169 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), options, getExceptionState()); | |
| 170 EXPECT_FALSE(getExceptionState().hadException()); | |
| 171 mojom::blink::PaymentResponsePtr response = mojom::blink::PaymentResponse::N ew(); | |
| 172 response->shipping_address = mojom::blink::PaymentAddress::New(); | |
| 173 response->shipping_address->region_code = "US"; | |
| 174 response->shipping_address->language_code = "en"; | |
| 175 response->shipping_address->script_code = "Latn"; | |
| 176 | |
| 177 ScriptValue outValue; | |
| 178 request->show(getScriptState()).then(PaymentResponseFunction::create(getScri ptState(), &outValue), UnreachableFunction::create(getScriptState())); | |
| 179 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse (std::move(response)); | |
| 180 v8::MicrotasksScope::PerformCheckpoint(getScriptState()->isolate()); | |
| 181 PaymentResponse* pr = V8PaymentResponse::toImplWithTypeCheck(getScriptState( )->isolate(), outValue.v8Value()); | |
| 182 | |
| 183 EXPECT_EQ("US", pr->shippingAddress()->regionCode()); | |
| 184 EXPECT_EQ("en-Latn", pr->shippingAddress()->languageCode()); | |
| 185 } | |
| 186 | |
| 187 TEST_F(PaymentResponseTest, ShippingAddressAttributeWithRequestShippingFalse) | |
| 188 { | |
| 189 ScriptState::Scope scope(getScriptState()); | |
| 190 PaymentOptions options; | |
| 191 options.setRequestShipping(false); | |
| 192 PaymentRequest* request = PaymentRequest::create(getScriptState(), Vector<St ring>(1, "foo"), buildPaymentDetailsForTest(), options, getExceptionState()); | |
| 193 EXPECT_FALSE(getExceptionState().hadException()); | |
| 194 mojom::blink::PaymentResponsePtr response = mojom::blink::PaymentResponse::N ew(); | |
| 195 response->shipping_address = mojom::blink::PaymentAddress::New(); | |
| 196 response->shipping_address->region_code = "US"; | |
| 197 response->shipping_address->language_code = "en"; | |
| 198 response->shipping_address->script_code = "Latn"; | |
| 199 | |
| 200 ScriptValue outValue; | |
| 201 request->show(getScriptState()).then(PaymentResponseFunction::create(getScri ptState(), &outValue), UnreachableFunction::create(getScriptState())); | |
|
please use gerrit instead
2016/06/06 01:09:35
My suggestion in PaymentRequest.cpp is to reject t
zino
2016/06/06 10:02:13
Done.
| |
| 202 static_cast<mojom::blink::PaymentRequestClient*>(request)->OnPaymentResponse (std::move(response)); | |
| 203 v8::MicrotasksScope::PerformCheckpoint(getScriptState()->isolate()); | |
| 204 PaymentResponse* pr = V8PaymentResponse::toImplWithTypeCheck(getScriptState( )->isolate(), outValue.v8Value()); | |
| 205 | |
| 206 EXPECT_EQ(nullptr, pr->shippingAddress()); | |
| 207 } | |
| 208 | |
| 107 } // namespace | 209 } // namespace |
| 108 } // namespace blink | 210 } // namespace blink |
| OLD | NEW |