Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: content/browser/android/string_message_codec_unittest.cc

Issue 2422793002: HTML MessagePort as mojo::MessagePipeHandle (Closed)
Patch Set: Add missing ScopedAsyncTaskScheduler instance for the new unit tests; required by a recent change t… Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 "content/browser/android/string_message_codec.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/test/scoped_async_task_scheduler.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "v8/include/v8.h"
11
12 namespace content {
13 namespace {
14
15 base::string16 DecodeWithV8(const base::string16& encoded) {
16 base::test::ScopedAsyncTaskScheduler task_scheduler;
17 base::string16 result;
18
19 v8::Isolate::CreateParams params;
20 params.array_buffer_allocator =
21 v8::ArrayBuffer::Allocator::NewDefaultAllocator();
22 v8::Isolate* isolate = v8::Isolate::New(params);
23 {
24 v8::HandleScope scope(isolate);
25 v8::TryCatch try_catch(isolate);
26
27 v8::Local<v8::Context> context = v8::Context::New(isolate);
28
29 v8::ValueDeserializer deserializer(
30 isolate,
31 reinterpret_cast<const uint8_t*>(encoded.data()),
32 encoded.size() * sizeof(base::char16));
33 deserializer.SetSupportsLegacyWireFormat(true);
34
35 EXPECT_TRUE(deserializer.ReadHeader(context).ToChecked());
36
37 v8::Local<v8::Value> value =
38 deserializer.ReadValue(context).ToLocalChecked();
39 v8::Local<v8::String> str = value->ToString(context).ToLocalChecked();
40
41 result.resize(str->Length());
42 str->Write(&result[0], 0, result.size());
43 }
44 isolate->Dispose();
45 delete params.array_buffer_allocator;
46
47 return result;
48 }
49
50 base::string16 EncodeWithV8(const base::string16& message) {
51 base::test::ScopedAsyncTaskScheduler task_scheduler;
52 base::string16 result;
53
54 v8::Isolate::CreateParams params;
55 params.array_buffer_allocator =
56 v8::ArrayBuffer::Allocator::NewDefaultAllocator();
57 v8::Isolate* isolate = v8::Isolate::New(params);
58 {
59 v8::HandleScope scope(isolate);
60 v8::TryCatch try_catch(isolate);
61
62 v8::Local<v8::Context> context = v8::Context::New(isolate);
63
64 v8::Local<v8::String> message_as_value =
65 v8::String::NewFromTwoByte(isolate,
66 message.data(),
67 v8::NewStringType::kNormal,
68 message.size()).ToLocalChecked();
69
70 v8::ValueSerializer serializer(isolate);
71 serializer.WriteHeader();
72 EXPECT_TRUE(serializer.WriteValue(context, message_as_value).ToChecked());
73
74 std::pair<uint8_t*, size_t> buffer = serializer.Release();
75
76 size_t result_num_bytes = (buffer.second + 1) & ~1;
77 result.resize(result_num_bytes / 2);
78 memcpy(reinterpret_cast<uint8_t*>(&result[0]), buffer.first, buffer.second);
79
80 free(buffer.first);
81 }
82 isolate->Dispose();
83 delete params.array_buffer_allocator;
84
85 return result;
86 }
87
88 TEST(StringMessageCodecTest, SelfTest_ASCII) {
89 base::string16 message = base::ASCIIToUTF16("hello");
90 base::string16 decoded;
91 EXPECT_TRUE(DecodeStringMessage(EncodeStringMessage(message), &decoded));
92 EXPECT_EQ(message, decoded);
93 }
94
95 TEST(StringMessageCodecTest, SelfTest_NonASCII) {
96 base::string16 message = base::WideToUTF16(L"hello \u263A");
97 base::string16 decoded;
98 EXPECT_TRUE(DecodeStringMessage(EncodeStringMessage(message), &decoded));
99 EXPECT_EQ(message, decoded);
100 }
101
102 TEST(StringMessageCodecTest, SelfTest_NonASCIILongEnoughToForcePadding) {
103 base::string16 message(200, 0x263A);
104 base::string16 decoded;
105 EXPECT_TRUE(DecodeStringMessage(EncodeStringMessage(message), &decoded));
106 EXPECT_EQ(message, decoded);
107 }
108
109 TEST(StringMessageCodecTest, SelfToV8Test_ASCII) {
110 base::string16 message = base::ASCIIToUTF16("hello");
111 EXPECT_EQ(message, DecodeWithV8(EncodeStringMessage(message)));
112 }
113
114 TEST(StringMessageCodecTest, SelfToV8Test_NonASCII) {
115 base::string16 message = base::WideToUTF16(L"hello \u263A");
116 EXPECT_EQ(message, DecodeWithV8(EncodeStringMessage(message)));
117 }
118
119 TEST(StringMessageCodecTest, SelfToV8Test_NonASCIILongEnoughToForcePadding) {
120 base::string16 message(200, 0x263A);
121 EXPECT_EQ(message, DecodeWithV8(EncodeStringMessage(message)));
122 }
123
124 TEST(StringMessageCodecTest, V8ToSelfTest_ASCII) {
125 base::string16 message = base::ASCIIToUTF16("hello");
126 base::string16 decoded;
127 EXPECT_TRUE(DecodeStringMessage(EncodeWithV8(message), &decoded));
128 EXPECT_EQ(message, decoded);
129 }
130
131 TEST(StringMessageCodecTest, V8ToSelfTest_NonASCII) {
132 base::string16 message = base::WideToUTF16(L"hello \u263A");
133 base::string16 decoded;
134 EXPECT_TRUE(DecodeStringMessage(EncodeWithV8(message), &decoded));
135 EXPECT_EQ(message, decoded);
136 }
137
138 TEST(StringMessageCodecTest, V8ToSelfTest_NonASCIILongEnoughToForcePadding) {
139 base::string16 message(200, 0x263A);
140 base::string16 decoded;
141 EXPECT_TRUE(DecodeStringMessage(EncodeWithV8(message), &decoded));
142 EXPECT_EQ(message, decoded);
143 }
144
145 } // namespace
146 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/android/string_message_codec.cc ('k') | content/browser/frame_host/render_frame_host_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698