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