OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "bindings/core/v8/V8ObjectBuilder.h" |
| 6 |
| 7 #include "bindings/core/v8/V8Binding.h" |
| 8 #include "bindings/core/v8/V8BindingForTesting.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace blink { |
| 12 |
| 13 namespace { |
| 14 |
| 15 TEST(V8ObjectBuilderTest, addNull) { |
| 16 V8TestingScope scope; |
| 17 ScriptState* scriptState = scope.getScriptState(); |
| 18 V8ObjectBuilder builder(scriptState); |
| 19 builder.addNull("null_check"); |
| 20 ScriptValue jsonObject = builder.scriptValue(); |
| 21 EXPECT_TRUE(jsonObject.isObject()); |
| 22 |
| 23 String jsonString = v8StringToWebCoreString<String>( |
| 24 v8::JSON::Stringify(scope.context(), |
| 25 jsonObject.v8Value().As<v8::Object>()) |
| 26 .ToLocalChecked(), |
| 27 DoNotExternalize); |
| 28 |
| 29 String expected = "{\"null_check\":null}"; |
| 30 EXPECT_EQ(expected, jsonString); |
| 31 } |
| 32 |
| 33 TEST(V8ObjectBuilderTest, addBoolean) { |
| 34 V8TestingScope scope; |
| 35 ScriptState* scriptState = scope.getScriptState(); |
| 36 V8ObjectBuilder builder(scriptState); |
| 37 builder.addBoolean("b1", true); |
| 38 builder.addBoolean("b2", false); |
| 39 ScriptValue jsonObject = builder.scriptValue(); |
| 40 EXPECT_TRUE(jsonObject.isObject()); |
| 41 |
| 42 String jsonString = v8StringToWebCoreString<String>( |
| 43 v8::JSON::Stringify(scope.context(), |
| 44 jsonObject.v8Value().As<v8::Object>()) |
| 45 .ToLocalChecked(), |
| 46 DoNotExternalize); |
| 47 |
| 48 String expected = "{\"b1\":true,\"b2\":false}"; |
| 49 EXPECT_EQ(expected, jsonString); |
| 50 } |
| 51 |
| 52 TEST(V8ObjectBuilderTest, addNumber) { |
| 53 V8TestingScope scope; |
| 54 ScriptState* scriptState = scope.getScriptState(); |
| 55 V8ObjectBuilder builder(scriptState); |
| 56 builder.addNumber("n1", 123); |
| 57 builder.addNumber("n2", 123.456); |
| 58 ScriptValue jsonObject = builder.scriptValue(); |
| 59 EXPECT_TRUE(jsonObject.isObject()); |
| 60 |
| 61 String jsonString = v8StringToWebCoreString<String>( |
| 62 v8::JSON::Stringify(scope.context(), |
| 63 jsonObject.v8Value().As<v8::Object>()) |
| 64 .ToLocalChecked(), |
| 65 DoNotExternalize); |
| 66 |
| 67 String expected = "{\"n1\":123,\"n2\":123.456}"; |
| 68 EXPECT_EQ(expected, jsonString); |
| 69 } |
| 70 |
| 71 TEST(V8ObjectBuilderTest, addString) { |
| 72 V8TestingScope scope; |
| 73 ScriptState* scriptState = scope.getScriptState(); |
| 74 V8ObjectBuilder builder(scriptState); |
| 75 |
| 76 WTF::String test1 = "test1"; |
| 77 WTF::String test2; |
| 78 WTF::String test3 = "test3"; |
| 79 WTF::String test4; |
| 80 |
| 81 builder.addString("test1", test1); |
| 82 builder.addString("test2", test2); |
| 83 builder.addStringOrNull("test3", test3); |
| 84 builder.addStringOrNull("test4", test4); |
| 85 ScriptValue jsonObject = builder.scriptValue(); |
| 86 EXPECT_TRUE(jsonObject.isObject()); |
| 87 |
| 88 String jsonString = v8StringToWebCoreString<String>( |
| 89 v8::JSON::Stringify(scope.context(), |
| 90 jsonObject.v8Value().As<v8::Object>()) |
| 91 .ToLocalChecked(), |
| 92 DoNotExternalize); |
| 93 |
| 94 String expected = |
| 95 "{\"test1\":\"test1\",\"test2\":\"\",\"test3\":\"test3\",\"test4\":" |
| 96 "null}"; |
| 97 EXPECT_EQ(expected, jsonString); |
| 98 } |
| 99 |
| 100 TEST(V8ObjectBuilderTest, add) { |
| 101 V8TestingScope scope; |
| 102 ScriptState* scriptState = scope.getScriptState(); |
| 103 V8ObjectBuilder builder(scriptState); |
| 104 V8ObjectBuilder result(scriptState); |
| 105 builder.addNumber("n1", 123); |
| 106 builder.addNumber("n2", 123.456); |
| 107 result.add("builder", builder); |
| 108 ScriptValue builderJsonObject = builder.scriptValue(); |
| 109 ScriptValue resultJsonObject = result.scriptValue(); |
| 110 EXPECT_TRUE(builderJsonObject.isObject()); |
| 111 EXPECT_TRUE(resultJsonObject.isObject()); |
| 112 |
| 113 String jsonString = v8StringToWebCoreString<String>( |
| 114 v8::JSON::Stringify(scope.context(), |
| 115 resultJsonObject.v8Value().As<v8::Object>()) |
| 116 .ToLocalChecked(), |
| 117 DoNotExternalize); |
| 118 |
| 119 String expected = "{\"builder\":{\"n1\":123,\"n2\":123.456}}"; |
| 120 EXPECT_EQ(expected, jsonString); |
| 121 } |
| 122 |
| 123 } // namespace |
| 124 |
| 125 } // namespace blink |
OLD | NEW |