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