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 <stddef.h> | 5 #include "bindings/core/v8/ScriptController.h" |
6 #include <stdint.h> | 6 #include "bindings/core/v8/ScriptSourceCode.h" |
| 7 #include "bindings/core/v8/V8BindingForCore.h" |
| 8 #include "bindings/core/v8/V8BindingForTesting.h" |
| 9 #include "bindings/core/v8/V8ScriptRunner.h" |
| 10 #include "core/frame/Settings.h" |
| 11 #include "core/mojo/MojoHandle.h" |
| 12 #include "core/page/Page.h" |
| 13 #include "mojo/public/cpp/bindings/binding.h" |
| 14 #include "mojo/public/cpp/system/wait.h" |
| 15 #include "platform/testing/UnitTestHelpers.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "third_party/WebKit/Source/core/mojo/tests/JsToCpp.mojom-blink.h" |
7 | 18 |
8 #include <string> | 19 namespace blink { |
9 #include <utility> | 20 namespace { |
10 | |
11 #include "base/at_exit.h" | |
12 #include "base/files/file_path.h" | |
13 #include "base/files/file_util.h" | |
14 #include "base/macros.h" | |
15 #include "base/run_loop.h" | |
16 #include "base/strings/utf_string_conversions.h" | |
17 #include "base/test/scoped_task_environment.h" | |
18 #include "base/threading/thread_task_runner_handle.h" | |
19 #include "gin/array_buffer.h" | |
20 #include "gin/public/isolate_holder.h" | |
21 #include "gin/v8_initializer.h" | |
22 #include "mojo/common/data_pipe_utils.h" | |
23 #include "mojo/edk/js/mojo_runner_delegate.h" | |
24 #include "mojo/edk/js/tests/js_to_cpp.mojom.h" | |
25 #include "mojo/public/cpp/bindings/binding.h" | |
26 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | |
27 #include "mojo/public/cpp/system/core.h" | |
28 #include "mojo/public/cpp/system/wait.h" | |
29 #include "testing/gtest/include/gtest/gtest.h" | |
30 | |
31 namespace mojo { | |
32 namespace edk { | |
33 namespace js { | |
34 | 21 |
35 // Global value updated by some checks to prevent compilers from optimizing | 22 // Global value updated by some checks to prevent compilers from optimizing |
36 // reads out of existence. | 23 // reads out of existence. |
37 uint32_t g_waste_accumulator = 0; | 24 uint32_t g_waste_accumulator = 0; |
38 | 25 |
39 namespace { | |
40 | |
41 // Negative numbers with different values in each byte, the last of | 26 // Negative numbers with different values in each byte, the last of |
42 // which can survive promotion to double and back. | 27 // which can survive promotion to double and back. |
43 const int8_t kExpectedInt8Value = -65; | 28 const int8_t kExpectedInt8Value = -65; |
44 const int16_t kExpectedInt16Value = -16961; | 29 const int16_t kExpectedInt16Value = -16961; |
45 const int32_t kExpectedInt32Value = -1145258561; | 30 const int32_t kExpectedInt32Value = -1145258561; |
46 const int64_t kExpectedInt64Value = -77263311946305LL; | 31 const int64_t kExpectedInt64Value = -77263311946305LL; |
47 | 32 |
48 // Positive numbers with different values in each byte, the last of | 33 // Positive numbers with different values in each byte, the last of |
49 // which can survive promotion to double and back. | 34 // which can survive promotion to double and back. |
50 const uint8_t kExpectedUInt8Value = 65; | 35 const uint8_t kExpectedUInt8Value = 65; |
51 const uint16_t kExpectedUInt16Value = 16961; | 36 const uint16_t kExpectedUInt16Value = 16961; |
52 const uint32_t kExpectedUInt32Value = 1145258561; | 37 const uint32_t kExpectedUInt32Value = 1145258561; |
53 const uint64_t kExpectedUInt64Value = 77263311946305LL; | 38 const uint64_t kExpectedUInt64Value = 77263311946305LL; |
54 | 39 |
55 // Double/float values, including special case constants. | 40 // Double/float values, including special case constants. |
56 const double kExpectedDoubleVal = 3.14159265358979323846; | 41 const double kExpectedDoubleVal = 3.14159265358979323846; |
57 const double kExpectedDoubleInf = std::numeric_limits<double>::infinity(); | 42 const double kExpectedDoubleInf = std::numeric_limits<double>::infinity(); |
58 const double kExpectedDoubleNan = std::numeric_limits<double>::quiet_NaN(); | 43 const double kExpectedDoubleNan = std::numeric_limits<double>::quiet_NaN(); |
59 const float kExpectedFloatVal = static_cast<float>(kExpectedDoubleVal); | 44 const float kExpectedFloatVal = static_cast<float>(kExpectedDoubleVal); |
60 const float kExpectedFloatInf = std::numeric_limits<float>::infinity(); | 45 const float kExpectedFloatInf = std::numeric_limits<float>::infinity(); |
61 const float kExpectedFloatNan = std::numeric_limits<float>::quiet_NaN(); | 46 const float kExpectedFloatNan = std::numeric_limits<float>::quiet_NaN(); |
62 | 47 |
63 // NaN has the property that it is not equal to itself. | 48 // NaN has the property that it is not equal to itself. |
64 #define EXPECT_NAN(x) EXPECT_NE(x, x) | 49 #define EXPECT_NAN(x) EXPECT_NE(x, x) |
65 | 50 |
66 void CheckDataPipe(ScopedDataPipeConsumerHandle data_pipe_handle) { | 51 String MojoBindingsScriptPath() { |
67 std::string buffer; | 52 String filepath = testing::ExecutableDir(); |
68 bool result = common::BlockingCopyToString(std::move(data_pipe_handle), | 53 filepath.append("/gen/mojo/public/js/mojo_bindings.js"); |
69 &buffer); | 54 return filepath; |
70 EXPECT_TRUE(result); | |
71 EXPECT_EQ(64u, buffer.size()); | |
72 for (int i = 0; i < 64; ++i) { | |
73 EXPECT_EQ(i, buffer[i]); | |
74 } | |
75 } | 55 } |
76 | 56 |
77 void CheckMessagePipe(MessagePipeHandle message_pipe_handle) { | 57 String TestBindingsScriptPath() { |
| 58 String filepath = testing::ExecutableDir(); |
| 59 filepath.append( |
| 60 "/gen/third_party/WebKit/Source/core/mojo/tests/JsToCpp.mojom.js"); |
| 61 return filepath; |
| 62 } |
| 63 |
| 64 String TestScriptPath() { |
| 65 String filepath = testing::BlinkRootDir(); |
| 66 filepath.append("/Source/core/mojo/tests/JsToCppTest.js"); |
| 67 return filepath; |
| 68 } |
| 69 |
| 70 v8::Local<v8::Value> ExecuteScript(const String& script_path, |
| 71 LocalFrame& frame) { |
| 72 RefPtr<SharedBuffer> script_src = testing::ReadFromFile(script_path); |
| 73 return frame.GetScriptController().ExecuteScriptInMainWorldAndReturnValue( |
| 74 ScriptSourceCode(String(script_src->Data(), script_src->size()))); |
| 75 } |
| 76 |
| 77 void CheckDataPipe(mojo::DataPipeConsumerHandle data_pipe_handle) { |
| 78 const void* buffer = nullptr; |
| 79 unsigned num_bytes = 0; |
| 80 MojoResult result = Wait(data_pipe_handle, MOJO_HANDLE_SIGNAL_READABLE); |
| 81 EXPECT_EQ(MOJO_RESULT_OK, result); |
| 82 result = BeginReadDataRaw(data_pipe_handle, &buffer, &num_bytes, |
| 83 MOJO_READ_DATA_FLAG_NONE); |
| 84 EXPECT_EQ(MOJO_RESULT_OK, result); |
| 85 EXPECT_EQ(64u, num_bytes); |
| 86 for (unsigned i = 0; i < num_bytes; ++i) { |
| 87 EXPECT_EQ(i, static_cast<unsigned>(static_cast<const char*>(buffer)[i])); |
| 88 } |
| 89 EndReadDataRaw(data_pipe_handle, num_bytes); |
| 90 } |
| 91 |
| 92 void CheckMessagePipe(mojo::MessagePipeHandle message_pipe_handle) { |
78 unsigned char buffer[100]; | 93 unsigned char buffer[100]; |
79 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); | 94 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); |
80 MojoResult result = Wait(message_pipe_handle, MOJO_HANDLE_SIGNAL_READABLE); | 95 MojoResult result = Wait(message_pipe_handle, MOJO_HANDLE_SIGNAL_READABLE); |
81 EXPECT_EQ(MOJO_RESULT_OK, result); | 96 EXPECT_EQ(MOJO_RESULT_OK, result); |
82 result = ReadMessageRaw( | 97 result = ReadMessageRaw(message_pipe_handle, buffer, &buffer_size, 0, 0, 0); |
83 message_pipe_handle, buffer, &buffer_size, 0, 0, 0); | |
84 EXPECT_EQ(MOJO_RESULT_OK, result); | 98 EXPECT_EQ(MOJO_RESULT_OK, result); |
85 EXPECT_EQ(64u, buffer_size); | 99 EXPECT_EQ(64u, buffer_size); |
86 for (int i = 0; i < 64; ++i) { | 100 for (int i = 0; i < 64; ++i) { |
87 EXPECT_EQ(255 - i, buffer[i]); | 101 EXPECT_EQ(255 - i, buffer[i]); |
88 } | 102 } |
89 } | 103 } |
90 | 104 |
91 js_to_cpp::EchoArgsPtr BuildSampleEchoArgs() { | 105 js_to_cpp::blink::EchoArgsPtr BuildSampleEchoArgs() { |
92 js_to_cpp::EchoArgsPtr args(js_to_cpp::EchoArgs::New()); | 106 auto args = js_to_cpp::blink::EchoArgs::New(); |
93 args->si64 = kExpectedInt64Value; | 107 args->si64 = kExpectedInt64Value; |
94 args->si32 = kExpectedInt32Value; | 108 args->si32 = kExpectedInt32Value; |
95 args->si16 = kExpectedInt16Value; | 109 args->si16 = kExpectedInt16Value; |
96 args->si8 = kExpectedInt8Value; | 110 args->si8 = kExpectedInt8Value; |
97 args->ui64 = kExpectedUInt64Value; | 111 args->ui64 = kExpectedUInt64Value; |
98 args->ui32 = kExpectedUInt32Value; | 112 args->ui32 = kExpectedUInt32Value; |
99 args->ui16 = kExpectedUInt16Value; | 113 args->ui16 = kExpectedUInt16Value; |
100 args->ui8 = kExpectedUInt8Value; | 114 args->ui8 = kExpectedUInt8Value; |
101 args->float_val = kExpectedFloatVal; | 115 args->float_val = kExpectedFloatVal; |
102 args->float_inf = kExpectedFloatInf; | 116 args->float_inf = kExpectedFloatInf; |
103 args->float_nan = kExpectedFloatNan; | 117 args->float_nan = kExpectedFloatNan; |
104 args->double_val = kExpectedDoubleVal; | 118 args->double_val = kExpectedDoubleVal; |
105 args->double_inf = kExpectedDoubleInf; | 119 args->double_inf = kExpectedDoubleInf; |
106 args->double_nan = kExpectedDoubleNan; | 120 args->double_nan = kExpectedDoubleNan; |
107 args->name.emplace("coming"); | 121 args->name = "coming"; |
108 args->string_array.emplace(3); | 122 args->string_array.emplace(3); |
109 (*args->string_array)[0] = "one"; | 123 (*args->string_array)[0] = "one"; |
110 (*args->string_array)[1] = "two"; | 124 (*args->string_array)[1] = "two"; |
111 (*args->string_array)[2] = "three"; | 125 (*args->string_array)[2] = "three"; |
112 return args; | 126 return args; |
113 } | 127 } |
114 | 128 |
115 void CheckSampleEchoArgs(js_to_cpp::EchoArgsPtr arg) { | 129 void CheckSampleEchoArgs(const js_to_cpp::blink::EchoArgsPtr& arg) { |
116 EXPECT_EQ(kExpectedInt64Value, arg->si64); | 130 EXPECT_EQ(kExpectedInt64Value, arg->si64); |
117 EXPECT_EQ(kExpectedInt32Value, arg->si32); | 131 EXPECT_EQ(kExpectedInt32Value, arg->si32); |
118 EXPECT_EQ(kExpectedInt16Value, arg->si16); | 132 EXPECT_EQ(kExpectedInt16Value, arg->si16); |
119 EXPECT_EQ(kExpectedInt8Value, arg->si8); | 133 EXPECT_EQ(kExpectedInt8Value, arg->si8); |
120 EXPECT_EQ(kExpectedUInt64Value, arg->ui64); | 134 EXPECT_EQ(kExpectedUInt64Value, arg->ui64); |
121 EXPECT_EQ(kExpectedUInt32Value, arg->ui32); | 135 EXPECT_EQ(kExpectedUInt32Value, arg->ui32); |
122 EXPECT_EQ(kExpectedUInt16Value, arg->ui16); | 136 EXPECT_EQ(kExpectedUInt16Value, arg->ui16); |
123 EXPECT_EQ(kExpectedUInt8Value, arg->ui8); | 137 EXPECT_EQ(kExpectedUInt8Value, arg->ui8); |
124 EXPECT_EQ(kExpectedFloatVal, arg->float_val); | 138 EXPECT_EQ(kExpectedFloatVal, arg->float_val); |
125 EXPECT_EQ(kExpectedFloatInf, arg->float_inf); | 139 EXPECT_EQ(kExpectedFloatInf, arg->float_inf); |
126 EXPECT_NAN(arg->float_nan); | 140 EXPECT_NAN(arg->float_nan); |
127 EXPECT_EQ(kExpectedDoubleVal, arg->double_val); | 141 EXPECT_EQ(kExpectedDoubleVal, arg->double_val); |
128 EXPECT_EQ(kExpectedDoubleInf, arg->double_inf); | 142 EXPECT_EQ(kExpectedDoubleInf, arg->double_inf); |
129 EXPECT_NAN(arg->double_nan); | 143 EXPECT_NAN(arg->double_nan); |
130 EXPECT_EQ(std::string("coming"), *arg->name); | 144 EXPECT_EQ(String("coming"), arg->name); |
131 EXPECT_EQ(std::string("one"), (*arg->string_array)[0]); | 145 EXPECT_EQ(String("one"), (*arg->string_array)[0]); |
132 EXPECT_EQ(std::string("two"), (*arg->string_array)[1]); | 146 EXPECT_EQ(String("two"), (*arg->string_array)[1]); |
133 EXPECT_EQ(std::string("three"), (*arg->string_array)[2]); | 147 EXPECT_EQ(String("three"), (*arg->string_array)[2]); |
134 CheckDataPipe(std::move(arg->data_handle)); | 148 CheckDataPipe(arg->data_handle.get()); |
135 CheckMessagePipe(arg->message_handle.get()); | 149 CheckMessagePipe(arg->message_handle.get()); |
136 } | 150 } |
137 | 151 |
138 void CheckSampleEchoArgsList(const js_to_cpp::EchoArgsListPtr& list) { | 152 void CheckSampleEchoArgsList(const js_to_cpp::blink::EchoArgsListPtr& list) { |
139 if (list.is_null()) | 153 if (list.is_null()) |
140 return; | 154 return; |
141 CheckSampleEchoArgs(std::move(list->item)); | 155 CheckSampleEchoArgs(list->item); |
142 CheckSampleEchoArgsList(list->next); | 156 CheckSampleEchoArgsList(list->next); |
143 } | 157 } |
144 | 158 |
145 // More forgiving checks are needed in the face of potentially corrupt | 159 // More forgiving checks are needed in the face of potentially corrupt |
146 // messages. The values don't matter so long as all accesses are within | 160 // messages. The values don't matter so long as all accesses are within |
147 // bounds. | 161 // bounds. |
148 void CheckCorruptedString(const std::string& arg) { | 162 void CheckCorruptedString(const String& arg) { |
149 for (size_t i = 0; i < arg.size(); ++i) | 163 for (size_t i = 0; i < arg.length(); ++i) |
150 g_waste_accumulator += arg[i]; | 164 g_waste_accumulator += arg[i]; |
151 } | 165 } |
152 | 166 |
153 void CheckCorruptedString(const base::Optional<std::string>& arg) { | 167 void CheckCorruptedStringArray(const Optional<Vector<String>>& string_array) { |
154 if (!arg) | |
155 return; | |
156 CheckCorruptedString(*arg); | |
157 } | |
158 | |
159 void CheckCorruptedStringArray( | |
160 const base::Optional<std::vector<std::string>>& string_array) { | |
161 if (!string_array) | 168 if (!string_array) |
162 return; | 169 return; |
163 for (size_t i = 0; i < string_array->size(); ++i) | 170 for (size_t i = 0; i < string_array->size(); ++i) |
164 CheckCorruptedString((*string_array)[i]); | 171 CheckCorruptedString((*string_array)[i]); |
165 } | 172 } |
166 | 173 |
167 void CheckCorruptedDataPipe(MojoHandle data_pipe_handle) { | 174 void CheckCorruptedDataPipe(mojo::DataPipeConsumerHandle data_pipe_handle) { |
168 unsigned char buffer[100]; | 175 unsigned char buffer[100]; |
169 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); | 176 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); |
170 MojoResult result = MojoReadData( | 177 MojoResult result = ReadDataRaw(data_pipe_handle, buffer, &buffer_size, |
171 data_pipe_handle, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE); | 178 MOJO_READ_DATA_FLAG_NONE); |
172 if (result != MOJO_RESULT_OK) | 179 if (result != MOJO_RESULT_OK) |
173 return; | 180 return; |
174 for (uint32_t i = 0; i < buffer_size; ++i) | 181 for (uint32_t i = 0; i < buffer_size; ++i) |
175 g_waste_accumulator += buffer[i]; | 182 g_waste_accumulator += buffer[i]; |
176 } | 183 } |
177 | 184 |
178 void CheckCorruptedMessagePipe(MojoHandle message_pipe_handle) { | 185 void CheckCorruptedMessagePipe(mojo::MessagePipeHandle message_pipe_handle) { |
179 unsigned char buffer[100]; | 186 unsigned char buffer[100]; |
180 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); | 187 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); |
181 MojoResult result = MojoReadMessage( | 188 MojoResult result = |
182 message_pipe_handle, buffer, &buffer_size, 0, 0, 0); | 189 ReadMessageRaw(message_pipe_handle, buffer, &buffer_size, 0, 0, 0); |
183 if (result != MOJO_RESULT_OK) | 190 if (result != MOJO_RESULT_OK) |
184 return; | 191 return; |
185 for (uint32_t i = 0; i < buffer_size; ++i) | 192 for (uint32_t i = 0; i < buffer_size; ++i) |
186 g_waste_accumulator += buffer[i]; | 193 g_waste_accumulator += buffer[i]; |
187 } | 194 } |
188 | 195 |
189 void CheckCorruptedEchoArgs(const js_to_cpp::EchoArgsPtr& arg) { | 196 void CheckCorruptedEchoArgs(const js_to_cpp::blink::EchoArgsPtr& arg) { |
190 if (arg.is_null()) | 197 if (arg.is_null()) |
191 return; | 198 return; |
192 CheckCorruptedString(arg->name); | 199 CheckCorruptedString(arg->name); |
193 CheckCorruptedStringArray(arg->string_array); | 200 CheckCorruptedStringArray(arg->string_array); |
194 if (arg->data_handle.is_valid()) | 201 if (arg->data_handle.is_valid()) |
195 CheckCorruptedDataPipe(arg->data_handle.get().value()); | 202 CheckCorruptedDataPipe(arg->data_handle.get()); |
196 if (arg->message_handle.is_valid()) | 203 if (arg->message_handle.is_valid()) |
197 CheckCorruptedMessagePipe(arg->message_handle.get().value()); | 204 CheckCorruptedMessagePipe(arg->message_handle.get()); |
198 } | 205 } |
199 | 206 |
200 void CheckCorruptedEchoArgsList(const js_to_cpp::EchoArgsListPtr& list) { | 207 void CheckCorruptedEchoArgsList(const js_to_cpp::blink::EchoArgsListPtr& list) { |
201 if (list.is_null()) | 208 if (list.is_null()) |
202 return; | 209 return; |
203 CheckCorruptedEchoArgs(list->item); | 210 CheckCorruptedEchoArgs(list->item); |
204 CheckCorruptedEchoArgsList(list->next); | 211 CheckCorruptedEchoArgsList(list->next); |
205 } | 212 } |
206 | 213 |
207 // Base Provider implementation class. It's expected that tests subclass and | 214 // Base Provider implementation class. It's expected that tests subclass and |
208 // override the appropriate Provider functions. When test is done quit the | 215 // override the appropriate Provider functions. When test is done quit the |
209 // run_loop(). | 216 // run_loop(). |
210 class CppSideConnection : public js_to_cpp::CppSide { | 217 class CppSideConnection : public js_to_cpp::blink::CppSide { |
211 public: | 218 public: |
212 CppSideConnection() | 219 CppSideConnection() : mishandled_messages_(0), binding_(this) {} |
213 : run_loop_(nullptr), | |
214 js_side_(nullptr), | |
215 mishandled_messages_(0), | |
216 binding_(this) {} | |
217 ~CppSideConnection() override {} | 220 ~CppSideConnection() override {} |
218 | 221 |
219 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } | 222 void set_js_side(js_to_cpp::blink::JsSidePtr js_side) { |
220 base::RunLoop* run_loop() { return run_loop_; } | 223 js_side_ = std::move(js_side); |
| 224 } |
| 225 js_to_cpp::blink::JsSide* js_side() { return js_side_.get(); } |
221 | 226 |
222 void set_js_side(js_to_cpp::JsSide* js_side) { js_side_ = js_side; } | 227 void Bind(mojo::InterfaceRequest<js_to_cpp::blink::CppSide> request) { |
223 js_to_cpp::JsSide* js_side() { return js_side_; } | |
224 | |
225 void Bind(InterfaceRequest<js_to_cpp::CppSide> request) { | |
226 binding_.Bind(std::move(request)); | 228 binding_.Bind(std::move(request)); |
227 // Keep the pipe open even after validation errors. | 229 // Keep the pipe open even after validation errors. |
228 binding_.EnableTestingMode(); | 230 binding_.EnableTestingMode(); |
229 } | 231 } |
230 | 232 |
231 // js_to_cpp::CppSide: | 233 // js_to_cpp::CppSide: |
232 void StartTest() override { NOTREACHED(); } | 234 void StartTest() override { NOTREACHED(); } |
233 | 235 |
234 void TestFinished() override { NOTREACHED(); } | 236 void TestFinished() override { NOTREACHED(); } |
235 | 237 |
236 void PingResponse() override { mishandled_messages_ += 1; } | 238 void PingResponse() override { mishandled_messages_ += 1; } |
237 | 239 |
238 void EchoResponse(js_to_cpp::EchoArgsListPtr list) override { | 240 void EchoResponse(js_to_cpp::blink::EchoArgsListPtr list) override { |
239 mishandled_messages_ += 1; | 241 mishandled_messages_ += 1; |
240 } | 242 } |
241 | 243 |
242 void BitFlipResponse( | 244 void BitFlipResponse( |
243 js_to_cpp::EchoArgsListPtr list, | 245 js_to_cpp::blink::EchoArgsListPtr list, |
244 js_to_cpp::ForTestingAssociatedPtrInfo not_used) override { | 246 js_to_cpp::blink::ForTestingAssociatedPtrInfo not_used) override { |
245 mishandled_messages_ += 1; | 247 mishandled_messages_ += 1; |
246 } | 248 } |
247 | 249 |
248 void BackPointerResponse(js_to_cpp::EchoArgsListPtr list) override { | 250 void BackPointerResponse(js_to_cpp::blink::EchoArgsListPtr list) override { |
249 mishandled_messages_ += 1; | 251 mishandled_messages_ += 1; |
250 } | 252 } |
251 | 253 |
252 protected: | 254 protected: |
253 base::RunLoop* run_loop_; | 255 js_to_cpp::blink::JsSidePtr js_side_; |
254 js_to_cpp::JsSide* js_side_; | |
255 int mishandled_messages_; | 256 int mishandled_messages_; |
256 mojo::Binding<js_to_cpp::CppSide> binding_; | 257 mojo::Binding<js_to_cpp::blink::CppSide> binding_; |
257 | |
258 private: | |
259 DISALLOW_COPY_AND_ASSIGN(CppSideConnection); | |
260 }; | 258 }; |
261 | 259 |
262 // Trivial test to verify a message sent from JS is received. | 260 // Trivial test to verify a message sent from JS is received. |
263 class PingCppSideConnection : public CppSideConnection { | 261 class PingCppSideConnection : public CppSideConnection { |
264 public: | 262 public: |
265 PingCppSideConnection() : got_message_(false) {} | 263 PingCppSideConnection() : got_message_(false) {} |
266 ~PingCppSideConnection() override {} | 264 ~PingCppSideConnection() override {} |
267 | 265 |
268 // js_to_cpp::CppSide: | 266 // js_to_cpp::CppSide: |
269 void StartTest() override { js_side_->Ping(); } | 267 void StartTest() override { js_side_->Ping(); } |
270 | 268 |
271 void PingResponse() override { | 269 void PingResponse() override { |
272 got_message_ = true; | 270 got_message_ = true; |
273 run_loop()->Quit(); | 271 testing::ExitRunLoop(); |
274 } | 272 } |
275 | 273 |
276 bool DidSucceed() { | 274 bool DidSucceed() { return got_message_ && !mishandled_messages_; } |
277 return got_message_ && !mishandled_messages_; | |
278 } | |
279 | 275 |
280 private: | 276 private: |
281 bool got_message_; | 277 bool got_message_; |
282 DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection); | |
283 }; | 278 }; |
284 | 279 |
285 // Test that parameters are passed with correct values. | 280 // Test that parameters are passed with correct values. |
286 class EchoCppSideConnection : public CppSideConnection { | 281 class EchoCppSideConnection : public CppSideConnection { |
287 public: | 282 public: |
288 EchoCppSideConnection() : | 283 EchoCppSideConnection() : message_count_(0), termination_seen_(false) {} |
289 message_count_(0), | |
290 termination_seen_(false) { | |
291 } | |
292 ~EchoCppSideConnection() override {} | 284 ~EchoCppSideConnection() override {} |
293 | 285 |
294 // js_to_cpp::CppSide: | 286 // js_to_cpp::CppSide: |
295 void StartTest() override { | 287 void StartTest() override { |
296 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgs()); | 288 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgs()); |
297 } | 289 } |
298 | 290 |
299 void EchoResponse(js_to_cpp::EchoArgsListPtr list) override { | 291 void EchoResponse(js_to_cpp::blink::EchoArgsListPtr list) override { |
300 const js_to_cpp::EchoArgsPtr& special_arg = list->item; | |
301 message_count_ += 1; | 292 message_count_ += 1; |
| 293 |
| 294 const js_to_cpp::blink::EchoArgsPtr& special_arg = list->item; |
302 EXPECT_EQ(-1, special_arg->si64); | 295 EXPECT_EQ(-1, special_arg->si64); |
303 EXPECT_EQ(-1, special_arg->si32); | 296 EXPECT_EQ(-1, special_arg->si32); |
304 EXPECT_EQ(-1, special_arg->si16); | 297 EXPECT_EQ(-1, special_arg->si16); |
305 EXPECT_EQ(-1, special_arg->si8); | 298 EXPECT_EQ(-1, special_arg->si8); |
306 EXPECT_EQ(std::string("going"), *special_arg->name); | 299 EXPECT_EQ(String("going"), special_arg->name); |
| 300 CheckDataPipe(special_arg->data_handle.get()); |
| 301 CheckMessagePipe(special_arg->message_handle.get()); |
| 302 |
307 CheckSampleEchoArgsList(list->next); | 303 CheckSampleEchoArgsList(list->next); |
308 } | 304 } |
309 | 305 |
310 void TestFinished() override { | 306 void TestFinished() override { |
311 termination_seen_ = true; | 307 termination_seen_ = true; |
312 run_loop()->Quit(); | 308 testing::ExitRunLoop(); |
313 } | 309 } |
314 | 310 |
315 bool DidSucceed() { | 311 bool DidSucceed() { |
316 return termination_seen_ && | 312 return termination_seen_ && !mishandled_messages_ && |
317 !mishandled_messages_ && | 313 message_count_ == kExpectedMessageCount; |
318 message_count_ == kExpectedMessageCount; | |
319 } | 314 } |
320 | 315 |
321 private: | 316 private: |
322 static const int kExpectedMessageCount = 10; | 317 static const int kExpectedMessageCount = 10; |
323 int message_count_; | 318 int message_count_; |
324 bool termination_seen_; | 319 bool termination_seen_; |
325 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection); | |
326 }; | 320 }; |
327 | 321 |
328 // Test that corrupted messages don't wreak havoc. | 322 // Test that corrupted messages don't wreak havoc. |
329 class BitFlipCppSideConnection : public CppSideConnection { | 323 class BitFlipCppSideConnection : public CppSideConnection { |
330 public: | 324 public: |
331 BitFlipCppSideConnection() : termination_seen_(false) {} | 325 BitFlipCppSideConnection() : termination_seen_(false) {} |
332 ~BitFlipCppSideConnection() override {} | 326 ~BitFlipCppSideConnection() override {} |
333 | 327 |
334 // js_to_cpp::CppSide: | 328 // js_to_cpp::CppSide: |
335 void StartTest() override { js_side_->BitFlip(BuildSampleEchoArgs()); } | 329 void StartTest() override { js_side_->BitFlip(BuildSampleEchoArgs()); } |
336 | 330 |
337 void BitFlipResponse( | 331 void BitFlipResponse( |
338 js_to_cpp::EchoArgsListPtr list, | 332 js_to_cpp::blink::EchoArgsListPtr list, |
339 js_to_cpp::ForTestingAssociatedPtrInfo not_used) override { | 333 js_to_cpp::blink::ForTestingAssociatedPtrInfo not_used) override { |
340 CheckCorruptedEchoArgsList(list); | 334 CheckCorruptedEchoArgsList(list); |
341 } | 335 } |
342 | 336 |
343 void TestFinished() override { | 337 void TestFinished() override { |
344 termination_seen_ = true; | 338 termination_seen_ = true; |
345 run_loop()->Quit(); | 339 testing::ExitRunLoop(); |
346 } | 340 } |
347 | 341 |
348 bool DidSucceed() { | 342 bool DidSucceed() { return termination_seen_; } |
349 return termination_seen_; | |
350 } | |
351 | 343 |
352 private: | 344 private: |
353 bool termination_seen_; | 345 bool termination_seen_; |
354 DISALLOW_COPY_AND_ASSIGN(BitFlipCppSideConnection); | |
355 }; | 346 }; |
356 | 347 |
357 // Test that severely random messages don't wreak havoc. | 348 // Test that severely random messages don't wreak havoc. |
358 class BackPointerCppSideConnection : public CppSideConnection { | 349 class BackPointerCppSideConnection : public CppSideConnection { |
359 public: | 350 public: |
360 BackPointerCppSideConnection() : termination_seen_(false) {} | 351 BackPointerCppSideConnection() : termination_seen_(false) {} |
361 ~BackPointerCppSideConnection() override {} | 352 ~BackPointerCppSideConnection() override {} |
362 | 353 |
363 // js_to_cpp::CppSide: | 354 // js_to_cpp::CppSide: |
364 void StartTest() override { js_side_->BackPointer(BuildSampleEchoArgs()); } | 355 void StartTest() override { js_side_->BackPointer(BuildSampleEchoArgs()); } |
365 | 356 |
366 void BackPointerResponse(js_to_cpp::EchoArgsListPtr list) override { | 357 void BackPointerResponse(js_to_cpp::blink::EchoArgsListPtr list) override { |
367 CheckCorruptedEchoArgsList(list); | 358 CheckCorruptedEchoArgsList(list); |
368 } | 359 } |
369 | 360 |
370 void TestFinished() override { | 361 void TestFinished() override { |
371 termination_seen_ = true; | 362 termination_seen_ = true; |
372 run_loop()->Quit(); | 363 testing::ExitRunLoop(); |
373 } | 364 } |
374 | 365 |
375 bool DidSucceed() { | 366 bool DidSucceed() { return termination_seen_; } |
376 return termination_seen_; | |
377 } | |
378 | 367 |
379 private: | 368 private: |
380 bool termination_seen_; | 369 bool termination_seen_; |
381 DISALLOW_COPY_AND_ASSIGN(BackPointerCppSideConnection); | |
382 }; | 370 }; |
383 | 371 |
384 } // namespace | 372 class JsToCppTest : public ::testing::Test { |
385 | |
386 class JsToCppTest : public testing::Test { | |
387 public: | 373 public: |
388 JsToCppTest() {} | 374 void RunTest(CppSideConnection* cpp_side) { |
389 | 375 js_to_cpp::blink::CppSidePtr cpp_side_ptr; |
390 void RunTest(const std::string& test, CppSideConnection* cpp_side) { | |
391 cpp_side->set_run_loop(&run_loop_); | |
392 | |
393 js_to_cpp::JsSidePtr js_side; | |
394 auto js_side_proxy = MakeRequest(&js_side); | |
395 | |
396 cpp_side->set_js_side(js_side.get()); | |
397 js_to_cpp::CppSidePtr cpp_side_ptr; | |
398 cpp_side->Bind(MakeRequest(&cpp_side_ptr)); | 376 cpp_side->Bind(MakeRequest(&cpp_side_ptr)); |
399 | 377 |
400 js_side->SetCppSide(std::move(cpp_side_ptr)); | 378 js_to_cpp::blink::JsSidePtr js_side_ptr; |
| 379 auto js_side_request = MakeRequest(&js_side_ptr); |
| 380 js_side_ptr->SetCppSide(std::move(cpp_side_ptr)); |
| 381 cpp_side->set_js_side(std::move(js_side_ptr)); |
401 | 382 |
402 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 383 V8TestingScope scope; |
403 gin::V8Initializer::LoadV8Snapshot(); | 384 scope.GetPage().GetSettings().SetScriptEnabled(true); |
404 gin::V8Initializer::LoadV8Natives(); | 385 ExecuteScript(MojoBindingsScriptPath(), scope.GetFrame()); |
405 #endif | 386 ExecuteScript(TestBindingsScriptPath(), scope.GetFrame()); |
406 | 387 |
407 gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, | 388 v8::Local<v8::Value> start_fn = |
408 gin::IsolateHolder::kStableV8Extras, | 389 ExecuteScript(TestScriptPath(), scope.GetFrame()); |
409 gin::ArrayBufferAllocator::SharedInstance()); | 390 ASSERT_FALSE(start_fn.IsEmpty()); |
410 gin::IsolateHolder instance(base::ThreadTaskRunnerHandle::Get()); | 391 ASSERT_TRUE(start_fn->IsFunction()); |
411 MojoRunnerDelegate delegate; | 392 v8::Local<v8::Object> global_proxy = scope.GetContext()->Global(); |
412 gin::ShellRunner runner(&delegate, instance.isolate()); | 393 v8::Local<v8::Value> args[1] = { |
413 delegate.Start(&runner, js_side_proxy.PassMessagePipe().release().value(), | 394 ToV8(MojoHandle::Create( |
414 test); | 395 mojo::ScopedHandle::From(js_side_request.PassMessagePipe())), |
415 | 396 global_proxy, scope.GetIsolate())}; |
416 run_loop_.Run(); | 397 V8ScriptRunner::CallFunction( |
| 398 start_fn.As<v8::Function>(), scope.GetExecutionContext(), global_proxy, |
| 399 WTF_ARRAY_LENGTH(args), args, scope.GetIsolate()); |
| 400 testing::EnterRunLoop(); |
417 } | 401 } |
418 | |
419 private: | |
420 base::ShadowingAtExitManager at_exit_; | |
421 base::test::ScopedTaskEnvironment scoped_task_environment_; | |
422 base::RunLoop run_loop_; | |
423 | |
424 DISALLOW_COPY_AND_ASSIGN(JsToCppTest); | |
425 }; | 402 }; |
426 | 403 |
427 TEST_F(JsToCppTest, Ping) { | 404 TEST_F(JsToCppTest, Ping) { |
428 PingCppSideConnection cpp_side_connection; | 405 PingCppSideConnection cpp_side_connection; |
429 RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection); | 406 RunTest(&cpp_side_connection); |
430 EXPECT_TRUE(cpp_side_connection.DidSucceed()); | 407 EXPECT_TRUE(cpp_side_connection.DidSucceed()); |
431 } | 408 } |
432 | 409 |
433 TEST_F(JsToCppTest, Echo) { | 410 TEST_F(JsToCppTest, Echo) { |
434 EchoCppSideConnection cpp_side_connection; | 411 EchoCppSideConnection cpp_side_connection; |
435 RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection); | 412 RunTest(&cpp_side_connection); |
436 EXPECT_TRUE(cpp_side_connection.DidSucceed()); | 413 EXPECT_TRUE(cpp_side_connection.DidSucceed()); |
437 } | 414 } |
438 | 415 |
439 TEST_F(JsToCppTest, BitFlip) { | 416 TEST_F(JsToCppTest, BitFlip) { |
440 // These tests generate a lot of expected validation errors. Suppress logging. | 417 // These tests generate a lot of expected validation errors. Suppress logging. |
441 mojo::internal::ScopedSuppressValidationErrorLoggingForTests log_suppression; | 418 mojo::internal::ScopedSuppressValidationErrorLoggingForTests log_suppression; |
442 | 419 |
443 BitFlipCppSideConnection cpp_side_connection; | 420 BitFlipCppSideConnection cpp_side_connection; |
444 RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection); | 421 RunTest(&cpp_side_connection); |
445 EXPECT_TRUE(cpp_side_connection.DidSucceed()); | 422 EXPECT_TRUE(cpp_side_connection.DidSucceed()); |
446 } | 423 } |
447 | 424 |
448 TEST_F(JsToCppTest, BackPointer) { | 425 TEST_F(JsToCppTest, BackPointer) { |
449 // These tests generate a lot of expected validation errors. Suppress logging. | 426 // These tests generate a lot of expected validation errors. Suppress logging. |
450 mojo::internal::ScopedSuppressValidationErrorLoggingForTests log_suppression; | 427 mojo::internal::ScopedSuppressValidationErrorLoggingForTests log_suppression; |
451 | 428 |
452 BackPointerCppSideConnection cpp_side_connection; | 429 BackPointerCppSideConnection cpp_side_connection; |
453 RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection); | 430 RunTest(&cpp_side_connection); |
454 EXPECT_TRUE(cpp_side_connection.DidSucceed()); | 431 EXPECT_TRUE(cpp_side_connection.DidSucceed()); |
455 } | 432 } |
456 | 433 |
457 } // namespace js | 434 } // namespace |
458 } // namespace edk | 435 } // namespace blink |
459 } // namespace mojo | |
OLD | NEW |