OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "bindings/core/v8/SerializedScriptValue.h" | 5 #include "bindings/core/v8/SerializedScriptValue.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
8 #include "bindings/core/v8/SerializedScriptValueFactory.h" | 8 #include "bindings/core/v8/SerializedScriptValueFactory.h" |
9 #include "bindings/core/v8/V8Binding.h" | 9 #include "bindings/core/v8/V8Binding.h" |
10 #include "bindings/core/v8/V8BindingForTesting.h" | 10 #include "bindings/core/v8/V8BindingForTesting.h" |
11 #include "bindings/core/v8/V8File.h" | 11 #include "bindings/core/v8/V8File.h" |
| 12 #include "bindings/core/v8/V8ImageData.h" |
12 #include "core/fileapi/File.h" | 13 #include "core/fileapi/File.h" |
13 #include "platform/testing/UnitTestHelpers.h" | 14 #include "platform/testing/UnitTestHelpers.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 namespace blink { | 17 namespace blink { |
17 | 18 |
| 19 TEST(SerializedScriptValueTest, WireFormatRoundTrip) { |
| 20 V8TestingScope scope; |
| 21 |
| 22 v8::Local<v8::Value> v8OriginalTrue = v8::True(scope.GetIsolate()); |
| 23 RefPtr<SerializedScriptValue> sourceSerializedScriptValue = |
| 24 SerializedScriptValue::Serialize( |
| 25 scope.GetIsolate(), v8OriginalTrue, |
| 26 SerializedScriptValue::SerializeOptions(), ASSERT_NO_EXCEPTION); |
| 27 |
| 28 Vector<char> wireData; |
| 29 sourceSerializedScriptValue->ToWireBytes(wireData); |
| 30 |
| 31 RefPtr<SerializedScriptValue> serializedScriptValue = |
| 32 SerializedScriptValue::Create(wireData.Data(), wireData.size()); |
| 33 v8::Local<v8::Value> deserialized = |
| 34 serializedScriptValue->Deserialize(scope.GetIsolate()); |
| 35 EXPECT_TRUE(deserialized->IsTrue()); |
| 36 } |
| 37 |
| 38 TEST(SerializedScriptValueTest, WireFormatVersion17NoByteSwapping) { |
| 39 V8TestingScope scope; |
| 40 |
| 41 const uint8_t data[] = {0xFF, 0x11, 0xFF, 0x0D, 0x54, 0x00}; |
| 42 RefPtr<SerializedScriptValue> serializedScriptValue = |
| 43 SerializedScriptValue::Create(reinterpret_cast<const char*>(data), |
| 44 sizeof(data)); |
| 45 v8::Local<v8::Value> deserialized = |
| 46 serializedScriptValue->Deserialize(scope.GetIsolate()); |
| 47 EXPECT_TRUE(deserialized->IsTrue()); |
| 48 } |
| 49 |
| 50 TEST(SerializedScriptValueTest, WireFormatVersion16ByteSwapping) { |
| 51 V8TestingScope scope; |
| 52 |
| 53 // Using UChar instead of uint8_t to get ntohs() byte swapping. |
| 54 const UChar data[] = {0xFF10, 0xFF0D, 0x5400}; |
| 55 RefPtr<SerializedScriptValue> serializedScriptValue = |
| 56 SerializedScriptValue::Create(reinterpret_cast<const char*>(data), |
| 57 sizeof(data)); |
| 58 v8::Local<v8::Value> deserialized = |
| 59 serializedScriptValue->Deserialize(scope.GetIsolate()); |
| 60 EXPECT_TRUE(deserialized->IsTrue()); |
| 61 } |
| 62 |
| 63 TEST(SerializedScriptValueTest, WireFormatVersion13ByteSwapping) { |
| 64 V8TestingScope scope; |
| 65 |
| 66 // Using UChar instead of uint8_t to get ntohs() byte swapping. |
| 67 const UChar data[] = {0xFF0D, 0x5400}; |
| 68 RefPtr<SerializedScriptValue> serializedScriptValue = |
| 69 SerializedScriptValue::Create(reinterpret_cast<const char*>(data), |
| 70 sizeof(data)); |
| 71 v8::Local<v8::Value> deserialized = |
| 72 serializedScriptValue->Deserialize(scope.GetIsolate()); |
| 73 EXPECT_TRUE(deserialized->IsTrue()); |
| 74 } |
| 75 |
| 76 TEST(SerializedScriptValueTest, WireFormatVersion0ByteSwapping) { |
| 77 V8TestingScope scope; |
| 78 |
| 79 // Using UChar instead of uint8_t to get ntohs() byte swapping. |
| 80 const UChar data[] = {0x5400}; |
| 81 RefPtr<SerializedScriptValue> serializedScriptValue = |
| 82 SerializedScriptValue::Create(reinterpret_cast<const char*>(data), |
| 83 sizeof(data)); |
| 84 v8::Local<v8::Value> deserialized = |
| 85 serializedScriptValue->Deserialize(scope.GetIsolate()); |
| 86 EXPECT_TRUE(deserialized->IsTrue()); |
| 87 } |
| 88 |
| 89 TEST(SerializedScriptValueTest, WireFormatVersion0ImageData) { |
| 90 V8TestingScope scope; |
| 91 v8::Isolate* isolate = scope.GetIsolate(); |
| 92 |
| 93 // Using UChar instead of uint8_t to get ntohs() byte swapping. |
| 94 // |
| 95 // This builds the smallest possible ImageData whose first data byte is 0xFF, |
| 96 // as follows. |
| 97 // |
| 98 // width = 127, encoded as 0xFF 0x00 (degenerate varint) |
| 99 // height = 1, encoded as 0x01 (varint) |
| 100 // pixelLength = 508 (127 * 1 * 4), encoded as 0xFC 0x03 (varint) |
| 101 // pixel data = 508 bytes, all zero |
| 102 Vector<UChar> data; |
| 103 data.push_back(0x23FF); |
| 104 data.push_back(0x001); |
| 105 data.push_back(0xFC03); |
| 106 data.Resize(257); // (508 pixel data + 6 header bytes) / 2 |
| 107 |
| 108 RefPtr<SerializedScriptValue> serializedScriptValue = |
| 109 SerializedScriptValue::Create(reinterpret_cast<const char*>(data.Data()), |
| 110 data.size() * sizeof(UChar)); |
| 111 v8::Local<v8::Value> deserialized = |
| 112 serializedScriptValue->Deserialize(isolate); |
| 113 ASSERT_TRUE(deserialized->IsObject()); |
| 114 v8::Local<v8::Object> deserializedObject = deserialized.As<v8::Object>(); |
| 115 ASSERT_TRUE(V8ImageData::hasInstance(deserializedObject, isolate)); |
| 116 ImageData* imageData = V8ImageData::toImpl(deserializedObject); |
| 117 EXPECT_EQ(imageData->width(), 127); |
| 118 EXPECT_EQ(imageData->height(), 1); |
| 119 } |
| 120 |
18 TEST(SerializedScriptValueTest, UserSelectedFile) { | 121 TEST(SerializedScriptValueTest, UserSelectedFile) { |
19 V8TestingScope scope; | 122 V8TestingScope scope; |
20 String file_path = testing::BlinkRootDir(); | 123 String file_path = testing::BlinkRootDir(); |
21 file_path.Append("/Source/bindings/core/v8/SerializedScriptValueTest.cpp"); | 124 file_path.Append("/Source/bindings/core/v8/SerializedScriptValueTest.cpp"); |
22 File* original_file = File::Create(file_path); | 125 File* original_file = File::Create(file_path); |
23 ASSERT_TRUE(original_file->HasBackingFile()); | 126 ASSERT_TRUE(original_file->HasBackingFile()); |
24 ASSERT_EQ(File::kIsUserVisible, original_file->GetUserVisibility()); | 127 ASSERT_EQ(File::kIsUserVisible, original_file->GetUserVisibility()); |
25 ASSERT_EQ(file_path, original_file->GetPath()); | 128 ASSERT_EQ(file_path, original_file->GetPath()); |
26 | 129 |
27 v8::Local<v8::Value> v8_original_file = | 130 v8::Local<v8::Value> v8_original_file = |
(...skipping 30 matching lines...) Expand all Loading... |
58 serialized_script_value->Deserialize(scope.GetIsolate()); | 161 serialized_script_value->Deserialize(scope.GetIsolate()); |
59 | 162 |
60 ASSERT_TRUE(V8File::hasInstance(v8_file, scope.GetIsolate())); | 163 ASSERT_TRUE(V8File::hasInstance(v8_file, scope.GetIsolate())); |
61 File* file = V8File::toImpl(v8::Local<v8::Object>::Cast(v8_file)); | 164 File* file = V8File::toImpl(v8::Local<v8::Object>::Cast(v8_file)); |
62 EXPECT_FALSE(file->HasBackingFile()); | 165 EXPECT_FALSE(file->HasBackingFile()); |
63 EXPECT_EQ(File::kIsNotUserVisible, file->GetUserVisibility()); | 166 EXPECT_EQ(File::kIsNotUserVisible, file->GetUserVisibility()); |
64 EXPECT_EQ("hello.txt", file->name()); | 167 EXPECT_EQ("hello.txt", file->name()); |
65 } | 168 } |
66 | 169 |
67 } // namespace blink | 170 } // namespace blink |
OLD | NEW |